scipy 对numpy中的行列式进行最小化时会出现“Cannot cast ufunc 'det'...”错误

14ifxucb  于 2023-03-18  发布在  其他
关注(0)|答案(1)|浏览(317)

我想用numpy/scipy最小化一个行列式,但是我得到了错误“UFuncTypeError:无法使用强制转换规则“same_kind”将ufunc“det”输入从数据类型('O')强制转换为数据类型('float 64')“
最小工作示例:

from scipy.optimize import minimize
import numpy as np

def N(xi,f):
    return np.array([[1,f],[xi,1]])

xi = 0.001
obj = lambda f: np.abs(np.linalg.det(N(xi,f)))
x0= 0.056

obj(0.054)
# returns 0.999946

m = minimize(obj, x0)
# returns UFuncTypeError: Cannot cast ufunc 'det' input from dtype('O') to dtype('float64') with casting rule 'same_kind'
2eafrhcq

2eafrhcq1#

赫保利的建议基本上是正确的,这一点让它发挥了作用:

from scipy.optimize import minimize
import numpy as np

def N(xi,f):
    val = []
    for ff in f:
        val.append( np.array([[1,ff],[xi,1]]) )
    return val

xi = 0.001

def obj (f): 
    return np.abs(np.linalg.det(N(xi,f)))

x0= 0.056

obj([0.054])
# returns 0.999946

m = minimize(obj, x0)

相关问题