Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from functools import reduce
- import operator
- class NVector:
- def __init__(self, vector):
- self.lst = []
- for values in vector:
- self.lst.append(values)
- def __len__(self):
- return len(self.lst)
- def __getitem__(self, index):
- return self.lst[index]
- def __setitem__(self, index, value):
- self.lst[index] = value
- def __str__(self):
- return str(self.lst)
- def __eq__(self, other):
- return self.lst == other
- def __ne__(self, other):
- return not self.__eq__(other)
- def __add__(self, vector):
- return list(map(sum, zip(self.lst, vector)))
- def __radd__(self, value):
- return [x + value for x in self.lst]
- @staticmethod
- def prod(iterable):
- return reduce(operator.mul, iterable, 1)
- def __mul__(self, value):
- multiplied_values = [a*b for a,b in zip(self.lst,value)]
- return sum(multiplied_values)
- def __rmul__(self, value):
- multiplied_values = [i * value for i in self.lst]
- return sum(multiplied_values)
- def zeros(self, n):
- return [0] * n
- t = NVector((3, 4, 5, 2))
- print(t[2])
- t[2] = 3
- print(t[2])
- print(str(t))
- r = NVector((3, 4, 5, 2))
- print(r == t)
- r = NVector((3, 4, 3, 2))
- print(r == t)
- print(t + r)
- print(t)
- print(4 + t)
- print(r, t)
- print(r * t)
- print(4 * t)
- print(t.zeros(4))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement