-
-
Notifications
You must be signed in to change notification settings - Fork 47k
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
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 4951686
Changed print to f-string
avivfaraj ff2a162
Add files via upload
avivfaraj 6550183
Fixes: #4710 provided return type
avivfaraj 552546f
File exists in another pull request
avivfaraj bc31e3f
imported radians from math
avivfaraj 974e0a6
Updated file according to pre-commit test
avivfaraj 1d4fc81
Updated file
avivfaraj 7bd84b4
Updated gamma
avivfaraj 2841c11
Deleted duplicate file
avivfaraj cea2deb
removed pi
avivfaraj db7db3b
reversed tests
avivfaraj 132e495
Fixed angle condition
avivfaraj c0e5071
Modified prints to f-string
avivfaraj 894fa7f
Update horizontal_projectile_motion.py
avivfaraj ee49ce7
Merge branch 'TheAlgorithms:master' into master
avivfaraj 07646ac
Update horizontal_projectile_motion.py
avivfaraj 4e2fcaf
Fixes #4710 added exceptions and tests
avivfaraj dcacc95
Added float tests
avivfaraj 3f2b238
Fixed type annotations
avivfaraj e3678fd
Fixed last annotation
avivfaraj c37bb95
Fixed annotations
avivfaraj 5b0249a
fixed format
avivfaraj ec790c6
Revert "fixed format"
avivfaraj e865929
Revert "Fixed annotations"
avivfaraj 82b4e0d
Revert "Fixed last annotation"
avivfaraj 63c7c6a
Revert "Fixed type annotations"
avivfaraj 0527866
Merge branch 'TheAlgorithms:master' into master
avivfaraj 805050e
Revert to 4e2fcaf6fb
avivfaraj 5f3486c
Fixing errors found during pre-commit
avivfaraj c382131
Merge branch 'TheAlgorithms:master' into master
avivfaraj 99f00b5
Added gauss law
avivfaraj c75582f
Implemented Lorenz tranformation with four vector
avivfaraj a462736
pre-commit fixes
avivfaraj 4eab40b
flake8 fixes
avivfaraj 9d0e75c
More flake8 fixes
avivfaraj 7cfaf9e
Added blank space for flake8
avivfaraj b88de32
Merge branch 'TheAlgorithms:master' into master
avivfaraj 83cbeca
Added reference
avivfaraj a080632
Trailing whitespace fix
avivfaraj 53a70f3
Merge branch 'master' of https://github.com/avivfaraj/Python
avivfaraj 36f0cd7
Replaced argument u with velocity (descriptive name fix)
avivfaraj b31721d
Added tests for functions + moved velocity check to beta function
avivfaraj b1bbc83
Modified condition to 'not symbolic' in the transform function
avivfaraj ee3601f
trainling whitespace fix
avivfaraj 4e337f6
Added type hint for 'smybolic' argument in transform function
avivfaraj 798b91f
Changed reference to avoid pre-commit fails because of spelling issue…
avivfaraj 982d66b
Added tests for gamma and transformation_matrix functions
avivfaraj ec4dbfb
Fixed transformation_matrix tests
avivfaraj 9c7a85e
Fixed tests on beta and gamma functions
avivfaraj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
avivfaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
>>> 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: | ||
avivfaraj marked this conversation as resolved.
Show resolved
Hide resolved
avivfaraj marked this conversation as resolved.
Show resolved
Hide resolved
avivfaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
>>> 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}") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.