Skip to content

Commit

Permalink
Dev algorithm (#55)
Browse files Browse the repository at this point in the history
* feat: menambahkan integral

* feat: menambahkan integral

* feat: menambahkan integral
  • Loading branch information
kayabaakihiko13 authored Feb 7, 2024
1 parent e177e01 commit e315cd1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
27 changes: 26 additions & 1 deletion OpenSeries/matematika.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -522,3 +522,28 @@ def gaussian(
/ np.sqrt(2 * constant.PI * sigma**2)
* np.exp(-((x - mu) ** 2) / (2 * sigma**2))
)


def integral(
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.
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.
"""
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):
result += f(a + i * delta)
return round(result * delta)
2 changes: 2 additions & 0 deletions testing/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
TestSigmoid,
TestDistribusiBinomial,
TestGaussian,
TestIntegral,
)

from testing.fisika_test import (
Expand Down Expand Up @@ -54,6 +55,7 @@
TestSigmoid,
TestDistribusiBinomial,
TestGaussian,
TestIntegral,
]

testing_fisika: list = [
Expand Down
33 changes: 33 additions & 0 deletions testing/matematika_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,36 @@ 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)

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

0 comments on commit e315cd1

Please sign in to comment.