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

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

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

"ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or 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))

Attached is a picture of the full error message

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,您必须 运行 您的代码,phibdoubleintegralt 的每个元素进行一次。在 numpy 中,我们尝试避免迭代,但 quad 仅适用于标量边界。所以需要很好的旧迭代。