我正在使用具有单位的Sympy表达式。这是一个简单的例子:from sympy.physics.units import newton, meteratmosphere = 101325. * newton / (meter **2)在上面的示例中,是否有一种简洁的方法来获取单位,即newton / (meter **2)从变量中获取atmosphere?
2 回答

胡说叔叔
TA贡献1804条经验 获得超8个赞
你的做法是很好的,但我会更换为检查is_number与has(Quantity)
from sympy.physics.units import Quantity,
def unit(expr):
return expr.subs({x: 1 for x in expr.args if not x.has(Quantity)})
然后它也适用于象征性的金额,例如
g = symbols('g')
acc = g*meter/second**2
unit(acc) # meter/second**2
添加回答
举报
0/150
提交
取消