Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from numbers import Real
- from typing import Iterable
- import math
- class VectorND:
- def __init__(self, iterable_object):
- if not isinstance(iterable_object, Iterable):
- raise TypeError("Not iterable object")
- for elem in iterable_object:
- if not isinstance(elem, Real):
- raise TypeError("Element is not Real")
- if len(list(iterable_object)) == 0:
- raise ValueError("Empty object")
- self._body = list(iterable_object)
- self._N = len(self._body)
- def __str__(self):
- message = ""
- till = min(self._N, 10)
- for i in range(till - 1):
- message += str(self._body[i]) + ", "
- message += str(self._body[till - 1])
- if self._N > 10:
- message += "..."
- return f"VectorND({message})"
- def __len__(self):
- return self._N
- def __iter__(self):
- return iter(self._body)
- def __contains__(self, item):
- if not isinstance(item, int):
- raise TypeError("Wrong key")
- return 1 <= item <= self._N
- def __getitem__(self, key):
- if not isinstance(key, int):
- raise TypeError("Wrong key")
- if not (1 <= key <= self._N):
- raise IndexError(key)
- return self._body[key - 1]
- def __eq__(self, other):
- if not isinstance(other, VectorND):
- raise TypeError
- a = self._body
- b = other._body
- if len(a) > len(b):
- a, b = b, a
- difference = len(b) - len(a)
- for i in range(difference):
- a.append(0)
- for i in range(len(a)):
- if a[i] != b[i]:
- return False
- return True
- def __lt__(self, other):
- if not isinstance(other, VectorND):
- raise TypeError
- a = self._body
- b = other._body
- swapped = False
- if len(a) > len(b):
- swapped = True
- a, b = b, a
- difference = len(b) - len(a)
- for i in range(difference):
- a.append(0)
- if swapped:
- a, b = b, a
- i = -1
- while i + 1 < len(a):
- i += 1
- if a[i] < b[i]:
- return True
- elif a[i] > b[i]:
- return False
- return False
- def __le__(self, other):
- if not isinstance(other, VectorND):
- raise TypeError
- return self.__lt__(other) or self.__eq__(other)
- def __abs__(self):
- s = 0
- for i in self._body:
- s += i**2
- return math.sqrt(s)
- def __bool__(self):
- return self.__abs__() > 0
- def __mul__(self, k):
- if not isinstance(k, Real):
- raise TypeError
- res = VectorND(self._body)
- for i in range(self._N):
- res._body[i] *= k
- return res
- def __rmul__(self, k):
- return self.__mul__(k)
- def __truediv__(self, k):
- return self.__mul__(1 / k)
- def __add__(self, other):
- if isinstance(other, Real):
- res = VectorND(self._body)
- for i in range(self._N):
- res._body[i] += other
- return res
- elif isinstance(other, VectorND):
- a = self._body
- b = other._body
- if len(a) > len(b):
- a, b = b, a
- difference = len(b) - len(a)
- for i in range(difference):
- a.append(0)
- for i in range(len(a)):
- a[i] += b[i]
- return VectorND(a)
- else:
- raise TypeError("+ is not defined for this type of object")
- def __radd__(self, other):
- return self.__add__(other)
- def __neg__(self):
- return self.__mul__(-1)
- def __sub__(self, other):
- return self.__add__(-other)
- def __rsub__(self, other):
- return self.__neg__().__add__(other)
- def __float__(self):
- return self.__abs__()
- def __int__(self):
- return int(self.__abs__())
- def __matmul__(self, other):
- if not isinstance(other, VectorND):
- raise TypeError
- a = self._body
- b = other._body
- if len(a) > len(b):
- a, b = b, a
- difference = len(b) - len(a)
- for i in range(difference):
- a.append(0)
- s = 0
- for i in range(len(a)):
- s += a[i] * b[i]
- return s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement