简体   繁体   中英

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. Anyways I have a Hessian matrix like this:

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:

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:

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. 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.

Use 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₉ ⎦

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM