繁体   English   中英

为什么我定义的函数不接受数组作为输入?

[英]Why does my defined function not accept an array as an input?

我正在使用二重积分,并且内部积分具有可变边界。 我使用 SciPy 的四元积分编写了一个函数,它允许我评估这个积分。 但是,我想要的只是评估内部积分,这样我剩下的就是关于某个变量的单个未评估的积分。 然后我想绘制这个“半途”评估的二重积分与该变量的范围,以便我可以看到一些趋势。 但是,当我输入该变量的数组时(它只是 0-10000 但增量为 1,它提供以下错误消息:

“ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()”

我之前已经定义了函数,允许我输入一个数组(无论数组中有多少点都输出该函数),所以我不确定为什么现在会弹出这个消息。 我认为这与我在定义函数时使用 SciPy 的“quad”集成有关。 我该如何解决这个问题,以便我可以输入一个数组?

import numpy as np
from scipy import integrate
from scipy.integrate import quad
import matplotlib.pyplot as plt


#This is the array of values of the variable I ultimately want to have the function plotted 
against
timearray = np.arange(0,10000,1)

#This below defines the general function, that is with respect to two variables (p and t)
def thomtest(p,t):
    d = 3.086e22
    c = 2.998e10
    return (3*((np.cos(p + (2*t/(d*p))))**2))/(8*(t+((p**2)*d/(2*c))))


#The function below evaluates just the inner-integral
def phib(t):
    d = 3.086e22
    c = 2.998e10
    return quad(thomtest,0.00001*(c*t)/d,np.pi, args=(t))[0]


#This evaluates the outer-integral, giving me the complete numerical answer
doubleintegral = quad(phib,0,((3.086e22)/(2.998e10)))


#This below is what gives me the error message: "ValueError: The truth 
#value of an array with more than one element is ambiguous.
#Use a.any() or a.all()". Apparently I cannot input an array
print(phib(timearray))

附件是完整错误信息的图片

In [2]: doubleintegral                                                                                               
Out[2]: (0.9892936902920587, 1.3643899787751934e-08)
In [3]: phib(0)                                                                                                      
Out[3]: 6.377997354641736e-10
In [4]: phib(np.arange(0,5))                                                                                         
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-89b7b858c2f5> in <module>
----> 1 phib(np.arange(0,5))

<ipython-input-1-4c64e69e4f62> in phib(t)
     10     d = 3.086e22
     11     c = 2.998e10
---> 12     return quad(thomtest,0.00001*(c*t)/d,np.pi, args=(t))[0]
     13 
     14 doubleintegral = quad(phib,0,((3.086e22)/(2.998e10)))

/usr/local/lib/python3.6/dist-packages/scipy/integrate/quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
    336 
    337     # check the limits of integration: \int_a^b, expect a < b
--> 338     flip, a, b = b < a, min(a, b), max(a, b)
    339 
    340     if weight is None:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
In [5]:       

当它测试集成边界时会出现错误,确保第一个小于第二个。

但是对于数组t ,该测试是多值的,不能使用:

In [6]: d = 3.086e22  
   ...: c = 2.998e10                                                                                                 
In [7]: 0.00001*(c*np.arange(5))/d                                                                                   
Out[7]: 
array([0.00000000e+00, 9.71484122e-18, 1.94296824e-17, 2.91445237e-17,
       3.88593649e-17])

使用quad您必须为t每个元素运行一次代码、 phibdoubleintegral numpy我们尽量避免迭代,但quad仅适用于标量边界。 所以需要好的旧迭代。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM