LDL

The LDL factorization factors a symmetric matrix A into a unit lower triangular matrix L, a block diagonal matrix D, and a permutation matrix P.

Excel Usage

=LDL(matrix, lower, ldl_ret_type)
  • matrix (list[list], required): Square symmetric 2D array of numeric values to decompose.
  • lower (bool, optional, default: true): If TRUE, compute the lower triangular factor; if FALSE, use the upper part.
  • ldl_ret_type (str, optional, default: “d”): The component of the decomposition to return.

Returns (list[list]): 2D array representing the requested component (LU, D, or perm).

Example 1: Block diagonal matrix D

Inputs:

matrix ldl_ret_type
2 -1 3 d
-1 2 0
3 0 1

Excel formula:

=LDL({2,-1,3;-1,2,0;3,0,1}, "d")

Expected output:

Result
2 0 0
0 1.5 0
0 0 -5
Example 2: Triangular factor matrix LU

Inputs:

matrix ldl_ret_type
2 -1 3 lu
-1 2 0
3 0 1

Excel formula:

=LDL({2,-1,3;-1,2,0;3,0,1}, "lu")

Expected output:

Result
1 0 0
-0.5 1 0
1.5 1 1
Example 3: Permutation indices

Inputs:

matrix ldl_ret_type
2 -1 3 perm
-1 2 0
3 0 1

Excel formula:

=LDL({2,-1,3;-1,2,0;3,0,1}, "perm")

Expected output:

Result
0 1 2

Python Code

import numpy as np
from scipy.linalg import ldl as scipy_ldl

def ldl(matrix, lower=True, ldl_ret_type='d'):
    """
    Compute the LDLt or Bunch-Kaufman factorization of a symmetric matrix.

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

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

    Args:
        matrix (list[list]): Square symmetric 2D array of numeric values to decompose.
        lower (bool, optional): If TRUE, compute the lower triangular factor; if FALSE, use the upper part. Default is True.
        ldl_ret_type (str, optional): The component of the decomposition to return. Valid options: Triangular Factor (L/U), Block Diagonal (D), Permutation (perm). Default is 'd'.

    Returns:
        list[list]: 2D array representing the requested component (LU, D, or perm).
    """
    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 = len(matrix)
        if any(len(row) != n for row in matrix):
            return "Error: matrix must be square (n x n)"

        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:
            lu, d, perm = scipy_ldl(a, lower=lower)
        except Exception as e:
            return f"Error: {str(e)}"

        rt = ldl_ret_type.lower()
        if rt == "lu":
            return lu.tolist()
        elif rt == "d":
            return d.tolist()
        else:
            return [perm.tolist()]

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

Online Calculator

Square symmetric 2D array of numeric values to decompose.
If TRUE, compute the lower triangular factor; if FALSE, use the upper part.
The component of the decomposition to return.