Skip to content
fn

lu_factor

→Tensor
lu_factor(A: Tensor, pivot: bool = True)

Implementing kernel

C++ engine symbols that back this Python API.

LU factorization with partial pivoting (packed form).

Computes the packed LU factorization

P A = L U,P\,A \,=\, L\,U,

where PP is a row-permutation matrix, LL is unit-lower-triangular, and UU is upper-triangular. The result is returned in LAPACK's packed format suitable for repeated solves via lu_solve.

Parameters

Matrix of shape (*, m, n). Need not be square.
pivot(bool, keyword - only)= True
Must be True (the default). Unpivoted LU is not implemented and raises NotImplementedError.

Returns

Tensor

Packed factorization of shape (*, m, n). UU occupies the diagonal and above; LL (without its unit diagonal) occupies the strict lower triangle.

Raises

LinAlgError
If a pivot UkkU_{kk} is exactly zero — lu_solve would divide by it. lu_factor_ex returns the factors of such a matrix with a non-zero info instead.

Notes

Backed by LAPACK getrf. Cost is O(23n3)O(\tfrac{2}{3} n^3) for a square input.

For a rectangular A∈Rm×nA \in \mathbb{R}^{m \times n} with k=min⁡(m,n)k = \min(m, n), LL is m×km \times k unit-lower-trapezoidal and UU is k×nk \times n upper-trapezoidal. lu_solve still requires a square factor: only a square system has a solution.

Use lu_factor + lu_solve instead of solve when the same coefficient matrix AA is reused with many right-hand sides — factorization is shared. For the explicit (P,L,U)(P, L, U) triple see lu.

Examples

>>> import lucid
>>> from lucid.linalg import lu_factor, lu_solve
>>> A = lucid.tensor([[2.0, 1.0], [4.0, 7.0]])
>>> LU, piv = lu_factor(A)
>>> b = lucid.tensor([[3.0], [13.0]])
>>> lu_solve(LU, piv, b)
tensor([[0.8], [1.4]])