简体   繁体   English

为什么 sympy 不支持 0 量级的单位?

[英]Why doesn't sympy support units with 0 magnitude?

The title says it all... why does sympy have the following behavior?标题说明了一切......为什么 sympy 有以下行为?

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. 0 是某个单位的适当大小。 For instance, 0 degrees kelvin has a definitive meaning... it's not just the absence of anything...例如,0 开尔文度具有明确的含义……这不仅仅是没有任何东西……

So I realize the contributors of sympy probably chose this implementation for some reason.所以我意识到 sympy 的贡献者可能出于某种原因选择了这个实现。 Can you help me see the light?你能帮我看到光明吗?

The discussion of reasons for implementation belongs on GitHub (where I raised this issue ), not here.实施原因的讨论属于 GitHub(我在那里提出了这个问题),而不是这里。 A short answer is that units are a bolted-on structure;一个简短的回答是,单元是一个螺栓固定的结构; the core of SymPy is not really unit-aware. SymPy 的核心并不是真正的单元感知。

You can create 0*meter expression by passing evaluate=False parameter to Mul:您可以通过将evaluate=False 参数传递给Mul 来创建0*meter表达式:

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

However, it will become 0 if combined with something else.但是,如果与其他内容结合,它将变为 0。

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

Wrapping in UnevaluatedExpr prevents the above, but causes more problems than it solves.包装在 UnevaluatedExpr 中可以防止上述情况,但会导致比它解决的问题更多的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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