Ma Y
Ma Y

Reputation: 706

An Integration leads to TypeError: 'numpy.float64' object cannot be interpreted as an integer

Dear Friends I have written a huge code, after running Python sends error: Sorry if the question is amateurish.

dacdm = 1/(z+1) * cdm
TypeError: 'numpy.float64' object cannot be interpreted as an integer

This is the 2 last sentences of the error. Python mentioned the following part of my code in error, so I decided that the error came from here:

def H_LCDM(z, H0, DM0):
        lcdm = 1/(H0 * sqrt(DM0 * (1 + z)**3 + (1 - DM0)))
        return lcdm

def DA_LCDM(z, DM0, H0):
         cdm = quad(H_LCDM, 0, z, args = (H0, DM0))
         dacdm = 1/(z+1) * cdm
         return dacdm


def fg(gg, DM0, H0, A, alpha, b, Omb):
        h = H0/100
        f_th = (0.824 /(1 + 0.19 * (h)**(1/2)))* (Omb/DM0) * (DA_LCDM(zgas[gg], H0, DM0)/DA(zgas[gg], DM0, H0, A, alpha, b))
        fgasth = ((fgas[gg] - f_th)/siggas[gg])**2
        return fgasth

def fgasmass(DM0, H0, A, alpha, b, Omb):
    h = H0/100
    num=0
    for gsgs in range(len(zgas)):
        num = num + fg(gsgs, DM0, H0, A, alpha, b, Omb)
        return num + ((Omb * h**2 - 0.0214)/0.002)**2 + ((h - 0.72)/0.08)**2

I could not manage to find the source of problem.

For more information, I put the whole error here:

 Traceback (most recent call last):
 File "C:\Users\esadr21\Desktop\Polytropic\Polytropic datasets.py", line 348, in <module>
 L = np.exp(-0.5 * (TOTAL(DM0n[i], H0n[i], An[i], alphan[i], bn[i], Mn[i], Ombn[i]) - TOTAL(DM0o[i-1], H0o[i-1], Ao[i-1], alphao[i-1], bo[i-1], Mo[i-1], Ombo[i-1])))    # L = exp(-( x^2 )/2)
  File "C:\Users\esadr21\Desktop\Polytropic\Polytropic datasets.py", line 328, in TOTAL
 total =  SN(DM0, H0, A, alpha, b, M) + BAO(DM0, H0, A, alpha, b, Omb) +  CMB(Omb, H0, DM0, A, alpha, b) + CC(DM0, H0, A, alpha, b) + SGL(DM0, H0, A,  alpha, b) + fgas(DM0, H0, A, alpha, b, Omb)
  File "C:\Users\esadr21\Desktop\Polytropic\Polytropic datasets.py", line 322, in fgas
num = num + fg(gsgs, DM0, H0, A, alpha, b, Omb)
 File "C:\Users\esadr21\Desktop\Polytropic\Polytropic datasets.py", line 315, in fg
f_th = (0.824 /(1 + 0.19 * sqrt(h)))* (Omb/DM0) * (DA_LCDM(zgas[gg], H0, DM0)/DA(zgas[gg], DM0, H0, A, alpha, b))
 File "C:\Users\esadr21\Desktop\Polytropic\Polytropic datasets.py", line 163, in DA_LCDM
dacdm = 1/(z+1) * cdm
TypeError: 'numpy.float64' object cannot be interpreted as an integer

THANK YOU SO MUCH.

Upvotes: 0

Views: 383

Answers (1)

user2357112
user2357112

Reputation: 280182

cdm is a sequence, rather than a number. Perhaps quad is scipy.integrate.quad, which returns a tuple of up to 5 elements, not just an integral. * with a sequence expects an integer representing a number of times to repeat the sequence; for example, we have

>>> 3 * (1, 2, 3)
(1, 2, 3, 1, 2, 3, 1, 2, 3)

If quad is scipy.integrate.quad, then the integral is the first element of the tuple, so if you wanted the integral, index it out of the result:

#                                         vvv
cdm = quad(H_LCDM, 0, z, args = (H0, DM0))[0]

Also, in the future, read the docs for the functions you use.

Upvotes: 1

Related Questions