From a16a4e0187b375ae47eca6cef946290d54047442 Mon Sep 17 00:00:00 2001 From: Muhammad Essa Date: Sun, 22 Mar 2026 21:58:36 +0500 Subject: [PATCH 1/2] Add relativistic kinetic energy calculation with doctests and type hints --- physics/relativistic_kinetic_energy.py | 67 ++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 physics/relativistic_kinetic_energy.py diff --git a/physics/relativistic_kinetic_energy.py b/physics/relativistic_kinetic_energy.py new file mode 100644 index 000000000000..6f0e7efe0731 --- /dev/null +++ b/physics/relativistic_kinetic_energy.py @@ -0,0 +1,67 @@ +""" +Find the relativistic kinetic energy of a particle, given its rest mass and velocity. + +Description: In special relativity, the kinetic energy of a particle is the extra +energy it has due to its motion beyond the energy associated with its rest mass. +It is defined as the difference between the total relativistic energy and the rest +energy of the particle. After a force does work to accelerate a particle from rest +to some high speed comparable to the speed of light, the particle carries this +relativistic kinetic energy as long as its speed stays the same. The same amount of +energy must be removed (for example, by an opposite force) to slow the particle +back down to rest. Formally, relativistic kinetic energy appears in the relativistic +energy momentum relation and depends on the Lorentz factor, which encodes how time +and space change at high speeds. + +In relativistic mechanics, the kinetic energy K of a particle with rest mass m +moving at speed v is + + K = (y - 1) m c^2, + +where c is the speed of light in vacuum and + + y = 1 / sqrt(1 - v^2 / c^2) + +is the Lorentz factor. At speeds much smaller than c, this expression reduces to the +classical formula K ≈ (1/2) m v^2, so the relativistic result agrees with Newtonian +kinetic energy in the low velocity limit.The standard unit of kinetic energy is the +joule, while the English unit of kinetic energy is the foot-pound. + +Reference : https://en.wikipedia.org/wiki/Kinetic_energy +""" + +from math import sqrt + +from scipy.constants import c # speed of light in vacuum (299792458 m/s) + + +def relativistic_kinetic_energy(mass: float, velocity: float) -> float: + """ + Calculate relativistic kinetic energy. + mass --- kg + velocity ---- m/s + K.E ---- j + + >>> relativistic_kinetic_energy(10,10) + 598.6912157608277 + >>> relativistic_kinetic_energy(40,200000) + 800000266962.5057 + >>> relativistic_kinetic_energy(50,100000) + 250000021062.11475 + >>> relativistic_kinetic_energy(0,0) + 0.0 + >>> relativistic_kinetic_energy(100,0) + 0.0 + + """ + + if (mass < 0 ): + raise ValueError("The mass of a body cannot be negative") + else: + gamma = 1/sqrt(1 - (velocity**2 / c**2)) + return (gamma-1) * mass * c**2 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 569f5151b167ee29f72e3e5d545000cc3eed57cf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 22 Mar 2026 17:42:31 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/relativistic_kinetic_energy.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/physics/relativistic_kinetic_energy.py b/physics/relativistic_kinetic_energy.py index 6f0e7efe0731..a2d2ab9e93f7 100644 --- a/physics/relativistic_kinetic_energy.py +++ b/physics/relativistic_kinetic_energy.py @@ -54,11 +54,11 @@ def relativistic_kinetic_energy(mass: float, velocity: float) -> float: """ - if (mass < 0 ): + if mass < 0: raise ValueError("The mass of a body cannot be negative") else: - gamma = 1/sqrt(1 - (velocity**2 / c**2)) - return (gamma-1) * mass * c**2 + gamma = 1 / sqrt(1 - (velocity**2 / c**2)) + return (gamma - 1) * mass * c**2 if __name__ == "__main__":