I'd like to calculate the derivative, then solve for when it is zero.
我想计算导数,然后在它为零时求解。
I am using the sympy module to do this.
我正在使用sympy模块来执行此操作。
r = somefunction(x1,x2)
Using this function, I'd like to be able to call these two matrices.
使用此功能,我希望能够调用这两个矩阵。
r_grad = [r.diff(x1), r.diff(x2)]
r_hess = [[r.diff(x1,x1), r.diff(x1,x2)],[r.diff(x2,x1), r.diff(x2,x2)]]
I'd then like to solve for when r_grad[0] and r_grad[1] == 0, and plug that into the hessian. How can I make these .diff() symbols callable?
然后我想解决r_grad [0]和r_grad [1] == 0时的问题,并将其插入到粗麻布中。如何使这些.diff()符号可调用?
1 个解决方案
#1
2
SymPy has a lambdify module for these purposes:
SymPy有一个lambdify模块用于以下目的:
from sympy.utilities.lambdify import lambdify
func = lambdify((x1, x2), r.diff(x1))
func(1, 2) # evaluate the function efficiently at (1, 2)
#1
2
SymPy has a lambdify module for these purposes:
SymPy有一个lambdify模块用于以下目的:
from sympy.utilities.lambdify import lambdify
func = lambdify((x1, x2), r.diff(x1))
func(1, 2) # evaluate the function efficiently at (1, 2)