lorentz transformations describe the transition between two inertial reference frames f and f each of which is moving in some direction with respect to the other this code only calculates lorentz transformations for movement in the x direction with no spatial rotation i e a lorentz boost in the x direction the lorentz transformations are calculated here as linear transformations of fourvectors ct x y z described by minkowski space note that t time is multiplied by c the speed of light in the first entry of each fourvector thus if x ct x y z and x ct x y z are the fourvectors for two inertial reference frames and x moves in the x direction with velocity v with respect to x then the lorentz transformation from x to x is x bx where 0 0 b 0 0 0 0 1 0 0 0 0 1 is the matrix describing the lorentz boost between x and x 1 1 vc is the lorentz factor and vc is the velocity as a fraction of c reference https en wikipedia orgwikilorentztransformation coefficient speed of light ms symbols vehicle s speed divided by speed of light no units calculates vc the given velocity as a fraction of c betac 1 0 beta199792458 0 666435904801848 beta1e5 0 00033356409519815205 beta0 2 traceback most recent call last valueerror speed must be greater than or equal to 1 usually the speed should be much higher than 1 c order of magnitude calculate the lorentz factor 1 1 vc for a given velocity gamma4 1 0000000000000002 gamma1e5 1 0000000556325075 gamma3e7 1 005044845777813 gamma2 8e8 2 7985595722318277 gamma299792451 4627 49902669495 gamma0 3 traceback most recent call last valueerror speed must be greater than or equal to 1 gamma2 c traceback most recent call last valueerror speed must not exceed light speed 299 792 458 ms calculate the lorentz transformation matrix for movement in the x direction 0 0 0 0 0 0 1 0 0 0 0 1 where is the lorentz factor and is the velocity as a fraction of c transformationmatrix29979245 array 1 00503781 0 10050378 0 0 0 10050378 1 00503781 0 0 0 0 1 0 0 0 0 1 transformationmatrix19979245 2 array 1 00222811 0 06679208 0 0 0 06679208 1 00222811 0 0 0 0 1 0 0 0 0 1 transformationmatrix1 array 1 00000000e00 3 33564095e09 0 00000000e00 0 00000000e00 3 33564095e09 1 00000000e00 0 00000000e00 0 00000000e00 0 00000000e00 0 00000000e00 1 00000000e00 0 00000000e00 0 00000000e00 0 00000000e00 0 00000000e00 1 00000000e00 transformationmatrix0 traceback most recent call last valueerror speed must be greater than or equal to 1 transformationmatrixc 1 5 traceback most recent call last valueerror speed must not exceed light speed 299 792 458 ms calculate a lorentz transformation for movement in the x direction given a velocity and a fourvector for an inertial reference frame if no fourvector is given then calculate the transformation symbolically with variables transform29979245 np array1 2 3 4 array 3 01302757e08 3 01302729e07 3 00000000e00 4 00000000e00 transform29979245 array1 00503781498831ct 0 100503778816875x 0 100503778816875ct 1 00503781498831x 1 0y 1 0z dtypeobject transform19879210 2 array1 0022057787097ct 0 066456172618675x 0 066456172618675ct 1 0022057787097x 1 0y 1 0z dtypeobject transform299792459 np array1 1 1 1 traceback most recent call last valueerror speed must not exceed light speed 299 792 458 ms transform1 np array1 1 1 1 traceback most recent call last valueerror speed must be greater than or equal to 1 ensure event is not empty example of symbolic vector substitute symbols with numerical values coefficient speed of light m s symbols vehicle s speed divided by speed of light no units calculates β v c the given velocity as a fraction of c beta c 1 0 beta 199792458 0 666435904801848 beta 1e5 0 00033356409519815205 beta 0 2 traceback most recent call last valueerror speed must be greater than or equal to 1 usually the speed should be much higher than 1 c order of magnitude calculate the lorentz factor γ 1 1 v² c² for a given velocity gamma 4 1 0000000000000002 gamma 1e5 1 0000000556325075 gamma 3e7 1 005044845777813 gamma 2 8e8 2 7985595722318277 gamma 299792451 4627 49902669495 gamma 0 3 traceback most recent call last valueerror speed must be greater than or equal to 1 gamma 2 c traceback most recent call last valueerror speed must not exceed light speed 299 792 458 m s calculate the lorentz transformation matrix for movement in the x direction γ γβ 0 0 γβ γ 0 0 0 0 1 0 0 0 0 1 where γ is the lorentz factor and β is the velocity as a fraction of c transformation_matrix 29979245 array 1 00503781 0 10050378 0 0 0 10050378 1 00503781 0 0 0 0 1 0 0 0 0 1 transformation_matrix 19979245 2 array 1 00222811 0 06679208 0 0 0 06679208 1 00222811 0 0 0 0 1 0 0 0 0 1 transformation_matrix 1 array 1 00000000e 00 3 33564095e 09 0 00000000e 00 0 00000000e 00 3 33564095e 09 1 00000000e 00 0 00000000e 00 0 00000000e 00 0 00000000e 00 0 00000000e 00 1 00000000e 00 0 00000000e 00 0 00000000e 00 0 00000000e 00 0 00000000e 00 1 00000000e 00 transformation_matrix 0 traceback most recent call last valueerror speed must be greater than or equal to 1 transformation_matrix c 1 5 traceback most recent call last valueerror speed must not exceed light speed 299 792 458 m s calculate a lorentz transformation for movement in the x direction given a velocity and a four vector for an inertial reference frame if no four vector is given then calculate the transformation symbolically with variables transform 29979245 np array 1 2 3 4 array 3 01302757e 08 3 01302729e 07 3 00000000e 00 4 00000000e 00 transform 29979245 array 1 00503781498831 ct 0 100503778816875 x 0 100503778816875 ct 1 00503781498831 x 1 0 y 1 0 z dtype object transform 19879210 2 array 1 0022057787097 ct 0 066456172618675 x 0 066456172618675 ct 1 0022057787097 x 1 0 y 1 0 z dtype object transform 299792459 np array 1 1 1 1 traceback most recent call last valueerror speed must not exceed light speed 299 792 458 m s transform 1 np array 1 1 1 1 traceback most recent call last valueerror speed must be greater than or equal to 1 ensure event is not empty symbolic four vector x0 is ct speed of light time example of symbolic vector substitute symbols with numerical values
from math import sqrt

import numpy as np
from sympy import symbols



c = 299792458


ct, x, y, z = symbols("ct x y z")



def beta(velocity: float) -> float:
    
    if velocity > c:
        raise ValueError("Speed must not exceed light speed 299,792,458 [m/s]!")
    elif velocity < 1:
        
        raise ValueError("Speed must be greater than or equal to 1!")

    return velocity / c


def gamma(velocity: float) -> float:
    
    return 1 / sqrt(1 - beta(velocity) ** 2)


def transformation_matrix(velocity: float) -> np.ndarray:
    
    return np.array(
        [
            [gamma(velocity), -gamma(velocity) * beta(velocity), 0, 0],
            [-gamma(velocity) * beta(velocity), gamma(velocity), 0, 0],
            [0, 0, 1, 0],
            [0, 0, 0, 1],
        ]
    )


def transform(velocity: float, event: np.ndarray | None = None) -> np.ndarray:
    
    
    if event is None:
        event = np.array([ct, x, y, z])  
    else:
        event[0] *= c  

    return transformation_matrix(velocity) @ event


if __name__ == "__main__":
    import doctest

    doctest.testmod()

    
    four_vector = transform(29979245)
    print("Example of four vector: ")
    print(f"ct' = {four_vector[0]}")
    print(f"x' = {four_vector[1]}")
    print(f"y' = {four_vector[2]}")
    print(f"z' = {four_vector[3]}")

    
    sub_dict = {ct: c, x: 1, y: 1, z: 1}
    numerical_vector = [four_vector[i].subs(sub_dict) for i in range(4)]

    print(f"\n{numerical_vector}")