Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
be3aacb
Module for Inelastic and Elastic Collisions
autobotx343 Jun 8, 2026
d56039a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 8, 2026
fc92cad
Merge pull request #1 from autobotx343/add-collisions
autobotx343 Jun 8, 2026
63c4a27
fixed error to push to main
autobotx343 Jun 9, 2026
809da52
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 9, 2026
27e8701
ruff changes
autobotx343 Jun 9, 2026
83f1c46
Merge branch 'add-collisions' of https://github.com/autobotx343/Pytho…
autobotx343 Jun 9, 2026
8fee167
ruff changes
autobotx343 Jun 9, 2026
45d51d9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 9, 2026
67e0218
fixed error to push to main
autobotx343 Jun 9, 2026
5aa3910
ruff changes
autobotx343 Jun 9, 2026
8864a1f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 9, 2026
d1ce624
ruff changes
autobotx343 Jun 9, 2026
7a94011
Merge branch 'add-collisions' of https://github.com/autobotx343/Pytho…
autobotx343 Jun 9, 2026
befc379
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 10, 2026
b4c6e5d
ruff changes 2.0
autobotx343 Jun 15, 2026
7f2f419
Merge branch 'add-collisions' of https://github.com/autobotx343/Pytho…
autobotx343 Jun 15, 2026
8aa7fee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 15, 2026
48bc582
final change
autobotx343 Jun 15, 2026
477dace
Merge branch 'add-collisions' of https://github.com/autobotx343/Pytho…
autobotx343 Jun 15, 2026
4262f71
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 15, 2026
facc61f
added spring potential energy
autobotx343 Jun 17, 2026
4d86697
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 17, 2026
eb4c3e3
Potential Energy, and Time complexity added
autobotx343 Jun 17, 2026
ff86242
Merge branch 'add-collisions' of https://github.com/autobotx343/Pytho…
autobotx343 Jun 17, 2026
b2b59b1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions physics/collisions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
"""
Collision types and final velocities are central to physics.
This module computes final velocities for inelastic and elastic collisions.
It also classifies the collision type from initial and final velocities.

Description: Collisions happen when two masses interact head-on.
There are two types: inelastic and elastic. In inelastic collisions, the
masses stick together and share one final velocity. In elastic collisions,
momentum and kinetic energy are conserved, and masses rebound.
Momentum is mass times velocity; kinetic energy is 1/2 mv^2.
The collision type comes from comparing initial and final momentum and
kinetic energy of the system.

Reference: https://en.wikipedia.org/wiki/Collision
"""


def inelastic_collisions(
mass1: float, mass2: float, velocity1: float, velocity2: float
) -> float:
"""Calculate final velocity after a perfectly inelastic collision.

The two objects stick together and share a common final velocity.

Parameters:
mass1: Mass of the first object.
mass2: Mass of the second object.
velocity1: Initial velocity of the first object.
velocity2: Initial velocity of the second object.

Returns:
The final combined velocity of the two objects.

Complexity:
Time Complexity: O(1) - Constant execution time for basic arithmetic.
Space Complexity: O(1) - Constant memory allocation.

Examples:
>>> inelastic_collisions(2.0, 3.0, 5.0, 6.0)
5.6
>>> inelastic_collisions(9.0, 8.1, -3.2, 3.1)
-0.22
"""
initial_momentum = (mass1 * velocity1) + (mass2 * velocity2)
total_mass = mass2 + mass1
final_velocity = round((initial_momentum / total_mass), 2)

return final_velocity


def elastic_collisions(
mass1: float, mass2: float, velocity1: float, velocity2: float
) -> str:
"""Calculate final velocities after a perfectly elastic collision.

The collision is head-on and conserves both momentum and kinetic energy.

Parameters:
mass1: Mass of the first object.
mass2: Mass of the second object.
velocity1: Initial velocity of the first object.
velocity2: Initial velocity of the second object.

Returns:
A formatted string containing the final velocities of both objects.

Complexity:
Time Complexity: O(1) - Constant execution time for basic math loops.
Space Complexity: O(1) - Constant memory allocation for fixed-size lists.

Examples:
>>> elastic_collisions(1.0, 2.0, -3.0, -1.0)
'-0.34 ; -2.34'
>>> elastic_collisions(9.0, 8.1, -3.2, 3.1)
'2.76 ; -3.54'
"""
com_velocity = inelastic_collisions(mass1, mass2, velocity1, velocity2)
initial_velocities = [velocity1, velocity2]
final_velocities = []

for vel in initial_velocities:
new_vel = -1 * (vel - com_velocity)
final_vel = com_velocity + new_vel
final_velocities.append(round(final_vel, 2))

return f"{final_velocities[0]} ; {final_velocities[1]}"


def type_collision(
mass1: float,
mass2: float,
velocity_initial1: float,
velocity_initial2: float,
velocity_final1: float,
velocity_final2: float,
) -> str:
"""Determine the collision type from initial and final velocities.

Compares initial and final momentum and kinetic energy to classify the collision.

Parameters:
mass1: Mass of the first object.
mass2: Mass of the second object.
velocity_initial1: Initial velocity of the first object.
velocity_initial2: Initial velocity of the second object.
velocity_final1: Final velocity of the first object.
velocity_final2: Final velocity of the second object.

Returns:
A string describing the collision type.

Complexity:
Time Complexity: O(1) - Constant execution time for evaluation logic.
Space Complexity: O(1) - Constant memory allocation.

Examples:
>>> type_collision(1.0, 1.0, 2.0, 3.0, 2.0, 3.0)
'Perfectly Elastic Collision'
>>> type_collision(1.0, 1.0, 2.0, 3.0, 2.5, 2.5)
'Perfectly Inelastic Collision'
>>> type_collision(1.0, 1.0, 2.0, 3.0, 0.0, 0.0)
'Inelastic Collision'
"""
momentum_initial = (mass1 * velocity_initial1) + (mass2 * velocity_initial2)
momentum_final = (mass1 * velocity_final1) + (mass2 * velocity_final2)
kinetic_initial = 0.5 * (
(mass1 * velocity_initial1**2) + (mass2 * velocity_initial2**2)
)
kinetic_final = 0.5 * ((mass1 * velocity_final1**2) + (mass2 * velocity_final2**2))

if kinetic_final == kinetic_initial and momentum_initial == momentum_final:
return "Perfectly Elastic Collision"
elif kinetic_final != kinetic_initial and momentum_initial == momentum_final:
return "Perfectly Inelastic Collision"
else:
return "Inelastic Collision"


if __name__ == "__main__":
import doctest

doctest.testmod()
114 changes: 82 additions & 32 deletions physics/potential_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,110 @@

"""
Finding the gravitational potential energy of an object with reference
to the earth,by taking its mass and height above the ground as input
to the earth,by taking its mass and height above the ground as input.

Description : Potential energy is stored energy that depends on the
position or configuration of objects within a force field. It can be
released as kinetic energy when the objects move under that force.

Description : Gravitational energy or gravitational potential energy
is the potential energy a massive object has in relation to another
massive object due to gravity. It is the potential energy associated
with the gravitational field, which is released (converted into
kinetic energy) when the objects fall towards each other.
Gravitational potential energy increases when two objects
are brought further apart.
Gravitational potential energy is the energy an object has because of
its position in a gravitational field. It is the potential energy a
massive object has in relation to another massive object due to gravity.
This energy is associated with the gravitational field and is released
when the objects fall toward each other. Near the Earth's surface this
potential energy is approximately proportional to mass, gravity, and
height, so it is often written as U = mgh for a body close to Earth.

For two pairwise interacting point particles, the gravitational
potential energy U is given by
Spring potential energy is the stored energy in an elastic spring when
it is compressed or stretched from its rest length. For small deformations,
it is proportional to the square of the displacement from equilibrium.

For two point particles interacting pairwise, the gravitational potential
energy U is given by
U=-GMm/R
where M and m are the masses of the two particles, R is the distance
between them, and G is the gravitational constant.
Close to the Earth's surface, the gravitational field is approximately
constant, and the gravitational potential energy of an object reduces to
U=mgh
where m is the object's mass, g=GM/R² is the gravity of Earth, and h is
the height of the object's center of mass above a chosen reference level.

Reference : "https://en.m.wikipedia.org/wiki/Gravitational_energy"
"""


def potential_energy(mass: float, height: float) -> float:
# function will accept mass and height as parameters and return potential energy
def gravitational_potential_energy(mass: float, height: float) -> float:
"""
>>> potential_energy(10,10)
980.665
>>> potential_energy(0,5)
0.0
>>> potential_energy(8,0)
0.0
>>> potential_energy(10,5)
490.3325
>>> potential_energy(0,0)
0.0
>>> potential_energy(2,8)
156.9064
>>> potential_energy(20,100)
19613.3
Function calculates the gravitational potential energy of an object.

Parameters:
mass: Mass of the object
height: Height the object is at

Returns:
The value of energy in Joules

Complexity:
Time Complexity: O(1) - Constant execution time for evaluation logic.
Space Complexity: O(1) - Constant memory allocation.

Examples:
>>> gravitational_potential_energy(10,10)
980.665
>>> gravitational_potential_energy(0,5)
0.0
>>> gravitational_potential_energy(8,0)
0.0
>>> gravitational_potential_energy(10,5)
490.3325
>>> gravitational_potential_energy(0,0)
0.0
>>> gravitational_potential_energy(2,8)
156.9064
>>> gravitational_potential_energy(20,100)
19613.3
"""
if mass < 0:
# handling of negative values of mass
raise ValueError("The mass of a body cannot be negative")
if height < 0:
# handling of negative values of height
raise ValueError("The height above the ground cannot be negative")

return mass * g * height


def spring_potential_energy(spr_con: float, dspl: float) -> float:
"""
Function calculates the spring potential energy of an object.

Parameters:
spr_con: The spring constant of a spring
dspl: The length of the displacement of the spring

Returns:
The value of energy in Joules

Complexity:
Time Complexity: O(1) - Constant execution time for evaluation logic.
Space Complexity: O(1) - Constant memory allocation.

Examples:
>>> spring_potential_energy(100,2)
200.0
>>> spring_potential_energy(10,0.5)
1.25
>>> spring_potential_energy(8,0)
0.0
>>> spring_potential_energy(14.6,8)
467.2
>>> spring_potential_energy(17,4.5)
172.125
"""
if spr_con < 0:
raise ValueError("The values for spring_constant cannot be negative")

return 0.5 * spr_con * (dspl**2)


if __name__ == "__main__":
from doctest import testmod

testmod(name="potential_energy")
print(spring_potential_energy(17, 4.5))
testmod(name="gravitational_potential_energy")