-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
feat: Add mass energy equivalence in physics and doctests #10202
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6d3b58e
updating DIRECTORY.md
8e79893
feat: Add mass energy equivalence in physics
SilverDragonOfR 6064787
Merge branch 'master' of https://github.com/SilverDragonOfR/Python
SilverDragonOfR 76647fe
updating DIRECTORY.md
77ac261
Merge branch 'master' into master
SilverDragonOfR 831fe78
updating DIRECTORY.md
4f5e312
Apply suggestions from code review
tianyizheng02 bed531e
Update physics/mass_energy_equivalence.py
tianyizheng02 ce57c90
Update mass_energy_equivalence.py
tianyizheng02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
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 | ||
""" | ||
|
||
# speed of light in vacuum (c) in m/s | ||
SPEED_OF_LIGHT = 299792458.0 | ||
|
||
|
||
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.1194894506345802e+19 | ||
>>> energy_from_mass(320) | ||
2.8760165719578165e+19 | ||
tianyizheng02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> 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 * SPEED_OF_LIGHT * SPEED_OF_LIGHT | ||
tianyizheng02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
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 / (SPEED_OF_LIGHT * SPEED_OF_LIGHT) | ||
tianyizheng02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.