几天前我在 stackoverflow 上问了这个问题,但我认为 scicomp.stackexchange 可能是一个更好的地方。重复发帖,抱歉。
我想使用 scipy.root 求解非线性方程组。出于性能原因,我想使用 LinearOperator 提供系统的雅可比。但是,我无法让它工作。这是一个使用 Rosenbrock 函数梯度的最小示例,我首先将 Jacobian(即 Rosenbrock 函数的 Hessian)定义为 LinearOperator。
import numpy as np
import scipy.optimize as opt
import scipy.sparse as sp
ndim = 10
def rosen_hess_LO(x):
return sp.linalg.LinearOperator((ndim,ndim), matvec = (lambda dx,xl=x : opt.rosen_hess_prod(xl,dx)))
opt_result = opt.root(fun=opt.rosen_der,x0=np.zeros((ndim),float),jac=rosen_hess_LO)
执行后,我收到以下错误:
TypeError: fsolve: there is a mismatch between the input and output shape of the 'fprime' argument 'rosen_hess_LO'.Shape should be (10, 10) but it is (1,).
我在这里想念什么?
编辑:这个问题的部分答案可以在这里找到。