From e5142d1b09e6e2a7c9de75fec453e552014102e4 Mon Sep 17 00:00:00 2001 From: ZeroDayOwl <56065602+ZeroDayOwl@users.noreply.github.com> Date: Tue, 4 Oct 2022 00:18:58 +0600 Subject: [PATCH 1/5] Add algorithm for Newton's Law of Gravitation --- physics/newtons_law_of_gravitation.py | 101 ++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 physics/newtons_law_of_gravitation.py diff --git a/physics/newtons_law_of_gravitation.py b/physics/newtons_law_of_gravitation.py new file mode 100644 index 000000000000..7a2c2a410651 --- /dev/null +++ b/physics/newtons_law_of_gravitation.py @@ -0,0 +1,101 @@ +""" +Title : Finding the value of either Gravitational Force, one of the masses or distance +provided that the other three parameters are given. + +Description : Newton's Law of Universal Gravitation explains the presence +of force of attraction between bodies having a definite mass situated at a distance. +It is usually stated as that, every particle attracts every other particle +in the universe with a force that is directly proportional to the product +of their masses and inversely proportional to the square of the distance +between their centers. The publication of the theory has become known as +the "first great unification", as it marked the unification of the previously +described phenomena of gravity on Earth with known astronomical +behaviors. + +The equation for the universal gravitation is as follows: +F = (G * mass_1 * mass_2) / (distance)^2 + +Source : +- https://en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation +- Newton (1687) "PhilosophiƦ Naturalis Principia Mathematica" +""" + +from __future__ import annotations + +# Define the Gravitational Constant G and the function +GRAVITATIONAL_CONSTANT = 6.6743e-11 # unit of G : m^3 * kg^-1 * s^-2 + + +def gravitational_law( + force: float, mass_1: float, mass_2: float, distance: float +) -> dict[str, float]: + + """ + Input Parameters + ---------------- + force : magnitude in float, unit in Newton + + mass_1 : magnitude in float, unit in Kilogram + + mass_2 : magnitude in float, unit in Kilogram + + distance : magnitude in float, unit in Meter + + Returns + ------- + result : dict name, value pair of the parameter having Zero as it's value + + Returns the value of one of the parameters specified as 0, provided the values of + other parameters are given. + >>> gravitational_law(force=0, mass_1=5, mass_2=10, distance=20) + {'force': 8.342875e-12} + + >>> gravitational_law(force=7367.382, mass_1=0, mass_2=74, distance=3048) + {'mass_1': 1.385816317292268e+19} + + >>> gravitational_law(force=36337.283, mass_1=0, mass_2=0, distance=35584) + Traceback (most recent call last): + ... + ValueError: Only one argument must be 0 + + >>> gravitational_law(force=36337.283, mass_1=-674, mass_2=0, distance=35584) + Traceback (most recent call last): + ... + ValueError: Mass can not be negative + + >>> gravitational_law(force=-847938e12, mass_1=674, mass_2=0, distance=9374) + Traceback (most recent call last): + ... + ValueError: Gravitational force can not be negative + """ + + product_of_mass = mass_1 * mass_2 + + if (force, mass_1, mass_2, distance).count(0) != 1: + raise ValueError("Only one argument must be 0") + if force < 0: + raise ValueError("Gravitational force can not be negative") + if distance < 0: + raise ValueError("Distance can not be negative") + if mass_1 < 0 or mass_2 < 0: + raise ValueError("Mass can not be negative") + if force == 0: + force = GRAVITATIONAL_CONSTANT * product_of_mass / (distance**2) + return {"force": force} + elif mass_1 == 0: + mass_1 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_2) + return {"mass_1": mass_1} + elif mass_2 == 0: + mass_2 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_1) + return {"mass_2": mass_2} + elif distance == 0: + distance = (GRAVITATIONAL_CONSTANT * product_of_mass / (force)) ** 0.5 + return {"distance": distance} + raise ValueError("One argument must be 0") + + +# Run doctest +if __name__ == "__main__": + import doctest + + doctest.testmod() \ No newline at end of file From 5dcf637e4c2d21cfb6ed9a02302069f7555dd920 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 3 Oct 2022 18:23:33 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/newtons_law_of_gravitation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/newtons_law_of_gravitation.py b/physics/newtons_law_of_gravitation.py index 7a2c2a410651..a7c55e773d30 100644 --- a/physics/newtons_law_of_gravitation.py +++ b/physics/newtons_law_of_gravitation.py @@ -98,4 +98,4 @@ def gravitational_law( if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From c58d92ec62ea49ee1c8f39e95d52adc418584b93 Mon Sep 17 00:00:00 2001 From: Paul <56065602+ZeroDayOwl@users.noreply.github.com> Date: Thu, 6 Oct 2022 21:44:59 +0600 Subject: [PATCH 3/5] Update physics/newtons_law_of_gravitation.py Co-authored-by: Christian Clauss --- physics/newtons_law_of_gravitation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/newtons_law_of_gravitation.py b/physics/newtons_law_of_gravitation.py index a7c55e773d30..19a378da6b62 100644 --- a/physics/newtons_law_of_gravitation.py +++ b/physics/newtons_law_of_gravitation.py @@ -72,7 +72,7 @@ def gravitational_law( product_of_mass = mass_1 * mass_2 if (force, mass_1, mass_2, distance).count(0) != 1: - raise ValueError("Only one argument must be 0") + raise ValueError("One and only one argument must be 0") if force < 0: raise ValueError("Gravitational force can not be negative") if distance < 0: From 2f4cafd40ab07a0e7c7712f9933010749b9d0be1 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 6 Oct 2022 19:08:10 +0200 Subject: [PATCH 4/5] One and only one argument must be 0 --- physics/newtons_law_of_gravitation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/physics/newtons_law_of_gravitation.py b/physics/newtons_law_of_gravitation.py index 19a378da6b62..4f60fcfd726b 100644 --- a/physics/newtons_law_of_gravitation.py +++ b/physics/newtons_law_of_gravitation.py @@ -56,7 +56,7 @@ def gravitational_law( >>> gravitational_law(force=36337.283, mass_1=0, mass_2=0, distance=35584) Traceback (most recent call last): ... - ValueError: Only one argument must be 0 + ValueError: One and only one argument must be 0 >>> gravitational_law(force=36337.283, mass_1=-674, mass_2=0, distance=35584) Traceback (most recent call last): @@ -91,7 +91,7 @@ def gravitational_law( elif distance == 0: distance = (GRAVITATIONAL_CONSTANT * product_of_mass / (force)) ** 0.5 return {"distance": distance} - raise ValueError("One argument must be 0") + raise ValueError("One and only one argument must be 0") # Run doctest From e8f1a7af6a1fe05b13651e6fd99c3f224ef01316 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 6 Oct 2022 19:13:27 +0200 Subject: [PATCH 5/5] Update newtons_law_of_gravitation.py --- physics/newtons_law_of_gravitation.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/physics/newtons_law_of_gravitation.py b/physics/newtons_law_of_gravitation.py index 4f60fcfd726b..0bb27bb2415d 100644 --- a/physics/newtons_law_of_gravitation.py +++ b/physics/newtons_law_of_gravitation.py @@ -2,14 +2,13 @@ Title : Finding the value of either Gravitational Force, one of the masses or distance provided that the other three parameters are given. -Description : Newton's Law of Universal Gravitation explains the presence -of force of attraction between bodies having a definite mass situated at a distance. -It is usually stated as that, every particle attracts every other particle -in the universe with a force that is directly proportional to the product -of their masses and inversely proportional to the square of the distance -between their centers. The publication of the theory has become known as -the "first great unification", as it marked the unification of the previously -described phenomena of gravity on Earth with known astronomical +Description : Newton's Law of Universal Gravitation explains the presence of force of +attraction between bodies having a definite mass situated at a distance. It is usually +stated as that, every particle attracts every other particle in the universe with a +force that is directly proportional to the product of their masses and inversely +proportional to the square of the distance between their centers. The publication of the +theory has become known as the "first great unification", as it marked the unification +of the previously described phenomena of gravity on Earth with known astronomical behaviors. The equation for the universal gravitation is as follows: @@ -33,13 +32,13 @@ def gravitational_law( """ Input Parameters ---------------- - force : magnitude in float, unit in Newton + force : magnitude in Newtons - mass_1 : magnitude in float, unit in Kilogram + mass_1 : mass in Kilograms - mass_2 : magnitude in float, unit in Kilogram + mass_2 : mass in Kilograms - distance : magnitude in float, unit in Meter + distance : distance in Meters Returns -------