Inspect matrix factors and pivoting
Numerical linear algebra
SciPy · Function
Computes a pivoted LU decomposition of an array. It returns separate permutation, lower, and upper factors by default, or a pre-permuted lower factor when requested.
lu factors an array using partial pivoting. By default it returns a permutation matrix P, a lower-triangular or trapezoidal L with unit diagonal, and an upper-triangular or trapezoidal U, satisfying A = P @ L @ U; setting permute_l=True instead returns P @ L, so the relation becomes A = (P @ L) @ U.
scipy.linalg.lu(a, permute_l=False, overwrite_a=False, check_finite=True, p_indices=False)| Name | Type | Description |
|---|---|---|
a | (…, M, N) array_like | Array to decompose. |
permute_l | bool, optional | Whether to return the product P @ L rather than P and L separately. |
overwrite_a | bool, optional | Allow the routine to overwrite the input array when possible, potentially improving performance. |
check_finite | bool, optional | Check that the input contains only finite values. |
p_indices | bool, optional | Return permutation information as row indices instead of a permutation matrix. |
If permute_l=False, returns p, l, and u; p contains permutation information, l is lower triangular or trapezoidal with unit diagonal, and u is upper triangular or trapezoidal. If permute_l=True, returns pl and u, where pl = P @ L.
A = P @ L @ U when permutation is not folded into L.permute_l=True to receive the permutation applied to L in advance.p_indices=True when row-index permutation data is more useful than a permutation matrix.check_finite can improve performance, but inputs containing infinities or NaNs may cause crashes or non-termination.p_indices defaults to False; enabling it changes the permutation output from a matrix to row indices.import numpy as np
from scipy.linalg import lu
A = np.array([[2., 5.], [5., 2.]])
P, L, U = lu(A)
assert np.allclose(A, P @ L @ U)
print(P)
print(L)
print(U)Computes the pivoted factorization and verifies that multiplying the returned factors reconstructs the input.
Numerical linear algebra
Rectangular matrix workflows
Permutation-aware algorithms
scipy.linalg.lu compute?The factorization uses partial pivoting and satisfies A = P @ L @ U. P is a permutation matrix, while L and U are lower- and upper-triangular factors.
Follow-up: What changes with permute_l=True? The function returns P @ L as pl, and the reconstruction becomes A = pl @ U.
permute_l?With permute_l=False, the function returns P, L, and U. With permute_l=True, it returns only the already-permuted lower factor pl together with U.
Follow-up: How can permutation information be represented without constructing a matrix? Set p_indices=True to request row indices instead.
For an input with shape (M, N), L has shape (M, K) and U has shape (K, N), where K = min(M, N). These shapes support both square and rectangular matrices.
Follow-up: What option changes the representation of the permutation? p_indices=True returns permutation information as row indices rather than a permutation matrix.
scipy.linalg.lu_factorscipy.linalg.solvescipy.linalg.lu_solve