Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. from functools import reduce
  2. import operator
  3.  
  4.  
  5. class NVector:
  6. def __init__(self, vector):
  7. self.lst = []
  8. for values in vector:
  9. self.lst.append(values)
  10.  
  11. def __len__(self):
  12. return len(self.lst)
  13. def __getitem__(self, index):
  14. return self.lst[index]
  15. def __setitem__(self, index, value):
  16. self.lst[index] = value
  17. def __str__(self):
  18. return str(self.lst)
  19. def __eq__(self, other):
  20. return self.lst == other
  21.  
  22. def __ne__(self, other):
  23. return not self.__eq__(other)
  24. def __add__(self, vector):
  25. return list(map(sum, zip(self.lst, vector)))
  26. def __radd__(self, value):
  27. return [x + value for x in self.lst]
  28.  
  29. @staticmethod
  30. def prod(iterable):
  31. return reduce(operator.mul, iterable, 1)
  32.  
  33. def __mul__(self, value):
  34. multiplied_values = [a*b for a,b in zip(self.lst,value)]
  35. return sum(multiplied_values)
  36.  
  37. def __rmul__(self, value):
  38. multiplied_values = [i * value for i in self.lst]
  39. return sum(multiplied_values)
  40. def zeros(self, n):
  41. return [0] * n
  42.  
  43.  
  44. t = NVector((3, 4, 5, 2))
  45.  
  46. print(t[2])
  47. t[2] = 3
  48. print(t[2])
  49. print(str(t))
  50.  
  51. r = NVector((3, 4, 5, 2))
  52. print(r == t)
  53. r = NVector((3, 4, 3, 2))
  54. print(r == t)
  55. print(t + r)
  56. print(t)
  57. print(4 + t)
  58. print(r, t)
  59. print(r * t)
  60. print(4 * t)
  61.  
  62. print(t.zeros(4))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement