TypeError的原因是什么:'Zero'对象没有属性'__getitem__'?

时间:2016-06-01 06:56:56

标签: python python-2.7 numpy typeerror zero

我在python 2.7中编写了以下代码,以便以数字方式计算集成,然后将此集成的结果用于项目的进一步步骤。

import numpy as np
from scipy import linspace,logspace
from cosmicpy import *



Omega_Matter, Omega_DarkEnergy, A, b, rho_critical, m = 0.306, 0.694, 0.3222, 0.707, 2.77536627e+11, 1000000

def D(z):
    a = 1/(1+z)
    x = (Omega_DarkEnergy/Omega_Matter)**(1/3)*a
    return (5/2)*(Omega_Matter/Omega_DarkEnergy)**(1/3)*x**(-3/2)*(1+x**3)**(1/2)* \
           (x**2/(3*x**3 + 3) - np.log(x + 1)/9 + np.log(x**2 - x + 1)/18 \
                          + np.sqrt(3)*np.arctan(2*np.sqrt(3)*x/3 \
                  - np.sqrt(3)/3)/9 + np.sqrt(3)*np.pi/54)     

def delta(z): 
    return D(z)/D(0) 
def W(k, M):
    rho_m = rho_critical*Omega_Matter
    R = (3*M/(4*np.pi*rho_m))**(1/3)
    x = k*R
    return (3/x)*(sin(x)-x*cos(x))

my_cosmology = cosmology(Omega_m=0.306, Omega_de=0.694, h=0.679, Omega_b=0.0483, n=0.968, tau=0.067, sigma8=0.815, w=-1)
k_array = np.logspace(-16,4,m)
P = my_cosmology.pk_lin(k_array)

def sigma_squared(z, M):
    dk = (np.max(k_array)-np.min(k_array))/(m-1)
    summation = []
    for k in k_array:
        Integral = 0
        Integrand = k**2*P[k]*(W(k, M))**2
        Integral += dk * np.sum(Integrand[k])  
    summation.append(Integral)  
    return ((delta(z))**2/(2*(np.pi)**2))*summation[-1]
    print(summation)  


sigma_squared(0.01, 1e+9) 

当我编写更多代码时,我通过获取打印逐个检查我的步骤,看看输出是否符合我的预期。但是,我无法生成最后一个函数的最终产品,该函数应该是给定变量z和M的值的值。特别是我确信该函数内部的集成有问题,因为我不是得到print(summation)的任何东西,它应该是一个大的1d数组,其最后一个元素print(summation[-1])应该给我曲线下的区域(直到在函数的最终返回中定义的前因子)。这是错误消息,我找不到我得到的特定错误消息的任何在线源。非常感谢您的帮助。

  

mycode.py:95:VisibleDeprecationWarning:使用非整数   而不是整数将导致将来出错   Integrand = k ** 2 * P [k] *(W(k,M))** 2追溯(最近一次调用最后一次):   文件“mycode.py”,第102行,in       sigma_squared(0.01,1e + 9)文件“mycode.py”,第96行,sigma_squared       积分+ = dk * np.sum(Integrand [k])TypeError:'零'对象没有属性' getitem '

已编辑的代码(速度太慢,无法确定其是否正常工作):

k_array = np.logspace(-16,4,m)
my_cosmology = cosmology(Omega_m=0.306, Omega_de=0.694, h=0.679, Omega_b=0.0483, n=0.968, tau=0.067, sigma8=0.815, w=-1)
P = my_cosmology.pk_lin(k_array)
M_array = np.logspace(8,16,n)

def W(k, M):
    rho_m = rho_critical*Omega_Matter
    R = (3*M/(4*np.pi*rho_m))**(1/3)
    y = k*R
    return (3/y**3)*(sin(y)-y*cos(y))


def sigma_squared(z, M):
    dk = (np.max(k_array)-np.min(k_array))/(m-1)
    summation = []
    for k in k_array:
        for M in M_array:
            Integral = 0
            Integrand = k**2*P[k]*(W(k, M))**2
            Integral += dk * np.sum(Integrand)
    summation.append(Integral)  
    return ((delta(z))**2/(2*(np.pi)**2))*summation[-1]


print(sigma_squared(0, 1e+9))

0 个答案:

没有答案