2017-01-26 8 views
-1

のコマンドは、私は特定の機能を呼び出すために使用するキーワードを呼び出すことができますいくつかの機能を作成していた場合に使用して、 とValueError機能

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] 

またはより重要なのは、

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

間違った時間は何ですかえ?

答えて

0

EmitterDistance()には、integrate.quad()の最初の引数に関数が必要です。しかし、代わりに配列です。

0

この種のエラーは、スカラー値が必要なコンテキストで配列を使用すると発生します。 ifの式でa != -Infをテストしています。 abは、ここでは統合の境界点です。それらは配列ではなくスカラーでなければなりません。ここ

quad(integrand, a, a) 

aa = 1./(1.+z)で、zlinspaceによって生成配列である:しかし、あなたがquadを呼び出しています。

integrandも問題があります。 aから派生した配列です。代わりに、関数でなければなりません。スカラーをとり、値を返すもの。 HubbleParamが動作する可能性があります。lambdaまたはそれを使用した関数defです。