Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

python - ValueError when using if commands in function

I'm creating some functions that I can call use keywords to call out specific functions,

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")

When trying to print this array, this is returned,

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]

or more importantly

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

What is wrong here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In EmitterDistance() your first argument to integrate.quad() should a function. But it is an array, instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...