sympy / sympy

A computer algebra system written in pure Python
https://sympy.org/
Other
12.78k stars 4.39k forks source link

Need a new class for physical constants and variable, separate from Quantity. #16757

Open schymans opened 5 years ago

schymans commented 5 years ago

The current class Quantity is well suited to represent units, but due to its limitations, it is not so good for physical constants and especially variables, which can be positive or negative.

@jirikuncar and I created a sympy-based system to deal with physical variables and equations here: https://github.com/environmentalscience/essm

We originally used Quantity to represent physical constants and variables, but we ran into all sorts of problems. Now we base our Variable class on Symbol and consider quantities just as units. Works like a charm. Perhaps this could be merged into sympy at some point. I think everything would be a lot clearer if we used the current Quantity for units and a separate Variable for physical constants. Then there would be no point in defining different quantities with the same dimension and scale factor, leading to problems such as described e.g. in #14595, #14374, #14596, #13267, #14374, ...

meganly commented 3 years ago

I think everything would be a lot clearer if we used the current Quantity for units

I totally agree! My idea is to then have a class called PhysicalQuantity to represent numerical quantities with units as PhysicalQuantity(magnitude, units). Is this similar to your Variable class? I found it a little confusing have a class for physical constants named "Variable" since constants are fixed, but variables change. Overall essm looks pretty neat though.

schymans commented 3 years ago

Good point, PhysicalQuantity would be a more accurate name, but a bit tedious to type. Perhaps PQ as you suggested in another issue. The way Variable works in ESSM, is that it behaves just like any other symbol, but has additional information stored with it, such as a docstring, latex representation, default units and an optional default value, e.g. for constants. For doing calculations, one needs to substitute values for the variables, just like substitution for symbols in normal sympy expressions. There is a special Equation class, which uses the unit information stored in Variable to check for dimensional consistency.