Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import functools
- class Vector:
- def __init__(self, list):
- self.list = list
- def __add__(self, other):
- if len(self.list) != len(other.list):
- raise TypeError()
- return Vector([x + y for (x, y) in zip(self.list, other.list)])
- def __sub__(self, other):
- if len(self.list) != len(other.list):
- raise TypeError()
- return Vector([x - y for (x, y) in zip(self.list, other.list)])
- def __mul__(self, other):
- if isinstance(other, Vector):
- if len(self.list) != len(other.list):
- raise TypeError()
- return sum([x * y for (x, y) in zip(self.list, other.list)])
- return [x * other for x in self.list]
- def __rmul__(self, other):
- return self.__mul__(other)
- def __eq__(self, other):
- return functools.reduce(
- (lambda a, b: a and b),
- [x == y for (x, y) in zip(self.list, other.list)]
- )
- def __len__(self):
- return len(self.list)
- def __getitem__(self, i):
- return self.list[i]
- def __setitem__(self, key, value):
- self.list[key] = value
- def __str__(self):
- return '[' + ', '.join(map(str, self.list)) + ']'
- def __iadd__(self, other):
- if len(self.list) != len(other.list):
- raise TypeError()
- self.list = [x + y for (x, y) in zip(self.list, other.list)]
- return self
- def __imul__(self, other):
- if isinstance(other, Vector):
- if len(self.list) != len(other.list):
- raise TypeError()
- return sum([x * y for (x, y) in zip(self.list, other.list)])
- self.list = [x * other for x in self.list]
- return self
- def __isub__(self, other):
- if len(self.list) != len(other.list):
- raise TypeError()
- self.list = [x - y for (x, y) in zip(self.list, other.list)]
- return self
- def __abs__(self):
- return sum([x ** 2 for x in self.list]) ** 0.5
Advertisement
Add Comment
Please, Sign In to add comment