我想集成一个3x3矩阵,并让它以3x3矩阵的形式返回解决方案,但是我不确定在python中是否可行。有没有我可以使用的命令?如果没有,我是否应该设置某种for循环(如果是这种情况,是否可以在此方面提供一些帮助)?
我尝试使用其他建议,例如vectorize(integrate.quad)(integrandtemp,0、10)和vectorize(quad)(integrandtemp,0、10),但我遇到了与我相同的错误:“仅size-1数组可以转换为Python标量”。
def integrandtemp(s):
K = 17.5
r = 0.7
x0 = 0.1
t = 5
x = K/(1+((K/x0)-1)*np.exp(-r*s))
dxdK = (x0*x0-x0*x0*np.exp(-r*s))/((x0+K*np.exp(-r*s)-x0*np.exp(-r*s))**2)
dxdr = (K*K*x0*s*np.exp(-r*s)-K*x0*x0*s*np.exp(-r*s))/((x0+K*np.exp(-r*s)-x0*np.exp(-r*s))**2)
dxdx0 = (K*x0+K*K*np.exp(-r*s)-K*x0*np.exp(-r*s)-K*x0+K*x0*np.exp(-r*s))/((x0+K*np.exp(-r*s)-x0*np.exp(-r*s))**2)
M = [dxdK, dxdr, dxdx0]
M = np.array([M])
print(M)
transpose = M.T
print(transpose)
var = 0.16
print(transpose@M)
return (1/var)*transpose@M
F = vectorize(integrate.quad)(integrandtemp, 0, 10)
F_inv = inv(F)
我收到此错误消息:
TypeError Traceback (most recent call last)
<ipython-input-106-a2757b67abc5> in <module>
34 return (1/var)*transpose@M
35
---> 36 F = vectorize(integrate.quad)(integrandtemp, 0, 10)
37 F_inv = inv(F)
38
~/anaconda3/lib/python3.7/site-packages/numpy/lib/function_base.py in __call__(self, *args, **kwargs)
1970 vargs.extend([kwargs[_n] for _n in names])
1971
-> 1972 return self._vectorize_call(func=func, args=vargs)
1973
1974 def _get_ufunc_and_otypes(self, func, args):
~/anaconda3/lib/python3.7/site-packages/numpy/lib/function_base.py in _vectorize_call(self, func, args)
2040 res = func()
2041 else:
-> 2042 ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
2043
2044 # Convert args to object arrays first
~/anaconda3/lib/python3.7/site-packages/numpy/lib/function_base.py in _get_ufunc_and_otypes(self, func, args)
2000
2001 inputs = [arg.flat[0] for arg in args]
-> 2002 outputs = func(*inputs)
2003
2004 # Performance note: profiling indicates that -- for simple
~/anaconda3/lib/python3.7/site-packages/scipy/integrate/quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
339 if weight is None:
340 retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit,
--> 341 points)
342 else:
343 retval = _quad_weight(func, a, b, args, full_output, epsabs, epsrel,
~/anaconda3/lib/python3.7/site-packages/scipy/integrate/quadpack.py in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points)
446 if points is None:
447 if infbounds == 0:
--> 448 return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
449 else:
450 return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)
TypeError: only size-1 arrays can be converted to Python scalars
我需要它返回另一个3x3矩阵,以便我可以求逆并将其用于实际尝试求解的事情。
答案 0 :(得分:0)
quadpy(属于我的项目)对数组值函数进行正交运算:
<div class="imgs">
<img src="https://example.com/upload/zxcvbn.jpg">
<img src="https://example.com/upload/qwerty.jpg">
</div>
import numpy as np
import quadpy
def integrandtemp(S):
out = []
for s in S:
K = 17.5
r = 0.7
x0 = 0.1
dxdK = (x0 * x0 - x0 * x0 * np.exp(-r * s)) / (
(x0 + K * np.exp(-r * s) - x0 * np.exp(-r * s)) ** 2
)
dxdr = (K * K * x0 * s * np.exp(-r * s) - K * x0 * x0 * s * np.exp(-r * s)) / (
(x0 + K * np.exp(-r * s) - x0 * np.exp(-r * s)) ** 2
)
dxdx0 = (
K * x0
+ K * K * np.exp(-r * s)
- K * x0 * np.exp(-r * s)
- K * x0
+ K * x0 * np.exp(-r * s)
) / ((x0 + K * np.exp(-r * s) - x0 * np.exp(-r * s)) ** 2)
M = [dxdK, dxdr, dxdx0]
M = np.array([M])
transpose = M.T
var = 0.16
out.append((1 / var) * transpose @ M)
out = np.array(out)
out = np.moveaxis(out, 0, -1)
return out
scheme = quadpy.line_segment.gauss_legendre(10)
val = scheme.integrate(integrandtemp, [0, 10])
print(val)
除了循环遍历[[4.78541989e+00 2.77802115e+02 3.35326738e+02]
[2.77802115e+02 2.32166322e+04 3.12478945e+04]
[3.35326738e+02 3.12478945e+04 4.37373431e+04]]
之外,您还可以向量化函数以提高速度。请注意,输出的形状必须为S
。