How do I find out eigenvectors corresponding to a particular eigenvalue of a matrix?
Asked Answered
C

2

7

How do I find out eigenvectors corresponding to a particular eigenvalue?

I have a stochastic matrix(P), one of the eigenvalues of which is 1. I need to find the eigenvector corresponding to the eigenvalue 1.

The scipy function scipy.linalg.eig returns the array of eigenvalues and eigenvectors.

D, V = scipy.linalg.eig(P)

Here D(array of values) and V(array of vectors) are both vectors.

One way is to do a search in D and extract the corresponding eigenvector in V. Is there an easier way?

Condescendence answered 14/8, 2012 at 13:56 Comment(0)
G
7

If you are looking for one eigenvector corresponding to one eigenvalue, it could be much more efficient to use the scipy.sparse.linalg implementation of the eig function. It allows to look for a fixed number of eigenvectors and to shift the search around a specific value. You could do for instance :

values, vectors = scipy.sparse.linalg.eigs(P, k=1, sigma=1)
Gratuitous answered 14/8, 2012 at 15:30 Comment(3)
Does it return the left eigenvalue of P?Condescendence
I think you meant eigs not eig. I tried with scipy.sparse.linalg.eigs(P, k=1, sigma=1) but an exception is raised - raise NotImplementedError("shifted eigenproblem not supported yet")Condescendence
Fixed eig -> eigs. Shifted eigenproblem is implemented in the version I use : scipy 0.10.1Gratuitous
P
7
import numpy as np
import numpy.linalg as linalg


P = np.array([[2, 0, 0], [0, 1, 0], [0, 0, 3]])

D, V = linalg.eig(P)
print(D)
# [ 2.  1.  3.]

The eigenvectors are columns of V:

V = V.T

for val, vec in zip(D, V):
    assert np.allclose(np.dot(P, vec), val*vec)

So the eigenvector corresponding to eigenvalue 1.0 is

def near(a, b, rtol = 1e-5, atol = 1e-8):
    return np.abs(a-b)<(atol+rtol*np.abs(b))

print(V[near(D, 1.0)])
# [[ 0.  1.  0.]]

Since there can be more than one eigenvector with the same eigenvalue, V[near(D, 1.0)] returns a 2-dimensional array -- each row of the array is an eigenvector with an eigenvalue of 1.0.

Phytosociology answered 14/8, 2012 at 15:19 Comment(0)
G
7

If you are looking for one eigenvector corresponding to one eigenvalue, it could be much more efficient to use the scipy.sparse.linalg implementation of the eig function. It allows to look for a fixed number of eigenvectors and to shift the search around a specific value. You could do for instance :

values, vectors = scipy.sparse.linalg.eigs(P, k=1, sigma=1)
Gratuitous answered 14/8, 2012 at 15:30 Comment(3)
Does it return the left eigenvalue of P?Condescendence
I think you meant eigs not eig. I tried with scipy.sparse.linalg.eigs(P, k=1, sigma=1) but an exception is raised - raise NotImplementedError("shifted eigenproblem not supported yet")Condescendence
Fixed eig -> eigs. Shifted eigenproblem is implemented in the version I use : scipy 0.10.1Gratuitous

© 2022 - 2024 — McMap. All rights reserved.