Obtain a matrix inverse
You need the explicit matrix that reverses a square linear transformation.
SciPy · Function
Computes the inverse of a square matrix and returns it as an ndarray. It validates finite inputs by default and reports invalid shapes or singular matrices through exceptions.
The function computes a square matrix inverse and returns it as an ndarray. By default it preserves the input, validates that values are finite, and raises an error when the matrix is singular or has an invalid shape.
scipy.linalg.inv(a, overwrite_a=False, check_finite=True)| Name | Type | Description |
|---|---|---|
a | array_like | Square matrix to be inverted. |
overwrite_a | bool, optional | Whether to discard data in a; may improve performance. Default: False. |
check_finite | bool, optional | Whether to check that the input matrix contains only finite numbers. Default: True. |
ainv ndarray — Inverse of the matrix a.
import numpy as np
from scipy import linalg
A = np.array([[1.0, 2.0], [3.0, 4.0]])
A_inv = linalg.inv(A)
print(A_inv)
print(A @ A_inv)Computes the inverse and verifies that multiplying the matrix by it produces the identity matrix.
You need the explicit matrix that reverses a square linear transformation.
You want to verify an inversion numerically.
It accepts an array-like square matrix and returns its inverse as an ndarray. The input is not overwritten by default, and finite-value checking is enabled by default.
Follow-up: What happens for a singular matrix? SciPy raises LinAlgError, so callers should handle that case or avoid explicitly forming the inverse when solving a linear system.
overwrite_a controls whether the input data may be discarded, potentially improving performance; its default is False. check_finite controls validation of finite values and defaults to True.
Follow-up: When might you disable check_finite? Only when inputs are known to contain finite values and the performance trade-off is justified, because invalid values can cause severe failures.
A non-square or non-2D input raises ValueError, while a singular matrix raises LinAlgError. These checks distinguish invalid shape from a matrix without an inverse.
Follow-up: How can the result be checked? Multiply the original matrix by the returned array and compare the product with the identity matrix.
scipy.linalg.solvescipy.linalg.detscipy.linalg.pinv