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 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 pi i 1 n each with mass mi that are located in space with coordinates ri i 1 n the coordinates r of the center of mass corresponds to r mi ri mi reference https en wikipedia orgwikicenterofmass input parameters particles listparticle a list of particles where each particle is a tuple with its x y z position and its mass returns coord3d a tuple with the coordinates of the center of mass xcm ycm zcm rounded to two decimal places examples centerofmass particle1 5 4 3 4 4 particle5 6 8 7 8 1 particle9 4 10 1 11 6 12 coord3dx6 61 y7 98 z8 69 centerofmass particle1 2 3 4 particle5 6 7 8 particle9 10 11 12 coord3dx6 33 y7 33 z8 33 centerofmass particle1 2 3 4 particle5 6 7 8 particle9 10 11 12 traceback most recent call last valueerror mass of all particles must be greater than 0 centerofmass particle1 2 3 0 particle5 6 7 8 particle9 10 11 12 traceback most recent call last valueerror mass of all particles must be greater than 0 centerofmass traceback most recent call last valueerror no particles provided noqa pyi024 noqa pyi024 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 returns coord3d 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 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 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 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 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
from collections import namedtuple

Particle = namedtuple("Particle", "x y z mass")  
Coord3D = namedtuple("Coord3D", "x y z")  


def center_of_mass(particles: list[Particle]) -> Coord3D:
    
    if not particles:
        raise ValueError("No particles provided")

    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)

    center_of_mass_x = round(
        sum(particle.x * particle.mass for particle in particles) / total_mass, 2
    )
    center_of_mass_y = round(
        sum(particle.y * particle.mass for particle in particles) / total_mass, 2
    )
    center_of_mass_z = round(
        sum(particle.z * particle.mass for particle in particles) / total_mass, 2
    )
    return Coord3D(center_of_mass_x, center_of_mass_y, center_of_mass_z)


if __name__ == "__main__":
    import doctest

    doctest.testmod()