Created
February 11, 2021 17:00
-
-
Save wilfreddesert/6871b89cc7cdfc00bf8c27b82411c56b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Vector: | |
""" | |
Class Vector is n-dimensional geometry vector. | |
Examples of usage: | |
>>> a = Vector([1, 2, 3, 4]) | |
>>> b = Vector([0, 1, -1, -4]) | |
>>> a | |
Vector([1, 2, 3, 4]) | |
>>> a + b | |
Vector([1, 3, 2, 0]) | |
>>> a - b | |
Vector([1, 1, 4, 8]) | |
>>> print(a * b) | |
Vector([0, 2, -3, -16]) | |
>>> print(b / a) | |
Vector([0.0, 0.5, -0.3333333333333333, -1.0]) | |
>>> a == Vector([1, 2, 3, 4]) | |
True | |
>>> a.append(144) | |
>>> print(a) | |
Vector([1, 2, 3, 4, 144]) | |
>>> len(a) | |
5 | |
>>> a.ndim() == 5 | |
True | |
>>> a[1] == 2 | |
True | |
>>> a[-1] = 5 | |
>>> a[-1] | |
5 | |
>>> a.clear() | |
>>> not a | |
True | |
>>> b.reverse() | |
>>> b | |
Vector([-4, -1, 1, 0]) | |
>>> abs(b) == sqrt(16 + 1 + 1 + 0) | |
True | |
>>> b.argmin() | |
0 | |
>>> b[b.argmin()] == -4 | |
True | |
>>> b.argmax() | |
2 | |
>>> b[b.argmax()] == 1 | |
True | |
>>> [i for i in b] == [-4, -1, 1, 0] | |
True | |
""" | |
def __init__(self, sequence=None): | |
if sequence is None: | |
sequence = [] | |
if not isinstance(sequence, Sequence) or not all( | |
isinstance(x, numbers.Number) for x in sequence | |
): | |
raise TypeError( | |
"Your input should be a sequence of int/float/complex numbers" | |
) | |
self.vec = [item for item in sequence] | |
def __repr__(self): | |
return f"Vector({self.vec})" | |
__str__ = __repr__ | |
def __len__(self): | |
return len(self.vec) | |
def __getitem__(self, index): | |
return self.vec[index] | |
def reverse(self): | |
self.vec.reverse() | |
def append(self, item): | |
if not isinstance(item, numbers.Number): | |
raise TypeError("Vector values must be numbers") | |
self.vec.append(item) | |
def __abs__(self): | |
return sqrt(sum(i ** 2 for i in self)) | |
def __setitem__(self, index, value): | |
if not isinstance(value, numbers.Number): | |
raise TypeError("Vector values must be numeric") | |
if not isinstance(index, int): | |
raise TypeError("Vector indexes must be integers") | |
self.vec[index] = value | |
def _calc(self, other, func): | |
if not isinstance(other, Vector): | |
raise TypeError("Both operands should be instances of the Vector class") | |
if len(self) != len(other): | |
raise ValueError("Vector lengths should be the same") | |
return Vector([func(a, b) for a, b in zip(self, other)]) | |
def __add__(self, other): | |
return self._calc(other, func=operator.add) | |
def __mul__(self, other): | |
return self._calc(other, func=operator.mul) | |
def __truediv__(self, other): | |
if 0 in other: | |
raise ValueError("Cannot Divide by Zero") | |
return self._calc(other, func=operator.truediv) | |
def __sub__(self, other): | |
return self._calc(other, func=operator.sub) | |
def ndim(self): | |
return len(self) | |
def __eq__(self, other): | |
if not isinstance(other, Vector): | |
return False | |
return self.vec == other.vec | |
def argmin(self): | |
return min(range(len(self)), key=self.__getitem__) | |
def argmax(self): | |
return max(range(len(self)), key=self.__getitem__) | |
def clear(self): | |
self.vec.clear() | |
def __iter__(self): | |
for item in self.vec: | |
yield item |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment