Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

linalg.lu

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.

How it works

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.

Syntax

scipy.linalg.lu(a, permute_l=False, overwrite_a=False, check_finite=True, p_indices=False)

Parameters

NameTypeDescription
a(…, M, N) array_likeArray to decompose.
permute_lbool, optionalWhether to return the product P @ L rather than P and L separately.
overwrite_abool, optionalAllow the routine to overwrite the input array when possible, potentially improving performance.
check_finitebool, optionalCheck that the input contains only finite values.
p_indicesbool, optionalReturn permutation information as row indices instead of a permutation matrix.

Returns

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.

Key points

  • The function computes an LU decomposition using partial pivoting.
  • The returned factors reconstruct the input through A = P @ L @ U when permutation is not folded into L.
  • Use permute_l=True to receive the permutation applied to L in advance.
  • Set p_indices=True when row-index permutation data is more useful than a permutation matrix.

Common mistakes

  • Turning off 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.

Example: Factor a matrix with pivoted LU

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.

When to use it

Inspect matrix factors and pivoting

Numerical linear algebra

Factor square or rectangular arrays

Rectangular matrix workflows

Use permutation matrices or row indices

Permutation-aware algorithms

Interview questions

QWhat decomposition does 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.

QHow do the return values depend on 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.

QWhat shapes should be expected for rectangular input?

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_factor
  • scipy.linalg.solve
  • scipy.linalg.lu_solve

Source: scipy.linalg.lu#. Checked 2026-10-15.