1
1
"""
2
- Lorentz transformation describes the transition from a reference frame P
3
- to another reference frame P', each of which is moving in a direction with
4
- respect to the other. The Lorentz transformation implemented in this code
5
- is the relativistic version using a four vector described by Minkowsky Space:
6
- x0 = ct, x1 = x, x2 = y, and x3 = z
7
-
8
- NOTE: Please note that x0 is c (speed of light) times t (time).
9
-
10
- So, the Lorentz transformation using a four vector is defined as:
11
-
12
- |ct'| | γ -γβ 0 0| |ct|
13
- |x' | = |-γβ γ 0 0| *|x |
14
- |y' | | 0 0 1 0| |y |
15
- |z' | | 0 0 0 1| |z |
16
-
17
- Where:
18
- 1
19
- γ = ---------------
20
- -----------
21
- / v^2 |
22
- /(1 - ---
23
- -/ c^2
24
-
25
- v
26
- β = -----
27
- c
2
+ Lorentz transformations describe the transition between two inertial reference
3
+ frames F and F', each of which is moving in some direction with respect to the
4
+ other. This code only calculates Lorentz transformations for movement in the x
5
+ direction with no spacial rotation (i.e., a Lorentz boost in the x direction).
6
+ The Lorentz transformations are calculated here as linear transformations of
7
+ four-vectors [ct, x, y, z] described by Minkowski space. Note that t (time) is
8
+ multiplied by c (the speed of light) in the first entry of each four-vector.
9
+
10
+ Thus, if X = [ct; x; y; z] and X' = [ct'; x'; y'; z'] are the four-vectors for
11
+ two inertial reference frames and X' moves in the x direction with velocity v
12
+ with respect to X, then the Lorentz transformation from X to X' is X' = BX,
13
+ where
14
+
15
+ | γ -γβ 0 0|
16
+ B = |-γβ γ 0 0|
17
+ | 0 0 1 0|
18
+ | 0 0 0 1|
19
+
20
+ is the matrix describing the Lorentz boost between X and X',
21
+ γ = 1 / √(1 - v²/c²) is the Lorentz factor, and β = v/c is the velocity as
22
+ a fraction of c.
28
23
29
24
Reference: https://en.wikipedia.org/wiki/Lorentz_transformation
30
25
"""
31
- from __future__ import annotations
32
26
33
27
from math import sqrt
34
28
35
- import numpy as np # type: ignore
36
- from sympy import symbols # type: ignore
29
+ import numpy as np
30
+ from sympy import symbols
37
31
38
32
# Coefficient
39
33
# Speed of light (m/s)
40
34
c = 299792458
41
35
42
36
# Symbols
43
37
ct , x , y , z = symbols ("ct x y z" )
44
- ct_p , x_p , y_p , z_p = symbols ("ct' x' y' z'" )
45
38
46
39
47
40
# Vehicle's speed divided by speed of light (no units)
48
41
def beta (velocity : float ) -> float :
49
42
"""
43
+ Calculates β = v/c, the given velocity as a fraction of c
50
44
>>> beta(c)
51
45
1.0
52
-
53
46
>>> beta(199792458)
54
47
0.666435904801848
55
-
56
48
>>> beta(1e5)
57
49
0.00033356409519815205
58
-
59
50
>>> beta(0.2)
60
51
Traceback (most recent call last):
61
52
...
62
- ValueError: Speed must be greater than 1!
53
+ ValueError: Speed must be greater than or equal to 1!
63
54
"""
64
55
if velocity > c :
65
- raise ValueError ("Speed must not exceed Light Speed 299,792,458 [m/s]!" )
66
-
67
- # Usually the speed u should be much higher than 1 (c order of magnitude)
56
+ raise ValueError ("Speed must not exceed light speed 299,792,458 [m/s]!" )
68
57
elif velocity < 1 :
69
- raise ValueError ("Speed must be greater than 1!" )
58
+ # Usually the speed should be much higher than 1 (c order of magnitude)
59
+ raise ValueError ("Speed must be greater than or equal to 1!" )
60
+
70
61
return velocity / c
71
62
72
63
73
64
def gamma (velocity : float ) -> float :
74
65
"""
66
+ Calculate the Lorentz factor γ = 1 / √(1 - v²/c²) for a given velocity
75
67
>>> gamma(4)
76
68
1.0000000000000002
77
-
78
69
>>> gamma(1e5)
79
70
1.0000000556325075
80
-
81
71
>>> gamma(3e7)
82
72
1.005044845777813
83
-
84
73
>>> gamma(2.8e8)
85
74
2.7985595722318277
86
-
87
75
>>> gamma(299792451)
88
76
4627.49902669495
89
-
90
77
>>> gamma(0.3)
91
78
Traceback (most recent call last):
92
79
...
93
- ValueError: Speed must be greater than 1!
94
-
95
- >>> gamma(2*c)
80
+ ValueError: Speed must be greater than or equal to 1!
81
+ >>> gamma(2 * c)
96
82
Traceback (most recent call last):
97
83
...
98
- ValueError: Speed must not exceed Light Speed 299,792,458 [m/s]!
84
+ ValueError: Speed must not exceed light speed 299,792,458 [m/s]!
99
85
"""
100
- return 1 / ( sqrt (1 - beta (velocity ) ** 2 ) )
86
+ return 1 / sqrt (1 - beta (velocity ) ** 2 )
101
87
102
88
103
- def transformation_matrix (velocity : float ) -> np .array :
89
+ def transformation_matrix (velocity : float ) -> np .ndarray :
104
90
"""
91
+ Calculate the Lorentz transformation matrix for movement in the x direction:
92
+
93
+ | γ -γβ 0 0|
94
+ |-γβ γ 0 0|
95
+ | 0 0 1 0|
96
+ | 0 0 0 1|
97
+
98
+ where γ is the Lorentz factor and β is the velocity as a fraction of c
105
99
>>> transformation_matrix(29979245)
106
100
array([[ 1.00503781, -0.10050378, 0. , 0. ],
107
101
[-0.10050378, 1.00503781, 0. , 0. ],
108
102
[ 0. , 0. , 1. , 0. ],
109
103
[ 0. , 0. , 0. , 1. ]])
110
-
111
104
>>> transformation_matrix(19979245.2)
112
105
array([[ 1.00222811, -0.06679208, 0. , 0. ],
113
106
[-0.06679208, 1.00222811, 0. , 0. ],
114
107
[ 0. , 0. , 1. , 0. ],
115
108
[ 0. , 0. , 0. , 1. ]])
116
-
117
109
>>> transformation_matrix(1)
118
110
array([[ 1.00000000e+00, -3.33564095e-09, 0.00000000e+00,
119
111
0.00000000e+00],
@@ -123,16 +115,14 @@ def transformation_matrix(velocity: float) -> np.array:
123
115
0.00000000e+00],
124
116
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
125
117
1.00000000e+00]])
126
-
127
118
>>> transformation_matrix(0)
128
119
Traceback (most recent call last):
129
120
...
130
- ValueError: Speed must be greater than 1!
131
-
121
+ ValueError: Speed must be greater than or equal to 1!
132
122
>>> transformation_matrix(c * 1.5)
133
123
Traceback (most recent call last):
134
124
...
135
- ValueError: Speed must not exceed Light Speed 299,792,458 [m/s]!
125
+ ValueError: Speed must not exceed light speed 299,792,458 [m/s]!
136
126
"""
137
127
return np .array (
138
128
[
@@ -144,44 +134,39 @@ def transformation_matrix(velocity: float) -> np.array:
144
134
)
145
135
146
136
147
- def transform (
148
- velocity : float , event : np .array = np .zeros (4 ), symbolic : bool = True # noqa: B008
149
- ) -> np .array :
137
+ def transform (velocity : float , event : np .ndarray | None = None ) -> np .ndarray :
150
138
"""
151
- >>> transform(29979245,np.array([1,2,3,4]), False)
152
- array([ 3.01302757e+08, -3.01302729e+07, 3.00000000e+00, 4.00000000e+00])
139
+ Calculate a Lorentz transformation for movement in the x direction given a
140
+ velocity and a four-vector for an inertial reference frame
153
141
142
+ If no four-vector is given, then calculate the transformation symbolically
143
+ with variables
144
+ >>> transform(29979245, np.array([1, 2, 3, 4]))
145
+ array([ 3.01302757e+08, -3.01302729e+07, 3.00000000e+00, 4.00000000e+00])
154
146
>>> transform(29979245)
155
147
array([1.00503781498831*ct - 0.100503778816875*x,
156
148
-0.100503778816875*ct + 1.00503781498831*x, 1.0*y, 1.0*z],
157
149
dtype=object)
158
-
159
150
>>> transform(19879210.2)
160
151
array([1.0022057787097*ct - 0.066456172618675*x,
161
152
-0.066456172618675*ct + 1.0022057787097*x, 1.0*y, 1.0*z],
162
153
dtype=object)
163
-
164
- >>> transform(299792459, np.array([1,1,1,1]))
154
+ >>> transform(299792459, np.array([1, 1, 1, 1]))
165
155
Traceback (most recent call last):
166
156
...
167
- ValueError: Speed must not exceed Light Speed 299,792,458 [m/s]!
168
-
169
- >>> transform(-1, np.array([1,1,1,1]))
157
+ ValueError: Speed must not exceed light speed 299,792,458 [m/s]!
158
+ >>> transform(-1, np.array([1, 1, 1, 1]))
170
159
Traceback (most recent call last):
171
160
...
172
- ValueError: Speed must be greater than 1!
161
+ ValueError: Speed must be greater than or equal to 1!
173
162
"""
174
- # Ensure event is not a vector of zeros
175
- if not symbolic :
176
-
177
- # x0 is ct (speed of ligt * time)
178
- event [0 ] = event [0 ] * c
163
+ # Ensure event is not empty
164
+ if event is None :
165
+ event = np .array ([ct , x , y , z ]) # Symbolic four vector
179
166
else :
167
+ event [0 ] *= c # x0 is ct (speed of light * time)
180
168
181
- # Symbolic four vector
182
- event = np .array ([ct , x , y , z ])
183
-
184
- return transformation_matrix (velocity ).dot (event )
169
+ return transformation_matrix (velocity ) @ event
185
170
186
171
187
172
if __name__ == "__main__" :
@@ -197,9 +182,8 @@ def transform(
197
182
print (f"y' = { four_vector [2 ]} " )
198
183
print (f"z' = { four_vector [3 ]} " )
199
184
200
- # Substitute symbols with numerical values:
201
- values = np .array ([1 , 1 , 1 , 1 ])
202
- sub_dict = {ct : c * values [0 ], x : values [1 ], y : values [2 ], z : values [3 ]}
203
- numerical_vector = [four_vector [i ].subs (sub_dict ) for i in range (0 , 4 )]
185
+ # Substitute symbols with numerical values
186
+ sub_dict = {ct : c , x : 1 , y : 1 , z : 1 }
187
+ numerical_vector = [four_vector [i ].subs (sub_dict ) for i in range (4 )]
204
188
205
189
print (f"\n { numerical_vector } " )
0 commit comments