[英]Solve double derivatives inside a hessian matrix with sympy
I asked a similar question before, but it didn't really go anywhere and I was less capable of explaining my problem then.我之前问过一个类似的问题,但它在任何地方都不是真正的 go ,那时我无法解释我的问题。 Anyways I have a Hessian matrix like this:
无论如何,我有一个这样的 Hessian 矩阵:
import sympy as sy
x1,x2,x3,x4,x5,x6,x7,x8,x9 = sy.symbols('x1 x2 x3 x4 x5 x6 x7 x8 x9',
real=True)
V = sy.Function("V")(x1,x2,x3,x4,x5,x6,x7,x8,x9)
H = sy.hessian(V,[x1,x2,x3,x4,x5,x6,x7,x8,x9])
And I want to test it with this simple function:我想用这个简单的 function 来测试它:
V_ = x1+x2+x3+x4+x5+x6+x7+x8**(-1)+x9**(-1)
By printing out the matrix element with already solved Derivative()
's like so:通过用已经解决的
Derivative()
打印出矩阵元素,如下所示:
H = H.subs(V,V_)
for i,j in enumerate(H):
print(i+1)
sy.pprint(sy.solve(j))
I don't know a lot about solvers in sympy and I only get confused reading the docs.我对 sympy 中的求解器知之甚少,我只会在阅读文档时感到困惑。 I know that
dsolve
only works with simple derivatives, so I wanted to know how I can eliminate the Derivative()
's and just get the "finished" Hessian in which the function has already been differentiated.我知道
dsolve
仅适用于简单的导数,所以我想知道如何消除Derivative()
并获得 function 已经区分的“完成” Hessian。
Use doit
:使用
doit
:
In [8]: H.doit()
Out[8]:
⎡0 0 0 0 0 0 0 0 0 ⎤
⎢ ⎥
⎢0 0 0 0 0 0 0 0 0 ⎥
⎢ ⎥
⎢0 0 0 0 0 0 0 0 0 ⎥
⎢ ⎥
⎢0 0 0 0 0 0 0 0 0 ⎥
⎢ ⎥
⎢0 0 0 0 0 0 0 0 0 ⎥
⎢ ⎥
⎢0 0 0 0 0 0 0 0 0 ⎥
⎢ ⎥
⎢0 0 0 0 0 0 0 0 0 ⎥
⎢ ⎥
⎢ 2 ⎥
⎢0 0 0 0 0 0 0 ─── 0 ⎥
⎢ 3 ⎥
⎢ x₈ ⎥
⎢ ⎥
⎢ 2 ⎥
⎢0 0 0 0 0 0 0 0 ───⎥
⎢ 3⎥
⎣ x₉ ⎦
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.