lzcostademoraes
lzcostademoraes

Reputation: 83

Store complex eigenvectors from linalg.eig into an array in Python

I'm having problems storing the out put of numpy.linalg.eig(). I want to store then into two different array. This is the way I've tried:

vec1 = np.zeros(y.shape[0],dtype=complex)
vec2 = np.zeros(y.shape[0],dtype=complex)
for i in np.arange(y.shape[0]):
    val,vec= np.linalg.eig(rho_t[:,:,i])
    vec1[i] = vec[0]
    vec2[i] = vec[1]

The ERROR message is the following:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-389-791a7e5e4801> in <module>
      3 for i in np.arange(y.shape[0]):
      4     val,vec= np.linalg.eig(rho_t[:,:,i])
----> 5     vec1[i] = vec[0]
      6     vec2[i] = vec[1]
      7     #vec2[i] = np.array(sol[1][1])

TypeError: only length-1 arrays can be converted to Python scalars

No idea what is the problem, can somebody help me please

Upvotes: 0

Views: 298

Answers (1)

Ralvi Isufaj
Ralvi Isufaj

Reputation: 482

According to documentation: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.eig.html

The normalized (unit “length”) eigenvectors, such that the column v[:,i] is the eigenvector corresponding to the eigenvalue w[i].

so perhaps the solution is this:

for i in np.arange(y.shape[0]):
    val,vec= np.linalg.eig(rho_t[:,:,i])
    vec1[i] = vec[:,0]
    vec2[i] = vec[:,1]

Upvotes: 1

Related Questions