Skip to content

Lorenz transformation - physics #6097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 50 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
a565258
Add files via upload
avivfaraj Sep 3, 2021
4951686
Changed print to f-string
avivfaraj Sep 4, 2021
ff2a162
Add files via upload
avivfaraj Sep 4, 2021
6550183
Fixes: #4710 provided return type
avivfaraj Sep 4, 2021
552546f
File exists in another pull request
avivfaraj Sep 4, 2021
bc31e3f
imported radians from math
avivfaraj Sep 5, 2021
974e0a6
Updated file according to pre-commit test
avivfaraj Sep 6, 2021
1d4fc81
Updated file
avivfaraj Sep 6, 2021
7bd84b4
Updated gamma
avivfaraj Sep 6, 2021
2841c11
Deleted duplicate file
avivfaraj Sep 6, 2021
cea2deb
removed pi
avivfaraj Sep 6, 2021
db7db3b
reversed tests
avivfaraj Sep 7, 2021
132e495
Fixed angle condition
avivfaraj Sep 7, 2021
c0e5071
Modified prints to f-string
avivfaraj Sep 7, 2021
894fa7f
Update horizontal_projectile_motion.py
avivfaraj Sep 7, 2021
ee49ce7
Merge branch 'TheAlgorithms:master' into master
avivfaraj Sep 7, 2021
07646ac
Update horizontal_projectile_motion.py
avivfaraj Sep 7, 2021
4e2fcaf
Fixes #4710 added exceptions and tests
avivfaraj Sep 7, 2021
dcacc95
Added float tests
avivfaraj Sep 7, 2021
3f2b238
Fixed type annotations
avivfaraj Sep 14, 2021
e3678fd
Fixed last annotation
avivfaraj Sep 14, 2021
c37bb95
Fixed annotations
avivfaraj Sep 14, 2021
5b0249a
fixed format
avivfaraj Sep 14, 2021
ec790c6
Revert "fixed format"
avivfaraj Sep 14, 2021
e865929
Revert "Fixed annotations"
avivfaraj Sep 14, 2021
82b4e0d
Revert "Fixed last annotation"
avivfaraj Sep 14, 2021
63c7c6a
Revert "Fixed type annotations"
avivfaraj Sep 14, 2021
0527866
Merge branch 'TheAlgorithms:master' into master
avivfaraj Apr 4, 2022
805050e
Revert to 4e2fcaf6fb
avivfaraj Apr 4, 2022
5f3486c
Fixing errors found during pre-commit
avivfaraj Apr 4, 2022
c382131
Merge branch 'TheAlgorithms:master' into master
avivfaraj Apr 4, 2022
99f00b5
Added gauss law
avivfaraj Apr 5, 2022
c75582f
Implemented Lorenz tranformation with four vector
avivfaraj Apr 13, 2022
a462736
pre-commit fixes
avivfaraj Apr 13, 2022
4eab40b
flake8 fixes
avivfaraj Apr 13, 2022
9d0e75c
More flake8 fixes
avivfaraj Apr 13, 2022
7cfaf9e
Added blank space for flake8
avivfaraj Apr 13, 2022
b88de32
Merge branch 'TheAlgorithms:master' into master
avivfaraj Apr 13, 2022
83cbeca
Added reference
avivfaraj Apr 13, 2022
a080632
Trailing whitespace fix
avivfaraj Apr 13, 2022
53a70f3
Merge branch 'master' of https://github.com/avivfaraj/Python
avivfaraj Apr 13, 2022
36f0cd7
Replaced argument u with velocity (descriptive name fix)
avivfaraj Apr 14, 2022
b31721d
Added tests for functions + moved velocity check to beta function
avivfaraj Apr 14, 2022
b1bbc83
Modified condition to 'not symbolic' in the transform function
avivfaraj Apr 14, 2022
ee3601f
trainling whitespace fix
avivfaraj Apr 14, 2022
4e337f6
Added type hint for 'smybolic' argument in transform function
avivfaraj Apr 15, 2022
798b91f
Changed reference to avoid pre-commit fails because of spelling issue…
avivfaraj Apr 15, 2022
982d66b
Added tests for gamma and transformation_matrix functions
avivfaraj Apr 15, 2022
ec4dbfb
Fixed transformation_matrix tests
avivfaraj Apr 17, 2022
9c7a85e
Fixed tests on beta and gamma functions
avivfaraj Apr 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added physics/__init__.py
Empty file.
205 changes: 205 additions & 0 deletions physics/lorenz_transformation_four_vector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
"""
Lorenz transformation describes the transition from a reference frame P
to another reference frame P', each of which is moving in a direction with
respect to the other. The Lorenz transformation implemented in this code
is the relativistic version using a four vector described by Minkowsky Space:
x0 = ct, x1 = x, x2 = y, and x3 = z

NOTE: Please note that x0 is c (speed of light) times t (time).

So, the Lorenz transformation using a four vector is defined as:

|ct'| | γ -γβ 0 0| |ct|
|x' | = |-γβ γ 0 0| *|x |
|y' | | 0 0 1 0| |y |
|z' | | 0 0 0 1| |z |

Where:
1
γ = ---------------
-----------
/ v^2 |
/(1 - ---
-/ c^2

v
β = -----
c

Reference: https://en.wikipedia.org/wiki/Lorentz_transformation
"""
from __future__ import annotations

from math import sqrt

import numpy as np # type: ignore
from 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)
def beta(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


def gamma(velocity: float) -> float:
"""
>>> 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 1!

>>> gamma(2*c)
Traceback (most recent call last):
...
ValueError: Speed must not exceed Light Speed 299,792,458 [m/s]!
"""
return 1 / (sqrt(1 - beta(velocity) ** 2))


def transformation_matrix(velocity: float) -> np.array:
"""
>>> 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 1!

>>> transformation_matrix(c * 1.5)
Traceback (most recent call last):
...
ValueError: Speed must not exceed Light Speed 299,792,458 [m/s]!
"""
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.array = np.zeros(4), symbolic: bool = True
) -> np.array:
"""
>>> transform(29979245,np.array([1,2,3,4]), False)
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 1!
"""
# Ensure event is not a vector of zeros
if not 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 in range(0, 4)]

print(f"\n{numerical_vector}")