Last Updated : 08 Jul, 2025
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.
Linear Algebra Operations in SciPyscipy.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.
1. Solving Systems of Linear EquationsSolving 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
from scipy import linalg
import numpy as np
a = np.array([[7, 2], [4, 5]])
b = np.array([8, 10])
x = linalg.solve(a, b)
print(x)
Output
2. Matrix Inversion[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():
Python
from scipy import linalg
import numpy as np
A = np.array([[7, 2], [4, 5]])
A_inv = linalg.inv(A)
print(A_inv)
Output
3. Pseudo-Inverse of a Matrix[[ 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.
Python
from scipy import linalg
import numpy as np
x = np.array([[8, 2], [3, 5], [1, 3]])
pinv_x = linalg.pinv(x)
print(pinv_x)
Output
4. Matrix Determinant[[ 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:
Python
from scipy import linalg
import numpy as np
A = np.array([[9, 6], [4, 5]])
det_A = linalg.det(A)
print(det_A)
Output
21.0
From above Python code, the determinant is calculated as:
Determinant 5. Singular Value Decomposition (SVD)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:
from scipy import linalg
import numpy as np
M = np.array([[1, 5], [6, 10]])
U, s, Vh = linalg.svd(M)
print("U =", U)
print("Singular values =", s)
print("V^H =", Vh)
Output
6. Eigenvalues and EigenvectorsU = [[-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.
Python
from scipy import linalg
import numpy as np
M = np.array([[9, 3], [2, 4]])
eigenvalues, eigenvectors = linalg.eig(M)
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
Output
7. Matrix NormsEigenvalues: [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().
Python
from scipy import linalg
import numpy as np
x = np.array([6, 3])
print("L2 norm:", linalg.norm(x)) # Euclidean norm
print("L1 norm:", linalg.norm(x, 1)) # Manhattan norm
Output
8. Other Matrix FunctionsL2 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.
Python
from scipy import linalg
import numpy as np
x = np.array([[16, 4], [100, 25]])
print("Matrix square root:\n", linalg.sqrtm(x))
print("\nMatrix exponential:\n", linalg.expm(x))
print("\nMatrix sine:\n", linalg.sinm(x))
print("\nMatrix cosine:\n", linalg.cosm(x))
print("\nMatrix tangent:\n", linalg.tanm(x))
Output
Related ArticlesMatrix 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 ]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4