我正在使用具有單位的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
提交
取消