math4610

Routine Name: LUFactorization

Author: David Merkley

Language: Python

Description/Purpose: Computes the LU factorization form of a matrix LU.

Input:

Required:

A - a matrix

Optional:

mutation - whether to modify A

Output: LU factorization of a matrix

Implementation/Code:

def LUFactorization(A, mutation=False):
    if (mutation is True):
        LU = A
    else:
        LU = [[a for a in row] for row in A]
    n = len(LU)
    for k in range(n - 1):
        for i in range(k + 1, n):
            factor = LU[i][k] / LU[k][k]
            for j in range(k + 1, n):
                LU[i][j] -= factor *  LU[k][j]
            LU[i][k] = factor
    return LU