-
Notifications
You must be signed in to change notification settings - Fork 0
/
ga.py
62 lines (49 loc) · 1.27 KB
/
ga.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
56
57
58
59
60
61
62
from numbers import Number
from mv import MultiVector
import math
class GA:
def __init__(self, n):
self._dim = n
@property
def n(self):
return self._dim
def scalar(self, s=0.):
x = MultiVector(self.n)
x[0] = s
return x
def blade(self, coef, *indices):
x = self.scalar(coef)
for i in indices:
y = MultiVector(self.n)
y[2 ** (i - 1)] = 1.
x *= y
return x
@property
def I(self):
x = MultiVector(self.n)
x[-1] = 1.
return x
def __getitem__(self, index):
if isinstance(index, tuple):
return self.blade(1., *index)
return self.blade(1., index)
@classmethod
def tensor_to_mv(cls, t, dim=None):
if dim:
shape = dim
else:
shape = math.ceil(math.sqrt(len(t)))
x = MultiVector(dim=shape, data=t)
return x
@classmethod
def mv_to_tensor(cls, mv):
return mv._data
if __name__ == '__main__':
import torch
ga = GA(3)
x = 3 + ga[1] + 2 * ga[2] - 2 * ga[3] + ga[1, 2] - ga[2, 3] + 2.5 * ga[1, 3] - ga[1, 2, 3]
print(x)
r = x * (~x) * ga.I
print(r)
q = ga.tensor_to_mv(torch.tensor([1, 1, 1, 1]))
print(q)