Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Java/src/main/java/com/thealgorithms/physics/Relativity.java at master · datpp/Java · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
datpp
Java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
thealgorithms
/
physics
/
Relativity.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
81 lines (74 loc) · 2.93 KB
master
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
thealgorithms
/
physics
/
Relativity.java
Copy path
Top
File metadata and controls
Code
Blame
81 lines (74 loc) · 2.93 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package
com
.
thealgorithms
.
physics
;
/**
* Implements relativity theory formulae.
* Provides simple static methods to calculate length contraction and time dilation
* in the laboratory frame with respect to the object's own frame, and velocity
* with respect to the moving frame.
*
* @see <a href="https://en.wikipedia.org/wiki/List_of_relativistic_equations">Wikipedia</a>
*/
public
final
class
Relativity
{
/* Speed of light in m s^-1 */
public
static
final
double
SPEED_OF_LIGHT
=
299792458.0
;
/**
* Private constructor to prevent instantiation of this utility class.
*/
private
Relativity
() {
}
/**
* Calculates the gamma parameter that is of paramount importance in relativity
* theory. It is a dimensionless parameter that is equal to 1 for zero velocity
* but tends to infinity when velocity approaches the speed of light.
*
* @param v The velocity (m/s).
* @return The value of gamma parameter.
*/
public
static
double
gamma
(
double
v
) {
if
(
Math
.
abs
(
v
) >=
SPEED_OF_LIGHT
) {
throw
new
IllegalArgumentException
(
"Speed must be lower than the speed of light"
);
}
return
1.0
/
Math
.
sqrt
(
1
-
v
*
v
/ (
SPEED_OF_LIGHT
*
SPEED_OF_LIGHT
));
}
/**
* Calculates the length of an object in the moving frame.
*
* @param length The length of an object in its own frame (m).
* @param v The velocity of the object (m/s).
* @return The length of an object in the laboratory frame (m).
*/
public
static
double
lengthContraction
(
double
length
,
double
v
) {
if
(
length
<
0
) {
throw
new
IllegalArgumentException
(
"Length must be non-negative"
);
}
return
length
/
gamma
(
v
);
}
/**
* Calculates the time that has passed in the moving frame.
*
* @param length The time that has passed in the object's own frame (s).
* @param v The velocity of the object (m/s).
* @return The time that has passed in the laboratory frame (s).
*/
public
static
double
timeDilation
(
double
time
,
double
v
) {
if
(
time
<
0
) {
throw
new
IllegalArgumentException
(
"Time must be non-negative"
);
}
return
time
*
gamma
(
v
);
}
/**
* Calculates the velocity with respect to the moving frame.
*
* @param v1 The velocity of the object with respect to laboratory frame (m/s).
* @param v The velocity of the moving frame (m/s).
* @return The velocity with respect to the moving frame (m/s).
*/
public
static
double
velocityAddition
(
double
v1
,
double
v
) {
if
(
Math
.
abs
(
v1
) >
SPEED_OF_LIGHT
) {
throw
new
IllegalArgumentException
(
"Speed must not exceed the speed of light"
);
}
if
(
Math
.
abs
(
v
) >=
SPEED_OF_LIGHT
) {
throw
new
IllegalArgumentException
(
"Frame speed must be lower than the speed of light"
);
}
return
(
v1
-
v
) / (
1
-
v1
*
v
/ (
SPEED_OF_LIGHT
*
SPEED_OF_LIGHT
));
}
}
You can’t perform that action at this time.