Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add mass energy equivalence in physics and doctests #10202

Merged
merged 9 commits into from
Oct 23, 2023
5 changes: 5 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

## Boolean Algebra
* [And Gate](boolean_algebra/and_gate.py)
* [Imply Gate](boolean_algebra/imply_gate.py)
* [Nand Gate](boolean_algebra/nand_gate.py)
* [Nor Gate](boolean_algebra/nor_gate.py)
* [Not Gate](boolean_algebra/not_gate.py)
Expand Down Expand Up @@ -174,7 +175,9 @@
## Data Structures
* Arrays
* [Equilibrium Index In Array](data_structures/arrays/equilibrium_index_in_array.py)
* [Find Triplets With 0 Sum](data_structures/arrays/find_triplets_with_0_sum.py)
* [Median Two Array](data_structures/arrays/median_two_array.py)
* [Pairs With Given Sum](data_structures/arrays/pairs_with_given_sum.py)
* [Permutations](data_structures/arrays/permutations.py)
* [Prefix Sum](data_structures/arrays/prefix_sum.py)
* [Product Sum](data_structures/arrays/product_sum.py)
Expand Down Expand Up @@ -385,6 +388,7 @@

## Financial
* [Equated Monthly Installments](financial/equated_monthly_installments.py)
* [Exponential Moving Average](financial/exponential_moving_average.py)
* [Interest](financial/interest.py)
* [Present Value](financial/present_value.py)
* [Price Plus Tax](financial/price_plus_tax.py)
Expand Down Expand Up @@ -793,6 +797,7 @@
* [Kinetic Energy](physics/kinetic_energy.py)
* [Lorentz Transformation Four Vector](physics/lorentz_transformation_four_vector.py)
* [Malus Law](physics/malus_law.py)
* [Mass Energy Equivalence](physics/mass_energy_equivalence.py)
* [Mirror Formulae](physics/mirror_formulae.py)
* [N Body Simulation](physics/n_body_simulation.py)
* [Newtons Law Of Gravitation](physics/newtons_law_of_gravitation.py)
Expand Down
77 changes: 77 additions & 0 deletions physics/mass_energy_equivalence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
Title:
Finding the energy equivalence of mass and mass equivalence of energy
by Einstein's equation.

Description:
Einstein's mass-energy equivalence is a pivotal concept in theoretical physics.
It asserts that energy (E) and mass (m) are directly related by the speed of
light in vacuum (c) squared, as described in the equation E = mc². This means that
mass and energy are interchangeable; a mass increase corresponds to an energy increase,
and vice versa. This principle has profound implications in nuclear reactions,
explaining the release of immense energy from minuscule changes in atomic nuclei.

Equations:
E = mc² and m = E/c², where m is mass, E is Energy, c is speed of light in vacuum.

Reference:
https://en.wikipedia.org/wiki/Mass%E2%80%93energy_equivalence
"""

from scipy.constants import c # speed of light in vacuum (299792458 m/s)


def energy_from_mass(mass: float) -> float:
"""
Calculates the Energy equivalence of the Mass using E = mc²
in SI units J from Mass in kg.

mass (float): Mass of body.

Usage example:
>>> energy_from_mass(124.56)
1.11948945063458e+19
>>> energy_from_mass(320)
2.8760165719578165e+19
>>> energy_from_mass(0)
0.0
>>> energy_from_mass(-967.9)
Traceback (most recent call last):
...
ValueError: Mass can't be negative.

"""
if mass < 0:
raise ValueError("Mass can't be negative.")
return mass * c**2


def mass_from_energy(energy: float) -> float:
"""
Calculates the Mass equivalence of the Energy using m = E/c²
in SI units kg from Energy in J.

energy (float): Mass of body.

Usage example:
>>> mass_from_energy(124.56)
1.3859169098203872e-15
>>> mass_from_energy(320)
3.560480179371579e-15
>>> mass_from_energy(0)
0.0
>>> mass_from_energy(-967.9)
Traceback (most recent call last):
...
ValueError: Energy can't be negative.

"""
if energy < 0:
raise ValueError("Energy can't be negative.")
return energy / c**2


if __name__ == "__main__":
import doctest

doctest.testmod()