From 6b973f95d740207ae61b643e6e732144676fbe51 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Mon, 9 Mar 2026 15:24:26 +0200 Subject: [PATCH 01/10] Relativistic velocity summation function The formula v2=(v1+v)/(1+v1*v/c^2) is implemented. --- physics/relativistic_velocity_summation.py | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 physics/relativistic_velocity_summation.py diff --git a/physics/relativistic_velocity_summation.py b/physics/relativistic_velocity_summation.py new file mode 100644 index 000000000000..6e7a4943d917 --- /dev/null +++ b/physics/relativistic_velocity_summation.py @@ -0,0 +1,29 @@ +c = 299792458 + +""" +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) +""" + +def relativistic_velocity_summation (v1: float, v: 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, and the frame speed must be lower than the light speed! + """ + if v1 > c or v >= c or v1 < -c or v <= -c: + raise ValueError("Speeds must not exceed light speed, and the frame speed must be lower than the light speed!") + return (v1 + v)/(1 + v1 * v / (c * c)) + +if __name__ == "__main__": + from doctest import testmod + + testmod() From 291588cbb59e551019765f66f9ea8c677ed7dd70 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Mar 2026 13:26:11 +0000 Subject: [PATCH 02/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/relativistic_velocity_summation.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/physics/relativistic_velocity_summation.py b/physics/relativistic_velocity_summation.py index 6e7a4943d917..65ed8a78f070 100644 --- a/physics/relativistic_velocity_summation.py +++ b/physics/relativistic_velocity_summation.py @@ -1,14 +1,15 @@ c = 299792458 """ -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 +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) """ -def relativistic_velocity_summation (v1: float, v: float) -> float: + +def relativistic_velocity_summation(v1: float, v: float) -> float: """ >>> relativistic_velocity_summation(200000000, 200000000) 276805111.0636436 @@ -20,8 +21,11 @@ def relativistic_velocity_summation (v1: float, v: float) -> float: ValueError: Speeds must not exceed light speed, and the frame speed must be lower than the light speed! """ if v1 > c or v >= c or v1 < -c or v <= -c: - raise ValueError("Speeds must not exceed light speed, and the frame speed must be lower than the light speed!") - return (v1 + v)/(1 + v1 * v / (c * c)) + raise ValueError( + "Speeds must not exceed light speed, and the frame speed must be lower than the light speed!" + ) + return (v1 + v) / (1 + v1 * v / (c * c)) + if __name__ == "__main__": from doctest import testmod From e6f70748f2941b018b2c7e0499b62f0dd93e4c17 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Mon, 9 Mar 2026 15:34:16 +0200 Subject: [PATCH 03/10] Lines divided --- physics/relativistic_velocity_summation.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/physics/relativistic_velocity_summation.py b/physics/relativistic_velocity_summation.py index 65ed8a78f070..bfb2b12ef15d 100644 --- a/physics/relativistic_velocity_summation.py +++ b/physics/relativistic_velocity_summation.py @@ -18,11 +18,13 @@ def relativistic_velocity_summation(v1: float, v: float) -> float: >>> relativistic_velocity_summation(100000000, 299792458) Traceback (most recent call last): ... - ValueError: Speeds must not exceed light speed, and the frame speed must be lower than the light speed! + ValueError: Speeds must not exceed light speed, and + the frame speed must be lower than the light speed! """ if v1 > c or v >= c or v1 < -c or v <= -c: raise ValueError( - "Speeds must not exceed light speed, and the frame speed must be lower than the light speed!" + "Speeds must not exceed light speed, and + the frame speed must be lower than the light speed!" ) return (v1 + v) / (1 + v1 * v / (c * c)) From aded8bb772b7c89ee0a7fef213b72ee80e810de5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Mar 2026 13:34:30 +0000 Subject: [PATCH 04/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/relativistic_velocity_summation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/physics/relativistic_velocity_summation.py b/physics/relativistic_velocity_summation.py index bfb2b12ef15d..1d9ab70ee1e2 100644 --- a/physics/relativistic_velocity_summation.py +++ b/physics/relativistic_velocity_summation.py @@ -18,12 +18,12 @@ def relativistic_velocity_summation(v1: float, v: float) -> float: >>> relativistic_velocity_summation(100000000, 299792458) Traceback (most recent call last): ... - ValueError: Speeds must not exceed light speed, and + ValueError: Speeds must not exceed light speed, and the frame speed must be lower than the light speed! """ if v1 > c or v >= c or v1 < -c or v <= -c: raise ValueError( - "Speeds must not exceed light speed, and + "Speeds must not exceed light speed, and the frame speed must be lower than the light speed!" ) return (v1 + v) / (1 + v1 * v / (c * c)) From ed9b0fd769b07cdb0fc73071c2011e43bc579136 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Mon, 9 Mar 2026 15:39:43 +0200 Subject: [PATCH 05/10] Long line cut --- physics/relativistic_velocity_summation.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/physics/relativistic_velocity_summation.py b/physics/relativistic_velocity_summation.py index 1d9ab70ee1e2..b9dc757adcbb 100644 --- a/physics/relativistic_velocity_summation.py +++ b/physics/relativistic_velocity_summation.py @@ -18,13 +18,12 @@ def relativistic_velocity_summation(v1: float, v: float) -> float: >>> relativistic_velocity_summation(100000000, 299792458) Traceback (most recent call last): ... - ValueError: Speeds must not exceed light speed, and - the frame speed must be lower than the light speed! + ValueError: Speeds must not exceed light speed... """ if v1 > c or v >= c or v1 < -c or v <= -c: raise ValueError( - "Speeds must not exceed light speed, and - the frame speed must be lower than the light speed!" + "Speeds must not exceed light speed, and " + "the frame speed must be lower than the light speed!" ) return (v1 + v) / (1 + v1 * v / (c * c)) From 4d4298c62a841129ee759582d19cf651bbf84024 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Mon, 9 Mar 2026 15:50:27 +0200 Subject: [PATCH 06/10] relativistic_velocity_summation function parameters explained --- physics/relativistic_velocity_summation.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/physics/relativistic_velocity_summation.py b/physics/relativistic_velocity_summation.py index b9dc757adcbb..8b220dcd2c19 100644 --- a/physics/relativistic_velocity_summation.py +++ b/physics/relativistic_velocity_summation.py @@ -6,6 +6,10 @@ 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 the vacuum +v2 - speed of the object relative to an observer """ From a01c7e4fe2661303d0fccce57d7818c37b544e88 Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Mon, 9 Mar 2026 16:11:29 +0200 Subject: [PATCH 07/10] Gave meaningful names to function parameters --- physics/relativistic_velocity_summation.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/physics/relativistic_velocity_summation.py b/physics/relativistic_velocity_summation.py index 8b220dcd2c19..cb8def23b57d 100644 --- a/physics/relativistic_velocity_summation.py +++ b/physics/relativistic_velocity_summation.py @@ -5,7 +5,7 @@ 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) +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 the vacuum @@ -13,7 +13,9 @@ """ -def relativistic_velocity_summation(v1: float, v: float) -> float: +def relativistic_velocity_summation( + object_velocity: float, frame_velocity: float +) -> float: """ >>> relativistic_velocity_summation(200000000, 200000000) 276805111.0636436 @@ -24,12 +26,15 @@ def relativistic_velocity_summation(v1: float, v: float) -> float: ... ValueError: Speeds must not exceed light speed... """ - if v1 > c or v >= c or v1 < -c or v <= -c: + 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!" ) - return (v1 + v) / (1 + v1 * v / (c * c)) + numerator = object_velocity + frame_velocity + denominator = 1 + object_velocity * frame_velocity / c**2 + return numerator / denominator if __name__ == "__main__": From 5b573f957a707273187c36c164fd750c5d0c78ba Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Mar 2026 14:14:04 +0000 Subject: [PATCH 08/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/relativistic_velocity_summation.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/physics/relativistic_velocity_summation.py b/physics/relativistic_velocity_summation.py index cb8def23b57d..5e843a803e39 100644 --- a/physics/relativistic_velocity_summation.py +++ b/physics/relativistic_velocity_summation.py @@ -26,8 +26,12 @@ def relativistic_velocity_summation( ... ValueError: Speeds must not exceed light speed... """ - if (object_velocity > c or frame_velocity >= c or - object_velocity < -c or frame_velocity <= -c): + 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!" From 545f810ad5b23ab3742b13033a06d46416008ecd Mon Sep 17 00:00:00 2001 From: kvadrik <41710943+kvadrik@users.noreply.github.com> Date: Mon, 9 Mar 2026 18:48:50 +0200 Subject: [PATCH 09/10] Comment put to a top --- physics/relativistic_velocity_summation.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/physics/relativistic_velocity_summation.py b/physics/relativistic_velocity_summation.py index 5e843a803e39..c590bbb143bc 100644 --- a/physics/relativistic_velocity_summation.py +++ b/physics/relativistic_velocity_summation.py @@ -1,5 +1,3 @@ -c = 299792458 - """ 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 @@ -12,6 +10,7 @@ v2 - speed of the object relative to an observer """ +c = 299792458 def relativistic_velocity_summation( object_velocity: float, frame_velocity: float From 7d77c883d4d282d1bf72c92a401ae25f1931b832 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Mar 2026 16:49:11 +0000 Subject: [PATCH 10/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/relativistic_velocity_summation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/physics/relativistic_velocity_summation.py b/physics/relativistic_velocity_summation.py index c590bbb143bc..a21cbd2b4565 100644 --- a/physics/relativistic_velocity_summation.py +++ b/physics/relativistic_velocity_summation.py @@ -12,6 +12,7 @@ c = 299792458 + def relativistic_velocity_summation( object_velocity: float, frame_velocity: float ) -> float: