LU

The LU decomposition factors a matrix A into the product of a permutation matrix P, a lower triangular matrix L (with unit diagonal), and an upper triangular matrix U:

A = PLU

If permute_l is set to TRUE, the function returns a permuted lower triangular matrix L' such that A = L'U.

Excel Usage

=LU(matrix, permute_l, lu_ret_type)
  • matrix (list[list], required): 2D array of numeric values to decompose.
  • permute_l (bool, optional, default: false): If TRUE, return a permuted L matrix satisfying A = LU.
  • lu_ret_type (str, optional, default: “u”): The component of the decomposition to return.

Returns (list[list]): 2D array representing the requested component (P, L, U, or PL).

Example 1: Upper triangular matrix of 2x2

Inputs:

matrix lu_ret_type
1 2 u
3 4

Excel formula:

=LU({1,2;3,4}, "u")

Expected output:

Result
3 4
0 0.666667
Example 2: Lower triangular matrix of 2x2

Inputs:

matrix lu_ret_type
1 2 l
3 4

Excel formula:

=LU({1,2;3,4}, "l")

Expected output:

Result
1 0
0.333333 1
Example 3: Permutation matrix of 2x2

Inputs:

matrix lu_ret_type
1 2 p
3 4

Excel formula:

=LU({1,2;3,4}, "p")

Expected output:

Result
0 1
1 0
Example 4: Permuted L matrix

Inputs:

matrix permute_l lu_ret_type
1 2 true pl
3 4

Excel formula:

=LU({1,2;3,4}, TRUE, "pl")

Expected output:

Result
0.333333 1
1 0

Python Code

import numpy as np
from scipy.linalg import lu as scipy_lu

def lu(matrix, permute_l=False, lu_ret_type='u'):
    """
    Compute LU decomposition of a matrix with partial pivoting.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.lu.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        matrix (list[list]): 2D array of numeric values to decompose.
        permute_l (bool, optional): If TRUE, return a permuted L matrix satisfying A = LU. Default is False.
        lu_ret_type (str, optional): The component of the decomposition to return. Valid options: Permutation Matrix (P), Lower Triangular (L), Upper Triangular (U), Permuted L (PL). Default is 'u'.

    Returns:
        list[list]: 2D array representing the requested component (P, L, U, or PL).
    """
    try:
        def to2d(x):
            return [[x]] if not isinstance(x, list) else x

        matrix = to2d(matrix)

        if not isinstance(matrix, list) or not matrix or not all(isinstance(row, list) for row in matrix):
            return "Error: matrix must be a non-empty 2D list"

        n_rows = len(matrix)
        n_cols = len(matrix[0])
        if n_cols == 0 or any(len(row) != n_cols for row in matrix):
            return "Error: All rows must have the same non-zero length"

        try:
            a = np.array(matrix, dtype=float)
        except (ValueError, TypeError):
            return "Error: matrix must contain numeric values"

        if not np.all(np.isfinite(a)):
            return "Error: matrix must contain only finite numbers"

        try:
            if permute_l:
                pl, u = scipy_lu(a, permute_l=True)
            else:
                p, l, u = scipy_lu(a, permute_l=False)
        except Exception as e:
            return f"Error: {str(e)}"

        rt = lu_ret_type.lower()
        if permute_l:
            if rt == "pl":
                return pl.tolist()
            elif rt == "u":
                return u.tolist()
            else:
                return "Error: With permute_l=TRUE, return_type must be 'pl' or 'u'"
        else:
            if rt == "p":
                return p.tolist()
            elif rt == "l":
                return l.tolist()
            elif rt == "u":
                return u.tolist()
            else:
                return "Error: With permute_l=FALSE, return_type must be 'p', 'l', or 'u'"

    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

2D array of numeric values to decompose.
If TRUE, return a permuted L matrix satisfying A = LU.
The component of the decomposition to return.