list index out of range - From Fortran to Python

Asked

Viewed 82 times

1

I wrote a program in Fortran and now I’m trying to write it in Python. However, it’s giving an error:

D[i][j+1] = - u_med[i+1]/(delta_r[i]*delta_r[i+1])

Indexerror: list index out of range

I’m a beginner in Python, what could possibly go wrong? This is the code:

D = []
u_med = []
delta_r = []
for i in range(2,n):
  for j in range(2,n):
    if i == j:
      D[i][j+1] = - u_med[i+1]/(delta_r[i]*delta_r[i+1])
      t1 = u_med[i+1]/(delta_r[i]*delta_r[i])
      t2 = u_med[i]/(delta_r[i]*delta_r[i])
      D[i][j]= t1 + t2 + V[i]
      D[i+1][j]= - u_med[i+1]/(delta_r[i]*delta_r[i+1])

t1 = u_med[2]/(delta_r[1]*delta_r[1])
t2 = 0
D[1][1]= t1 + t2 + V[1]
D[1][2]= - u_med[2]/(delta_r[1]*delta_r[2])
D[2,1]= - u_med[2]/(delta_r[2]*delta_r[1])
t1 = 0
t2 = u_med[n]/(delta_r[n]*delta_r[n])

D[n][n]= t1 + t2 + V[n]

Edit: I’m working with the one-dimensional Schrödinger equation on a variable grid... Anyway, I’m trying to write it in matrix form, with a symmetric N x N matrix.

Same code in Fortran:

do i=2,n-1
do j=2,n-1

if (i.eq.j) then

D(i,j+1)=-u_med(i+1)/(delta_r(i)*delta_r(i+1))
t1 =u_med(i+1)/(delta_r(i)*delta_r(i))
t2 = u_med(i)/(delta_r(i)*delta_r(i))
D(i,j)= t1 + t2 + V(i)
D(i+1,j)=-u_med(i+1)/(delta_r(i)*delta_r(i+1))

end if

end do
end do

t1 =u_med(2)/(delta_r(1)*delta_r(1))
t2 = 0.d0

D(1,1)= t1 + t2 + V(1)
D(1,2)= -u_med(2)/(delta_r(1)*delta_r(2))
D(2,1)= -u_med(2)/(delta_r(2)*delta_r(1))

t1 =0.d0
t2 = u_med(n)/(delta_r(n)*delta_r(n))

D(n,n)= t1 + t2 + V(n)
  • 2

    All your lists are empty, so no index you have used will exist. What exactly do you need to do? Can describe with words, editing the question?

  • Hi, thanks for the answer, but I don’t understand.. How come all the lists are empty? In Python I first need to create an empty vector, then add the information in the loop?

1 answer

2


See an excerpt of your code with some comments:

  1 D = []                                                                                                                                                                                                                       
  2 #    ↳ Aqui você definiu D como uma lista vazia                                                                                                                                                                              
  3                                                                                                                                                                                                                              
  4 u_med = []                                                                                                                                                                                                                   
  5 #        ↳ Aqui você definiu u_med como uma lista vazia                                                                                                                                                                      
  6                                                                                                                                                                                                                              
  7 delta_r = []                                                                                                                                                                                                                 
  8 #          ↳ Aqui você definiu delta_r como uma lista vazia                                                                                                                                                                  
  9                                                                                                                                                                                                                              
 10 #                  ↱ Aqui você utilizou n que não está definido                                                                                                                                                              
 11 for i in range(2, n):                                                                                                                                                                                                        
 12     for j in range(2, n):                                                                                                                                                                                                    
 13     ┆   if i == j:                                                                                                                                                                                                           
 14     ┆   ┆   D[i][j+1] = - u_med[i+1]/(delta_r[i]*delta_r[i+1])                                                                                                                                                               
 15     ┆   ┆   #   |             |            |         ↳ Aqui você está acessando a posição i+1 de uma lista vazia                                                                                                             
 16     ┆   ┆   #   |             |            ↳ Aqui você está acessando a posição i de uma lista vazia                                                                                                                         
 17     ┆   ┆   #   |             ↳ Aqui você está acessando a posição i+1 de uma lista vazia                                                                                                                                    
 18     ┆   ┆   #   ↳ Aqui você está tentando definir o valor da posição i, j+1 de uma lista vazia                                                                                                                               
 19     ┆   ┆   t1 = u_med[i+1]/(delta_r[i]*delta_r[i])                                                                                                                                                                          
 20     ┆   ┆   t2 = u_med[i]/(delta_r[i]*delta_r[i])                                                                                                                                                                            
 21     ┆   ┆   D[i][j]= t1 + t2 + V[i]                                                                                                                                                                                          
 22     ┆   ┆   D[i+1][j]= - u_med[i+1]/(delta_r[i]*delta_r[i+1])   

The error occurs on line 14 precisely by trying to access values in empty lists, this considering that the object n is properly defined in your code and just not posted in the question.

Since it makes no sense to access values from empty lists, there is no way to infer what you want to do, so I asked on commenting to describe, in words, what the purpose of the code is. Without the description, there is no way to help you beyond that.

  • All right, the information provided was very useful. Thank you very much. I managed to solve the problem. Since this is the construction of a tridiagonal matrix, I ended up using np.zeros(n,2) for the list in question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.