mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-09-29 17:08:49 +08:00
* Relativistic velocity summation function The formula v2=(v1+v)/(1+v1*v/c^2) is implemented. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Lines divided * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Long line cut * relativistic_velocity_summation function parameters explained * Gave meaningful names to function parameters * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Comment put to a top * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Put # in comments * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Line shortened * Enhance docstring with reference link Updated docstring to include a reference link for the velocity addition formula. * updating DIRECTORY.md * updating DIRECTORY.md * Fix grammar in relativistic kinetic energy comments Corrected minor grammatical errors in comments and documentation. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
"""
|
|
The relativistic velocity summation formula calculates the combined velocity v2 of
|
|
an object moving at speed v1 relative to a frame that is itself moving at velocity v
|
|
relative to an observer. I take the last one to be strictly lower than the speed of
|
|
light.
|
|
The formula is v2 = (v1 + v)/(1 + v1 * v / c**2)
|
|
v1 - speed of the object relative to a moving frame
|
|
v - speed of the moving frame
|
|
c - speed of light in a vacuum
|
|
v2 - speed of the object relative to an observer
|
|
|
|
https://en.wikipedia.org/wiki/Velocity-addition_formula
|
|
"""
|
|
|
|
c = 299792458
|
|
|
|
|
|
def relativistic_velocity_summation(
|
|
object_velocity: float, frame_velocity: float
|
|
) -> float:
|
|
"""
|
|
>>> relativistic_velocity_summation(200000000, 200000000)
|
|
276805111.0636436
|
|
>>> relativistic_velocity_summation(299792458, 100000000)
|
|
299792458.0
|
|
>>> relativistic_velocity_summation(100000000, 299792458)
|
|
Traceback (most recent call last):
|
|
...
|
|
ValueError: Speeds must not exceed light speed...
|
|
"""
|
|
if (
|
|
object_velocity > c
|
|
or frame_velocity >= c
|
|
or object_velocity < -c
|
|
or frame_velocity <= -c
|
|
):
|
|
raise ValueError(
|
|
"Speeds must not exceed light speed, and "
|
|
"the frame speed must be lower than the light speed!"
|
|
)
|
|
numerator = object_velocity + frame_velocity
|
|
denominator = 1 + object_velocity * frame_velocity / c**2
|
|
return numerator / denominator
|
|
|
|
|
|
if __name__ == "__main__":
|
|
from doctest import testmod
|
|
|
|
testmod()
|