0
0
SciPydata~5 mins

Eigenvalue problems (eigs, eigsh) in SciPy

Choose your learning style9 modes available
Introduction

Eigenvalue problems help us find special numbers and vectors that describe important properties of a matrix. This is useful to understand systems like vibrations, stability, or data patterns.

Finding the main directions of data in Principal Component Analysis (PCA).
Analyzing vibrations in mechanical structures to find natural frequencies.
Solving large systems where only a few eigenvalues and eigenvectors are needed.
Studying stability of systems by checking eigenvalues of matrices.
Reducing data dimensions by focusing on important eigenvectors.
Syntax
SciPy
from scipy.sparse.linalg import eigs, eigsh

# eigs for general square matrices
values, vectors = eigs(A, k=number_of_eigenvalues)

# eigsh for symmetric or Hermitian matrices
values, vectors = eigsh(A, k=number_of_eigenvalues)

eigs works for any square matrix but is slower for symmetric ones.

eigsh is faster and more accurate for symmetric or Hermitian matrices.

Examples
Finds the largest eigenvalue of a 2x2 matrix using eigs.
SciPy
from scipy.sparse.linalg import eigs
import numpy as np

A = np.array([[1, 2], [3, 4]])
values, vectors = eigs(A, k=1)
print(values)
Finds the largest eigenvalue of a symmetric matrix using eigsh.
SciPy
from scipy.sparse.linalg import eigsh
import numpy as np

A = np.array([[2, 1], [1, 2]])
values, vectors = eigsh(A, k=1)
print(values)
Finds the two largest eigenvalues of a symmetric matrix.
SciPy
from scipy.sparse.linalg import eigsh
import numpy as np

A = np.array([[2, 1], [1, 2]])
values, vectors = eigsh(A, k=2)
print(values)
Sample Program

This program shows how to use eigs for a general matrix and eigsh for a symmetric matrix. It prints the eigenvalues found.

SciPy
from scipy.sparse.linalg import eigs, eigsh
import numpy as np

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

# Use eigs to find the largest eigenvalue and eigenvector
values_eigs, vectors_eigs = eigs(A, k=1)

# Define a symmetric matrix
B = np.array([[2, 1], [1, 2]])

# Use eigsh to find the two largest eigenvalues and eigenvectors
values_eigsh, vectors_eigsh = eigsh(B, k=2)

print("Largest eigenvalue using eigs:", values_eigs)
print("Eigenvalues using eigsh:", values_eigsh)
OutputSuccess
Important Notes

Always use eigsh for symmetric matrices for better speed and accuracy.

The k parameter controls how many eigenvalues you want to find.

Eigenvalues can be complex numbers when using eigs on non-symmetric matrices.

Summary

Eigenvalue problems find special numbers (eigenvalues) and vectors (eigenvectors) that reveal matrix properties.

eigs works for any square matrix; eigsh is optimized for symmetric matrices.

Use these functions to analyze data, systems, or reduce dimensions efficiently.