from matrix import Matrix class Vector(Matrix): __data__ = [] def __init__(self, data): if isinstance(data, list): self.__data__ = data elif isinstance(data, int): self.__init__([0] * data) else: raise ValueError("data must be a list or an integer for dimension") def get_data(self): return self.__data__ def get_dimension(self): return len(self.__data__) def __iter__(self): return iter(self.__data__) def __eq__(self, other): return self.__data__ == other.__data__ def __str__(self): return f"{self.__data__}" def __neg__(self): return Vector([-x for x in self.__data__]) def __add__(self, other): if isinstance(other, Vector): if self.get_dimension() != other.get_dimension(): raise ValueError("The vectors to be added must have the same dimension") return Vector([(x + y) for (x, y) in zip(self, other)]) elif isinstance(other, int) or isinstance(other, float): return Vector([(x + other) for x in self.__data__]) else: raise ValueError("A vector can only be multiplied with an vector (dot product) or a scalar") def __radd__(self, other): return self + other def __sub__(self, other): return self + (-other) def __rsub__(self, other): return -self + other def __mul__(self, other): if isinstance(other, Vector): return sum([(x * y) for (x, y) in zip(self, other)]) elif isinstance(other, int) or isinstance(other, float): return Vector([(other * x) for x in self.__data__]) else: raise ValueError("A vector can only be multiplied with an vector (dot product) or a scalar") def __rmul__(self, other): return self * other