diff --git a/Maths/images/gaussian.png b/Maths/images/gaussian.png new file mode 100644 index 000000000000..eb007c7e21b2 Binary files /dev/null and b/Maths/images/gaussian.png differ diff --git a/maths/gaussian.py b/maths/gaussian.py new file mode 100644 index 000000000000..7907eb2633ec --- /dev/null +++ b/maths/gaussian.py @@ -0,0 +1,59 @@ +""" +Reference: https://en.wikipedia.org/wiki/Gaussian_function + +python/black : True + + +""" +from numpy import pi, sqrt, exp + + +def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int: + """ + >>> gaussian(1) + 0.24197072451914337 + + >>> gaussian(24) + 3.342714441794458e-126 + + # Supports NumPy Arrays + # Use numpy.meshgrid with this to generate gaussian blur on images. + >>> import numpy as np + >>> x = np.arange(15) + >>> gaussian(x) + array([3.98942280e-01, 2.41970725e-01, 5.39909665e-02, 4.43184841e-03, + 1.33830226e-04, 1.48671951e-06, 6.07588285e-09, 9.13472041e-12, + 5.05227108e-15, 1.02797736e-18, 7.69459863e-23, 2.11881925e-27, + 2.14638374e-32, 7.99882776e-38, 1.09660656e-43]) + + >>> gaussian(15) + 5.530709549844416e-50 + + >>> gaussian([1,2, 'string']) + Traceback (most recent call last): + ... + TypeError: unsupported operand type(s) for -: 'list' and 'float' + + >>> gaussian('hello world') + Traceback (most recent call last): + ... + TypeError: unsupported operand type(s) for -: 'str' and 'float' + + >>> gaussian(10**234) + Traceback (most recent call last): + ... + OverflowError: (34, 'Result too large') + + >>> gaussian(10**-326) + 0.3989422804014327 + + >>> gaussian(2523, mu=234234, sigma=3425) + 0.0 + """ + return 1 / sqrt(2 * pi * sigma ** 2) * exp(-(x - mu) ** 2 / 2 * sigma ** 2) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/physics/harmonic_oscillator.py b/physics/harmonic_oscillator.py new file mode 100644 index 000000000000..3534a14369af --- /dev/null +++ b/physics/harmonic_oscillator.py @@ -0,0 +1,87 @@ +""" +Class to return properties of harmonic motion at x displacement. +This object can be used in game physics too. + +python/black: True +""" +__authors__ = ["n0vice"] +__created__ = "2-14-2019" +__last_modified__ = "7-20-2019" + +from numpy import sqrt, linspace, cos + + +class HarmonicOscillator: + """ + Harmonic Oscillator object. + Parameters: + mass + k ==> stifness of the oscillator + angular_frequency ==> + Amplitude + """ + + def __init__(self): + self.mass = 1 + self.k = 1 + self.angular_frequency = sqrt(self.k / self.mass) + self.AMPLITUDE = 6 + self.E = 0.5 * self.k * self.AMPLITUDE ** 2 + + def force(self, x): + return -self.k * x + + def position(self, t, A, phase): + return A * cos(self.angular_frequency * t + phase) + + def kinetic_energy(self, x): + return (0.5 * self.mass * self.angular_frequency ** 2) * ( + self.AMPLITUDE ** 2 - x ** 2 + ) + + def potential_energy(self, x): + return self.E - self.kinetic_energy(x) + + def hamiltonian(self, x): + return self.potential_energy(x) + self.kinetic_energy(x) + + def lagrangian(self, x): + return self.kinetic_energy(x) - self.potential_energy(x) + + def demo(self): + import matplotlib.pyplot as plt + + oscillator = self + # Displacement of oscillator + x = linspace(-6, 6, 100) + + plt.title("Properties of Harmonic Oscillator") + + plt.subplot(2, 2, 1) + plt.plot(oscillator.force(x), oscillator.potential_energy(x)) + plt.xlabel("Applied Force") + plt.ylabel("Potential") + + plt.subplot(2, 2, 2) + plt.plot(oscillator.force(x), oscillator.position(x, 1, 1)) + plt.xlabel("Applied Force") + plt.ylabel("Position") + + plt.subplot(2, 2, 3) + plt.plot(oscillator.potential_energy(x), label="Potential Energy") + plt.plot(oscillator.kinetic_energy(x), label="Kinetic Energy") + plt.xlabel("Displacement") + plt.ylabel("Energy") + plt.legend() + + plt.subplot(2, 2, 4) + plt.plot(oscillator.hamiltonian(x), label="Hamiltonian") + plt.plot(oscillator.lagrangian(x), label="Lagrangian") + plt.xlabel("Displacement") + plt.ylabel("Energy") + plt.legend() + plt.show() + + +if __name__ == "__main__": + HarmonicOscillator().demo()