From 6db42499b1518650b8cd508be1db47f5ecf933fb Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Wed, 19 Nov 2025 19:02:53 +0700 Subject: [PATCH 01/10] feat: menambahkan algoritma rieman --- math/rieman_integral.py | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 math/rieman_integral.py diff --git a/math/rieman_integral.py b/math/rieman_integral.py new file mode 100644 index 00000000..38c34a42 --- /dev/null +++ b/math/rieman_integral.py @@ -0,0 +1,62 @@ +from typing import Callable +import math + + +def rieman_integral(f: Callable[[float], float], + a: float, + b: float, + n: int, + approx: str) -> float: + """ + >>> rieman_integral(math.sin,0,math.pi/2,100,"tengah") + 1.0000102809119051 + >>> rieman_integral(math.sin,0,math.pi/2,100,"kanan") + 1.007833419873582 + >>> rieman_integral(math.sin,0,math.pi/2,100,"kiri") + 0.9921254566056331 + """ + delta_x = (b - a) / n + sigma = 0.0 + approx = approx.lower() + + for i in range(n): + left = a + i * delta_x + right = left + delta_x + + if approx == "kiri": + x_i = left + elif approx == "kanan": + x_i = right + elif approx == "tengah": + x_i = left + 0.5 * delta_x + else: + raise ValueError("masukkan approx benar") + + sigma += f(x_i) + + return delta_x * sigma + + +def main(args=None): + import doctest + + doctest.testmod() + + # persamaan x + def f(x): + return x + print(rieman_integral(f, 0, 1, 1, "tengah")) # 0.5 + + # persamaan 4/(1+x^2) + def f(x): + return (4) / (1 + x**2) + print(rieman_integral(f, 0, 1, 1000, "tengah")) # 3.1415927369231227 + + # Persamaan sin + def f(x): + return math.sin(x) + print(rieman_integral(f, 0, math.pi / 2, 100, "kiri")) # 0.9921254566056331 + + +if __name__ == "__main__": + main() From 37526453aac91d44b4c26194341cef5f2977855d Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Wed, 19 Nov 2025 19:08:32 +0700 Subject: [PATCH 02/10] fix: memperbaiki variabel algoritma rieman --- math/rieman_integral.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/math/rieman_integral.py b/math/rieman_integral.py index 38c34a42..a42044cc 100644 --- a/math/rieman_integral.py +++ b/math/rieman_integral.py @@ -48,14 +48,14 @@ def f(x): print(rieman_integral(f, 0, 1, 1, "tengah")) # 0.5 # persamaan 4/(1+x^2) - def f(x): + def g(x): return (4) / (1 + x**2) - print(rieman_integral(f, 0, 1, 1000, "tengah")) # 3.1415927369231227 + print(rieman_integral(g, 0, 1, 1000, "tengah")) # 3.1415927369231227 # Persamaan sin - def f(x): + def y(x): return math.sin(x) - print(rieman_integral(f, 0, math.pi / 2, 100, "kiri")) # 0.9921254566056331 + print(rieman_integral(y, 0, math.pi / 2, 100, "kiri")) # 0.9921254566056331 if __name__ == "__main__": From 0a7e7530359a52fdce905cdc1f921f4fe5e1eab6 Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Thu, 20 Nov 2025 21:50:06 +0700 Subject: [PATCH 03/10] fix: memperbaiki CodeQL --- math/rieman_integral.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/math/rieman_integral.py b/math/rieman_integral.py index a42044cc..b70043ee 100644 --- a/math/rieman_integral.py +++ b/math/rieman_integral.py @@ -2,7 +2,7 @@ import math -def rieman_integral(f: Callable[[float], float], +def rieman_integral(fungsiHitung: Callable[[float], float], a: float, b: float, n: int, @@ -32,7 +32,7 @@ def rieman_integral(f: Callable[[float], float], else: raise ValueError("masukkan approx benar") - sigma += f(x_i) + sigma += fungsiHitung(x_i) return delta_x * sigma @@ -52,10 +52,8 @@ def g(x): return (4) / (1 + x**2) print(rieman_integral(g, 0, 1, 1000, "tengah")) # 3.1415927369231227 - # Persamaan sin - def y(x): - return math.sin(x) - print(rieman_integral(y, 0, math.pi / 2, 100, "kiri")) # 0.9921254566056331 + + print(rieman_integral(math.sin, 0, math.pi / 2, 100, "kiri")) # 0.9921254566056331 if __name__ == "__main__": From b9e85b82072b2ffd57a91c7a238ba6954e36f6d1 Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Fri, 21 Nov 2025 09:03:17 +0700 Subject: [PATCH 04/10] fix: memperbaiki CodeQL --- math/rieman_integral.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/math/rieman_integral.py b/math/rieman_integral.py index b70043ee..3d6ed949 100644 --- a/math/rieman_integral.py +++ b/math/rieman_integral.py @@ -52,8 +52,10 @@ def g(x): return (4) / (1 + x**2) print(rieman_integral(g, 0, 1, 1000, "tengah")) # 3.1415927369231227 - - print(rieman_integral(math.sin, 0, math.pi / 2, 100, "kiri")) # 0.9921254566056331 + # persamaan sin + def y(x): + return math.sin(x) + print(rieman_integral(y, 0, math.pi / 2, 100, "kiri")) # 0.9921254566056331 if __name__ == "__main__": From 976d60e4dbfc3be555c6e716676aa85fdd16a47d Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Fri, 21 Nov 2025 09:12:08 +0700 Subject: [PATCH 05/10] fix: memperbaiki CodeQL --- math/rieman_integral.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/math/rieman_integral.py b/math/rieman_integral.py index 3d6ed949..b70043ee 100644 --- a/math/rieman_integral.py +++ b/math/rieman_integral.py @@ -52,10 +52,8 @@ def g(x): return (4) / (1 + x**2) print(rieman_integral(g, 0, 1, 1000, "tengah")) # 3.1415927369231227 - # persamaan sin - def y(x): - return math.sin(x) - print(rieman_integral(y, 0, math.pi / 2, 100, "kiri")) # 0.9921254566056331 + + print(rieman_integral(math.sin, 0, math.pi / 2, 100, "kiri")) # 0.9921254566056331 if __name__ == "__main__": From 206e2f8d6526292a01bd4974b4988c3a91edfdbc Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Fri, 21 Nov 2025 09:43:46 +0700 Subject: [PATCH 06/10] fix: memperbaiki CodeQL --- math/rieman_integral.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/rieman_integral.py b/math/rieman_integral.py index b70043ee..08df961c 100644 --- a/math/rieman_integral.py +++ b/math/rieman_integral.py @@ -44,7 +44,7 @@ def main(args=None): # persamaan x def f(x): - return x + return 1/x print(rieman_integral(f, 0, 1, 1, "tengah")) # 0.5 # persamaan 4/(1+x^2) From 2047669cd0b993bea23f0f3e4650dffc201051f4 Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Fri, 21 Nov 2025 09:54:02 +0700 Subject: [PATCH 07/10] fix: memperbaiki CodeQL --- math/rieman_integral.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/math/rieman_integral.py b/math/rieman_integral.py index 08df961c..f554c4b2 100644 --- a/math/rieman_integral.py +++ b/math/rieman_integral.py @@ -31,7 +31,7 @@ def rieman_integral(fungsiHitung: Callable[[float], float], x_i = left + 0.5 * delta_x else: raise ValueError("masukkan approx benar") - + print("type fungsiHitung:", type(fungsiHitung),"callable",callable(fungsiHitung)) sigma += fungsiHitung(x_i) return delta_x * sigma @@ -42,11 +42,6 @@ def main(args=None): doctest.testmod() - # persamaan x - def f(x): - return 1/x - print(rieman_integral(f, 0, 1, 1, "tengah")) # 0.5 - # persamaan 4/(1+x^2) def g(x): return (4) / (1 + x**2) From f9b6ab00019c7ed52288b336fd5eb7beb27192fd Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Fri, 21 Nov 2025 10:00:29 +0700 Subject: [PATCH 08/10] fix: memperbaiki CodeQL --- math/rieman_integral.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/math/rieman_integral.py b/math/rieman_integral.py index f554c4b2..b70043ee 100644 --- a/math/rieman_integral.py +++ b/math/rieman_integral.py @@ -31,7 +31,7 @@ def rieman_integral(fungsiHitung: Callable[[float], float], x_i = left + 0.5 * delta_x else: raise ValueError("masukkan approx benar") - print("type fungsiHitung:", type(fungsiHitung),"callable",callable(fungsiHitung)) + sigma += fungsiHitung(x_i) return delta_x * sigma @@ -42,6 +42,11 @@ def main(args=None): doctest.testmod() + # persamaan x + def f(x): + return x + print(rieman_integral(f, 0, 1, 1, "tengah")) # 0.5 + # persamaan 4/(1+x^2) def g(x): return (4) / (1 + x**2) From 608cafc815a59b4e1f9753b02d40233356818cb8 Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Fri, 21 Nov 2025 10:07:58 +0700 Subject: [PATCH 09/10] fix: memperbaiki CodeQL --- math/rieman_integral.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/math/rieman_integral.py b/math/rieman_integral.py index b70043ee..898c0d39 100644 --- a/math/rieman_integral.py +++ b/math/rieman_integral.py @@ -23,6 +23,8 @@ def rieman_integral(fungsiHitung: Callable[[float], float], left = a + i * delta_x right = left + delta_x + if not callable(fungsiHitung): + raise TypeError("Fungsi harus cabble") if approx == "kiri": x_i = left elif approx == "kanan": From 1212229f217f58a05ee48172b54041b0a9c12e95 Mon Sep 17 00:00:00 2001 From: Arlan Tengga Date: Sun, 23 Nov 2025 11:51:53 +0700 Subject: [PATCH 10/10] fix: test pre-commit --- math/rieman_integral.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/math/rieman_integral.py b/math/rieman_integral.py index 898c0d39..3f6b84ec 100644 --- a/math/rieman_integral.py +++ b/math/rieman_integral.py @@ -2,11 +2,9 @@ import math -def rieman_integral(fungsiHitung: Callable[[float], float], - a: float, - b: float, - n: int, - approx: str) -> float: +def rieman_integral( + fungsiHitung: Callable[[float], float], a: float, b: float, n: int, approx: str +) -> float: """ >>> rieman_integral(math.sin,0,math.pi/2,100,"tengah") 1.0000102809119051 @@ -47,13 +45,14 @@ def main(args=None): # persamaan x def f(x): return x + print(rieman_integral(f, 0, 1, 1, "tengah")) # 0.5 # persamaan 4/(1+x^2) def g(x): return (4) / (1 + x**2) - print(rieman_integral(g, 0, 1, 1000, "tengah")) # 3.1415927369231227 + print(rieman_integral(g, 0, 1, 1000, "tengah")) # 3.1415927369231227 print(rieman_integral(math.sin, 0, math.pi / 2, 100, "kiri")) # 0.9921254566056331