MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / transformation_matrix

Function transformation_matrix

physics/lorentz_transformation_four_vector.py:89–134  ·  view source on GitHub ↗

Calculate the Lorentz transformation matrix for movement in the x direction: | y -γβ 0 0| |-γβ y 0 0| | 0 0 1 0| | 0 0 0 1| where y is the Lorentz factor and β is the velocity as a fraction of c >>> transformation_matrix(29979245) array([[ 1.005037

(velocity: float)

Source from the content-addressed store, hash-verified

87
88
89def transformation_matrix(velocity: float) -> np.ndarray:
90 """
91 Calculate the Lorentz transformation matrix for movement in the x direction:
92
93 | y -γβ 0 0|
94 |-γβ y 0 0|
95 | 0 0 1 0|
96 | 0 0 0 1|
97
98 where y is the Lorentz factor and β is the velocity as a fraction of c
99 >>> transformation_matrix(29979245)
100 array([[ 1.00503781, -0.10050378, 0. , 0. ],
101 [-0.10050378, 1.00503781, 0. , 0. ],
102 [ 0. , 0. , 1. , 0. ],
103 [ 0. , 0. , 0. , 1. ]])
104 >>> transformation_matrix(19979245.2)
105 array([[ 1.00222811, -0.06679208, 0. , 0. ],
106 [-0.06679208, 1.00222811, 0. , 0. ],
107 [ 0. , 0. , 1. , 0. ],
108 [ 0. , 0. , 0. , 1. ]])
109 >>> transformation_matrix(1)
110 array([[ 1.00000000e+00, -3.33564095e-09, 0.00000000e+00,
111 0.00000000e+00],
112 [-3.33564095e-09, 1.00000000e+00, 0.00000000e+00,
113 0.00000000e+00],
114 [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00,
115 0.00000000e+00],
116 [ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
117 1.00000000e+00]])
118 >>> transformation_matrix(0)
119 Traceback (most recent call last):
120 ...
121 ValueError: Speed must be greater than or equal to 1!
122 >>> transformation_matrix(c * 1.5)
123 Traceback (most recent call last):
124 ...
125 ValueError: Speed must not exceed light speed 299,792,458 [m/s]!
126 """
127 return np.array(
128 [
129 [gamma(velocity), -gamma(velocity) * beta(velocity), 0, 0],
130 [-gamma(velocity) * beta(velocity), gamma(velocity), 0, 0],
131 [0, 0, 1, 0],
132 [0, 0, 0, 1],
133 ]
134 )
135
136
137def transform(velocity: float, event: np.ndarray | None = None) -> np.ndarray:

Callers 1

transformFunction · 0.85

Calls 2

gammaFunction · 0.85
betaFunction · 0.85

Tested by

no test coverage detected