2 Replies - 344 Views - Last Post: 10 November 2017 - 07:29 AM Rate Topic: -----

#1 esadri21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-November 17

multplication 3 matrix gets error list index

Posted 09 November 2017 - 04:34 PM

I'm writing the full code, In this code we run into list index out of range error from python. but we have 7 loops and we must have `2*2*2*2*2*2=64` outputs, each loops contains 2 numbers.
We read a data file, calculating some equations and finally multiply 3 matrix into each other and print the final answer.
Note that we must have 64 out puts. if we move the `x` and `y` out of last loop we just find one answer in out put.

Could any one help what should I do?


I write a full code, using `sample.txt` data file as following

`sample.txt` is:

0.01 32.95388698
0.012 33.87900347
0.014 33.84214074
0.016 34.11856704
0.019 34.59344598
0.023 34.93902653
0.026 35.25209633
0.031 35.74850165
0.037 36.06978761
0.043 36.43457047
0.051 36.65111059



the full code is:

    from math import *
    import numpy as np
    from scipy.integrate import quad
    from scipy.integrate import odeint
    min=l=m=n=bb=b=t=chi=r=q1=q2=x=y=None


    z,mo=np.genfromtxt('sample.txt',unpack=True)
    r=[]
    def ant(z,O_m,O_D):            
        return 1/sqrt(((1+z)**2)*(1+O_m*z)-z*(2+z)*O_D)

    def HDE(y,z):
        a=1/(1+z)
        p=log(alpha+beta*a)
        pd=beta*a/(alpha+beta*a)    
        dydz = -((C**2-y)*((pd/p)*(y-2)-(pd**3)/(p**2)+(pd**2)/p+1.5*(y*(b+1)-1))/(1-(pd**2)/(p**2)+2*pd/p-0.5*(C**2+y)))*(1/(1+z))
        return dydz

    Cov=[[21282,-10840,1918,451,946,614,785,686,581,233],
    [0,28155,-2217,1702,74,322,380,273,424,487],
    [0,0,6162,-1593,1463,419,715,580,664,465],
    [0,0,0,5235,-722,776,588,591,583,403],
    [0,0,0,0,7303,-508,1026,514,596,315],
    [0,0,0,0,0,3150,-249,800,431,358],
    [0,0,0,0,0,0,3729,-88,730,321],
    [0,0,0,0,0,0,0,3222,-143,568],
    [0,0,0,0,0,0,0,0,3225,-508],
    [0,0,0,0,0,0,0,0,0,5646]]

    for B in range(1,3):
        b=0.01*B
        for m in range(7,9):
            M=0.1*m
            for c in range(5,7):
                C=0.1*c
                for o_m in range(23,25):
                    O_m=0.01*o_m
                    for AL in range(1,3):
                        alpha=AL
                        for BE in range(1,3):
                                beta=BE 
                                f=0
                                for i in range(len(z)):
                                    
                                    
                                    y0=1-O_m
                                    z0=0
                                    z1=z[i]
                                    y = odeint(HDE,y0,[z0,z1])
                                    O_D=y[-1,0]
                                    q=quad(ant,0,z[i],args=(O_m,O_D))[0]     #Integration o to z
                                    h=5*log10((1+z[i])*(299000/70)*q)+25     #function of dL
                                    r.append(mo[i]-M-h)  
                                    x=[[r[0],r[1],r[2],r[3],r[4],r[5],r[6],r[7],r[8],r[9],r[10]]
                                    y=[[r[0]],[r[1]],[r[2]],[r[3]],[r[4]],[r[5]],[r[6]],[r[7]],[r[8]],[r[9]],[r[10]],[r[11]],[r[12]],[r[13]],[r[14]],[r[15]],[r[16]],
                                         [r[17]],[r[18]],[r[19]],[r[20]],[r[21]],[r[22]],[r[23]],[r[24]],[r[25]],[r[26]],[r[27]],[r[28]],[r[29]],[r[30]]]
                                    q1=np.dot(x,Cov)  
                                    q2=np.dot(q1,y)
                                    chi=np.linalg.det(q2)  #Determinant of final matrix
                                
                                                         
                                    m=alpha
                                    n=beta                
                                    l=O_m
                                    bb=O_D
                                    t=C
                                    print('Minimum chi^2 =',chi,'alpha=',m,'beta=',n,'O_m=',l,'O_D=',bb,'c=',t)


IndexError: list index out of range

This post has been edited by andrewsw: 09 November 2017 - 11:20 PM
Reason for edit:: added missing [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: multplication 3 matrix gets error list index

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: multplication 3 matrix gets error list index

Posted 09 November 2017 - 11:21 PM

Please wrap your code in code tags, it demonstrates how to do this directly in the area where you typed your post. (These are essential for Python code.) I have added them for you this time.

Post the full error details.
Was This Post Helpful? 0
  • +
  • -

#3 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 320
  • View blog
  • Posts: 1,055
  • Joined: 27-December 13

Re: multplication 3 matrix gets error list index

Posted 10 November 2017 - 07:29 AM

Missing end ']' in line 55.
Indentation in line 55-68 is not correct (not blaming andrewsw, difficult to know what it should be) - please submit the code between code tags. Use the code icon just above the input window.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1