update
This commit is contained in:
parent
981d5bc473
commit
5d212ad3cf
124
classes/multivector_v1.py
Normal file
124
classes/multivector_v1.py
Normal file
@ -0,0 +1,124 @@
|
||||
"""
|
||||
A multi-dimensional ``MultiVector`` class, take 1
|
||||
|
||||
A ``MultiVector`` is built from an iterable of numbers::
|
||||
|
||||
>>> MultiVector([3.1, 4.2])
|
||||
MultiVector([3.1, 4.2])
|
||||
>>> MultiVector((3, 4, 5))
|
||||
MultiVector([3.0, 4.0, 5.0])
|
||||
>>> MultiVector(range(10))
|
||||
MultiVector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
|
||||
|
||||
|
||||
Tests with 2-dimensions (same results as ``vector_v1.py``)::
|
||||
|
||||
>>> v1 = MultiVector([3, 4])
|
||||
>>> x, y = v1
|
||||
>>> x, y
|
||||
(3.0, 4.0)
|
||||
>>> v1
|
||||
MultiVector([3.0, 4.0])
|
||||
>>> v1_clone = eval(repr(v1))
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
>>> print(v1)
|
||||
(3.0, 4.0)
|
||||
>>> octets = bytes(v1)
|
||||
>>> octets
|
||||
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
|
||||
>>> abs(v1)
|
||||
5.0
|
||||
>>> bool(v1), bool(MultiVector([0, 0]))
|
||||
(True, False)
|
||||
|
||||
|
||||
Test of ``.frombytes()`` class method:
|
||||
|
||||
>>> v1_clone = MultiVector.frombytes(bytes(v1))
|
||||
>>> v1_clone
|
||||
MultiVector([3.0, 4.0])
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
|
||||
|
||||
Tests with 3-dimensions::
|
||||
|
||||
>>> v1 = MultiVector([3, 4, 5])
|
||||
>>> x, y, z = v1
|
||||
>>> x, y, z
|
||||
(3.0, 4.0, 5.0)
|
||||
>>> v1
|
||||
MultiVector([3.0, 4.0, 5.0])
|
||||
>>> v1_clone = eval(repr(v1))
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
>>> print(v1)
|
||||
(3.0, 4.0, 5.0)
|
||||
>>> abs(v1) # doctest:+ELLIPSIS
|
||||
7.071067811...
|
||||
>>> bool(v1), bool(MultiVector([0, 0, 0]))
|
||||
(True, False)
|
||||
|
||||
|
||||
Tests with many dimensions::
|
||||
|
||||
>>> v7 = MultiVector(range(7))
|
||||
>>> v7
|
||||
MultiVector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
|
||||
>>> abs(v7) # doctest:+ELLIPSIS
|
||||
9.53939201...
|
||||
|
||||
|
||||
Test of ``.__bytes__`` and ``.frombytes()`` methods::
|
||||
|
||||
>>> v1 = MultiVector([3, 4, 5])
|
||||
>>> v1_clone = MultiVector.frombytes(bytes(v1))
|
||||
>>> v1_clone
|
||||
MultiVector([3.0, 4.0, 5.0])
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
|
||||
|
||||
"""
|
||||
|
||||
# BEGIN MULTIVECTOR_V1
|
||||
from array import array
|
||||
import reprlib
|
||||
import math
|
||||
|
||||
|
||||
class MultiVector:
|
||||
typecode = 'd'
|
||||
|
||||
def __init__(self, components):
|
||||
self._components = array(MultiVector.typecode, components) # <1>
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._components) # <2>
|
||||
|
||||
def __repr__(self):
|
||||
components = reprlib.repr(self._components) # <3>
|
||||
components = components[components.find('['):-1] # <4>
|
||||
return 'MultiVector({})'.format(components)
|
||||
|
||||
def __str__(self):
|
||||
return str(tuple(self))
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self._components) # <5>
|
||||
|
||||
def __eq__(self, other):
|
||||
return tuple(self) == tuple(other)
|
||||
|
||||
def __abs__(self):
|
||||
return math.sqrt(sum(x * x for x in self)) # <6>
|
||||
|
||||
def __bool__(self):
|
||||
return bool(abs(self))
|
||||
|
||||
@classmethod
|
||||
def frombytes(cls, octets):
|
||||
memv = memoryview(octets).cast(cls.typecode)
|
||||
return cls(memv) # <7>
|
||||
# END MULTIVECTOR_V1
|
123
classes/multivector_v1_hash.py
Normal file
123
classes/multivector_v1_hash.py
Normal file
@ -0,0 +1,123 @@
|
||||
"""
|
||||
A multi-dimensional ``MultiVector`` class, take 1
|
||||
|
||||
Tests with 2-dimensions (same results as ``vector_v1.py``)::
|
||||
|
||||
>>> v1 = MultiVector([3, 4])
|
||||
>>> x, y = v1
|
||||
>>> x, y
|
||||
(3.0, 4.0)
|
||||
>>> v1
|
||||
MultiVector([3.0, 4.0])
|
||||
>>> v1_clone = eval(repr(v1))
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
>>> print(v1)
|
||||
(3.0, 4.0)
|
||||
>>> octets = bytes(v1)
|
||||
>>> octets
|
||||
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
|
||||
>>> abs(v1)
|
||||
5.0
|
||||
>>> bool(v1), bool(MultiVector([0, 0]))
|
||||
(True, False)
|
||||
|
||||
Test of ``.frombytes()`` class method:
|
||||
|
||||
>>> v1_clone = MultiVector.frombytes(bytes(v1))
|
||||
>>> v1_clone
|
||||
MultiVector([3.0, 4.0])
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
|
||||
Tests with 3-dimensions::
|
||||
|
||||
>>> v1 = MultiVector([3, 4, 5])
|
||||
>>> x, y, z = v1
|
||||
>>> x, y, z
|
||||
(3.0, 4.0, 5.0)
|
||||
>>> v1
|
||||
MultiVector([3.0, 4.0, 5.0])
|
||||
>>> v1_clone = eval(repr(v1))
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
>>> print(v1)
|
||||
(3.0, 4.0, 5.0)
|
||||
>>> abs(v1) # doctest:+ELLIPSIS
|
||||
7.071067811...
|
||||
>>> bool(v1), bool(MultiVector([0, 0, 0]))
|
||||
(True, False)
|
||||
|
||||
Tests with many dimensions::
|
||||
|
||||
>>> v7 = MultiVector(range(7))
|
||||
>>> v7
|
||||
MultiVector([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
|
||||
>>> abs(v7) # doctest:+ELLIPSIS
|
||||
9.53939201...
|
||||
|
||||
Test of ``.__bytes__`` and ``.frombytes()`` methods::
|
||||
|
||||
>>> v1 = MultiVector([3, 4, 5])
|
||||
>>> v1_clone = MultiVector.frombytes(bytes(v1))
|
||||
>>> v1_clone
|
||||
MultiVector([3.0, 4.0, 5.0])
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
|
||||
Tests of hashing::
|
||||
|
||||
>>> v1 = MultiVector([3, 4])
|
||||
>>> v2 = MultiVector([3.1, 4.2])
|
||||
>>> v3 = MultiVector([3, 4, 5])
|
||||
>>> v4 = MultiVector(range(10))
|
||||
>>> hash(v1), hash(v2), hash(v3), hash(v4)
|
||||
(7, 384307168202284039, 2, 1)
|
||||
>>> len(set([v1, v2, v3, v4]))
|
||||
4
|
||||
|
||||
|
||||
"""
|
||||
|
||||
from array import array
|
||||
import math
|
||||
import functools
|
||||
import operator
|
||||
|
||||
|
||||
class MultiVector:
|
||||
typecode = 'd'
|
||||
|
||||
def __init__(self, components):
|
||||
self._components = array(MultiVector.typecode, components)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._components)
|
||||
|
||||
def __repr__(self):
|
||||
components = ', '.join(repr(x) for x in self)
|
||||
return 'MultiVector([{}])'.format(components)
|
||||
|
||||
def __str__(self):
|
||||
return str(tuple(self))
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self._components)
|
||||
|
||||
def __eq__(self, other):
|
||||
return tuple(self) == tuple(other)
|
||||
|
||||
def __hash__(self):
|
||||
hashes = (hash(x) for x in self)
|
||||
return functools.reduce(operator.xor, hashes)
|
||||
|
||||
def __abs__(self):
|
||||
return math.sqrt(sum(x * x for x in self))
|
||||
|
||||
def __bool__(self):
|
||||
return bool(abs(self))
|
||||
|
||||
@classmethod
|
||||
def frombytes(cls, octets):
|
||||
memv = memoryview(octets).cast(cls.typecode)
|
||||
return cls(memv)
|
164
classes/multivector_v2.py
Normal file
164
classes/multivector_v2.py
Normal file
@ -0,0 +1,164 @@
|
||||
"""
|
||||
A multi-dimensional ``MultiVector`` class, take 2
|
||||
|
||||
A ``MultiVector`` is built from an iterable of numbers::
|
||||
|
||||
>>> MultiVector([3.1, 4.2])
|
||||
MultiVector([3.1, 4.2])
|
||||
>>> MultiVector((3, 4, 5))
|
||||
MultiVector([3.0, 4.0, 5.0])
|
||||
>>> MultiVector(range(10))
|
||||
MultiVector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
|
||||
|
||||
|
||||
Tests with 2-dimensions (same results as ``vector_v1.py``)::
|
||||
|
||||
>>> v1 = MultiVector([3, 4])
|
||||
>>> x, y = v1
|
||||
>>> x, y
|
||||
(3.0, 4.0)
|
||||
>>> v1
|
||||
MultiVector([3.0, 4.0])
|
||||
>>> v1_clone = eval(repr(v1))
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
>>> print(v1)
|
||||
(3.0, 4.0)
|
||||
>>> octets = bytes(v1)
|
||||
>>> octets
|
||||
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
|
||||
>>> abs(v1)
|
||||
5.0
|
||||
>>> bool(v1), bool(MultiVector([0, 0]))
|
||||
(True, False)
|
||||
|
||||
|
||||
Test of ``.frombytes()`` class method:
|
||||
|
||||
>>> v1_clone = MultiVector.frombytes(bytes(v1))
|
||||
>>> v1_clone
|
||||
MultiVector([3.0, 4.0])
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
|
||||
|
||||
Tests with 3-dimensions::
|
||||
|
||||
>>> v1 = MultiVector([3, 4, 5])
|
||||
>>> x, y, z = v1
|
||||
>>> x, y, z
|
||||
(3.0, 4.0, 5.0)
|
||||
>>> v1
|
||||
MultiVector([3.0, 4.0, 5.0])
|
||||
>>> v1_clone = eval(repr(v1))
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
>>> print(v1)
|
||||
(3.0, 4.0, 5.0)
|
||||
>>> abs(v1) # doctest:+ELLIPSIS
|
||||
7.071067811...
|
||||
>>> bool(v1), bool(MultiVector([0, 0, 0]))
|
||||
(True, False)
|
||||
|
||||
|
||||
Tests with many dimensions::
|
||||
|
||||
>>> v7 = MultiVector(range(7))
|
||||
>>> v7
|
||||
MultiVector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
|
||||
>>> abs(v7) # doctest:+ELLIPSIS
|
||||
9.53939201...
|
||||
|
||||
|
||||
Test of ``.__bytes__`` and ``.frombytes()`` methods::
|
||||
|
||||
>>> v1 = MultiVector([3, 4, 5])
|
||||
>>> v1_clone = MultiVector.frombytes(bytes(v1))
|
||||
>>> v1_clone
|
||||
MultiVector([3.0, 4.0, 5.0])
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
|
||||
|
||||
Tests of sequence behavior::
|
||||
|
||||
>>> v1 = MultiVector([3, 4, 5])
|
||||
>>> len(v1)
|
||||
3
|
||||
>>> v1[0], v1[len(v1)-1], v1[-1]
|
||||
(3.0, 5.0, 5.0)
|
||||
|
||||
|
||||
Test of slicing::
|
||||
|
||||
# BEGIN MULTIVECTOR_V2_DEMO
|
||||
|
||||
>>> v7 = MultiVector(range(7))
|
||||
>>> v7[-1] # <1>
|
||||
6.0
|
||||
>>> v7[1:4] # <2>
|
||||
MultiVector([1.0, 2.0, 3.0])
|
||||
>>> v7[-1:] # <3>
|
||||
MultiVector([6.0])
|
||||
>>> v7[1,2] # <4>
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: MultiVector indices must be integers
|
||||
|
||||
# END MULTIVECTOR_V2_DEMO
|
||||
|
||||
"""
|
||||
|
||||
from array import array
|
||||
import reprlib
|
||||
import math
|
||||
|
||||
|
||||
class MultiVector:
|
||||
typecode = 'd'
|
||||
|
||||
def __init__(self, components):
|
||||
self._components = array(MultiVector.typecode, components)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._components)
|
||||
|
||||
def __repr__(self):
|
||||
components = reprlib.repr(self._components)
|
||||
components = components[components.find('['):-1]
|
||||
return 'MultiVector({})'.format(components)
|
||||
|
||||
def __str__(self):
|
||||
return str(tuple(self))
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self._components)
|
||||
|
||||
def __eq__(self, other):
|
||||
return tuple(self) == tuple(other)
|
||||
|
||||
def __abs__(self):
|
||||
return math.sqrt(sum(x * x for x in self))
|
||||
|
||||
def __bool__(self):
|
||||
return bool(abs(self))
|
||||
|
||||
# BEGIN MULTIVECTOR_V2
|
||||
def __len__(self):
|
||||
return len(self._components)
|
||||
|
||||
def __getitem__(self, index):
|
||||
cls = type(self) # <1>
|
||||
if isinstance(index, slice):
|
||||
return cls(self._components[index]) # <2>
|
||||
elif isinstance(index, int):
|
||||
return self._components[index] # <3>
|
||||
else:
|
||||
msg = '{cls.__name__} indices must be integers'
|
||||
raise TypeError(msg.format(cls=cls)) # <4>
|
||||
# END MULTIVECTOR_V2
|
||||
|
||||
@classmethod
|
||||
def frombytes(cls, octets):
|
||||
memv = memoryview(octets).cast(cls.typecode)
|
||||
return cls(memv)
|
198
classes/multivector_v3.py
Normal file
198
classes/multivector_v3.py
Normal file
@ -0,0 +1,198 @@
|
||||
"""
|
||||
A multi-dimensional ``MultiVector`` class, take 2
|
||||
|
||||
A ``MultiVector`` is built from an iterable of numbers::
|
||||
|
||||
>>> MultiVector([3.1, 4.2])
|
||||
MultiVector([3.1, 4.2])
|
||||
>>> MultiVector((3, 4, 5))
|
||||
MultiVector([3.0, 4.0, 5.0])
|
||||
>>> MultiVector(range(10))
|
||||
MultiVector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
|
||||
|
||||
|
||||
Tests with 2-dimensions (same results as ``vector_v1.py``)::
|
||||
|
||||
>>> v1 = MultiVector([3, 4])
|
||||
>>> x, y = v1
|
||||
>>> x, y
|
||||
(3.0, 4.0)
|
||||
>>> v1
|
||||
MultiVector([3.0, 4.0])
|
||||
>>> v1_clone = eval(repr(v1))
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
>>> print(v1)
|
||||
(3.0, 4.0)
|
||||
>>> octets = bytes(v1)
|
||||
>>> octets
|
||||
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
|
||||
>>> abs(v1)
|
||||
5.0
|
||||
>>> bool(v1), bool(MultiVector([0, 0]))
|
||||
(True, False)
|
||||
|
||||
|
||||
Test of ``.frombytes()`` class method:
|
||||
|
||||
>>> v1_clone = MultiVector.frombytes(bytes(v1))
|
||||
>>> v1_clone
|
||||
MultiVector([3.0, 4.0])
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
|
||||
|
||||
Tests with 3-dimensions::
|
||||
|
||||
>>> v1 = MultiVector([3, 4, 5])
|
||||
>>> x, y, z = v1
|
||||
>>> x, y, z
|
||||
(3.0, 4.0, 5.0)
|
||||
>>> v1
|
||||
MultiVector([3.0, 4.0, 5.0])
|
||||
>>> v1_clone = eval(repr(v1))
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
>>> print(v1)
|
||||
(3.0, 4.0, 5.0)
|
||||
>>> abs(v1) # doctest:+ELLIPSIS
|
||||
7.071067811...
|
||||
>>> bool(v1), bool(MultiVector([0, 0, 0]))
|
||||
(True, False)
|
||||
|
||||
|
||||
Tests with many dimensions::
|
||||
|
||||
>>> v7 = MultiVector(range(7))
|
||||
>>> v7
|
||||
MultiVector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
|
||||
>>> abs(v7) # doctest:+ELLIPSIS
|
||||
9.53939201...
|
||||
|
||||
|
||||
Test of ``.__bytes__`` and ``.frombytes()`` methods::
|
||||
|
||||
>>> v1 = MultiVector([3, 4, 5])
|
||||
>>> v1_clone = MultiVector.frombytes(bytes(v1))
|
||||
>>> v1_clone
|
||||
MultiVector([3.0, 4.0, 5.0])
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
|
||||
|
||||
Tests of sequence behavior::
|
||||
|
||||
>>> v1 = MultiVector([3, 4, 5])
|
||||
>>> len(v1)
|
||||
3
|
||||
>>> v1[0], v1[len(v1)-1], v1[-1]
|
||||
(3.0, 5.0, 5.0)
|
||||
|
||||
|
||||
Test of slicing::
|
||||
|
||||
>>> v7 = MultiVector(range(7))
|
||||
>>> v7[-1]
|
||||
6.0
|
||||
>>> v7[1:4]
|
||||
MultiVector([1.0, 2.0, 3.0])
|
||||
>>> v7[-1:]
|
||||
MultiVector([6.0])
|
||||
>>> v7[1,2]
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: MultiVector indices must be integers
|
||||
|
||||
|
||||
Tests of dynamic attribute access::
|
||||
|
||||
>>> v7 = MultiVector(range(10))
|
||||
>>> v7.x
|
||||
0.0
|
||||
>>> v7.y, v7.z, v7.t, v7.u, v7.v, v7.w
|
||||
(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)
|
||||
|
||||
Dynamic attribute lookup failures::
|
||||
|
||||
>>> v7.k
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: 'MultiVector' object has no attribute 'k'
|
||||
>>> v3 = MultiVector(range(3))
|
||||
>>> v3.t
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: 'MultiVector' object has no attribute 't'
|
||||
>>> v3.spam
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: 'MultiVector' object has no attribute 'spam'
|
||||
|
||||
|
||||
"""
|
||||
|
||||
from array import array
|
||||
import reprlib
|
||||
import math
|
||||
|
||||
|
||||
class MultiVector:
|
||||
typecode = 'd'
|
||||
|
||||
def __init__(self, components):
|
||||
self._components = array(MultiVector.typecode, components)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._components)
|
||||
|
||||
def __repr__(self):
|
||||
components = reprlib.repr(self._components)
|
||||
components = components[components.find('['):-1]
|
||||
return 'MultiVector({})'.format(components)
|
||||
|
||||
def __str__(self):
|
||||
return str(tuple(self))
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self._components)
|
||||
|
||||
def __eq__(self, other):
|
||||
return tuple(self) == tuple(other)
|
||||
|
||||
def __abs__(self):
|
||||
return math.sqrt(sum(x * x for x in self))
|
||||
|
||||
def __bool__(self):
|
||||
return bool(abs(self))
|
||||
|
||||
def __len__(self):
|
||||
return len(self._components)
|
||||
|
||||
def __getitem__(self, index):
|
||||
cls = type(self)
|
||||
if isinstance(index, slice):
|
||||
return cls(self._components[index])
|
||||
elif isinstance(index, int):
|
||||
return self._components[index]
|
||||
else:
|
||||
msg = '{.__name__} indices must be integers'
|
||||
raise TypeError(msg.format(cls))
|
||||
|
||||
# BEGIN MULTIVECTOR_V3
|
||||
shortcut_names = 'xyztuvw'
|
||||
|
||||
def __getattr__(self, name):
|
||||
cls = type(self) # <1>
|
||||
if len(name) == 1: # <2>
|
||||
pos = cls.shortcut_names.find(name) # <3>
|
||||
if 0 <= pos < len(self._components): # <4>
|
||||
return self._components[pos]
|
||||
msg = '{.__name__!r} object has no attribute {!r}' # <5>
|
||||
raise AttributeError(msg.format(cls, name))
|
||||
|
||||
# END MULTIVECTOR_V3
|
||||
|
||||
@classmethod
|
||||
def frombytes(cls, octets):
|
||||
memv = memoryview(octets).cast(cls.typecode)
|
||||
return cls(memv)
|
213
classes/multivector_v4.py
Normal file
213
classes/multivector_v4.py
Normal file
@ -0,0 +1,213 @@
|
||||
"""
|
||||
A multi-dimensional ``MultiVector`` class, take 2
|
||||
|
||||
A ``MultiVector`` is built from an iterable of numbers::
|
||||
|
||||
>>> MultiVector([3.1, 4.2])
|
||||
MultiVector([3.1, 4.2])
|
||||
>>> MultiVector((3, 4, 5))
|
||||
MultiVector([3.0, 4.0, 5.0])
|
||||
>>> MultiVector(range(10))
|
||||
MultiVector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
|
||||
|
||||
|
||||
Tests with 2-dimensions (same results as ``vector_v1.py``)::
|
||||
|
||||
>>> v1 = MultiVector([3, 4])
|
||||
>>> x, y = v1
|
||||
>>> x, y
|
||||
(3.0, 4.0)
|
||||
>>> v1
|
||||
MultiVector([3.0, 4.0])
|
||||
>>> v1_clone = eval(repr(v1))
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
>>> print(v1)
|
||||
(3.0, 4.0)
|
||||
>>> octets = bytes(v1)
|
||||
>>> octets
|
||||
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
|
||||
>>> abs(v1)
|
||||
5.0
|
||||
>>> bool(v1), bool(MultiVector([0, 0]))
|
||||
(True, False)
|
||||
|
||||
|
||||
Test of ``.frombytes()`` class method:
|
||||
|
||||
>>> v1_clone = MultiVector.frombytes(bytes(v1))
|
||||
>>> v1_clone
|
||||
MultiVector([3.0, 4.0])
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
|
||||
|
||||
Tests with 3-dimensions::
|
||||
|
||||
>>> v1 = MultiVector([3, 4, 5])
|
||||
>>> x, y, z = v1
|
||||
>>> x, y, z
|
||||
(3.0, 4.0, 5.0)
|
||||
>>> v1
|
||||
MultiVector([3.0, 4.0, 5.0])
|
||||
>>> v1_clone = eval(repr(v1))
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
>>> print(v1)
|
||||
(3.0, 4.0, 5.0)
|
||||
>>> abs(v1) # doctest:+ELLIPSIS
|
||||
7.071067811...
|
||||
>>> bool(v1), bool(MultiVector([0, 0, 0]))
|
||||
(True, False)
|
||||
|
||||
|
||||
Tests with many dimensions::
|
||||
|
||||
>>> v7 = MultiVector(range(7))
|
||||
>>> v7
|
||||
MultiVector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
|
||||
>>> abs(v7) # doctest:+ELLIPSIS
|
||||
9.53939201...
|
||||
|
||||
|
||||
Test of ``.__bytes__`` and ``.frombytes()`` methods::
|
||||
|
||||
>>> v1 = MultiVector([3, 4, 5])
|
||||
>>> v1_clone = MultiVector.frombytes(bytes(v1))
|
||||
>>> v1_clone
|
||||
MultiVector([3.0, 4.0, 5.0])
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
|
||||
|
||||
Tests of sequence behavior::
|
||||
|
||||
>>> v1 = MultiVector([3, 4, 5])
|
||||
>>> len(v1)
|
||||
3
|
||||
>>> v1[0], v1[len(v1)-1], v1[-1]
|
||||
(3.0, 5.0, 5.0)
|
||||
|
||||
|
||||
Test of slicing::
|
||||
|
||||
>>> v7 = MultiVector(range(7))
|
||||
>>> v7[-1]
|
||||
6.0
|
||||
>>> v7[1:4]
|
||||
MultiVector([1.0, 2.0, 3.0])
|
||||
>>> v7[-1:]
|
||||
MultiVector([6.0])
|
||||
>>> v7[1,2]
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: MultiVector indices must be integers
|
||||
|
||||
|
||||
Tests of dynamic attribute access::
|
||||
|
||||
>>> v7 = MultiVector(range(10))
|
||||
>>> v7.x
|
||||
0.0
|
||||
>>> v7.y, v7.z, v7.t, v7.u, v7.v, v7.w
|
||||
(1.0, 2.0, 3.0, 4.0, 5.0, 6.0)
|
||||
|
||||
Dynamic attribute lookup failures::
|
||||
|
||||
>>> v7.k
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: 'MultiVector' object has no attribute 'k'
|
||||
>>> v3 = MultiVector(range(3))
|
||||
>>> v3.t
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: 'MultiVector' object has no attribute 't'
|
||||
>>> v3.spam
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: 'MultiVector' object has no attribute 'spam'
|
||||
|
||||
|
||||
Tests of hashing::
|
||||
|
||||
>>> v1 = MultiVector([3, 4])
|
||||
>>> v2 = MultiVector([3.1, 4.2])
|
||||
>>> v3 = MultiVector([3, 4, 5])
|
||||
>>> v6 = MultiVector(range(6))
|
||||
>>> hash(v1), hash(v2), hash(v3), hash(v6)
|
||||
(7, 384307168202284039, 2, 1)
|
||||
>>> len(set([v1, v2, v3, v6]))
|
||||
4
|
||||
|
||||
|
||||
"""
|
||||
|
||||
from array import array
|
||||
import reprlib
|
||||
import math
|
||||
import functools
|
||||
import operator
|
||||
|
||||
|
||||
class MultiVector:
|
||||
typecode = 'd'
|
||||
|
||||
def __init__(self, components):
|
||||
self._components = array(MultiVector.typecode, components)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._components)
|
||||
|
||||
def __repr__(self):
|
||||
components = reprlib.repr(self._components)
|
||||
components = components[components.find('['):-1]
|
||||
return 'MultiVector({})'.format(components)
|
||||
|
||||
def __str__(self):
|
||||
return str(tuple(self))
|
||||
|
||||
def __bytes__(self):
|
||||
return bytes(self._components)
|
||||
|
||||
def __eq__(self, other):
|
||||
return tuple(self) == tuple(other)
|
||||
|
||||
def __hash__(self):
|
||||
hashes = (hash(x) for x in self)
|
||||
return functools.reduce(operator.xor, hashes)
|
||||
|
||||
def __abs__(self):
|
||||
return math.sqrt(sum(x * x for x in self))
|
||||
|
||||
def __bool__(self):
|
||||
return bool(abs(self))
|
||||
|
||||
def __len__(self):
|
||||
return len(self._components)
|
||||
|
||||
def __getitem__(self, index):
|
||||
cls = type(self)
|
||||
if isinstance(index, slice):
|
||||
return cls(self._components[index])
|
||||
elif isinstance(index, int):
|
||||
return self._components[index]
|
||||
else:
|
||||
msg = '{.__name__} indices must be integers'
|
||||
raise TypeError(msg.format(cls))
|
||||
|
||||
shortcut_names = 'xyztuvw'
|
||||
|
||||
def __getattr__(self, name):
|
||||
cls = type(self) # <1>
|
||||
if len(name) == 1: # <2>
|
||||
pos = cls.shortcut_names.find(name) # <3>
|
||||
if 0 <= pos < len(self._components): # <4>
|
||||
return self._components[pos]
|
||||
msg = '{.__name__!r} object has no attribute {!r}' # <5>
|
||||
raise AttributeError(msg.format(cls, name))
|
||||
|
||||
@classmethod
|
||||
def frombytes(cls, octets):
|
||||
memv = memoryview(octets).cast(cls.typecode)
|
||||
return cls(memv)
|
@ -80,7 +80,6 @@ class Vector:
|
||||
# BEGIN VECTOR_V1
|
||||
@classmethod # <1>
|
||||
def frombytes(cls, octets): # <2>
|
||||
arr = array(Vector.typecode) # <3>
|
||||
arr.frombytes(octets) # <4>
|
||||
return cls(*arr) # <5>
|
||||
memv = memoryview(octets).cast(cls.typecode) # <3>
|
||||
return cls(*memv) # <4>
|
||||
# END VECTOR_V1
|
||||
|
@ -108,6 +108,5 @@ class Vector:
|
||||
|
||||
@classmethod
|
||||
def frombytes(cls, octets):
|
||||
arr = array(Vector.typecode)
|
||||
arr.frombytes(octets)
|
||||
return cls(*arr)
|
||||
memv = memoryview(octets).cast(cls.typecode)
|
||||
return cls(*memv)
|
||||
|
@ -110,6 +110,5 @@ class Vector:
|
||||
|
||||
@classmethod
|
||||
def frombytes(cls, octets):
|
||||
arr = array(Vector.typecode)
|
||||
arr.frombytes(octets)
|
||||
return cls(*arr)
|
||||
memv = memoryview(octets).cast(cls.typecode)
|
||||
return cls(*memv)
|
||||
|
@ -46,8 +46,9 @@ Test of `x` and `y` read-only properties:
|
||||
>>> v2 = Vector(3.1, 4.2)
|
||||
>>> hash(v1), hash(v2)
|
||||
(7, 384307168202284039)
|
||||
>>> set([v1, v2])
|
||||
{Vector(3.1, 4.2), Vector(3.0, 4.0)}
|
||||
>>> len(set([v1, v2]))
|
||||
2
|
||||
|
||||
|
||||
# END VECTOR_V3_DEMO
|
||||
|
||||
@ -118,6 +119,5 @@ class Vector:
|
||||
|
||||
@classmethod
|
||||
def frombytes(cls, octets):
|
||||
arr = array(Vector.typecode)
|
||||
arr.frombytes(octets)
|
||||
return cls(*arr)
|
||||
memv = memoryview(octets).cast(cls.typecode)
|
||||
return cls(*memv)
|
||||
|
@ -46,8 +46,8 @@ Test of `x` and `y` read-only properties:
|
||||
>>> v2 = Vector(3.1, 4.2)
|
||||
>>> hash(v1), hash(v2)
|
||||
(7, 384307168202284039)
|
||||
>>> set([v1, v2])
|
||||
{Vector(3.1, 4.2), Vector(3.0, 4.0)}
|
||||
>>> len(set([v1, v2]))
|
||||
2
|
||||
|
||||
# END VECTOR_V3_DEMO
|
||||
|
||||
@ -120,6 +120,5 @@ class Vector:
|
||||
|
||||
@classmethod
|
||||
def frombytes(cls, octets):
|
||||
arr = array(Vector.typecode)
|
||||
arr.frombytes(octets)
|
||||
return cls(*arr)
|
||||
memv = memoryview(octets).cast(cls.typecode)
|
||||
return cls(*memv)
|
||||
|
Loading…
x
Reference in New Issue
Block a user