diff --git a/DIRECTORY.md b/DIRECTORY.md index 67d468db..e49bece0 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -236,6 +236,7 @@ * [Transformation 2D](https://github.com/bellshade/Python/blob/main/implementation/linear_algebra/transformation_2d.py) * Physics * [N Body Simulation](https://github.com/bellshade/Python/blob/main/implementation/physics/n_body_simulation.py) + * [Pendulum](https://github.com/bellshade/Python/blob/main/implementation/physics/pendulum.py) * [Percepatan](https://github.com/bellshade/Python/blob/main/implementation/physics/percepatan.py) * Statistic * [Labelencoding](https://github.com/bellshade/Python/blob/main/implementation/statistic/LabelEncoding.py) @@ -267,6 +268,7 @@ * [Faktor Prima](https://github.com/bellshade/Python/blob/main/math/faktor_prima.py) * [Faktorial Looping](https://github.com/bellshade/Python/blob/main/math/faktorial_looping.py) * [Faktorial Rekursif](https://github.com/bellshade/Python/blob/main/math/faktorial_rekursif.py) + * [Fft](https://github.com/bellshade/Python/blob/main/math/fft.py) * [Fibonacci](https://github.com/bellshade/Python/blob/main/math/fibonacci.py) * [Fungsi Gamma](https://github.com/bellshade/Python/blob/main/math/fungsi_gamma.py) * [Geometric Sum](https://github.com/bellshade/Python/blob/main/math/geometric_sum.py) @@ -359,6 +361,7 @@ * [Happy Number](https://github.com/bellshade/Python/blob/main/other/happy_number.py) * [Hex To Rgb](https://github.com/bellshade/Python/blob/main/other/hex_to_rgb.py) * [Is Palindrom](https://github.com/bellshade/Python/blob/main/other/is_palindrom.py) + * [Kaprekar Number](https://github.com/bellshade/Python/blob/main/other/kaprekar_number.py) * [Krisnamurtycheck](https://github.com/bellshade/Python/blob/main/other/krisnamurtyCheck.py) * [Logic Gate](https://github.com/bellshade/Python/blob/main/other/logic_gate.py) * [Lucky Number](https://github.com/bellshade/Python/blob/main/other/lucky_number.py) diff --git a/implementation/physics/pendulum.py b/implementation/physics/pendulum.py new file mode 100644 index 00000000..ec465cb9 --- /dev/null +++ b/implementation/physics/pendulum.py @@ -0,0 +1,100 @@ +# https://en.wikipedia.org/wiki/Pendulum +import numpy as np +import matplotlib.pyplot as plt +from matplotlib.animation import FuncAnimation + + +def parameter(g=9.8, L=1.0, a=float, b=float) -> float: + time_step = a / b + time_stap = np.linspace(0, a, b) + return g, L, time_step, time_stap + + +def full_pendulum(g, L, theta, theta_velocity, time_step): + """ + fungsi untuk menghitung persamaan pendulum. + """ + theta_acceleration = -(g / L) * np.sin(theta) + theta_velocity += time_step * theta_acceleration + theta += time_step * theta_velocity + return theta, theta_velocity + + +def calculate_pendulum() -> float: + """ + Melakukan kalkulasi pendulum + """ + theta = [np.radians(90)] + theta_velocity = 0 + + for _ in time_stap: + theta_new, theta_velocity = full_pendulum( + g, L, theta[-1], theta_velocity, time_step + ) + theta.append(theta_new) + + # persamaan proyeksi panjang tali + x = L * np.sin(theta) + y = -L * np.cos(theta) + return x, y + + +def setup_axis(axis) -> None: + """ + Setup sumbu x dan y. + """ + axis.set_xlim(-L - 0.2, L + 0.2) + axis.set_ylim(-L - 0.2, L) + axis.grid() + return axis + + +def create_plot_elements(axis) -> None: + """ + Membuat elemen plot yang akan dianimasu. + """ + (rod_line,) = axis.plot([], [], lw=2) + (mass_point,) = axis.plot([], [], marker="o", markersize=10) + (trace,) = axis.plot([], [], "-", lw=1, alpha=0.6) + return rod_line, mass_point, trace + + +def animate(frame, x, y, rod_line, mass_point, trace) -> None: + """ + Fungsi ini berguna untuk mengambil setiap hasil hitungan + dan menjadikan setiap hitungan frame. + """ + rod_line.set_data([0, x[frame]], [0, y[frame]]) + mass_point.set_data([x[frame]], [y[frame]]) + trace.set_data(x[:frame], y[:frame]) + return rod_line, mass_point, trace + + +if __name__ == "__main__": + g, L, time_step, time_stap = parameter(a=20, b=300) + x, y = calculate_pendulum() + fig, axis = plt.subplots() + axis = setup_axis(axis) + rod_line, mass_point, trace = create_plot_elements(axis) + + def init(): + """ + Parameter awal. + """ + rod_line.set_data([], []) + mass_point.set_data([], []) + trace.set_data([], []) + return rod_line, mass_point, trace + + animation = FuncAnimation( + fig=fig, + func=animate, + fargs=(x, y, rod_line, mass_point, trace), + init_func=init, + frames=len(time_stap), + interval=25, + blit=True, + ) + + print(animation) + plt.show() diff --git a/math/fft.py b/math/fft.py new file mode 100644 index 00000000..0b86e9df --- /dev/null +++ b/math/fft.py @@ -0,0 +1,33 @@ +import cmath +import numpy as np + + +def fft(A): + """ + Fungsi untuk kalkulasi Fast Fourier Transform + + >>> y = [i for i in range(1, 10)] + >>> fft(y) + array([45. -0.j , 3.27090469+9.43955428j, + 4.30540729+3.93923101j, 4.77196636+1.65987555j, + 5. +0.j , 4.25695936-1.83110215j, + 5.69459271-3.93923101j, 7.70016959-9.26832768j, + 0. +0.j ]) + """ + N = len(A) + if N == 1: + return A + odd = fft(A[1::2]) + even = fft(A[::2]) + res = [0] * N + for i in range(N // 2): + W = cmath.exp(-1j * 2 * cmath.pi * i / N) + res[i] = even[i] + W * odd[i] + res[i + N // 2] = even[i] - W * odd[i] + return np.array(res) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/other/kaprekar_number.py b/other/kaprekar_number.py new file mode 100644 index 00000000..c6d9c4f6 --- /dev/null +++ b/other/kaprekar_number.py @@ -0,0 +1,61 @@ +# mengecek apakah bilangan apakah bilangan itu masuk kaprekar +# https://en.wikipedia.org/wiki/Kaprekar_number +import math + + +def kaprekar_number(number: int) -> str: + """ + Angka kaprekar adalah angka yang yang di kuadratkan, + dan di pisahmenjadi dua bagian dan di jumlahkan lagi + maka akat menjadi angka itu kembali. + + >>> kaprekar_number(45) + 'Angka Kaprekar' + >>> kaprekar_number(9) + 'Angka Kaprekar' + >>> kaprekar_number(5) + 'Bukan Angka Kaprekar' + >>> kaprekar_number(10) + 'Bukan Angka Kaprekar' + + """ + benar, bukan = "Angka Kaprekar", "Bukan Angka Kaprekar" + if number == 1: + return benar + + square_number = number * number + + cout_d = 0 + while not square_number == 0: + cout_d += 1 + square_number = square_number // 10 + + square_number = number * number + + r_digits = 0 + while r_digits < cout_d: + r_digits += 1 + eq_parts = math.pow(10, r_digits) + + if eq_parts == number: + continue + + jumlah = square_number // eq_parts + square_number % eq_parts + if jumlah == number: + return benar + + return bukan + + +def main(args=None): + import doctest + + doctest.testmod() + + +if __name__ == "__main__": + # sampel case + print(kaprekar_number(45)) # Angka Kaprekar + print(kaprekar_number(9)) # Angka Kaprekar + print(kaprekar_number(5)) # Bukan Angka Kaprekar + print(kaprekar_number(10)) # Bukan Angka Kaprekar