From 26b5995b27e4157fb9c84747be88ee69ef98572a Mon Sep 17 00:00:00 2001 From: kayabaakihiko13 Date: Wed, 7 Feb 2024 01:25:02 +0700 Subject: [PATCH 1/3] feat: menambahkan integral --- OpenSeries/matematika.py | 23 ++++++++++++++++++++++- testing/main_test.py | 2 ++ testing/matematika_test.py | 12 ++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/OpenSeries/matematika.py b/OpenSeries/matematika.py index 55c2b36..7ff1d8a 100644 --- a/OpenSeries/matematika.py +++ b/OpenSeries/matematika.py @@ -1,6 +1,6 @@ from OpenSeries.util import constant as constant from OpenSeries.util import error as error -from typing import Union, Sequence +from typing import Union, Sequence, Callable import numpy as np import math @@ -522,3 +522,24 @@ def gaussian( / np.sqrt(2 * constant.PI * sigma**2) * np.exp(-((x - mu) ** 2) / (2 * sigma**2)) ) + + +def integral( + f: Callable[[float], float], a: float, b: float, iterable: int = 4 +) -> float: + """ + integral merupakan suatu konsep yang merupakan operasi kebalikan dari diferensiasi. + Integral memiliki dua bentuk utama: integral tak tentu (indefinite integral) + dan integral tentu (definite integral). + Args: + f (Callable[[float],float]): fungsi input + a (float): nilai awal + b (float): nilai atas + iterable (int, optional): mengatur putaran. Defaults to 4. + + """ + delta = (b - a) / iterable + result = 0.5 * (f(a) + f(b)) + for i in range(1, iterable): + result += f(a + i * delta) + return round(result * delta) diff --git a/testing/main_test.py b/testing/main_test.py index 1139cb8..4d196ac 100644 --- a/testing/main_test.py +++ b/testing/main_test.py @@ -16,6 +16,7 @@ TestSigmoid, TestDistribusiBinomial, TestGaussian, + TestIntegral, ) from testing.fisika_test import ( @@ -54,6 +55,7 @@ TestSigmoid, TestDistribusiBinomial, TestGaussian, + TestIntegral, ] testing_fisika: list = [ diff --git a/testing/matematika_test.py b/testing/matematika_test.py index cf33121..4d5a414 100644 --- a/testing/matematika_test.py +++ b/testing/matematika_test.py @@ -252,3 +252,15 @@ def test_data_valid_input(self): ekspetasi_nilai = 0.12098536225957168 hasil = matematika.gaussian(x, mu, sigma) self.assertAlmostEqual(hasil, ekspetasi_nilai, places=10) + + +class TestIntegral(unittest.TestCase): + def test_nilai_integral(self): + def f(x): + return x * x + + a = 0 + b = 3 + hasil = matematika.integral(f, a, b) + + self.assertAlmostEqual(hasil, 9.00, places=2) From 4a730f1c77bd59feb1ace313fad1cbc7581827b3 Mon Sep 17 00:00:00 2001 From: kayabaakihiko13 Date: Wed, 7 Feb 2024 01:25:02 +0700 Subject: [PATCH 2/3] feat: menambahkan integral --- OpenSeries/matematika.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/OpenSeries/matematika.py b/OpenSeries/matematika.py index 7ff1d8a..6764d01 100644 --- a/OpenSeries/matematika.py +++ b/OpenSeries/matematika.py @@ -525,8 +525,8 @@ def gaussian( def integral( - f: Callable[[float], float], a: float, b: float, iterable: int = 4 -) -> float: + f: Callable[[float], float], a: float, b: float, iterasi: int = 4 +) -> Union[float, error.ErrorTipeData]: """ integral merupakan suatu konsep yang merupakan operasi kebalikan dari diferensiasi. Integral memiliki dua bentuk utama: integral tak tentu (indefinite integral) @@ -538,8 +538,13 @@ def integral( iterable (int, optional): mengatur putaran. Defaults to 4. """ - delta = (b - a) / iterable + if not all(isinstance(data, float) for data in [a, b]) and not isinstance( + iterasi, int + ): + raise error.ErrorTipeData(["float", "int"]) + + delta = (b - a) / iterasi result = 0.5 * (f(a) + f(b)) - for i in range(1, iterable): + for i in range(1, iterasi): result += f(a + i * delta) return round(result * delta) From bd77955a817b55d782805fe3e87effa482781871 Mon Sep 17 00:00:00 2001 From: kayabaakihiko13 Date: Wed, 7 Feb 2024 01:25:02 +0700 Subject: [PATCH 3/3] feat: menambahkan integral --- OpenSeries/matematika.py | 11 +++++------ testing/matematika_test.py | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/OpenSeries/matematika.py b/OpenSeries/matematika.py index 6764d01..9270534 100644 --- a/OpenSeries/matematika.py +++ b/OpenSeries/matematika.py @@ -525,7 +525,7 @@ def gaussian( def integral( - f: Callable[[float], float], a: float, b: float, iterasi: int = 4 + f: Callable[[float], float], a: int, b: int, iterasi: int = 4 ) -> Union[float, error.ErrorTipeData]: """ integral merupakan suatu konsep yang merupakan operasi kebalikan dari diferensiasi. @@ -538,11 +538,10 @@ def integral( iterable (int, optional): mengatur putaran. Defaults to 4. """ - if not all(isinstance(data, float) for data in [a, b]) and not isinstance( - iterasi, int - ): - raise error.ErrorTipeData(["float", "int"]) - + if not all(isinstance(data, (int)) for data in [a, b]): + return error.ErrorTipeData(["int"]) + if not isinstance(iterasi, int): + return error.ErrorTipeData(["int"]) delta = (b - a) / iterasi result = 0.5 * (f(a) + f(b)) for i in range(1, iterasi): diff --git a/testing/matematika_test.py b/testing/matematika_test.py index 4d5a414..83f0172 100644 --- a/testing/matematika_test.py +++ b/testing/matematika_test.py @@ -264,3 +264,24 @@ def f(x): hasil = matematika.integral(f, a, b) self.assertAlmostEqual(hasil, 9.00, places=2) + + def testing_tipe_input_data_string(self): + def f(x): + return x * x + + a = "0" + b = "3" + hasil = matematika.integral(f, a, b) + with self.assertRaises(error.ErrorTipeData): + raise hasil + + def testing_tipe_iterasi_string(self): + def f(x): + return x * x + + a = 0 + b = 3 + iterasi = "4" + hasil = matematika.integral(a, b, iterasi) + with self.assertRaises(error.ErrorTipeData): + raise hasil