Eigenvalues are scalar solutions to the equation Ax = ax
, where
A
is a two-dimensional matrix and x
is a one-dimensional vector. Eigenvectors are vectors corresponding to eigenvalues.
The eigvals()
subroutine in the numpy.linalg
package computes eigenvalues. The eig()
function gives back a tuple holding eigenvalues and eigenvectors.
We will obtain the eigenvalues and eigenvectors of a matrix with the eigvals()
and eig()
functions of the numpy.linalg
subpackage. We will check the outcome by applying the dot()
function (see eigenvalues.py
in this book's code):
import numpy as np A = np.mat("3 -2;1 0") print "A ", A print "Eigenvalues", np.linalg.eigvals(A) eigenvalues, eigenvectors = np.linalg.eig(A) print "First tuple of eig", eigenvalues print "Second tuple of eig ", eigenvectors for i in range(len(eigenvalues)): print "Left", np.dot(A, eigenvectors[:,i]) print "Right", eigenvalues[i] * eigenvectors[:,i] print
Let's calculate the eigenvalues of a matrix:
The following code will create a matrix:
A = np.mat("3 -2;1 0") print "A ", A
The matrix we created looks like this:
A [[ 3 -2] [ 1 0]]
eig()
function.Apply the eig()
subroutine:
print "Eigenvalues", np.linalg.eigvals(A)
The eigenvalues of the matrix are as follows:
Eigenvalues [ 2. 1.]
eig()
.Get the eigenvalues and eigenvectors with the eig()
function. This routine returns a tuple, where the first element holds eigenvalues and the second element contains matching eigenvectors
, set up column-wise:
eigenvalues, eigenvectors = np.linalg.eig(A) print "First tuple of eig", eigenvalues print "Second tuple of eig ", eigenvectors
The eigenvalues
and eigenvectors
values will be:
First tuple of eig [ 2. 1.] Second tuple of eig [[ 0.89442719 0.70710678] [ 0.4472136 0.70710678]]
Check the answer with the dot
()
function by computing both sides of the eigenvalues equation Ax = ax
:
for i in range(len(eigenvalues)): print "Left", np.dot(A, eigenvectors[:,i]) print "Right", eigenvalues[i] * eigenvectors[:,i] print
The output is as follows:
Left [[ 1.78885438] [ 0.89442719]] Right [[ 1.78885438] [ 0.89442719]] Left [[ 0.70710678] [ 0.70710678]] Right [[ 0.70710678] [ 0.70710678]]
3.148.104.124