python - sympy.physics.units 替换给出 TypeError
问题描述
我正在尝试使用 sympy 作为一些转换/数学代码的后端并遇到了这个问题。
from sympy.parsing.sympy_parser import parse_expr
from sympy.physics import units
type(units.newton) # -> sympy.physics.units.quantities.Quantity
parse_expr('2*Newton').subs({'Newton':units.newton}) # -> 2N
parse_expr('2*newton').subs({'newton':units.newton}) # -> 2N
parse_expr('2*n').subs({'n':units.newton}) # -> 2N
parse_expr('2*N').subs({'N':units.newton}) # -> raises TypeError below
parse_expr('N').subs() # -> raises AttributeError below
parse_expr('N') # -> <function sympy.core.evalf.N(x, n=15, **options)>
TypeError: unsupported operand type(s) for *: 'Integer' and 'function'
AttributeError: 'function' object has no attribute 'subs'
似乎 sympy 替换了 evalf.N 函数而不是提供的 units.newton 。是否有可以调整的替换顺序,或者从替换选项中删除“N”功能的方法?
编辑:已验证 evalf.N
evalf.N 函数是所使用的,但不使用 if 似乎是个问题。尝试使用.subs(..., global_dict=...)
对错误没有影响。
parse_expr('N') is sympify('N') # -> True
sympify('N') is evalf.N # -> True
解决方案
从文档parse_expr
中获取一个可选参数:
global_dict : dict, optional
A dictionary of global variables. By default, this is initialized with
from sympy import *; provide this parameter to override this behavior
(for instance, to parse "Q & S").
from sympy import *
N
在全局命名空间中引入了一个函数,该函数parse_expr()
在解析最后三个示例中的“N”时使用。
您可以在全局命名空间中重新定义“N”:
N = units.newton
parse_expr('2*N') -> 2*newton
如果您无法重新定义“N”,则复制globals()
,修改副本,并将其传递给parse_expr()
:
globals_with_units = dict(globals())
globals_with_units['N'] = units.newton
parse_expr('2*N', global_dict=globals_with_units) -> 2*newton
parse_expr()
还需要一个local_dict
参数:
local_dict : dict, optional
A dictionary of local variables to use when parsing.
它可用于覆盖全局命名空间中“N”的定义。
parse_expr('2*N', local_dict={'N':units.newton}) -> 2*newton
推荐阅读
- python - 如何在没有滚动条的情况下将 difflib.HtmlDiff 的 html 输出放入一个窗口
- powerbi - Power BI:百分比变化公式
- reactjs - 使用 Material UI 的 List Item 链接
- pyspark - 在 Spark 1.6 中将 Spark 数据帧拆分为多列
- javascript - 不使用动画的 HTML/JS 轮播
- java - 超类变量如何保存仅存在于子类中的数据成员值?
- swift - 在 Square Reader SDK 中允许用户定义的金额
- python - Python 将时间、纬度和经度分配给来自 netcdf 文件的 xarray.DataSet 的“维度”
- arrays - 使用下一个运算符后迭代数组不移动到下一个对象
- c++ - 使用 C++ 项目将数据保存在磁盘上