Python/arithmetic_analysis/lu_decomposition.py
Sven 1495382367
Fixed bug with incorrect LU decomposition (#2261)
* Fixed Bug #2257

* =

Co-authored-by: Svn-Sp <svn-sp@email>
Co-authored-by: Christian Clauss <cclauss@me.com>
2020-08-01 22:11:39 +02:00

35 lines
969 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Lower-Upper (LU) Decomposition."""
# lowerupper (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):
sum = 0
for k in range(j):
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, columns):
sum1 = 0
for k in range(i):
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)