from __future__ import annotations
from math import sqrt
import numpy as np # type: ignorefrom sympy import symbols # type: ignore# Coefficient# Speed of light (m/s)
c =299792458# Symbols
ct, x, y, z = symbols("ct x y z")
ct_p, x_p, y_p, z_p = symbols("ct' x' y' z'")# Vehicle's speed divided by speed of light (no units)defbeta(velocity:float)->float:"""
>>> 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 1!
"""if velocity > c:raise ValueError("Speed must not exceed Light Speed 299,792,458 [m/s]!")# Usually the speed u should be much higher than 1 (c order of magnitude)elif velocity <1:raise ValueError("Speed must be greater than 1!")return velocity / c
defgamma(velocity:float)->float:return1/(sqrt(1- beta(velocity)**2))deftransformation_matrix(velocity:float)-> np.array: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],])deftransform(
velocity:float, event: np.array = np.zeros(4), symbolic:bool=True)-> np.array:# Ensure event is not a vector of zerosifnot symbolic:# x0 is ct (speed of ligt * time)
event[0]= event[0]* c
else:# Symbolic four vector
event = np.array([ct, x, y, z])return transformation_matrix(velocity).dot(event)if __name__ =="__main__":import doctest
doctest.testmod()# Example of symbolic vector:
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]}")# Substitute symbols with numerical values:
values = np.array([1,1,1,1])
sub_dict ={ct: c * values[0], x: values[1], y: values[2], z: values[3]}
numerical_vector =[four_vector[i].subs(sub_dict)for i inrange(0,4)]print(f"\n{numerical_vector}")