Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluating Jacobian at specific points using sympy

Tags:

python

sympy

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 
like image 272
Chikorita Rai Avatar asked Oct 31 '14 07:10

Chikorita Rai


People also ask

How do you evaluate a SymPy expression?

To evaluate a numerical expression into a floating point number, use evalf . SymPy can evaluate floating point expressions to arbitrary precision. By default, 15 digits of precision are used, but you can pass any number as the argument to evalf .

How do you find the determinant of a matrix in SymPy?

With the help of sympy. det() method, we can find the determinant of a matrix by using sympy. det() method.

Is SymPy fast?

SymPy (as of now) is purely Python-based and hence slow.


1 Answers

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]])
like image 65
Oliver W. Avatar answered Oct 20 '22 01:10

Oliver W.