Sympy diffgeom:度量取决于函数

时间:2021-01-22 18:02:19

I'm having problems defining a metric using sympy's diffgeom package, where the metric depends on a function f(x,y).

我在使用sympy的diffgeom包定义度量时遇到问题,其中度量取决于函数f(x,y)。

I get the error ValueError: Can't calculate 1st derivative wrt x.

我得到错误ValueError:无法计算一阶导数wrt x。

import sympy as sym
from sympy.diffgeom import Manifold, Patch, CoordSystem

m = Manifold("M",2)
patch = Patch("P",m)
cartesian = CoordSystem("cartesian", patch, ["x", "y"])
x, y = cartesian.coord_functions()
f = sym.Function('f')
xi = sym.Symbol('xi')

g = sym.Matrix([
    [ xi + sym.diff(f,x)**2         , sym.diff(f,x) * sym.diff(f,y) ],
    [ sym.diff(f,x) * sym.diff(f,y) , xi + sym.diff(f,y)**2 ]   
])

I have a feeling it's because of how x and y are defined, but I haven't been able to figure it out.

我感觉这是因为x和y是如何定义的,但我无法弄明白。

1 个解决方案

#1


1  

Indeed, differentiation with respect to these x and y (objects of class sympy.diffgeom.diffgeom.BaseScalarField) is not supported. You can see this by accessing the internal property _diff_wrt which indicates if something can be a thing with respect to which to differentiate.

实际上,不支持关于这些x和y(类sympy.diffgeom.diffgeom.BaseScalarField的对象)的区别。您可以通过访问内部属性_diff_wrt来查看此信息,该属性指示某些内容是否可以区分哪些内容。

>>> x._diff_wrt
False

Do those derivative (with respect to a scalar field) make mathematical sense here? I'm not sure.

那些衍生物(关于标量场)在这里是否具有数学意义?我不确定。

An additional issue is that SymPy does not differentiate functions, so

另一个问题是SymPy没有区分功能,所以

f = Function('f')
diff(f, x) 

is always an error. SymPy can differentiate expressions, e.g., diff(f(x, y), x).

总是一个错误。 SymPy可以区分表达式,例如diff(f(x,y),x)。

Aside: diff can be used as a method of an expression, which in your case would result in shorter code, like f(x, y).diff(x).

旁白:diff可以用作表达式的方法,在您的情况下会导致更短的代码,如f(x,y).diff(x)。

#1


1  

Indeed, differentiation with respect to these x and y (objects of class sympy.diffgeom.diffgeom.BaseScalarField) is not supported. You can see this by accessing the internal property _diff_wrt which indicates if something can be a thing with respect to which to differentiate.

实际上,不支持关于这些x和y(类sympy.diffgeom.diffgeom.BaseScalarField的对象)的区别。您可以通过访问内部属性_diff_wrt来查看此信息,该属性指示某些内容是否可以区分哪些内容。

>>> x._diff_wrt
False

Do those derivative (with respect to a scalar field) make mathematical sense here? I'm not sure.

那些衍生物(关于标量场)在这里是否具有数学意义?我不确定。

An additional issue is that SymPy does not differentiate functions, so

另一个问题是SymPy没有区分功能,所以

f = Function('f')
diff(f, x) 

is always an error. SymPy can differentiate expressions, e.g., diff(f(x, y), x).

总是一个错误。 SymPy可以区分表达式,例如diff(f(x,y),x)。

Aside: diff can be used as a method of an expression, which in your case would result in shorter code, like f(x, y).diff(x).

旁白:diff可以用作表达式的方法,在您的情况下会导致更短的代码,如f(x,y).diff(x)。