lu decomposition Algorithm

The LU decomposition algorithm, also known as lower-upper factorization, is a method used in numerical linear algebra for solving linear systems of equations, inverting matrices, and calculating the determinant of a matrix. It is a key technique in numerous applications, such as engineering, physics, and computer graphics, among others. The main idea behind the LU decomposition is to factorize a given square matrix A into the product of two matrices, a lower triangular matrix L and an upper triangular matrix U, such that A = LU. This decomposition allows for efficient solutions to linear systems, as triangular matrices can be easily solved using forward and backward substitution techniques. There are several methods for computing the LU decomposition, with the most common being Doolittle's algorithm, Crout's algorithm, and Gaussian elimination with partial pivoting. These algorithms work by performing a series of elementary row operations on the given matrix A to obtain the lower and upper triangular matrices. Doolittle's and Crout's algorithms differ in the way they compute the elements of L and U, while Gaussian elimination with partial pivoting enhances the numerical stability of the decomposition by permuting rows to avoid division by small numbers. Once the LU decomposition is computed, the solution to the linear system Ax = b can be found in two steps: first, solve the lower triangular system Ly = b for y, and then solve the upper triangular system Ux = y for x. This two-step process is computationally efficient and can be easily implemented in modern programming languages and software packages.
"""Lower-Upper (LU) Decomposition."""

# lower–upper (LU) decomposition - https://en.wikipedia.org/wiki/LU_decomposition
import numpy


def LUDecompose(table):
    # Table that contains our data
    # Table has to be a square array so we need to check first
    rows, columns = numpy.shape(table)
    L = numpy.zeros((rows, columns))
    U = numpy.zeros((rows, columns))
    if rows != columns:
        return []
    for i in range(columns):
        for j in range(i - 1):
            sum = 0
            for k in range(j - 1):
                sum += L[i][k] * U[k][j]
            L[i][j] = (table[i][j] - sum) / U[j][j]
        L[i][i] = 1
        for j in range(i - 1, columns):
            sum1 = 0
            for k in range(i - 1):
                sum1 += L[i][k] * U[k][j]
            U[i][j] = table[i][j] - sum1
    return L, U


if __name__ == "__main__":
    matrix = numpy.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]])
    L, U = LUDecompose(matrix)
    print(L)
    print(U)

LANGUAGE:

DARK MODE: