![]() |
VOOZH | about |
Linear algebra deals with vectors, matrices and systems of linear equations. SciPy’s scipy.linalg module provides useful tools to perform various linear algebra operations such as solving equations, computing matrix decompositions, finding eigenvalues, matrix inverses etc. It is similar to NumPy’s linalg module but offers extended functionality and better performance for larger problems.
scipy.linalg module offers efficient functions for common linear algebra tasks like solving equations, inverting matrices, computing decompositions, eigenvalues all using NumPy arrays.
Let’s explore these operations one by one.
Solving systems of linear equations means finding variable values that satisfy all equations. SciPy’s linalg.solve() finds solution using coefficient matrix and output values.
Syntax:
scipy.linalg.solve(a, b)
Parameter:
Example: This code solves two linear equations by finding values of variables using linalg.solve()
7x + 2y = 8,
4x + 5y = 10
Output
[0.74074074 1.40740741]
Matrix inversion finds another matrix that, when multiplied with the original, gives the identity matrix. SciPy uses the linalg.inv() method to compute the inverse.
Syntax:
scipy.linalg.inv(a)
Parameter:a is square matrix to invert.
Example: This code computes inverse for below 2×2 matrix using scipy.linalg.inv():
👁 Matrixand_InvMatrixOutput
[[ 0.18518519 -0.07407407]
[-0.14814815 0.25925926]]
pseudo-inverse (or Moore-Penrose inverse) is used to find solutions to systems of linear equations that may not have a unique or exact solution. In SciPy, it's computed using scipy.linalg.pinv().
Syntax:
scipy.linalg.pinv(a)
Parameter: a is an input matrix (can be rectangular).
Example: In the code below, scipy.linalg.pinv takes a matrix x and returns its Moore-Penrose pseudo inverse.
Output
[[ 0.14251208 -0.03381643 -0.03864734]
[-0.07487923 0.16183575 0.11352657]]
The determinant is a scalar value that describes certain properties of a square matrix, such as whether it is invertible. In SciPy, scipy.linalg.det() is used to compute the determinant.
Syntax:
scipy.linalg.det(a)
Parameter:a is an input square matrix.
Example: The scipy.linalg.det takes a square matrix A and returns D, the determinant of A. The determinant is a specific property of the linear transformation of a matrix. The determinant of a 2×2 matrix is given by:
👁 Matrix_determinantOutput
21.0
From above Python code, the determinant is calculated as:
Singular Value Decomposition (SVD) breaks a matrix into three components: two orthogonal matrices and a diagonal matrix of singular values to make specific subsequent matrix calculations simpler. It is calculated using scipy.linalg.svd.
Syntax:
scipy.linalg.svd(a)
Parameter: a is an input matrix.
Example: This example performs Singular Value Decomposition (SVD) on a 2×2 matrix M. It breaks the matrix into three components:
Output
U = [[-0.38684104 -0.92214641]
[-0.92214641 0.38684104]]Singular values = [12.62901571 1.58365469]
V^H = [[-0.46873958 -0.88333641]
[ 0.88333641 -0.46873958]]
Eigenvalues and eigenvectors help describe how a matrix transforms space.
Let M be an n × n matrix and X a non-zero vector. If M · X = λ · X for some number λ, then λ is called an eigenvalue and X is eigenvector.
Syntax:
scipy.linalg.eig(a, b=None)
Parameter:
Example: This example calculates eigenvalues and eigenvectors of a 2×2 matrix using scipy.linalg.eig(). Eigenvalues indicate how the matrix scales vectors, while eigenvectors show directions that remain unchanged.
Output
Eigenvalues: [10.+0.j 3.+0.j]
Eigenvectors: [[ 0.9486833 -0.4472136 ]
[ 0.31622777 0.89442719]]
Matrix norms measure "size" or "length" of a matrix or vector. They help compare matrices, check stability or define distances. SciPy’s linalg.norm() function computes various norms like L1, L2 or Frobenius depending on the need.
Syntax:
scipy.linalg.norm(a, ord=None, axis=None, keepdims=False, check_finite=True)
Parameter:
scipy.linalg.norm computes vector or matrix norms.
Example: This example demonstrates how to compute the L2 (Euclidean) and L1 (Manhattan) norms of a vector using scipy.linalg.norm().
Output
L2 norm: 6.708203932499369
L1 norm: 9.0
Other Matrix Functions in scipy.linalg provide tools for advanced matrix operations like computing matrix square roots, exponentials, and trigonometric functions used in solving differential equations. Below are most common matrix functions:
| Function Name | Definition |
|---|---|
| scipy.linalg.sqrtm(A) | Finds the square root of the matrix. |
| scipy.linalg.expm(A) | Computes the matrix exponential using Pade approximation. |
| scipy.linalg.sinm(A) | Computes the sine of the matrix. |
| scipy.linalg.cosm(A) | Computes the cosine of the matrix. |
| scipy.linalg.tanm(A) | Computes the tangent of the matrix. |
Let's explore these functions.
Example: This example demonstrates how to apply various matrix functions from scipy.linalg module to a 2x2 matrix.
Output
Matrix square root: [[ 2.49878019 0.62469505]
[15.61737619 3.90434405]]Matrix exponential: [[2.49695022e+17 6.24237555e+16]
[1.56059389e+18 3.90148472e+17]]Matrix sine: [[-0.06190153 -0.01547538]
[-0.38688456 -0.09672114]]Matrix cosine: [[ 0.22445296 -0.19388676]
[-4.84716897 -0.21179224]]Matrix tangent: [[0.0626953 0.01567382]
[0.39184561 0.0979614 ]]