Skip to content

Commit cc0a700

Browse files
authored
Create Relativity.java
Created the file that implements relativity formulae.
1 parent 7d57c57 commit cc0a700

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.thealgorithms.physics;
2+
3+
/**
4+
* Implements relativity theory formulae.
5+
* Provides simple static methods to calculate length contraction and time dilation
6+
* in the laboratory frame with respect to the object's own frame, and velocity
7+
* with respect to the moving frame.
8+
*
9+
* @see
10+
* <a href="https://en.wikipedia.org/wiki/List_of_relativistic_equations">Wikipedia</a>
11+
*/
12+
public final class Relativity {
13+
14+
/* Speed of light in m s^-1 */
15+
public static final double SPEED_OF_LIGHT = 299792458.0;
16+
17+
/**
18+
* Private constructor to prevent instantiation of this utility class.
19+
*/
20+
private Relativity() {
21+
}
22+
23+
/**
24+
* Calculates the gamma parameter that is of paramount importance in relativity
25+
* theory. It is a dimensionless parameter that is equal to 1 for zero velocity
26+
* but tends to infinity when velocity approaches the speed of light.
27+
*
28+
* @param v The velocity (m/s).
29+
* @return The value of gamma parameter.
30+
*/
31+
public static double gamma(double v) {
32+
if (Math.abs(v) >= SPEED_OF_LIGHT)
33+
throw new IllegalArgumentException("Speed must be lower than the speed of light");
34+
return 1.0/Math.sqrt(1-v*v/(SPEED_OF_LIGHT*SPEED_OF_LIGHT));
35+
}
36+
37+
/**
38+
* Calculates the length of an object in the moving frame.
39+
*
40+
* @param length The length of an object in its own frame (m).
41+
* @param v The velocity of the object (m/s).
42+
* @return The length of an object in the laboratory frame (m).
43+
*/
44+
public static double lengthContraction (double length, double v) {
45+
if (length < 0)
46+
throw new IllegalArgumentException("Length must be non-negative");
47+
return length/gamma(v);
48+
}
49+
50+
/**
51+
* Calculates the time that has passed in the moving frame.
52+
*
53+
* @param length The time that has passed in the object's own frame (s).
54+
* @param v The velocity of the object (m/s).
55+
* @return The time that has passed in the laboratory frame (s).
56+
*/
57+
public static double timeDilation (double time, double v) {
58+
if (time < 0)
59+
throw new IllegalArgumentException("Time must be non-negative");
60+
return time*gamma(v);
61+
}
62+
63+
/**
64+
* Calculates the velocity with respect to the moving frame.
65+
*
66+
* @param v1 The velocity of the object with respect to laboratory frame (m/s).
67+
* @param v The velocity of the moving frame (m/s).
68+
* @return The velocity with respect to the moving frame (m/s).
69+
*/
70+
public static double velocityAddition (double v1, double v) {
71+
if (Math.abs(v1) > SPEED_OF_LIGHT)
72+
throw new IllegalArgumentException("Speed must not exceed the speed of light");
73+
if (Math.abs(v) >= SPEED_OF_LIGHT)
74+
throw new IllegalArgumentException("Frame speed must be lower than the speed of light");
75+
return (v1 - v)/(1 - v1*v/(SPEED_OF_LIGHT*SPEED_OF_LIGHT));
76+
}
77+
}
78+

0 commit comments

Comments
 (0)