简体   繁体   中英

Why doesn't sympy support units with 0 magnitude?

The title says it all... why does sympy have the following behavior?

import sympy.physics.units as u
print(0*u.meter)
# >>> 0

And Pint has this behavior:

import pint
u = pint.UnitRegistry()
print(0*u.meter)
# >>> 0 meter

I think I prefer pint's behavior, because it allows for dimensional consistency. 0 is a proper magnitude of some unit. For instance, 0 degrees kelvin has a definitive meaning... it's not just the absence of anything...

So I realize the contributors of sympy probably chose this implementation for some reason. Can you help me see the light?

The discussion of reasons for implementation belongs on GitHub (where I raised this issue ), not here. A short answer is that units are a bolted-on structure; the core of SymPy is not really unit-aware.

You can create 0*meter expression by passing evaluate=False parameter to Mul:

>>> Mul(0, u.meter, evaluate=False)
0*meter 

However, it will become 0 if combined with something else.

>>> 3*Mul(0, u.meter, evaluate=False)
0

Wrapping in UnevaluatedExpr prevents the above, but causes more problems than it solves.

>>> 3*UnevaluatedExpr(Mul(0, u.meter, evaluate=False))
3*(0*meter)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM