我正在尝试使用 SymPy 和 SymPy 的 diffgeom 包在流形上定义一个方程。由于这个方程是由用户输入的,它作为字符串发送到程序中,因此在代码中的流形定义之前定义。为了执行有意义的计算,我试图用歧管上定义的符号替换“简化”符号。
这是一个由用户提供的函数。
H = sympify('m*a - f')
歧管的坐标也由用户作为字符串提供。
# Variables defined as symbols (non-diffgeom)
a = Symbol('a')
f = Symbol('f')
之后的一切都是自动化的。
from sympy.diffgeom import Manifold, Patch, CoordSystem
from sympy import sympify, Symbol
# Standard manifold definitions
M = Manifold(name='Temp', dim=2)
P = Patch('P', M)
R = CoordSystem('R', P, ['a','f'])
coords = R.coord_functions()
Dx = R.base_vectors()
print(H.diff(a)) # Returns 'm' as expected
print(Differential(H)(Dx[0])) # Returns '0' as expected
第一个替换效果很好。我可以使用微分()按预期获取导数。
H = H.subs(a,coords[0])
print(H.diff(a)) # Returns '0' as expected
print(Differential(H)(Dx[0])) # Returns 'm' as expected
print(Differential(H)(Dx[1])) # Returns '0' as expected
第二个替换是事情变得奇怪的地方。().diff() 命令工作正常并返回 0,因为新坐标是在流形上定义的,而不是标准符号,但我不能再使用微分() 求导数。
H = H.subs(f,coords[1])
print(H.diff(f)) # Returns '0' as expected
print(Differential(H)(Dx[0])) # Crashes code
print(Differential(H)(Dx[1])) # Also crashes code
似乎 diffgeom 正在尝试执行转换以计算导数,但实际上不应该进行任何转换,因为这都在同一个坐标系中。我从根本上错过了什么吗?或者是否有更简单的方法将字符串解析为流形上的表达式?
这是抛出的完整错误。除了 SymPy 在我没想到的时候尝试转换坐标之外,我真的无法从中得到太多。
Traceback (most recent call last):
File "/Users/msparapa/Documents/Python/gprops/examples/test.py", line 37, in <module>
print(Differential(H)(Dx[0])) # Crashes code
File "/Users/msparapa/anaconda/lib/python3.5/site-packages/sympy/diffgeom/diffgeom.py", line 765, in __call__
return vector_fields[0].rcall(self._form_field)
File "/Users/msparapa/anaconda/lib/python3.5/site-packages/sympy/core/basic.py", line 538, in rcall
return Basic._recursive_call(self, args)
File "/Users/msparapa/anaconda/lib/python3.5/site-packages/sympy/core/basic.py", line 552, in _recursive_call
return expr_to_call(*on_args)
File "/Users/msparapa/anaconda/lib/python3.5/site-packages/sympy/diffgeom/diffgeom.py", line 592, in __call__
jac = self._coord_sys.jacobian(b._coord_sys, coords)
File "/Users/msparapa/anaconda/lib/python3.5/site-packages/sympy/diffgeom/diffgeom.py", line 277, in jacobian
to_sys, self._dummies).jacobian(self._dummies)
File "/Users/msparapa/anaconda/lib/python3.5/site-packages/sympy/diffgeom/diffgeom.py", line 270, in coord_tuple_transform_to
transf = self.transforms[to_sys]
KeyError: CoordSystem(R, Patch(P, Manifold(Temp, 2)), (a, f))