-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmv.py
419 lines (355 loc) · 11.3 KB
/
mv.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import functools
from numbers import Number
import numpy as np
import torch
HANDLED_FUNCTIONS = {}
# def implements(torch_function):
# """Register a torch function override for ScalarTensor"""
#
# @functools.wraps(torch_function)
# def decorator(func):
# HANDLED_FUNCTIONS[torch_function] = func
# return func
#
# return decorator
#
#
# def ensure_tensor(data):
# if isinstance(data, MultiVector):
# return data.tensor()
# return torch.as_tensor(data)
def comb(n, k):
"""\
Returns /n\\
\\k/
comb(n, k) --> PyInt
"""
def fact(n):
if n == 0:
return 1
return np.multiply.reduce(range(1, n + 1))
return int(fact(n) / (fact(k) * fact(n - k)))
def elements(dims, firstIdx=0):
"""Return a list of tuples representing all 2**dims of blades
in a dims-dimensional GA.
elements(dims, firstIdx=0) --> bladeTupList
"""
indcs = list(range(firstIdx, firstIdx + dims))
blades = [()]
for k in range(1, dims + 1):
# k = grade
if k == 1:
for i in indcs:
blades.append((i,))
continue
curBladeX = indcs[:k]
for i in range(comb(dims, k)):
if curBladeX[-1] < firstIdx + dims - 1:
# increment last index
blades.append(tuple(curBladeX))
curBladeX[-1] = curBladeX[-1] + 1
else:
marker = -2
tmp = curBladeX[:] # copy
tmp.reverse()
# locate where the steady increase begins
for j in range(k - 1):
if tmp[j] - tmp[j + 1] == 1:
marker = marker - 1
else:
break
if marker < -k:
blades.append(tuple(curBladeX))
continue
# replace
blades.append(tuple(curBladeX))
curBladeX[marker:] = list(range(
curBladeX[marker] + 1, curBladeX[marker] + 1 - marker))
return blades
class MultiVector:
'''
for now, this just supports Cl(p, q=0)
'''
def __init__(self, dim=4, data=None, names=None, device='cpu'):
if data is None:
self.device = torch.device(device)
self._data = torch.zeros(2 ** dim, dtype=torch.float32, device=self.device)
else:
self.device = data.device
self._data = data
self._dim = dim
# sig = [+1] * dim
bladeTupleList = elements(dim, firstIdx=1)
self.bladeTupleList = list(map(tuple, bladeTupleList))
self.gradeList = list(map(len, self.bladeTupleList))
# create names
if names is None or isinstance(names, str):
if isinstance(names, str):
e = names
else:
e = 'e'
self.names = []
for i in range(self.gaDims):
if self.gradeList[i] >= 1:
self.names.append(e + ''.join(map(str, self.bladeTupleList[i])))
else:
self.names.append('')
elif len(names) == self.gaDims:
self.names = names
else:
raise ValueError(
"names list of length %i needs to be of length %i" %
(len(names), self.gaDims))
@property
def dim(self):
return self._dim
@property
def len(self):
return len(self._data)
@property
def gaDims(self):
return 2 ** self.dim
@property
def value(self):
return self._data
def __getitem__(self, i):
return self._data[i]
def __setitem__(self, i, value):
self._data[i] = value
def __delitem__(self, i):
self._data[i] = 0.
def __iter__(self):
for i in range(self.gaDims):
if self._data[i]:
yield i
def __pos__(self):
x = MultiVector(self.dim, device=self.device)
x._data = self._data.clone()
return x
def __neg__(self):
x = MultiVector(self.dim, device=self.device)
x._data = - self._data
return x
def __eq__(self, other):
return torch.eq(self._data, other._data)
def __len__(self):
"""Return the number of nonzero terms in this multivector."""
# return len(self._data) - self._data.count(0.0)
try:
return len(self._data.nonzero(as_tuple=True)[0])
except IndexError:
return 0
def __add__(self, other):
if isinstance(other, (Number, torch.Tensor)):
x = +self
x[0] += other
return x
elif isinstance(other, MultiVector):
# "mv have the same gaDims"
if self.gaDims == other.gaDims:
x = MultiVector(self.dim, device=self.device)
x._data = self._data + other._data
return x
else:
m = max(self.dim, other.dim)
x = MultiVector(m)
for i in range(self.gaDims):
x._data[i] = self._data[i]
for i in range(other.gaDims):
x._data[i] += other._data[i]
return x
else:
raise NotImplemented
def __radd__(self, other):
return self + other
def __sub__(self, other):
return self + (-other)
def __rsub__(self, other):
return - self + other
def __mul__(self, other):
if isinstance(other, (Number, torch.Tensor)):
x = MultiVector(self.dim, device=self.device)
x._data = self._data * other
return x
elif isinstance(other, MultiVector):
if self.dim == other.dim:
x = MultiVector(self.dim, device=self.device)
for i in range(self.gaDims):
for j in range(other.gaDims):
k, s = MultiVector._blade_combine(i, j)
x._data[k] += self._data[i] * other._data[j] * s
return x
else:
m = max(self.dim, other.dim)
x = MultiVector(m, device=self.device)
for i in range(self.len):
if self._data[i]:
for j in range(other.len):
if other._data[j]:
k, s = MultiVector._blade_combine(i, j)
x._data[k] += self._data[i] * other._data[j] * s
return x
else:
raise NotImplemented
def __rmul__(self, other):
if isinstance(other, (Number, torch.Tensor)):
return self * other
elif isinstance(other, MultiVector):
return other * self
else:
raise NotImplemented
def __and__(self, other):
'''
Find the outer (wedge) product of the two multivectors, or of a multivector
and a number.
'''
x = self * other - other * self
x._data = x._data / 2
return x
def __matmul__(self, other):
'''
Find the inner (dot) product of the two multivectors, or of a multivector
and a number.
'''
x = self * other + other * self
x._data = x._data / 2
return x
def __or__(self, other):
'''
Find the meet (vee) of the two multivectors, or of a multivector
and a number.
'''
return self.dual() * other
def __abs__(self):
'''
Find the norm of the multivector
'''
x = 0.0
for i in self:
a = self._data[i]
x += a * a
return torch.sqrt(x)
def __invert__(self):
x = MultiVector(self.dim, device=self.device)
for i in range(self.len):
v = self._data[i]
if v:
r = MultiVector._rank(i) % 4
if r == 2 or r == 3:
x[i] = -v
else:
x[i] = v
return x
def rank(self):
r = - torch.Tensor(float('Inf'))
for i in range(len(self._data)):
r = max(r, MultiVector._rank(i))
return r
def cross_product(self, other):
return (self & other).dual()
# def left_inv(self):
# try:
# x = ~self
# s = 1.0 / float(self * x)
# except TypeError:
# return NotImplemented
# x._data = x._data * s
# return x
#
# def right_inv(self):
# try:
# x = ~self
# s = 1.0 / float(x * self)
# except TypeError:
# return NotImplemented
# x._data = x._data * s
# return x
def __truediv__(self, other):
'''
Divide the multivectors, if possible. Or divide the multivector by the scalar.
'''
if isinstance(other, (Number, torch.Tensor)):
if isinstance(other, torch.Tensor):
assert len(other.shape) == 0, "the tensor must be scalar"
x = MultiVector(self.dim, device=self.device)
x._data = self._data / other
return x
if isinstance(other, MultiVector):
# TODO: still have some bugs! Not use this!
raise NotImplementedError("Not Implement the mv / mv")
def dual(self):
"""
Return the dual of the multivector.
"""
x = self.I
return self * x * x * x
@property
def I(self):
"""
Return the standard pseudoscalar of the algebra this multivector is in.
"""
x = MultiVector(self.dim, device=self.device)
x._data[-1] = 1.0
return x
@staticmethod
def _rank(a):
return bin(a).count('1')
@staticmethod
def _blade_combine(a, b):
if a == 0:
return b, 1
if b == 0:
return a, 1
c = a ^ b
s = 1
p = max(a, b)
d = MultiVector._rank(a)
e = 1
while e <= p:
if e & a:
d -= 1
if (d & 1) and (e & b):
s = -s
e *= 2
return c, s
def __str__(self):
result = ""
for i in self:
coef = self[i].item()
if result:
result += ' + '
result += str(coef)
bit = 1
k = 1
first = True
while bit <= i:
if i & bit:
if first:
result += '*['
first = False
else:
result += ','
result += 'e%d' % k
k += 1
bit *= 2
if not first:
result += ']'
if not result:
return '0'
return result
def __repr__(self):
return self.__str__()
def set_values(self, values, index):
self._data = torch.zeros(self.gaDims, dtype=torch.float32, device=self.device)
self._data.index_add_(0, index, values)
if __name__ == '__main__':
a = MultiVector(4)
a.set_values(torch.tensor([1., 1, 2, 3, 4]), torch.tensor([0, 1, 2, 3, 4]))
b = MultiVector(4)
b.set_values(torch.tensor([1., -1]), torch.tensor([1, 2]))
c = MultiVector(4)
c.set_values(torch.ones(16, dtype=torch.float32), torch.arange(16))
print(a + b)
print(a - b)
print(a * b)
print(a / 2)