-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensor.py
55 lines (43 loc) · 1.06 KB
/
tensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import random
import math
def normalize_ms(n)->float:
"""
Meters per second version of normalizing.
In: m/s
Out: c unit vector
Normalize number input in to speed of light unit vector.
Args:
n: number
Returns:
m: float
"""
C = 299792458
if n > C:
m = 1.0
elif n < 0:
m = 0.0
else:
m= n / C
return m
def normalize(v: tuple) -> tuple:
vector = list(v)
vector = [float(f)/max(vector) for f in vector]
return tuple(vector)
def check_four(v: tuple) -> tuple:
"""
The 4-vector (x,y,z,t) must sum to 1.0 per relativity.
Args:
v (tuple[float]): 4-vector of (x,y,z,t) which must sum to 1
Returns:
tuple: 4-vector renormalized to sum to 1
"""
x, y, z, t = v[0], v[1], v[2], v[3]
if (sum(v) > 1):
if max(v) == t:
x, y, z, t = 0.0, 0.0, 0.0, 1.0
outvec = (x,y,z,t)
else:
outvec = normalize(v)
else:
outvec = normalize(v)
return outvec