def __pow__(self, other): if other != 1 and not self.is_square: raise NonSquareMatrixError("Power of non-square matrix %s" % self) if other == 0: return Identity(self.rows) if other < 1: raise NonInvertibleMatrixError("Matrix det == 0; not invertible") return self
def __new__(cls, mat, exp=S.NegativeOne): # exp is there to make it consistent with # inverse.func(*inverse.args) == inverse mat = _sympify(mat) if not mat.is_Matrix: raise TypeError("mat should be a matrix") if not mat.is_square: raise NonSquareMatrixError("Inverse of non-square matrix %s" % mat) return Basic.__new__(cls, mat, exp)
def __new__(cls, mat): mat = sympify(mat) if not mat.is_Matrix: raise TypeError("Input to Determinant, %s, not a matrix" % str(mat)) if not mat.is_square: raise NonSquareMatrixError("Det of a non-square matrix") return Basic.__new__(cls, mat)
def __new__(cls, mat): mat = sympify(mat) if not mat.is_Matrix: raise TypeError("input to Trace, %s, is not a matrix" % str(mat)) if not mat.is_square: raise NonSquareMatrixError("Trace of a non-square matrix") return Basic.__new__(cls, mat)
def __pow__(self, other): if not self.is_square: raise NonSquareMatrixError("Power of non-square matrix %s" % self) elif self.is_Identity: return self elif other == S.Zero: return Identity(self.rows) elif other == S.One: return self return MatPow(self, other).doit(deep=False)
def __new__(cls, base, exp, evaluate=False, **options): base = _sympify(base) if not base.is_Matrix: raise TypeError("MatPow base should be a matrix") if not base.is_square: raise NonSquareMatrixError("Power of non-square matrix %s" % base) exp = _sympify(exp) obj = super().__new__(cls, base, exp) if evaluate: obj = obj.doit(deep=False) return obj
def _entry(self, i, j, **kwargs): from sympy.matrices.expressions import MatMul A = self.doit() if isinstance(A, MatPow): # We still have a MatPow, make an explicit MatMul out of it. if not A.base.is_square: raise NonSquareMatrixError("Power of non-square matrix %s" % A.base) elif A.exp.is_Integer and A.exp.is_positive: A = MatMul(*[A.base for k in range(A.exp)]) #elif A.exp.is_Integer and self.exp.is_negative: # Note: possible future improvement: in principle we can take # positive powers of the inverse, but carefully avoid recursion, # perhaps by adding `_entry` to Inverse (as it is our subclass). # T = A.base.as_explicit().inverse() # A = MatMul(*[T for k in range(-A.exp)]) else: # Leave the expression unevaluated: from sympy.matrices.expressions.matexpr import MatrixElement return MatrixElement(self, i, j) return A._entry(i, j)
def inverse(self): if not self.is_square: raise NonSquareMatrixError('Inverse of non-square matrix') return self._eval_inverse()
def charpoly(self): m, n = self.shape if m != n: raise NonSquareMatrixError("not square") return self.rep.charpoly()