{
  "id": "lu-decomposition",
  "version": "7b6b33eb675a",
  "status": "published",
  "name": "LU Decomposition Calculator",
  "question": "What is the LU factorization of A?",
  "summary": "Factors a square matrix into A = LU (or PA = LU with row swaps) by Gaussian elimination in exact fractions: L lower triangular with 1s on the diagonal, U upper triangular, with the determinant and every step.",
  "category": "math",
  "subcategory": "linear-algebra",
  "url": "https://www.acalculator.org/math/lu-decomposition-calculator",
  "markdown": "https://www.acalculator.org/math/lu-decomposition-calculator.md",
  "kind": "function",
  "method": "Gaussian elimination: for each pivot column k, l_ik = u_ik ÷ u_kk and row i → row i − l_ik × row k; a zero pivot swaps in the first lower row with a nonzero entry (PA = LU); det A = (−1)^swaps × Π u_kk.",
  "assumptions": [
    "Entries are read exactly: a decimal such as 4.5 is 9/2, so L and U are exact fractions.",
    "Rows are swapped only when a pivot is 0, not for size (no partial pivoting by largest entry), so the L and U match a hand calculation.",
    "A singular matrix still factors; a column with no nonzero pivot is skipped and U has a 0 on its diagonal."
  ],
  "inputs": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "a": {
        "title": "Matrix A",
        "description": "The square matrix to factor, 2 to 6 rows and the same number of columns. Fractions such as 1/3 work.",
        "type": "array",
        "items": {
          "type": "array",
          "items": {
            "type": "number"
          }
        }
      }
    }
  },
  "outputs": {
    "l": {
      "label": "L (lower triangular)",
      "description": "The multipliers of elimination under a diagonal of 1s, as exact fractions, rows separated by semicolons.",
      "format": "text"
    },
    "u": {
      "label": "U (upper triangular)",
      "description": "The matrix after elimination, as exact fractions.",
      "format": "text"
    },
    "p": {
      "label": "P (row swaps)",
      "description": "The permutation matrix when rows were swapped, so PA = LU; the identity when none were.",
      "format": "text"
    },
    "form": {
      "label": "Form",
      "description": "\"A = LU\" with no row swaps, or \"PA = LU\" with them.",
      "format": "text"
    },
    "determinant": {
      "label": "Determinant",
      "description": "det A = (−1)^swaps × the product of U’s diagonal, as an exact fraction.",
      "format": "text"
    },
    "steps": {
      "label": "Elimination steps",
      "description": "Each row swap and each row operation, with the multiplier it puts into L.",
      "format": "text"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "a": [
        [
          2,
          1,
          1
        ],
        [
          4,
          -6,
          0
        ],
        [
          -2,
          7,
          2
        ]
      ]
    },
    "outputs": {
      "l": "[1, 0, 0; 2, 1, 0; −1, −1, 1]",
      "u": "[2, 1, 1; 0, −8, −2; 0, 0, 1]",
      "p": "[1, 0, 0; 0, 1, 0; 0, 0, 1]",
      "form": "A = LU",
      "determinant": "−16",
      "steps": "R2 → R2 − (2) × R1, so l21 = 2; R3 → R3 − (−1) × R1, so l31 = −1; R3 → R3 − (−1) × R2, so l32 = −1"
    },
    "text": "A = LU with L = [1, 0, 0; 2, 1, 0; −1, −1, 1] and U = [2, 1, 1; 0, −8, −2; 0, 0, 1]."
  },
  "examples": [
    {
      "given": {
        "a": [
          [
            2,
            1,
            1
          ],
          [
            4,
            -6,
            0
          ],
          [
            -2,
            7,
            2
          ]
        ]
      },
      "expect": {
        "l": "[1, 0, 0; 2, 1, 0; −1, −1, 1]",
        "u": "[2, 1, 1; 0, −8, −2; 0, 0, 1]",
        "form": "A = LU",
        "determinant": "−16"
      },
      "source": "hand calculation in content.mdx: multipliers 2, −1, −1; MIT OpenCourseWare, 18.06 Linear Algebra (Gilbert Strang), Lecture 4: Factorization into A = LU (L holds the multipliers with 1s on the diagonal, U is the upper triangular result of elimination; PA = LU with row exchanges), https://ocw.mit.edu/courses/18-06-linear-algebra-spring-2010/resources/lecture-4-factorization-into-a-lu/ (retrieved 2026-10-02)"
    },
    {
      "given": {
        "a": [
          [
            2,
            1
          ],
          [
            8,
            7
          ]
        ]
      },
      "expect": {
        "l": "[1, 0; 4, 1]",
        "u": "[2, 1; 0, 3]",
        "determinant": "6"
      },
      "source": "hand calculation in content.mdx: l21 = 8 ÷ 2 = 4, U row 2 = [8, 7] − 4 × [2, 1] = [0, 3]; MIT OpenCourseWare, 18.06 Linear Algebra (Gilbert Strang), Lecture 4: Factorization into A = LU (L holds the multipliers with 1s on the diagonal, U is the upper triangular result of elimination; PA = LU with row exchanges), https://ocw.mit.edu/courses/18-06-linear-algebra-spring-2010/resources/lecture-4-factorization-into-a-lu/ (retrieved 2026-10-02)"
    },
    {
      "given": {
        "a": [
          [
            0,
            1
          ],
          [
            2,
            3
          ]
        ]
      },
      "expect": {
        "p": "[0, 1; 1, 0]",
        "l": "[1, 0; 0, 1]",
        "u": "[2, 3; 0, 1]",
        "form": "PA = LU",
        "determinant": "−2"
      },
      "source": "a 0 pivot needs a row exchange: PA = LU; hand calculation in content.mdx; MIT OpenCourseWare, 18.06 Linear Algebra (Gilbert Strang), Lecture 4: Factorization into A = LU (L holds the multipliers with 1s on the diagonal, U is the upper triangular result of elimination; PA = LU with row exchanges), https://ocw.mit.edu/courses/18-06-linear-algebra-spring-2010/resources/lecture-4-factorization-into-a-lu/ (retrieved 2026-10-02)"
    },
    {
      "given": {
        "a": [
          [
            1,
            2
          ],
          [
            3,
            4.5
          ]
        ]
      },
      "expect": {
        "l": "[1, 0; 3, 1]",
        "u": "[1, 2; 0, −3/2]",
        "determinant": "−3/2"
      },
      "source": "hand calculation in content.mdx: 4.5 − 3 × 2 = −3/2; OpenStax, Algebra and Trigonometry 2e, §11.6 Solving Systems with Gaussian Elimination (row operations to upper triangular form), https://openstax.org/books/algebra-and-trigonometry-2e/pages/11-6-solving-systems-with-gaussian-elimination (retrieved 2026-10-02)"
    }
  ],
  "sources": [
    "MIT OpenCourseWare, 18.06 Linear Algebra (Gilbert Strang), Lecture 4: Factorization into A = LU (L holds the multipliers, U the result of elimination; PA = LU with row exchanges). https://ocw.mit.edu/courses/18-06-linear-algebra-spring-2010/resources/lecture-4-factorization-into-a-lu/ (retrieved 2026-10-02)",
    "OpenStax, Algebra and Trigonometry 2e, §11.6 Solving Systems with Gaussian Elimination (row operations to row echelon form), CC BY 4.0. https://openstax.org/books/algebra-and-trigonometry-2e/pages/11-6-solving-systems-with-gaussian-elimination (retrieved 2026-10-02)"
  ],
  "related": [
    "determinant",
    "rref",
    "inverse-matrix",
    "matrix"
  ],
  "changelog": []
}
