我想知道在sympy
中是否可以用向量表示法对多项式和表达式求导。例如,如果我有一个表达式作为两个坐标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
中的向量求导数?你知道吗
谢谢你。你知道吗
也许你在想
jacobian
:这个
[x, y]
参数也可以是Matrix([x, y])
或者它的转置。你知道吗相关问题 更多 >
编程相关推荐