mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 01:00:15 +00:00
07e991d553
* ci(pre-commit): Add pep8-naming to `pre-commit` hooks (#7038) * refactor: Fix naming conventions (#7038) * Update arithmetic_analysis/lu_decomposition.py Co-authored-by: Christian Clauss <cclauss@me.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor(lu_decomposition): Replace `NDArray` with `ArrayLike` (#7038) * chore: Fix naming conventions in doctests (#7038) * fix: Temporarily disable project euler problem 104 (#7069) * chore: Fix naming conventions in doctests (#7038) Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
68 lines
1.9 KiB
Python
68 lines
1.9 KiB
Python
"""Lower-Upper (LU) Decomposition.
|
|
|
|
Reference:
|
|
- https://en.wikipedia.org/wiki/LU_decomposition
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import numpy as np
|
|
from numpy import float64
|
|
from numpy.typing import ArrayLike
|
|
|
|
|
|
def lower_upper_decomposition(
|
|
table: ArrayLike[float64],
|
|
) -> tuple[ArrayLike[float64], ArrayLike[float64]]:
|
|
"""Lower-Upper (LU) Decomposition
|
|
|
|
Example:
|
|
|
|
>>> matrix = np.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]])
|
|
>>> outcome = lower_upper_decomposition(matrix)
|
|
>>> outcome[0]
|
|
array([[1. , 0. , 0. ],
|
|
[0. , 1. , 0. ],
|
|
[2.5, 8. , 1. ]])
|
|
>>> outcome[1]
|
|
array([[ 2. , -2. , 1. ],
|
|
[ 0. , 1. , 2. ],
|
|
[ 0. , 0. , -17.5]])
|
|
|
|
>>> matrix = np.array([[2, -2, 1], [0, 1, 2]])
|
|
>>> lower_upper_decomposition(matrix)
|
|
Traceback (most recent call last):
|
|
...
|
|
ValueError: 'table' has to be of square shaped array but got a 2x3 array:
|
|
[[ 2 -2 1]
|
|
[ 0 1 2]]
|
|
"""
|
|
# Table that contains our data
|
|
# Table has to be a square array so we need to check first
|
|
rows, columns = np.shape(table)
|
|
if rows != columns:
|
|
raise ValueError(
|
|
f"'table' has to be of square shaped array but got a {rows}x{columns} "
|
|
+ f"array:\n{table}"
|
|
)
|
|
lower = np.zeros((rows, columns))
|
|
upper = np.zeros((rows, columns))
|
|
for i in range(columns):
|
|
for j in range(i):
|
|
total = 0
|
|
for k in range(j):
|
|
total += lower[i][k] * upper[k][j]
|
|
lower[i][j] = (table[i][j] - total) / upper[j][j]
|
|
lower[i][i] = 1
|
|
for j in range(i, columns):
|
|
total = 0
|
|
for k in range(i):
|
|
total += lower[i][k] * upper[k][j]
|
|
upper[i][j] = table[i][j] - total
|
|
return lower, upper
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|