Skip to content

Commit 56aa9fa

Browse files
committed
update multivector examples
1 parent b38a2f9 commit 56aa9fa

File tree

5 files changed

+344
-2
lines changed

5 files changed

+344
-2
lines changed

classes/multivector_v5.py

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
"""
2+
A multi-dimensional ``MultiVector`` class, take 2
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+
Tests of sequence behavior::
84+
85+
>>> v1 = MultiVector([3, 4, 5])
86+
>>> len(v1)
87+
3
88+
>>> v1[0], v1[len(v1)-1], v1[-1]
89+
(3.0, 5.0, 5.0)
90+
91+
92+
Test of slicing::
93+
94+
>>> v7 = MultiVector(range(7))
95+
>>> v7[-1]
96+
6.0
97+
>>> v7[1:4]
98+
MultiVector([1.0, 2.0, 3.0])
99+
>>> v7[-1:]
100+
MultiVector([6.0])
101+
>>> v7[1,2]
102+
Traceback (most recent call last):
103+
...
104+
TypeError: MultiVector indices must be integers
105+
106+
107+
Tests of dynamic attribute access::
108+
109+
>>> v7 = MultiVector(range(10))
110+
>>> v7.x
111+
0.0
112+
>>> v7.y, v7.z, v7.t, v7.u, v7.v, v7.w
113+
(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)
114+
115+
Dynamic attribute lookup failures::
116+
117+
>>> v7.k
118+
Traceback (most recent call last):
119+
...
120+
AttributeError: 'MultiVector' object has no attribute 'k'
121+
>>> v3 = MultiVector(range(3))
122+
>>> v3.t
123+
Traceback (most recent call last):
124+
...
125+
AttributeError: 'MultiVector' object has no attribute 't'
126+
>>> v3.spam
127+
Traceback (most recent call last):
128+
...
129+
AttributeError: 'MultiVector' object has no attribute 'spam'
130+
131+
132+
Tests of hashing::
133+
134+
>>> v1 = MultiVector([3, 4])
135+
>>> v2 = MultiVector([3.1, 4.2])
136+
>>> v3 = MultiVector([3, 4, 5])
137+
>>> v6 = MultiVector(range(6))
138+
>>> hash(v1), hash(v2), hash(v3), hash(v6)
139+
(7, 384307168202284039, 2, 1)
140+
>>> len(set([v1, v2, v3, v6]))
141+
4
142+
143+
144+
Tests of ``format()`` with Cartesian coordinates in 2D:
145+
146+
>>> v1 = MultiVector([3, 4])
147+
>>> format(v1)
148+
'(3.0, 4.0)'
149+
>>> format(v1, '.2f')
150+
'(3.00, 4.00)'
151+
>>> format(v1, '.3e')
152+
'(3.000e+00, 4.000e+00)'
153+
154+
155+
Tests of ``format()`` with Cartesian coordinates in 3D and 7D:
156+
157+
>>> v3 = MultiVector([3, 4, 5])
158+
>>> format(v3)
159+
'(3.0, 4.0, 5.0)'
160+
>>> format(MultiVector(range(7)))
161+
'(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0)'
162+
163+
164+
Tests of the ``angle`` method::
165+
166+
>>> MultiVector([0, 0]).angle()
167+
0.0
168+
>>> MultiVector([1, 0]).angle()
169+
0.0
170+
>>> epsilon = 10**-8
171+
>>> abs(MultiVector([0, 1]).angle() - math.pi/2) < epsilon
172+
True
173+
>>> abs(MultiVector([1, 1]).angle() - math.pi/4) < epsilon
174+
True
175+
176+
177+
Tests of ``format()`` with polar coordinates:
178+
179+
>>> format(MultiVector([1, 1]), 'p') # doctest:+ELLIPSIS
180+
'<1.414213..., 0.785398...>'
181+
>>> format(MultiVector([1, 1]), '.3ep')
182+
'<1.414e+00, 7.854e-01>'
183+
>>> format(MultiVector([1, 1]), '0.5fp')
184+
'<1.41421, 0.78540>'
185+
186+
187+
"""
188+
189+
from array import array
190+
import reprlib
191+
import math
192+
import functools
193+
import operator
194+
195+
196+
class MultiVector:
197+
typecode = 'd'
198+
199+
def __init__(self, components):
200+
self._components = array(self.typecode, components)
201+
202+
def __iter__(self):
203+
return iter(self._components)
204+
205+
def __repr__(self):
206+
components = reprlib.repr(self._components)
207+
components = components[components.find('['):-1]
208+
return 'MultiVector({})'.format(components)
209+
210+
def __str__(self):
211+
return str(tuple(self))
212+
213+
def __bytes__(self):
214+
return bytes(self._components)
215+
216+
def __eq__(self, other):
217+
return tuple(self) == tuple(other)
218+
219+
def __hash__(self):
220+
hashes = (hash(x) for x in self)
221+
return functools.reduce(operator.xor, hashes)
222+
223+
def __abs__(self):
224+
return math.sqrt(sum(x * x for x in self))
225+
226+
def __bool__(self):
227+
return bool(abs(self))
228+
229+
def __len__(self):
230+
return len(self._components)
231+
232+
def __getitem__(self, index):
233+
cls = type(self)
234+
if isinstance(index, slice):
235+
return cls(self._components[index])
236+
elif isinstance(index, int):
237+
return self._components[index]
238+
else:
239+
msg = '{.__name__} indices must be integers'
240+
raise TypeError(msg.format(cls))
241+
242+
shortcut_names = 'xyztuvw'
243+
244+
def __getattr__(self, name):
245+
cls = type(self)
246+
if len(name) == 1:
247+
pos = cls.shortcut_names.find(name)
248+
if 0 <= pos < len(self._components):
249+
return self._components[pos]
250+
msg = '{.__name__!r} object has no attribute {!r}'
251+
raise AttributeError(msg.format(cls, name))
252+
253+
def angle(self):
254+
return math.atan2(self.y, self.x) # <1>
255+
256+
def __format__(self, fmt_spec=''):
257+
if fmt_spec.endswith('p'):
258+
fmt_spec = fmt_spec[:-1]
259+
coords = (abs(self), self.angle())
260+
outer_fmt = '<{}>' # <2>
261+
else:
262+
coords = self
263+
outer_fmt = '({})' # <3>
264+
components = (format(c, fmt_spec) for c in coords)
265+
return outer_fmt.format(', '.join(components)) # <4>
266+
267+
@classmethod
268+
def frombytes(cls, octets):
269+
memv = memoryview(octets).cast(cls.typecode)
270+
return cls(memv)

classes/vector_v2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
>>> v1 == v1_clone
2929
True
3030
31-
Tests of ``format()`` with rectangular coordinates:
31+
Tests of ``format()`` with Cartesian coordinates:
3232
3333
>>> format(v1)
3434
'(3.0, 4.0)'
@@ -37,6 +37,7 @@
3737
>>> format(v1, '.3e')
3838
'(3.000e+00, 4.000e+00)'
3939
40+
4041
Tests of the ``angle`` method::
4142
4243
>>> Vector(0, 0).angle()
@@ -49,6 +50,7 @@
4950
>>> abs(Vector(1, 1).angle() - math.pi/4) < epsilon
5051
True
5152
53+
5254
Tests of ``format()`` with polar coordinates:
5355
5456
>>> format(Vector(1, 1), 'p') # doctest:+ELLIPSIS

classes/vector_v2_fmt_snippet.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
>>> bool(v1), bool(Vector(0, 0))
2121
(True, False)
2222
23+
2324
Test of ``.frombytes()`` class method:
2425
2526
>>> v1_clone = Vector.frombytes(bytes(v1))
@@ -28,7 +29,8 @@
2829
>>> v1 == v1_clone
2930
True
3031
31-
Tests of ``format()`` with rectangular coordinates:
32+
33+
Tests of ``format()`` with Cartesian coordinates:
3234
3335
>>> format(v1)
3436
'(3.0, 4.0)'
@@ -37,6 +39,7 @@
3739
>>> format(v1, '.3e')
3840
'(3.000e+00, 4.000e+00)'
3941
42+
4043
Tests of the ``angle`` method::
4144
4245
>>> Vector(0, 0).angle()
@@ -49,6 +52,7 @@
4952
>>> abs(Vector(1, 1).angle() - math.pi/4) < epsilon
5053
True
5154
55+
5256
Tests of ``format()`` with polar coordinates:
5357
5458
>>> format(Vector(1, 1), 'p') # doctest:+ELLIPSIS

classes/vector_v3.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,38 @@
2828
>>> v1 == v1_clone
2929
True
3030
31+
Tests of ``format()`` with Cartesian coordinates:
32+
33+
>>> format(v1)
34+
'(3.0, 4.0)'
35+
>>> format(v1, '.2f')
36+
'(3.00, 4.00)'
37+
>>> format(v1, '.3e')
38+
'(3.000e+00, 4.000e+00)'
39+
40+
41+
Tests of the ``angle`` method::
42+
43+
>>> Vector(0, 0).angle()
44+
0.0
45+
>>> Vector(1, 0).angle()
46+
0.0
47+
>>> epsilon = 10**-8
48+
>>> abs(Vector(0, 1).angle() - math.pi/2) < epsilon
49+
True
50+
>>> abs(Vector(1, 1).angle() - math.pi/4) < epsilon
51+
True
52+
53+
54+
Tests of ``format()`` with polar coordinates:
55+
56+
>>> format(Vector(1, 1), 'p') # doctest:+ELLIPSIS
57+
'<1.414213..., 0.785398...>'
58+
>>> format(Vector(1, 1), '.3ep')
59+
'<1.414e+00, 7.854e-01>'
60+
>>> format(Vector(1, 1), '0.5fp')
61+
'<1.41421, 0.78540>'
62+
3163
# BEGIN VECTOR_V3_DEMO
3264
Test of `x` and `y` read-only properties:
3365

0 commit comments

Comments
 (0)