Matrix invertibility checks
Assess whether a square matrix is singular or nearly singular by examining whether its determinant is near zero.
NumPy · Function
Computes determinants for square arrays, including stacks of matrices. Each trailing M×M matrix yields one determinant.
Pass a square array to compute its determinant. For arrays with leading dimensions, NumPy treats each trailing M×M slice as a separate matrix and returns the corresponding determinants; internally, the calculation uses LU factorization.
numpy.linalg.det(a)| Name | Type | Description |
|---|---|---|
a | (…, M, M) array_like | Input array to compute determinants for. |
det(…) array_like — determinant of a.
import numpy as np
matrices = np.array([
[[1, 2], [3, 4]],
[[1, 2], [2, 1]],
])
print(np.linalg.det(matrices)) # [-2. -3.]A stack of two 2×2 matrices produces one determinant for each matrix.
Assess whether a square matrix is singular or nearly singular by examining whether its determinant is near zero.
Compute one determinant per matrix when processing batched linear-algebra inputs.
Measure signed scaling or orientation effects represented by a transformation matrix.
The input is an array whose last two dimensions are M×M. Any preceding dimensions represent a stack of matrices, and the output contains one determinant per matrix.
Follow-up: How would you avoid overflow or underflow for very large or small determinants? Use numpy.linalg.slogdet, which returns the sign and natural logarithm of the determinant.
For a two-dimensional matrix, the result is a scalar-like determinant value. For a stack, the result has the same leading dimensions as the input, because each M×M matrix contributes one value.
Follow-up: What related function provides logarithmic scaling information? numpy.linalg.slogdet returns both the determinant sign and its natural logarithm.
NumPy computes the determinant using LU factorization backed by the LAPACK z/dgetrf routine. This supports both real and complex matrix inputs.
Follow-up: Which external-library function is documented as similar? scipy.linalg.det is listed as a similar function.
numpy.linalg.slogdetscipy.linalg.det