使用 sympy 在特定点评估雅可比行列式 [英] Evaluating Jacobian at specific points using sympy

查看:32
本文介绍了使用 sympy 在特定点评估雅可比行列式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 (x,y)=(0,0) 处评估雅可比行列式,但无法这样做.

import sympy as sp从 sympy 导入 *将 numpy 导入为 npx,y=sp.symbols('x,y', real=True)J = 函数('J')(x,y)f1=-yf2=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)打印 J1

对应的错误

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

TypeError: 'numpy.ndarray' 对象不可调用

解决方案

你得到的错误确实是因为你正在重新绑定 J 到一个 numpy 数组不是可调用的.

您应该使用 sympy 表达式的 subs 方法来计算点中的表达式(如 Sympy 的基本操作文档):

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

另外,你可能有兴趣知道 sympy 也提供了一个 jacobian 方法:

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

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 

解决方案

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

这篇关于使用 sympy 在特定点评估雅可比行列式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆