def __mul__(self, other): """Multiply this matrix with another matrix""" if isinstance(other, (int, float)): return Matrix( *[Vector(*i) for i in np.multiply(self._columns, other).T]) elif isinstance(other, (Vector)): return Vector(*np.dot(self._columns, other)) elif type(self) == type(other): return Matrix( *[Vector(*i) for i in self._columns.dot(other.to_array()).T])
def inverse(self): """Returns a new matrix that is the inverse of the given matrix""" # Take the inverse of the matrix and then turn it back into a Matrix try: return Matrix( *[Vector(*i) for i in np.linalg.inv(self._columns).T]) except np.linalg.linalg.LinAlgError as e: raise ValueError("Matrix is not invertible") from e
def __pow__(self, p): """Returns a matrix to the power p""" return Matrix( *[Vector(*i) for i in np.linalg.matrix_power(self._columns, p).T])
def transpose(self): """Returns the transpose of the matrix""" return Matrix(*[Vector(*i) for i in self._columns])
def __sub__(self, other): """Subtract this matrix with another matrix""" return Matrix( *[Vector(*i) for i in (self._columns - other.to_array()).T])
def __add__(self, other): """Add this matrix with another matrix""" return Matrix( *[Vector(*i) for i in (self._columns + other.to_array()).T])
def __iter__(self): """Iterate over the vectors of the matrix""" for i in self._columns.T: yield Vector(*i)
def __getitem__(self, val): """Access individual vector of matrix""" return Vector(*self._columns[:, val])
def __sub__(self, p): """Subtract two points to obtain a Vector""" if len(self) != len(p): raise ValueError('Dimension mismatch') return Vector(*[x - y for x, y in zip(self, p)])