repl.it
@anonymous/

CrushingPreemptiveBuckets

Python

No description

fork
loading
Files
  • main.py
main.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
def star(cd):
	v = [-x for x in cd.v]
	v[0] *= -1
	return CayleyDickson(v)

class CayleyDickson:
	def __init__(self, v):
		i, k, n = 0, 1, len(v)
		while k<n: i, k = i+1, 2*k
		v.extend([0]*(k-n))
		self.i, self.v = i, v

	def __add__(self, s2):
		i1, i2 = self.i, s2.i
		if i1<i2: return s2+self
		if i1>i2: return self+CayleyDickson(s2.v+[0]*(2**i1-2**i2))
		return CayleyDickson([a+b for a, b in zip(self.v, s2.v)])

	def __sub__(self, s2): return self+CayleyDickson([-x for x in s2.v])

	def __mul__(self, s2):
		i1, i2 = self.i, s2.i
		if i1<i2: return s2*self
		if i1>i2: return self*CayleyDickson(s2.v+[0]*(2**i1-2**i2))
		n = len(self.v)
		if n==1: return CayleyDickson([self.v[0]*s2.v[0]])
		n = n//2
		v1, v2 = self.v, s2.v
		a, b, c, d = v1[:n], v1[n:], v2[:n], v2[n:]
		p, q, r, s = [CayleyDickson(x) for x in [a, b, c, d]]
		u = p*r-star(s)*q
		v = s*p+q*star(r)
		return CayleyDickson(u.v+v.v)

	def __repr__(self): return str(self.v)

print(CayleyDickson([1, 2, 3, 4, 5, 6, 7, 8])*CayleyDickson([8, 7, 6, 5, 4, 3, 2, 1]))