Skip to content

Commit 5d212ad

Browse files
committed
update
1 parent 981d5bc commit 5d212ad

File tree

10 files changed

+837
-19
lines changed

10 files changed

+837
-19
lines changed

classes/multivector_v1.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
"""
2+
A multi-dimensional ``MultiVector`` class, take 1
3+
4+
A ``MultiVector`` is built from an iterable of numbers::
5+
6+
>>> MultiVector([3.1, 4.2])
7+
MultiVector([3.1, 4.2])
8+
>>> MultiVector((3, 4, 5))
9+
MultiVector([3.0, 4.0, 5.0])
10+
>>> MultiVector(range(10))
11+
MultiVector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
12+
13+
14+
Tests with 2-dimensions (same results as ``vector_v1.py``)::
15+
16+
>>> v1 = MultiVector([3, 4])
17+
>>> x, y = v1
18+
>>> x, y
19+
(3.0, 4.0)
20+
>>> v1
21+
MultiVector([3.0, 4.0])
22+
>>> v1_clone = eval(repr(v1))
23+
>>> v1 == v1_clone
24+
True
25+
>>> print(v1)
26+
(3.0, 4.0)
27+
>>> octets = bytes(v1)
28+
>>> octets
29+
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
30+
>>> abs(v1)
31+
5.0
32+
>>> bool(v1), bool(MultiVector([0, 0]))
33+
(True, False)
34+
35+
36+
Test of ``.frombytes()`` class method:
37+
38+
>>> v1_clone = MultiVector.frombytes(bytes(v1))
39+
>>> v1_clone
40+
MultiVector([3.0, 4.0])
41+
>>> v1 == v1_clone
42+
True
43+
44+
45+
Tests with 3-dimensions::
46+
47+
>>> v1 = MultiVector([3, 4, 5])
48+
>>> x, y, z = v1
49+
>>> x, y, z
50+
(3.0, 4.0, 5.0)
51+
>>> v1
52+
MultiVector([3.0, 4.0, 5.0])
53+
>>> v1_clone = eval(repr(v1))
54+
>>> v1 == v1_clone
55+
True
56+
>>> print(v1)
57+
(3.0, 4.0, 5.0)
58+
>>> abs(v1) # doctest:+ELLIPSIS
59+
7.071067811...
60+
>>> bool(v1), bool(MultiVector([0, 0, 0]))
61+
(True, False)
62+
63+
64+
Tests with many dimensions::
65+
66+
>>> v7 = MultiVector(range(7))
67+
>>> v7
68+
MultiVector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
69+
>>> abs(v7) # doctest:+ELLIPSIS
70+
9.53939201...
71+
72+
73+
Test of ``.__bytes__`` and ``.frombytes()`` methods::
74+
75+
>>> v1 = MultiVector([3, 4, 5])
76+
>>> v1_clone = MultiVector.frombytes(bytes(v1))
77+
>>> v1_clone
78+
MultiVector([3.0, 4.0, 5.0])
79+
>>> v1 == v1_clone
80+
True
81+
82+
83+
"""
84+
85+
# BEGIN MULTIVECTOR_V1
86+
from array import array
87+
import reprlib
88+
import math
89+
90+
91+
class MultiVector:
92+
typecode = 'd'
93+
94+
def __init__(self, components):
95+
self._components = array(MultiVector.typecode, components) # <1>
96+
97+
def __iter__(self):
98+
return iter(self._components) # <2>
99+
100+
def __repr__(self):
101+
components = reprlib.repr(self._components) # <3>
102+
components = components[components.find('['):-1] # <4>
103+
return 'MultiVector({})'.format(components)
104+
105+
def __str__(self):
106+
return str(tuple(self))
107+
108+
def __bytes__(self):
109+
return bytes(self._components) # <5>
110+
111+
def __eq__(self, other):
112+
return tuple(self) == tuple(other)
113+
114+
def __abs__(self):
115+
return math.sqrt(sum(x * x for x in self)) # <6>
116+
117+
def __bool__(self):
118+
return bool(abs(self))
119+
120+
@classmethod
121+
def frombytes(cls, octets):
122+
memv = memoryview(octets).cast(cls.typecode)
123+
return cls(memv) # <7>
124+
# END MULTIVECTOR_V1

classes/multivector_v1_hash.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"""
2+
A multi-dimensional ``MultiVector`` class, take 1
3+
4+
Tests with 2-dimensions (same results as ``vector_v1.py``)::
5+
6+
>>> v1 = MultiVector([3, 4])
7+
>>> x, y = v1
8+
>>> x, y
9+
(3.0, 4.0)
10+
>>> v1
11+
MultiVector([3.0, 4.0])
12+
>>> v1_clone = eval(repr(v1))
13+
>>> v1 == v1_clone
14+
True
15+
>>> print(v1)
16+
(3.0, 4.0)
17+
>>> octets = bytes(v1)
18+
>>> octets
19+
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
20+
>>> abs(v1)
21+
5.0
22+
>>> bool(v1), bool(MultiVector([0, 0]))
23+
(True, False)
24+
25+
Test of ``.frombytes()`` class method:
26+
27+
>>> v1_clone = MultiVector.frombytes(bytes(v1))
28+
>>> v1_clone
29+
MultiVector([3.0, 4.0])
30+
>>> v1 == v1_clone
31+
True
32+
33+
Tests with 3-dimensions::
34+
35+
>>> v1 = MultiVector([3, 4, 5])
36+
>>> x, y, z = v1
37+
>>> x, y, z
38+
(3.0, 4.0, 5.0)
39+
>>> v1
40+
MultiVector([3.0, 4.0, 5.0])
41+
>>> v1_clone = eval(repr(v1))
42+
>>> v1 == v1_clone
43+
True
44+
>>> print(v1)
45+
(3.0, 4.0, 5.0)
46+
>>> abs(v1) # doctest:+ELLIPSIS
47+
7.071067811...
48+
>>> bool(v1), bool(MultiVector([0, 0, 0]))
49+
(True, False)
50+
51+
Tests with many dimensions::
52+
53+
>>> v7 = MultiVector(range(7))
54+
>>> v7
55+
MultiVector([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
56+
>>> abs(v7) # doctest:+ELLIPSIS
57+
9.53939201...
58+
59+
Test of ``.__bytes__`` and ``.frombytes()`` methods::
60+
61+
>>> v1 = MultiVector([3, 4, 5])
62+
>>> v1_clone = MultiVector.frombytes(bytes(v1))
63+
>>> v1_clone
64+
MultiVector([3.0, 4.0, 5.0])
65+
>>> v1 == v1_clone
66+
True
67+
68+
Tests of hashing::
69+
70+
>>> v1 = MultiVector([3, 4])
71+
>>> v2 = MultiVector([3.1, 4.2])
72+
>>> v3 = MultiVector([3, 4, 5])
73+
>>> v4 = MultiVector(range(10))
74+
>>> hash(v1), hash(v2), hash(v3), hash(v4)
75+
(7, 384307168202284039, 2, 1)
76+
>>> len(set([v1, v2, v3, v4]))
77+
4
78+
79+
80+
"""
81+
82+
from array import array
83+
import math
84+
import functools
85+
import operator
86+
87+
88+
class MultiVector:
89+
typecode = 'd'
90+
91+
def __init__(self, components):
92+
self._components = array(MultiVector.typecode, components)
93+
94+
def __iter__(self):
95+
return iter(self._components)
96+
97+
def __repr__(self):
98+
components = ', '.join(repr(x) for x in self)
99+
return 'MultiVector([{}])'.format(components)
100+
101+
def __str__(self):
102+
return str(tuple(self))
103+
104+
def __bytes__(self):
105+
return bytes(self._components)
106+
107+
def __eq__(self, other):
108+
return tuple(self) == tuple(other)
109+
110+
def __hash__(self):
111+
hashes = (hash(x) for x in self)
112+
return functools.reduce(operator.xor, hashes)
113+
114+
def __abs__(self):
115+
return math.sqrt(sum(x * x for x in self))
116+
117+
def __bool__(self):
118+
return bool(abs(self))
119+
120+
@classmethod
121+
def frombytes(cls, octets):
122+
memv = memoryview(octets).cast(cls.typecode)
123+
return cls(memv)

0 commit comments

Comments
 (0)