Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在函数中使用if命令时出现ValueError_Python_Numpy_If Statement_Scipy - Fatal编程技术网

Python 在函数中使用if命令时出现ValueError

Python 在函数中使用if命令时出现ValueError,python,numpy,if-statement,scipy,Python,Numpy,If Statement,Scipy,我正在创建一些可以调用的函数,使用关键字调用特定函数 import scipy.integrate as integrate import numpy as np def HubbleParam(a, model = "None"): if model == "LCDM": Omega_L0 = 0.7 Omega_m0 = 0.3 return np.sqrt( Omega_m0/a/a/a+ Omega_L0 ) if

我正在创建一些可以调用的函数,使用关键字调用特定函数

import scipy.integrate as integrate
import numpy as np

def HubbleParam(a, model = "None"):
    if model == "LCDM":
        Omega_L0 = 0.7
        Omega_m0 = 0.3 
        return np.sqrt( Omega_m0/a/a/a+ Omega_L0  )

    if model == "Q":
        Omega_Q0 = 0.7
        Omega_m0 = 0.3 
        return np.sqrt( Omega_m0/a/a/a + Omega_Q0/a )


def EmitterDistance(z, model = "None"):
    a = 1./(1.+z)
    if model == 'LCDM':
        integrand = 1./a/a/HubbleParam(a, model="LCDM")
        return [z , integrate.quad(integrand, a, 1.)[0] ]

    if model == "Q":
        integrand = 1/a/a/HubbleParam(a, model="Q")
        return [z, integrate.quad(inta, a, 1.)[0] ]

z = np.linspace(0.,5., 1000)

print EmitterDistance(z, model="LCDM")
尝试打印此数组时,将返回

Traceback (most recent call last):
  File      "/Users/alexandres/Desktop/Formation_Galaxies/Homework1/FoG_HW1.py", line 95, in <module>
    print EmitterDistance(z, model="LCDM")
  File "/Users/alexandres/Desktop/Formation_Galaxies/Homework1/FoG_HW1.py", line 87, in EmitterDistance
    return [z , integrate.quad(integrand, a, a)[0] ]
  File     "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/integrate/quadpack.py", line 315, in quad
points)
  File     "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/integrate/quadpack.py", line 364, in _quad
    if (b != Inf and a != -Inf):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
[Finished in 0.5s with exit code 1]
[shell_cmd: python -u     "/Users/alexandres/Desktop/Formation_Galaxies/Homework1/FoG_HW1.py"]
[dir: /Users/alexandres/Desktop/Formation_Galaxies/Homework1]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
回溯(最近一次呼叫最后一次):
文件“/Users/alexandres/Desktop/Formation_galays/Homework1/FoG_HW1.py”,第95行,在
打印发射器距离(z,model=“LCDM”)
文件“/Users/alexandres/Desktop/Formation_galays/Homework1/FoG_HW1.py”,第87行,发射距离
返回[z,integrate.quad(被积函数,a,a)[0]]
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/scipy/integrate/quadpack.py”,第315行,四行
积分)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/integrate/quadpack.py”,第364行,四行
如果(b!=Inf和a!=Inf):
ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all()
[在0.5s内完成,退出代码为1]
[shell_cmd:python-u”/Users/alexandres/Desktop/Formation_galays/Homework1/FoG_HW1.py”]
[dir:/Users/alexandres/Desktop/Formation\u/Homework1]
[路径:/usr/bin:/bin:/usr/sbin:/sbin]
或者更重要的是

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


这里出了什么问题?

EmitterDistance()
中,
integrate.quad()
的第一个参数应该是一个函数。但是它是一个数组。

当您在需要标量值的上下文中使用数组时,会出现这种错误。它正在测试
a!=-Inf
if
表达式中
a
b
这里是积分的边界点。它们应该是标量,而不是数组。但您正在使用以下命令调用
quad

 quad(integrand, a, a)
a
这里是
a=1./(1.+z)
,而
z
是由
linspace
生成的数组

integrand
看起来也有问题。它是从
a
派生的数组。相反,它应该是一个函数。接受标量并返回值的东西
HubbleParam
可能工作,或者使用它的
lambda
或函数def可能工作