From 5cde83f23b361d45251c3ca5091b504815853669 Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Tue, 30 Dec 2025 07:03:56 +0700 Subject: [PATCH 1/6] menambahkan algoritma terbaru --- .../linear_algebra/LU_decomposition.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 implementation/linear_algebra/LU_decomposition.py diff --git a/implementation/linear_algebra/LU_decomposition.py b/implementation/linear_algebra/LU_decomposition.py new file mode 100644 index 00000000..24156e29 --- /dev/null +++ b/implementation/linear_algebra/LU_decomposition.py @@ -0,0 +1,41 @@ +# https://en.wikipedia.org/wiki/LU_decomposition#Algorithms + + +def LU(A) -> tuple: + """ + >>> 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() From cf4d48aaed1c41e462c063e0c02c36897fcd447b Mon Sep 17 00:00:00 2001 From: Ateng Date: Mon, 5 Jan 2026 10:27:45 +0700 Subject: [PATCH 2/6] fix : memperbaiki sytax \ --- implementation/linear_algebra/LU_decomposition.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/implementation/linear_algebra/LU_decomposition.py b/implementation/linear_algebra/LU_decomposition.py index 24156e29..9cce6a71 100644 --- a/implementation/linear_algebra/LU_decomposition.py +++ b/implementation/linear_algebra/LU_decomposition.py @@ -1,7 +1,7 @@ # https://en.wikipedia.org/wiki/LU_decomposition#Algorithms -def LU(A) -> tuple: +def LU(A : list[list[int]]) -> list[list[int]]: """ >>> A = [[2, 3, 1], [4, 7, 7], [-2, 4, 5]] >>> L, U = LU(A) From ee6c342ad9220daf460e320b1c1a3301bcb541d3 Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Mon, 5 Jan 2026 11:11:22 +0700 Subject: [PATCH 3/6] feat: menambahkan faktorisasi LU --- .../linear_algebra/Faktorisasi_LU.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 implementation/linear_algebra/Faktorisasi_LU.py diff --git a/implementation/linear_algebra/Faktorisasi_LU.py b/implementation/linear_algebra/Faktorisasi_LU.py new file mode 100644 index 00000000..ed88e677 --- /dev/null +++ b/implementation/linear_algebra/Faktorisasi_LU.py @@ -0,0 +1,40 @@ +# 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() From 8aad6fa7bd75c000d7f277e27e21f8b95ba0d6bf Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Mon, 5 Jan 2026 11:13:21 +0700 Subject: [PATCH 4/6] fix: memperbaiki test --- implementation/linear_algebra/Faktorisasi_LU.py | 1 + 1 file changed, 1 insertion(+) diff --git a/implementation/linear_algebra/Faktorisasi_LU.py b/implementation/linear_algebra/Faktorisasi_LU.py index ed88e677..88813bbe 100644 --- a/implementation/linear_algebra/Faktorisasi_LU.py +++ b/implementation/linear_algebra/Faktorisasi_LU.py @@ -18,6 +18,7 @@ def LU(A : list[list[Union[int, float]]]) -> list[list[Union[int, float]]]: 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] From 3625c4b3a987072432248ecb4a53207c0e17bd33 Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Mon, 5 Jan 2026 11:15:17 +0700 Subject: [PATCH 5/6] fix: memperbaiki test --- implementation/linear_algebra/Faktorisasi_LU.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/implementation/linear_algebra/Faktorisasi_LU.py b/implementation/linear_algebra/Faktorisasi_LU.py index 88813bbe..c219e35d 100644 --- a/implementation/linear_algebra/Faktorisasi_LU.py +++ b/implementation/linear_algebra/Faktorisasi_LU.py @@ -3,7 +3,7 @@ from typing import Union -def LU(A : list[list[Union[int, float]]]) -> list[list[Union[int, float]]]: +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) From 5b8adff42eb22fe15b00e08935dcda2873d010a6 Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Mon, 5 Jan 2026 11:22:56 +0700 Subject: [PATCH 6/6] fix: memperbaiki test --- implementation/linear_algebra/LU_decomposition.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/implementation/linear_algebra/LU_decomposition.py b/implementation/linear_algebra/LU_decomposition.py index 9cce6a71..c5d141a7 100644 --- a/implementation/linear_algebra/LU_decomposition.py +++ b/implementation/linear_algebra/LU_decomposition.py @@ -1,7 +1,8 @@ # https://en.wikipedia.org/wiki/LU_decomposition#Algorithms +from typing import Union -def LU(A : list[list[int]]) -> list[list[int]]: +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)