Esempio n. 1
0
 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])
Esempio n. 2
0
    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
Esempio n. 3
0
 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])
Esempio n. 4
0
 def transpose(self):
     """Returns the transpose of the matrix"""
     return Matrix(*[Vector(*i) for i in self._columns])
Esempio n. 5
0
 def __sub__(self, other):
     """Subtract this matrix with another matrix"""
     return Matrix(
         *[Vector(*i) for i in (self._columns - other.to_array()).T])
Esempio n. 6
0
 def __add__(self, other):
     """Add this matrix with another matrix"""
     return Matrix(
         *[Vector(*i) for i in (self._columns + other.to_array()).T])
Esempio n. 7
0
 def __iter__(self):
     """Iterate over the vectors of the matrix"""
     for i in self._columns.T:
         yield Vector(*i)
Esempio n. 8
0
 def __getitem__(self, val):
     """Access individual vector of matrix"""
     return Vector(*self._columns[:, val])
Esempio n. 9
0
 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)])