SciPy - Eigenvalues and Eigenvectors



In SciPy Eigenvalues and Eigenvectors are computed as part of solving problems involving linear transformations specifically for square matrices. These values and vectors provide insight into the properties of a matrix and are widely used in various scientific and engineering domains.

Before understanding how scipy is used to work with the Eigenvalues and Eigenvectors, let's understand about Eigenvalues and Eigenvectors as follows −

What are Eigenvalues and Eigenvectors?

Eigenvalues and Eigenvectors are mathematical concepts used in linear algebra to analyze transformations represented by square matrices. They provide insights into how a matrix transforms a vector.

What is Eigenvector (v)?

An Eigenvector of a matrix A is a non-zero vector that only changes in scale i.e., not direction when the matrix is applied to it. Mathematically for a square matrix A the Eigenvector is given as follows −

Av = v

Where, v is the Eigenvector, A is a square matrix of size n x n and is the corresponding eigenvalue.

What is Eigenvalue()?

The eigenvalue is the scalar by which the eigenvector v is stretched or compressed during the transformation. can be positive, negative or even complex.

Finding Eigenvalues and Eigenvectors

When we want to find the Eigenvalues and Eigenvectors of a square matrix A then we have to follow the below steps −

Step 1: Eigenvalues

The Eigenvalues of a sqaure matrix A are the scalars that satisfy the eqaution as follows −

det(A - I) = 0

Now we have to rewrite the Egienvalue equation Av = v as follows −

(A - I)v = 0

Where, I is the identity matrix of the same size as A and A - I is the new matrix obtained by subtracting times the identity matrix from A.

This equation has non-zero v only if det(A - I) = 0. This is called the characteristic equation and solving it gives the eigenvalues .

To solve the equation det(A - I) = 0 first we have to substitute into the natrix A - I. Next we have to compute the determinant of the resulting matrix and then solve the resulting polynomial equation for .

The roots of the polynomial are the eigenvalues of A.

Step 2: Eigenvectors

Once when we found the Eigenvalues then we can compute the Eigenvectors by solving the beloow equation −

(A - I)v = 0

This equation is a system of linear equations. It has infinitely many solutions because (AI) is singular i.e., its determinant is zero. Typically these solutions are expressed as a scaled version of one eigenvector.

Computing Eigenvalues and Eigenvectors

In SciPy Eigenvalues and Eigenvectors can be calculated using the scipy.linalg.eig() function which is part of the scipy.linalg module. This function is used for both general matrices and with specific optimizations, Hermitian or symmetric matrices.

Computing Eigenvalues and Eigenvectors of General matrix

Here is the example of computing the eigenvalues and eigenvectors of a general n x n matrix using scipy −

import numpy as np
from scipy.linalg import eig

# Define a 3x3 matrix
A = np.array([[6, 2, 1],
              [2, 3, 1],
              [1, 1, 1]])

# Compute eigenvalues and eigenvectors
eigenvalues, eigenvectors = eig(A)

# Print results
print("Matrix A:")
print(A)

print("\nEigenvalues:")
print(eigenvalues)

print("\nEigenvectors (columns):")
print(eigenvectors)

Here is the output of computing the eigenvalues and eigenvectors using scipy library −

Matrix A:
[[6 2 1]
 [2 3 1]
 [1 1 1]]

Eigenvalues:
[7.28799214+0.j 2.13307448+0.j 0.57893339+0.j]

Eigenvectors (columns):
[[ 0.86643225  0.49742503 -0.0431682 ]
 [ 0.45305757 -0.8195891  -0.35073145]
 [ 0.20984279 -0.28432735  0.9354806 ]]

Hermitian and Symmetric Matrices

Hermitian Matrix is a square matrix that is equal to its own conjugate transpose, which is defined as follows −

A = A^H

Where, AH is the conjugate transpose of A.

Symmetric Matrix is a special case of a Hermitian matrix where all elements are real so it satisfies and it is given as follows −

A = A^T

Where AT is the transpose of A.

Here's the example which shows how to compute the eigenvalues and eigenvectors of Hermitian matrix using scipy library −

import numpy as np
from scipy.linalg import eigh  

# Define a Hermitian matrix
A = np.array([[2, 1j],
              [-1j, 3]])

# Compute eigenvalues and eigenvectors
eigenvalues, eigenvectors = eigh(A)

print("Hermitian Matrix A:")
print(A)

print("\nEigenvalues:")
print(eigenvalues)

print("\nEigenvectors:")
print(eigenvectors)

Here is the output of computing the eigenvalues and eigenvectors of Hermitian matrix using scipy library −

Hermitian Matrix A:
[[ 2.+0.j  0.+1.j]
 [-0.-1.j  3.+0.j]]

Eigenvalues:
[1.38196601 3.61803399]

Eigenvectors:
[[-0.85065081+0.j          0.52573111+0.j        ]
 [ 0.        -0.52573111j  0.        -0.85065081j]]

Following is the example of computing the eigenvalues and eigenvectors of symmetric matrix using scipy library −

import numpy as np
from scipy.linalg import eigh  

# Define a Symmetric matrix
A = np.array([[4, 2],
              [2, 3]])

# Compute eigenvalues and eigenvectors
eigenvalues, eigenvectors = eigh(A)

print("Symmetric Matrix A:")
print(A)

print("\nEigenvalues:")
print(eigenvalues)

print("\nEigenvectors:")
print(eigenvectors)

Here is the output of computing the eigenvalues and eigenvectors of Symmetric matrix using scipy library −

Symmetric Matrix A:
[[4 2]
 [2 3]]

Eigenvalues:
[1.43844719 5.56155281]

Eigenvectors:
[[ 0.61541221 -0.78820544]
 [-0.78820544 -0.61541221]]
Advertisements