Scipy Linalg



In SciPy the linalg module is abbrivated as linear algebra which provides a comprehensive set of functions for performing various linear algebra operations such as solving linear systems, computing matrix factorizations and handling eigenvalue problems.

It is a highly optimized and efficient module built on top of BLAS (Basic Linear Algebra Subprograms) and LAPACK (Linear Algebra PACKage) libraries which are widely used in scientific computing.

When to use scipy.linalg vs numpy.linalg?

scipy.linalg and numpy.linalg both provide functions for linear algebra operations but they differ in terms of functionality, performance and specific use cases. Heres a comparison to help us to decide when to use each deping upon the requirement we have −

Criteria scipy.linalg numpy.linalg
Functionality Extensive linear algebra routines, including additional functions for matrix decompositions such as QR, LU, Cholesky and matrix exponentials.
Supports specialized matrix types such as sparse matrices.
Basic linear algebra routines like matrix inversion, determinants, eigenvalues, SVD.
Lacks advanced decompositions and specialized matrix support.
Performance Often faster especially for larger matrices or specialized tasks due to optimized routines and bindings to BLAS and LAPACK libraries. Sufficient for smaller matrices and general-purpose tasks.
Can be slower than scipy.linalg for certain operations.
Use Case Ideal for advanced linear algebra tasks, large matrices and scientific computing applications requiring high performance and specialized functions. Suitable for basic linear algebra tasks and simpler applications.
Dependency Requires SciPy. Part of the NumPy package.

Key Features of scipy.linalg

The scipy.linalg module offers a wide range of linear algebra functions many of which extend the capabilities of NumPy's numpy.linalg module. Some of the most important functions are as mentioned below −

Matrix Decompositions

Matrix decomposition or matrix factorization which is indeed a powerful tool in linear algebra with broad applications across various fields. Following is the overview of some commonly used types of matrix decompositions, their forms and primary applications −

S.No Decomposition Type Function and Description
1 LU Decomposition scipy.linalg.lu()
Decomposes a matrix into lower and upper triangular matrices, (A = LU).
2 QR Decomposition scipy.linalg.qr()
Decomposes a matrix into an orthogonal matrix (Q) and an upper triangular matrix (R).
3 Cholesky Decomposition scipy.linalg.cholesky()
Decomposes a positive-definite matrix into a lower triangular matrix, ( A = LLT ).
4 Eigen Decomposition scipy.linalg.eig()
Computes the eigenvalues and eigenvectors of a square matrix.
5 Singular Value Decomposition (SVD) scipy.linalg.svd()
Computes the singular value decomposition, ( A = UVT).
6 Schur Decomposition scipy.linalg.schur()
Computes the Schur decomposition, breaking a matrix into quasi-triangular form.
7 Hessenberg Decomposition scipy.linalg.hessenberg()
Decomposes a matrix into Hessenberg form, which has zero entries below the first sub-diagonal.
8 Polar Decomposition scipy.linalg.polar()
Decomposes a matrix into a product of a unitary and positive semi-definite matrix.
9 Jordan Decomposition Not directly available
Jordan decomposition is not directly available in SciPy and custom implementations may be used.

Solving Linear Systems

SciPy provides several efficient functions for solving linear systems of equations which is suitable for different types of matrices such as dense, sparse, symmetric, etc. Heres a are the overview of some key functions available in scipy.linalg and scipy.sparse.linalg for solving linear systems −

S.No Function & Description
1

scipy.linalg.solve()

Solves a linear matrix equation (Ax = b) for dense matrices using LU decomposition. General-purpose solver for dense matrices.

2 scipy.linalg.lu_solve()
Solves (Ax = b) using LU decomposition from lu_factor. Useful when solving multiple systems with the same matrix ( A ).
3

scipy.linalg.lstsq()

Solves linear least-squares problems for over-determined systems. Suitable for systems where (Ax = b) has no exact solution.

4 scipy.linalg.cho_solve()
Solves (Ax = b) using Cholesky factorization from cho_factor. Efficient for symmetric positive-definite matrices.
5

scipy.linalg.solve_triangular()

Solves (Ax = b) for triangular matrices (upper or lower). Optimized for triangular matrices, used in back-substitution.

6 scipy.sparse.linalg.spsolve()
Solves (Ax = b) for sparse matrices using LU decomposition. Ideal for sparse matrices, conserving memory.
7 scipy.sparse.linalg.cg()
Conjugate gradient solver for large, sparse, symmetric positive-definite matrices. Efficient for large symmetric positive-definite matrices.
8 scipy.sparse.linalg.gmres()
Generalized minimal residual method for sparse linear systems. Effective for non-symmetric sparse matrices in iterative solutions.
9 scipy.sparse.linalg.lsmr()
Iterative least-squares solver for large-scale sparse problems. Suitable for large, sparse, and over-determined systems.
10 scipy.sparse.linalg.minres()
Minimum residual method for symmetric matrices. Used for symmetric, indefinite matrices.

Function to Solve Eigenvalue Problems

Eigenvalues and Eigenvectors are used to compute the eigenvalues and eigenvectors of a square matrix. Eigenvalues are important in many areas such as stability analysis and principal component analysis. Below are some important functions in Scipy which are used to compute the Eigenvalues and Eigenvectors −

S.No Function & Description
1 scipy.linalg.eig()
Computes the eigenvalues and right eigenvectors of a square matrix. Suitable for general eigenvalue problems.
2 scipy.linalg.eigh()
Computes the eigenvalues and eigenvectors of a symmetric or Hermitian matrix. Optimized for symmetric matrices.
3 scipy.linalg.eigvals()
Computes only the eigenvalues of a square matrix. Useful when only eigenvalues are needed.
4 scipy.linalg.eigvalsh()
Computes only the eigenvalues of a symmetric or Hermitian matrix. Efficient for symmetric matrices.
5 scipy.sparse.linalg.eigs()
Computes a few eigenvalues and eigenvectors of a sparse matrix using iterative methods. Suitable for large sparse matrices.
6 scipy.sparse.linalg.eigsh()
Computes a few eigenvalues and eigenvectors of a sparse symmetric or Hermitian matrix using iterative methods. Optimized for large symmetric matrices.

Matrix Inversion and Determinant

In SciPy matrix inversion and determinant calculation are key operations in linear algebra which often used to solve systems of linear equations, analyze matrix properties and perform various transformations in scientific computing. SciPy provides functions in the scipy.linalg module that are optimized for these tasks.

Here's an overview of the key functions for matrix inversion and determinant calculation in SciPy −

S.No. Function & Description
1

scipy.linalg.inv()

Computes the inverse of a matrix A. This function is useful when you need to find the matrix that, when multiplied by A, results in the identity matrix.

2

scipy.linalg.det()

Calculates the determinant of a matrix A. The determinant is a scalar value that provides insight into the matrix's invertibility and other properties. If the determinant is zero, the matrix is singular (non-invertible).

Matrix Functions

In SciPy matrix functions provide efficient methods for handling and operating on matrices, particularly when working with large datasets, linear algebra and scientific computing. These matrix functions are built on top of the NumPy library and provide higher-level operations for advanced linear algebra tasks. Here are the key matrix functions that are available in SciPy −

S.No. Function & Description
1

scipy.linalg.expm()

Compute the matrix exponential of an array.

2

scipy.linalg.logm()

Compute matrix logarithm.

3

scipy.linalg.cosm()

Compute the matrix cosine.

4

scipy.linalg.sinm()

Compute the matrix sine.

5

scipy.linalg.tanm()

Compute the matrix tangent.

6

scipy.linalg.coshm()

Compute the hyperbolic matrix cosine.

7

scipy.linalg.sinhm()

Compute the hyperbolic matrix sine.

8

scipy.linalg.tanhm()

Compute the hyperbolic matrix tangent.

9

scipy.linalg.signm()

Matrix sign function.

10

scipy.linalg.sqrtm()

Matrix square root.

11

scipy.linalg.funm()

Evaluate a matrix function specified by a callable.

12

scipy.linalg.expm_frechet()

Frechet derivative of the matrix exponential of A in the direction E.

13

scipy.linalg.expm_cond()

Relative condition number of the matrix exponential in the Frobenius norm.

14

scipy.linalg.fractional_matrix_power()

Compute the fractional power of a matrix.

The scipy.linalg module is an essential part of the SciPy ecosystem for linear algebra which provide a wide range of optimized functions for matrix decompositions, eigenvalue problems and solving linear systems, among others. It is an indispensable tool for scientists, engineers and data analysts working with large, complex datasets.

Linear Algebra Methods and Operations

Linear algebra is crucial in domains including machine learning, data science, and scientific computing. The procedures and techniques listed above are applied to solve tough problems with matrices and vectors and have numerous applications across various disciplines.

Sr.No. Function & Description
1

scipy.solve.banded

Solves a linear system with a banded matrix, often used for sparse systems.

2

scipy.solveh.banded

Solves a Hermitian banded system, applicable in complex matrix problems.

3

scipy.solve.circulant

Solves a system with a circulant matrix, common in signal processing.

4

scipy.solve.toeplitz

Solves a system with a Toeplitz matrix, used in time series and signal analysis.

5

scipy.matmul.toeplitz

Performs matrix multiplication with a Toeplitz matrix.

6

scipy.norm

Computes various norms (e.g., Euclidean, Frobenius) for matrices or vectors.

7

scipy.pinv

Computes the Moore-Penrose pseudo-inverse of a matrix.

8

scipy.pinvh

Computes the pseudo-inverse of a Hermitian matrix.

9

scipy.kron

Computes the Kronecker product of two matrices.

10

scipy.khatri.rao

Computes the Khatri-Rao product of two matrices.

11

scipy.orthogonal.procrustes

Finds the orthogonal matrix that best aligns two matrices.

12

scipy.matrix.balance

Balances a matrix to improve numerical stability.

13

scipy.subspace.angles

Computes the angles between subspaces of two matrices.

14

scipy.bandwidth

Computes the bandwidth of a matrix (distance from the main diagonal to the farthest non-zero element)

15

scipy.issymmetric

Checks if a matrix is symmetric.

16

scipy.ishermitian

Checks if a matrix is Hermitian (equal to its conjugate transpose).

17

scipy.solve.sylvester

Solves the Sylvester equation AX+XB=C, where A, B, and C are given matrices. This method is commonly used in control theory and system analysis.

Advertisements