Python 如何求向量的辛导数?

Python 如何求向量的辛导数?,python,python-2.7,sympy,symbolic-math,Python,Python 2.7,Sympy,Symbolic Math,我想知道在symphy中是否可以使用向量表示法对多项式和表达式求导。例如,如果我有一个表达式作为两个坐标x1和x2的函数,我可以只调用一次diff(x),其中x是x1和x2的向量,或者我需要对x1和x2进行两次单独的diff调用,并将它们堆叠在一个矩阵中吗 这说明了什么是有效的,而我理想的工作方式是: import sympy from sympy.matrices import Matrix # I understand that this is possible: x1 = sympy.

我想知道在
symphy
中是否可以使用向量表示法对多项式和表达式求导。例如,如果我有一个表达式作为两个坐标x1和x2的函数,我可以只调用一次
diff(x)
,其中
x
x1
x2
的向量,或者我需要对
x1
x2
进行两次单独的
diff
调用,并将它们堆叠在一个矩阵中吗

这说明了什么是有效的,而我理想的工作方式是:

import sympy
from sympy.matrices import Matrix

# I understand that this is possible: 
x1 = sympy.symbol.symbols('x1')  
x2 = sympy.symbol.symbols('x2')

expr_1 = x1**2+x2
poly_1 = sympy.poly(expr_1, x1, x2)

print Matrix([[poly_1.diff(x1)],[poly_1.diff(x2)]])

# but is something like this also possible?
x1 = sympy.symbol.symbols('x1')  
x2 = sympy.symbol.symbols('x2')
x_vec = Matrix([[x1],[x2]])

expr_1 = x1**2+x2
poly_1 = sympy.poly(expr_1, x1, x2)

# taking derivative with respect to a vector
poly_1.diff(x_vec)

# should ideally return same as first example: 
'''
Matrix([
[Poly(2*x1, x1, x2, domain='ZZ')],
[   Poly(1, x1, x2, domain='ZZ')]])
'''
# but it fails :(
有没有一种方法可以在
sympy
中对向量求导数


谢谢。

也许您想到的是雅可比的

>>> Matrix([Poly(x**2+y,x,y)]).jacobian([x, y])
Matrix([[Poly(2*x, x, y, domain='ZZ'), Poly(1, x, y, domain='ZZ')]])
[x,y]
参数可以是
矩阵([x,y])
或其转置