Advertisement
Korotkodul

VectorND

Nov 29th, 2024 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. from numbers import Real
  2. from typing import Iterable
  3. import math
  4.  
  5. class VectorND:
  6.     def __init__(self, iterable_object):
  7.         if not isinstance(iterable_object, Iterable):
  8.             raise TypeError("Not iterable object")
  9.         for elem in iterable_object:
  10.             if not isinstance(elem, Real):
  11.                 raise TypeError("Element is not Real")
  12.         if len(list(iterable_object)) == 0:
  13.             raise ValueError("Empty object")
  14.         self._body = list(iterable_object)
  15.         self._N = len(self._body)
  16.  
  17.     def __str__(self):
  18.         message = ""
  19.         till = min(self._N, 10)
  20.         for i in range(till - 1):
  21.             message += str(self._body[i]) + ", "
  22.         message += str(self._body[till - 1])
  23.         if self._N > 10:
  24.             message += "..."
  25.         return f"VectorND({message})"
  26.  
  27.     def __len__(self):
  28.         return self._N
  29.  
  30.     def __iter__(self):
  31.         return iter(self._body)
  32.  
  33.     def __contains__(self, item):
  34.         if not isinstance(item, int):
  35.             raise TypeError("Wrong key")
  36.         return 1 <= item <= self._N
  37.  
  38.     def __getitem__(self, key):
  39.         if not isinstance(key, int):
  40.             raise TypeError("Wrong key")
  41.         if not (1 <= key <= self._N):
  42.             raise IndexError(key)
  43.         return self._body[key - 1]
  44.  
  45.     def __eq__(self, other):
  46.         if not isinstance(other, VectorND):
  47.             raise TypeError
  48.         a = self._body
  49.         b = other._body
  50.         if len(a) > len(b):
  51.             a, b = b, a
  52.         difference = len(b) - len(a)
  53.         for i in range(difference):
  54.             a.append(0)
  55.         for i in range(len(a)):
  56.             if a[i] != b[i]:
  57.                 return False
  58.         return True
  59.  
  60.     def __lt__(self, other):
  61.         if not isinstance(other, VectorND):
  62.             raise TypeError
  63.         a = self._body
  64.         b = other._body
  65.         swapped = False
  66.         if len(a) > len(b):
  67.             swapped = True
  68.             a, b = b, a
  69.         difference = len(b) - len(a)
  70.         for i in range(difference):
  71.             a.append(0)
  72.         if swapped:
  73.             a, b = b, a
  74.         i = -1
  75.         while i + 1 < len(a):
  76.             i += 1
  77.             if a[i] < b[i]:
  78.                 return True
  79.             elif a[i] > b[i]:
  80.                 return False
  81.         return False
  82.  
  83.     def __le__(self, other):
  84.         if not isinstance(other, VectorND):
  85.             raise TypeError
  86.         return self.__lt__(other) or self.__eq__(other)
  87.  
  88.     def __abs__(self):
  89.         s = 0
  90.         for i in self._body:
  91.             s += i**2
  92.         return math.sqrt(s)
  93.  
  94.     def __bool__(self):
  95.         return self.__abs__() > 0
  96.  
  97.     def __mul__(self, k):
  98.         if not isinstance(k, Real):
  99.             raise TypeError
  100.         res = VectorND(self._body)
  101.         for i in range(self._N):
  102.             res._body[i] *= k
  103.         return res
  104.  
  105.     def __rmul__(self, k):
  106.         return self.__mul__(k)
  107.  
  108.     def __truediv__(self, k):
  109.         return self.__mul__(1 / k)
  110.  
  111.     def __add__(self, other):
  112.         if isinstance(other, Real):
  113.             res = VectorND(self._body)
  114.             for i in range(self._N):
  115.                 res._body[i] += other
  116.             return res
  117.         elif isinstance(other, VectorND):
  118.             a = self._body
  119.             b = other._body
  120.             if len(a) > len(b):
  121.                 a, b = b, a
  122.             difference = len(b) - len(a)
  123.             for i in range(difference):
  124.                 a.append(0)
  125.             for i in range(len(a)):
  126.                 a[i] += b[i]
  127.             return VectorND(a)
  128.         else:
  129.             raise TypeError("+ is not defined for this type of object")
  130.  
  131.     def __radd__(self, other):
  132.         return self.__add__(other)
  133.  
  134.     def __neg__(self):
  135.         return self.__mul__(-1)
  136.  
  137.     def __sub__(self, other):
  138.         return self.__add__(-other)
  139.  
  140.     def __rsub__(self, other):
  141.         return self.__neg__().__add__(other)
  142.  
  143.     def __float__(self):
  144.         return self.__abs__()
  145.  
  146.     def __int__(self):
  147.         return int(self.__abs__())
  148.  
  149.     def __matmul__(self, other):
  150.         if not isinstance(other, VectorND):
  151.             raise TypeError
  152.         a = self._body
  153.         b = other._body
  154.         if len(a) > len(b):
  155.             a, b = b, a
  156.         difference = len(b) - len(a)
  157.         for i in range(difference):
  158.             a.append(0)
  159.         s = 0
  160.         for i in range(len(a)):
  161.             s += a[i] * b[i]
  162.         return s
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement