Reputation: 3
Does anyone knows why it's telling me list index out of range? I'm trying to "translate" a Matlab file and I created lists instead of cell arrays, the main problem now lies in the index, it seems correct to me and I don't know how there is an issue
#Projection operator Pi
piOperator = np.zeros((N*N, N*N))
#Psi0 state
Psi0 = np.zeros((N*N, 1))
for i in xrange(0 , N-1 ):
aux = np.zeros((N,1)) #Auxiliary vector
aux[i]= 1
A = np.sqrt(G[:,i])
psi = []
P = []
psi.append(np.tensordot(aux, A))
P.append(np.dot(psi[i],np.transpose(psi[i])))
piOperator = piOperator + P[i]
Psi0 = Psi0 + psi[i]
Psi0 = 1/np.sqrt(N)*Psi0
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-450-0816498ad018> in <module>()
11 P = []
12 psi.append(np.tensordot(aux, A))
---> 13 P.append(np.dot(psi[i],np.transpose(psi[i])))
14 piOperator = piOperator + P[i]
15 Psi0 = Psi0 + psi[i]
IndexError: list index out of range
The original Matlab code was:
Pi = zeros(n^2,n^2);
Psi0 = zeros(n^2,1);
for k=1:n
aux = zeros(n,1);
aux(k) = 1;
psi{k} = kron(aux,sqrt(G(:,k)));
P{k} = psi{k} * psi{k}';
Pi = Pi + P{k};
Psi0 = Psi0 + psi{k};
end
Psi0 = 1/sqrt(n)*Psi0;
Upvotes: 0
Views: 70
Reputation: 782
You are clearing your psi variable every iteration. Just initialize it before the loop:
#Projection operator Pi
piOperator = np.zeros((N*N, N*N))
#Psi0 state
Psi0 = np.zeros((N*N, 1))
psi = []
P = []
for i in xrange(0 , N-1 ):
aux = np.zeros((N,1)) #Auxiliary vector
aux[i]= 1
A = np.sqrt(G[:,i])
psi.append(np.tensordot(aux, A))
P.append(np.dot(psi[i],np.transpose(psi[i])))
piOperator = piOperator + P[i]
Psi0 = Psi0 + psi[i]
Psi0 = 1/np.sqrt(N)*Psi0
Upvotes: 2