Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@
* Image Processing
* [Gambar Sepia](https://github.com/bellshade/Python/blob/main/implementation/image_processing/gambar_sepia.py)
* Linear Algebra
* [Faktorisasi Lu](https://github.com/bellshade/Python/blob/main/implementation/linear_algebra/Faktorisasi_LU.py)
* [Ldlt Decomposition](https://github.com/bellshade/Python/blob/main/implementation/linear_algebra/LDLT_decomposition.py)
* [Lu Decomposition](https://github.com/bellshade/Python/blob/main/implementation/linear_algebra/LU_decomposition.py)
* [Conjugate Gradient](https://github.com/bellshade/Python/blob/main/implementation/linear_algebra/conjugate_gradient.py)
* [Operasi Matriks](https://github.com/bellshade/Python/blob/main/implementation/linear_algebra/operasi_matriks.py)
* [Polynom For Point](https://github.com/bellshade/Python/blob/main/implementation/linear_algebra/polynom_for_point.py)
Expand Down
41 changes: 41 additions & 0 deletions implementation/linear_algebra/Faktorisasi_LU.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# https://en.wikipedia.org/wiki/LU_decomposition#Algorithms

from typing import Union


def LU(A: list[list[Union[int, float]]]) -> list[list[Union[int, float]]]:
"""
>>> A = [[2, 3, 1], [4, 7, 7], [-2, 4, 5]]
>>> L, U = LU(A)
>>> L
[[1, 0, 0], [2.0, 1, 0], [-1.0, 7.0, 1]]
>>> U
[[2, 3, 1], [0.0, 1.0, 5.0], [0.0, 0.0, -29.0]]
"""
n = len(A)
L = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
U = [[A[i][j] for j in range(n)] for i in range(n)]

for k in range(n - 1):
if U[k][k] == 0:

raise ValueError("Faktorisasi LU tidak ada")
for i in range(k + 1, n):
L[i][k] = U[i][k] / U[k][k]
for j in range(k, n):
U[i][j] -= L[i][k] * U[k][j]
return L, U


def main(args=None):
import doctest

doctest.testmod()
A = [[2, 3, 1], [4, 7, 7], [-2, 4, 5]]
L, U = LU(A)
print(L)
print(U)


if __name__ == "__main__":
main()
42 changes: 42 additions & 0 deletions implementation/linear_algebra/LU_decomposition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# https://en.wikipedia.org/wiki/LU_decomposition#Algorithms
from typing import Union


def LU(A: list[list[Union[int, float]]]) -> list[list[Union[int, float]]]:
"""
>>> A = [[2, 3, 1], [4, 7, 7], [-2, 4, 5]]
>>> L, U = LU(A)
>>> L
[[1, 0, 0], [2.0, 1, 0], [-1.0, 7.0, 1]]
>>> U
[[2, 3, 1], [0.0, 1.0, 5.0], [0.0, 0.0, -29.0]]
"""
n = len(A)
L = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
U = [[A[i][j] for j in range(n)] for i in range(n)]

for k in range(n - 1):
if U[k][k] == 0:
raise ValueError("Faktorisasi LU tidak ada")
for i in range(k + 1, n):
L[i][k] = U[i][k] / U[k][k]
for j in range(k, n):
U[i][j] -= L[i][k] * U[k][j]
return L, U


def main(args=None):
import doctest

doctest.testmod()
A = [[2, 3, 1], [4, 7, 7], [-2, 4, 5]]
L, U = LU(A)
for row in L:
print(row)
print("\n")
for row in U:
print(row)


if __name__ == "__main__":
main()
Loading