From fd30bc80aa150365c12e4180a4b5944db75da05e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santino=20Di=20Tom=C3=A1s?= Date: Fri, 20 Oct 2023 22:37:32 -0300 Subject: [PATCH 1/5] adding new physics algorithm: center of mass --- physics/center_of_mass.py | 95 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 physics/center_of_mass.py diff --git a/physics/center_of_mass.py b/physics/center_of_mass.py new file mode 100644 index 000000000000..dd9857c9a8d9 --- /dev/null +++ b/physics/center_of_mass.py @@ -0,0 +1,95 @@ +""" +Calculating the center of mass for a discrete system of particles, +given their positions and masses. + +Description: + +In physics, the center of mass of a distribution of mass in space +(sometimes referred to as the barycenter or balance point) is the +unique point at any given time where the weighted relative position +of the distributed mass sums to zero. This is the point to which a +force may be applied to cause a linear acceleration without an angular acceleration. +Calculations in mechanics are often simplified when formulated with respect to +the center of mass. It is a hypothetical point where the entire mass of an object +may be assumed to be concentrated to visualise its motion. In other words, the +center of mass is the particle equivalent of a given object for application of +Newton's laws of motion. + +In the case of a system of particles P_i, i = 1, ..., n , each with mass m_i +that are located in space with coordinates r_i, i = 1, ..., n , the coordinates +R of the center of mass correspond to: + +R = (Σ(mi * ri) / Σ(mi)) + +Reference: https://en.wikipedia.org/wiki/Center_of_mass#:~:text=The%20center%20of%20mass%20is%20the%20unique%20point%20at%20the,distribution%20of%20mass%20in%20space. + +""" + + +def center_of_mass( + particles: list[tuple[float, float, float, float]] +) -> tuple[float, float, float]: + """ + Input Parameters + ---------------- + particles (List[Tuple[float, float, float, float]]): + A list of particles where each particle is a tuple with it´s (x, y, z) + position and it´s mass. + + Returns + ------- + Tuple[float, float, float]: + A tuple with de coordinates of the center of mass (Xcm, Ycm, Zcm) + rounded to two decimal places. + + Examples + -------- + + >>> center_of_mass([(1.5, 4, 3.4, 4), (5, 6.8, 7, 8.1), (9.4, 10.1, 11.6, 12.9)]) + (6.71, 8.05, 8.8) + + + >>> center_of_mass([(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)]) + (6.33, 7.33, 8.33) + + >>> center_of_mass([(1, 2, 3, -4), (5, 6, 7, 8), (9, 10, 11, 12)]) + Traceback (most recent call last): + ... + ValueError: Mass of all particles must be greater than 0 + + >>> center_of_mass([(1, 2, 3, 0), (5, 6, 7, 8), (9, 10, 11, 12)]) + Traceback (most recent call last): + ... + ValueError: Mass of all particles must be greater than 0 + + >>> center_of_mass([]) + Traceback (most recent call last): + ... + ValueError: No particles provided + + """ + if not particles: + raise ValueError("No particles provided") + + for particle in particles: + if particle[3] <= 0: + raise ValueError("Mass of all particles must be greater than 0") + + total_mass = sum(particle[3] for particle in particles) + + center_of_mass_x = round( + sum(particle[0] * particle[3] for particle in particles) / total_mass, 2 + ) + center_of_mass_y = round( + sum(particle[1] * particle[3] for particle in particles) / total_mass, 2 + ) + center_of_mass_z = round( + sum(particle[2] * particle[3] for particle in particles) / total_mass, 2 + ) + return (center_of_mass_x, center_of_mass_y, center_of_mass_z) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 719c969036ab88f524ca171cc9750ca17c623d63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santino=20Di=20Tom=C3=A1s?= Date: Sat, 21 Oct 2023 08:43:32 -0300 Subject: [PATCH 2/5] Add changes requested by the reviewer --- physics/center_of_mass.py | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/physics/center_of_mass.py b/physics/center_of_mass.py index dd9857c9a8d9..27219070e372 100644 --- a/physics/center_of_mass.py +++ b/physics/center_of_mass.py @@ -24,22 +24,34 @@ Reference: https://en.wikipedia.org/wiki/Center_of_mass#:~:text=The%20center%20of%20mass%20is%20the%20unique%20point%20at%20the,distribution%20of%20mass%20in%20space. """ +from typing import NamedTuple -def center_of_mass( - particles: list[tuple[float, float, float, float]] -) -> tuple[float, float, float]: +class Particle(NamedTuple): + x: float + y: float + z: float + mass: float + + +class Coord3D(NamedTuple): + x: float + y: float + z: float + + +def center_of_mass(particles: list[Particle]) -> Coord3D: """ Input Parameters ---------------- - particles (List[Tuple[float, float, float, float]]): + particles: list(Particle): A list of particles where each particle is a tuple with it´s (x, y, z) position and it´s mass. Returns ------- - Tuple[float, float, float]: - A tuple with de coordinates of the center of mass (Xcm, Ycm, Zcm) + Coord3D: + A tuple with the coordinates of the center of mass (Xcm, Ycm, Zcm) rounded to two decimal places. Examples @@ -72,21 +84,21 @@ def center_of_mass( raise ValueError("No particles provided") for particle in particles: - if particle[3] <= 0: + if particle.mass <= 0: raise ValueError("Mass of all particles must be greater than 0") - total_mass = sum(particle[3] for particle in particles) + total_mass = sum(particle.mass for particle in particles) center_of_mass_x = round( - sum(particle[0] * particle[3] for particle in particles) / total_mass, 2 + sum(particle.x * particle.mass for particle in particles) / total_mass, 2 ) center_of_mass_y = round( - sum(particle[1] * particle[3] for particle in particles) / total_mass, 2 + sum(particle.y * particle.mass for particle in particles) / total_mass, 2 ) center_of_mass_z = round( - sum(particle[2] * particle[3] for particle in particles) / total_mass, 2 + sum(particle.z * particle.mass for particle in particles) / total_mass, 2 ) - return (center_of_mass_x, center_of_mass_y, center_of_mass_z) + return Coord3D(center_of_mass_x, center_of_mass_y, center_of_mass_z) if __name__ == "__main__": From 6a364a8b77fda0cd7fa9608ab4b91291d63130d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santino=20Di=20Tom=C3=A1s?= Date: Sat, 21 Oct 2023 09:06:42 -0300 Subject: [PATCH 3/5] Add changes requested by the reviewer --- physics/center_of_mass.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/physics/center_of_mass.py b/physics/center_of_mass.py index 27219070e372..aa7057cb71f4 100644 --- a/physics/center_of_mass.py +++ b/physics/center_of_mass.py @@ -24,6 +24,8 @@ Reference: https://en.wikipedia.org/wiki/Center_of_mass#:~:text=The%20center%20of%20mass%20is%20the%20unique%20point%20at%20the,distribution%20of%20mass%20in%20space. """ + + from typing import NamedTuple @@ -57,19 +59,23 @@ def center_of_mass(particles: list[Particle]) -> Coord3D: Examples -------- - >>> center_of_mass([(1.5, 4, 3.4, 4), (5, 6.8, 7, 8.1), (9.4, 10.1, 11.6, 12.9)]) - (6.71, 8.05, 8.8) + >>> center_of_mass([\ + Particle(1.5, 4, 3.4, 4), Particle(5, 6.8, 7, 8.1), Particle(9.4, 10.1, 11.6, 12)]) + Coord3D(x=6.61, y=7.98, z=8.69) - >>> center_of_mass([(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)]) - (6.33, 7.33, 8.33) + >>> center_of_mass([\ + Particle(1, 2, 3, 4), Particle(5, 6, 7, 8), Particle(9, 10, 11, 12)]) + Coord3D(x=6.33, y=7.33, z=8.33) - >>> center_of_mass([(1, 2, 3, -4), (5, 6, 7, 8), (9, 10, 11, 12)]) + >>> center_of_mass([\ + Particle(1, 2, 3, -4), Particle(5, 6, 7, 8), Particle(9, 10, 11, 12)]) Traceback (most recent call last): ... ValueError: Mass of all particles must be greater than 0 - >>> center_of_mass([(1, 2, 3, 0), (5, 6, 7, 8), (9, 10, 11, 12)]) + >>> center_of_mass([\ + Particle(1, 2, 3, 0), Particle(5, 6, 7, 8), Particle(9, 10, 11, 12)]) Traceback (most recent call last): ... ValueError: Mass of all particles must be greater than 0 From 1bede8f71ed32a4d12fb192e7557e4199da705d7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 22 Oct 2023 01:53:15 +0200 Subject: [PATCH 4/5] Update center_of_mass.py --- physics/center_of_mass.py | 98 +++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 51 deletions(-) diff --git a/physics/center_of_mass.py b/physics/center_of_mass.py index aa7057cb71f4..920845f919dc 100644 --- a/physics/center_of_mass.py +++ b/physics/center_of_mass.py @@ -1,45 +1,33 @@ """ -Calculating the center of mass for a discrete system of particles, -given their positions and masses. +Calculating the center of mass for a discrete system of particles, given their +positions and masses. Description: -In physics, the center of mass of a distribution of mass in space -(sometimes referred to as the barycenter or balance point) is the -unique point at any given time where the weighted relative position -of the distributed mass sums to zero. This is the point to which a -force may be applied to cause a linear acceleration without an angular acceleration. -Calculations in mechanics are often simplified when formulated with respect to -the center of mass. It is a hypothetical point where the entire mass of an object -may be assumed to be concentrated to visualise its motion. In other words, the -center of mass is the particle equivalent of a given object for application of -Newton's laws of motion. - -In the case of a system of particles P_i, i = 1, ..., n , each with mass m_i -that are located in space with coordinates r_i, i = 1, ..., n , the coordinates -R of the center of mass correspond to: +In physics, the center of mass of a distribution of mass in space (sometimes referred +to as the barycenter or balance point) is the unique point at any given time where the +weighted relative positionof the distributed mass sums to zero. This is the point to +which a force may be applied to cause a linear acceleration without an angular +acceleration. -R = (Σ(mi * ri) / Σ(mi)) - -Reference: https://en.wikipedia.org/wiki/Center_of_mass#:~:text=The%20center%20of%20mass%20is%20the%20unique%20point%20at%20the,distribution%20of%20mass%20in%20space. - -""" - - -from typing import NamedTuple +Calculations in mechanics are often simplified when formulated with respect to the +center of mass. It is a hypothetical point where the entire mass of an object may be +assumed to be concentrated to visualize its motion. In other words, the center of mass +is the particle equivalent of a given object for the application of Newton's laws of +motion. +In the case of a system of particles P_i, i = 1, ..., n , each with mass m_i that are +located in space with coordinates r_i, i = 1, ..., n , the coordinates R of the center +of mass corresponds to: -class Particle(NamedTuple): - x: float - y: float - z: float - mass: float +R = (Σ(mi * ri) / Σ(mi)) +Reference: https://en.wikipedia.org/wiki/Center_of_mass +""" +from collections import namedtuple -class Coord3D(NamedTuple): - x: float - y: float - z: float +Particle = namedtuple("Particle", "x y z mass") # noqa: PYI024 +Coord3D = namedtuple("Coord3D", "x y z") # noqa: PYI024 def center_of_mass(particles: list[Particle]) -> Coord3D: @@ -47,35 +35,45 @@ def center_of_mass(particles: list[Particle]) -> Coord3D: Input Parameters ---------------- particles: list(Particle): - A list of particles where each particle is a tuple with it´s (x, y, z) - position and it´s mass. + A list of particles where each particle is a tuple with it´s (x, y, z) position and + it´s mass. Returns ------- Coord3D: - A tuple with the coordinates of the center of mass (Xcm, Ycm, Zcm) - rounded to two decimal places. + A tuple with the coordinates of the center of mass (Xcm, Ycm, Zcm) rounded to two + decimal places. Examples -------- - - >>> center_of_mass([\ - Particle(1.5, 4, 3.4, 4), Particle(5, 6.8, 7, 8.1), Particle(9.4, 10.1, 11.6, 12)]) + >>> center_of_mass([ + ... Particle(1.5, 4, 3.4, 4), + ... Particle(5, 6.8, 7, 8.1), + ... Particle(9.4, 10.1, 11.6, 12) + ... ]) Coord3D(x=6.61, y=7.98, z=8.69) - - >>> center_of_mass([\ - Particle(1, 2, 3, 4), Particle(5, 6, 7, 8), Particle(9, 10, 11, 12)]) + >>> center_of_mass([ + ... Particle(1, 2, 3, 4), + ... Particle(5, 6, 7, 8), + ... Particle(9, 10, 11, 12) + ... ]) Coord3D(x=6.33, y=7.33, z=8.33) - >>> center_of_mass([\ - Particle(1, 2, 3, -4), Particle(5, 6, 7, 8), Particle(9, 10, 11, 12)]) + >>> center_of_mass([ + ... Particle(1, 2, 3, -4), + ... Particle(5, 6, 7, 8), + ... Particle(9, 10, 11, 12) + ... ]) Traceback (most recent call last): ... ValueError: Mass of all particles must be greater than 0 - >>> center_of_mass([\ - Particle(1, 2, 3, 0), Particle(5, 6, 7, 8), Particle(9, 10, 11, 12)]) + >>> center_of_mass([ + ... Particle(1, 2, 3, 0), + ... Particle(5, 6, 7, 8), + ... Particle(9, 10, 11, 12) + ... ]) Traceback (most recent call last): ... ValueError: Mass of all particles must be greater than 0 @@ -84,14 +82,12 @@ def center_of_mass(particles: list[Particle]) -> Coord3D: Traceback (most recent call last): ... ValueError: No particles provided - """ if not particles: raise ValueError("No particles provided") - for particle in particles: - if particle.mass <= 0: - raise ValueError("Mass of all particles must be greater than 0") + if any(particle.mass <= 0 for particle in particles): + raise ValueError("Mass of all particles must be greater than 0") total_mass = sum(particle.mass for particle in particles) From 8fc8300368ce67df559f078c2de36f3eb0e414d1 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 22 Oct 2023 01:56:35 +0200 Subject: [PATCH 5/5] Update center_of_mass.py --- physics/center_of_mass.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/center_of_mass.py b/physics/center_of_mass.py index 920845f919dc..bd9ba2480584 100644 --- a/physics/center_of_mass.py +++ b/physics/center_of_mass.py @@ -6,7 +6,7 @@ In physics, the center of mass of a distribution of mass in space (sometimes referred to as the barycenter or balance point) is the unique point at any given time where the -weighted relative positionof the distributed mass sums to zero. This is the point to +weighted relative position of the distributed mass sums to zero. This is the point to which a force may be applied to cause a linear acceleration without an angular acceleration.