Now, I have a formula with some symbols. These symbols have units. Does anyone know a python library to evaluate the dimension of the formula.
I checked sympy
and pint
.
Sympy
seems to be based on the unit system of physics, but what I want to deal with is general units like USD or KG. Sympy doesn't seem to define these.
pint
can define units by using UnitRegistry.define
, but it doesn't work unless every element in the expression has a unit.
from pint import UnitRegistry
ureg = UnitRegistry(filename=None)
ureg.define("KG = []")
ureg.define("MT = 1000 * KG")
ureg.define("USD = []")
ureg.define("JPY = []")
X = 1 * ureg.USD / ureg.MT
Y = 1 * ureg.JPY / ureg.USD
formula = (X + 50) / 1000 * Y
formula.u
=> JPY/USD
The result I want to get is JPY/KG
or USD/MT
. This undesirable result is because X + 50
will be dimensionless. If I manually define the units for the numbers, I will get an accurate answer. But then there is no need for dimensional analysis.
I am not obsessed with pint
, sympy
. Is there any good way to do a dimensional analysis?
P.S.
Thanks to @wsdookadr, I would like to show examples. What I want to do is to evaluate the dimension of a formula (maybe expressed by string).
Let me have two formulas.
F1 = (X + 50) / 1000 * Y
F2 = (X + 1000) * 0.5
The unit of this X and Y are the same as the previous example. If I could perform dimension analysis, the results would be as follows.
F1 = ([USD/MT] + 50) / 1000 * [JPY/USD]
= 10^-3 [USD/MT] * [JPY/USD]
= 10^-3 [JPY/MT]
= [JPY/KG]
F2 = ([USD / MT] + 1000) * 0.5
= [USD/MT]