Evaluating Jacobian at specific points using sympy

10,337

Solution 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]])

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

Share:
10,337
Chikorita Rai
Author by

Chikorita Rai

Updated on June 18, 2022

Comments

  • Chikorita Rai
    Chikorita Rai almost 2 years

    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