Skip to content

Commit 57c12fa

Browse files
tianyizheng02github-actions
and
github-actions
authored
Fix mypy errors in lorentz_transformation_four_vector.py (#8075)
* updating DIRECTORY.md * Fix mypy errors in lorentz_transformation_four_vector.py * Remove unused symbol vars * Add function documentation and rewrite algorithm explanation Previous explanation was misleading, as the code only calculates Lorentz transformations for movement in the x direction (0 velocity in the y and z directions) and not movement in any direction * updating DIRECTORY.md * Update error message for speed Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent c00af45 commit 57c12fa

File tree

2 files changed

+65
-80
lines changed

2 files changed

+65
-80
lines changed

Diff for: DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@
557557
* [Gamma Recursive](maths/gamma_recursive.py)
558558
* [Gaussian](maths/gaussian.py)
559559
* [Gaussian Error Linear Unit](maths/gaussian_error_linear_unit.py)
560+
* [Gcd Of N Numbers](maths/gcd_of_n_numbers.py)
560561
* [Greatest Common Divisor](maths/greatest_common_divisor.py)
561562
* [Greedy Coin Change](maths/greedy_coin_change.py)
562563
* [Hamming Numbers](maths/hamming_numbers.py)

Diff for: physics/lorentz_transformation_four_vector.py

+64-80
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,111 @@
11
"""
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.
2823
2924
Reference: https://en.wikipedia.org/wiki/Lorentz_transformation
3025
"""
31-
from __future__ import annotations
3226

3327
from math import sqrt
3428

35-
import numpy as np # type: ignore
36-
from sympy import symbols # type: ignore
29+
import numpy as np
30+
from sympy import symbols
3731

3832
# Coefficient
3933
# Speed of light (m/s)
4034
c = 299792458
4135

4236
# Symbols
4337
ct, x, y, z = symbols("ct x y z")
44-
ct_p, x_p, y_p, z_p = symbols("ct' x' y' z'")
4538

4639

4740
# Vehicle's speed divided by speed of light (no units)
4841
def beta(velocity: float) -> float:
4942
"""
43+
Calculates β = v/c, the given velocity as a fraction of c
5044
>>> beta(c)
5145
1.0
52-
5346
>>> beta(199792458)
5447
0.666435904801848
55-
5648
>>> beta(1e5)
5749
0.00033356409519815205
58-
5950
>>> beta(0.2)
6051
Traceback (most recent call last):
6152
...
62-
ValueError: Speed must be greater than 1!
53+
ValueError: Speed must be greater than or equal to 1!
6354
"""
6455
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]!")
6857
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+
7061
return velocity / c
7162

7263

7364
def gamma(velocity: float) -> float:
7465
"""
66+
Calculate the Lorentz factor γ = 1 / √(1 - v²/c²) for a given velocity
7567
>>> gamma(4)
7668
1.0000000000000002
77-
7869
>>> gamma(1e5)
7970
1.0000000556325075
80-
8171
>>> gamma(3e7)
8272
1.005044845777813
83-
8473
>>> gamma(2.8e8)
8574
2.7985595722318277
86-
8775
>>> gamma(299792451)
8876
4627.49902669495
89-
9077
>>> gamma(0.3)
9178
Traceback (most recent call last):
9279
...
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)
9682
Traceback (most recent call last):
9783
...
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]!
9985
"""
100-
return 1 / (sqrt(1 - beta(velocity) ** 2))
86+
return 1 / sqrt(1 - beta(velocity) ** 2)
10187

10288

103-
def transformation_matrix(velocity: float) -> np.array:
89+
def transformation_matrix(velocity: float) -> np.ndarray:
10490
"""
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
10599
>>> transformation_matrix(29979245)
106100
array([[ 1.00503781, -0.10050378, 0. , 0. ],
107101
[-0.10050378, 1.00503781, 0. , 0. ],
108102
[ 0. , 0. , 1. , 0. ],
109103
[ 0. , 0. , 0. , 1. ]])
110-
111104
>>> transformation_matrix(19979245.2)
112105
array([[ 1.00222811, -0.06679208, 0. , 0. ],
113106
[-0.06679208, 1.00222811, 0. , 0. ],
114107
[ 0. , 0. , 1. , 0. ],
115108
[ 0. , 0. , 0. , 1. ]])
116-
117109
>>> transformation_matrix(1)
118110
array([[ 1.00000000e+00, -3.33564095e-09, 0.00000000e+00,
119111
0.00000000e+00],
@@ -123,16 +115,14 @@ def transformation_matrix(velocity: float) -> np.array:
123115
0.00000000e+00],
124116
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
125117
1.00000000e+00]])
126-
127118
>>> transformation_matrix(0)
128119
Traceback (most recent call last):
129120
...
130-
ValueError: Speed must be greater than 1!
131-
121+
ValueError: Speed must be greater than or equal to 1!
132122
>>> transformation_matrix(c * 1.5)
133123
Traceback (most recent call last):
134124
...
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]!
136126
"""
137127
return np.array(
138128
[
@@ -144,44 +134,39 @@ def transformation_matrix(velocity: float) -> np.array:
144134
)
145135

146136

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:
150138
"""
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
153141
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])
154146
>>> transform(29979245)
155147
array([1.00503781498831*ct - 0.100503778816875*x,
156148
-0.100503778816875*ct + 1.00503781498831*x, 1.0*y, 1.0*z],
157149
dtype=object)
158-
159150
>>> transform(19879210.2)
160151
array([1.0022057787097*ct - 0.066456172618675*x,
161152
-0.066456172618675*ct + 1.0022057787097*x, 1.0*y, 1.0*z],
162153
dtype=object)
163-
164-
>>> transform(299792459, np.array([1,1,1,1]))
154+
>>> transform(299792459, np.array([1, 1, 1, 1]))
165155
Traceback (most recent call last):
166156
...
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]))
170159
Traceback (most recent call last):
171160
...
172-
ValueError: Speed must be greater than 1!
161+
ValueError: Speed must be greater than or equal to 1!
173162
"""
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
179166
else:
167+
event[0] *= c # x0 is ct (speed of light * time)
180168

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
185170

186171

187172
if __name__ == "__main__":
@@ -197,9 +182,8 @@ def transform(
197182
print(f"y' = {four_vector[2]}")
198183
print(f"z' = {four_vector[3]}")
199184

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)]
204188

205189
print(f"\n{numerical_vector}")

0 commit comments

Comments
 (0)