Evaluating Jacobian at specific points using sympy

二次信任 提交于 2019-12-07 08:42:46

问题


I am trying to evaluate the Jacobian at (x,y)=(0,0) but unable to do so.

import sympy as sp
from sympy import *
import numpy as np
x,y=sp.symbols('x,y', real=True)
J = Function('J')(x,y)
f1=-y
f2=x - 3*y*(1-x**2)
f1x=diff(f1,x)
f1y=diff(f1,y)
f2x=diff(f2,x)
f2y=diff(f2,y)
J=np.array([[f1x,f1y],[f2x,f2y]])
J1=J(0,0)
print J1

The error corresponding to

---> 16 J1=J(0,0)

is

TypeError: 'numpy.ndarray' object is not callable 

回答1:


The error you're getting is indeed because you're rebinding J to a numpy array which is not a callable.

You should use the subs method of sympy expressions to evaluate an expression in a point (as described in the basic operations documentation of Sympy):

J = sympy.Matrix([[f1x,f1y],[f2x,f2y]])
J.subs([(x,0), (y,0)])

Also, you might be interested in knowing that sympy offers a jacobian method too:

>>> F = sympy.Matrix([f1,f2])
>>> F.jacobian([x,y])
Matrix([
[        0,         -1],
[6*x*y + 1, 3*x**2 - 3]])
>>> F.jacobian([x,y]).subs([(x,0), (y,0)])
Matrix([
[0, -1],
[1, -3]])



回答2:


I'm not sure, because I don't know sympy. You created function:

J = Function('J')(x,y)

and next step you assigned numpy array to J:

J = np.array([[f1x,f1y],[f2x,f2y]])

You called numpy array as function.



来源:https://stackoverflow.com/questions/26669706/evaluating-jacobian-at-specific-points-using-sympy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!