Exemple #1
0
    def __new__(cls, mat, **kwargs):

        if not mat.is_Matrix:
            return mat**(-1)

        try:
            return mat.eval_inverse(**kwargs)
        except (AttributeError, NotImplementedError):
            pass

        if hasattr(mat, 'inv'):
            return mat.inv()

        if mat.is_Inverse:
            return mat.arg

        if mat.is_Identity:
            return mat

        if not mat.is_square:
            raise ShapeError("Inverse of non-square matrix %s" % mat)

        if mat.is_Mul:
            try:
                return MatMul(*[Inverse(arg) for arg in mat.args[::-1]])
            except ShapeError:
                pass

        return MatPow.__new__(cls, mat, -1)
Exemple #2
0
    def __new__(cls, mat, **kwargs):

        if not mat.is_Matrix:
            return mat ** (-1)

        try:
            return mat.eval_inverse(**kwargs)
        except (AttributeError, NotImplementedError):
            pass

        if hasattr(mat, "inv"):
            return mat.inv()

        if mat.is_Inverse:
            return mat.arg

        if mat.is_Identity:
            return mat

        if not mat.is_square:
            raise ShapeError("Inverse of non-square matrix %s" % mat)

        if mat.is_Mul:
            try:
                return MatMul(*[Inverse(arg) for arg in mat.args[::-1]])
            except ShapeError:
                pass

        return MatPow.__new__(cls, mat, -1)
Exemple #3
0
 def __pow__(self, other):
     if not self.is_square:
         raise ShapeError("Power of non-square matrix %s" % self)
     if other is S.NegativeOne:
         return Inverse(self)
     elif other is S.Zero:
         return Identity(self.rows)
     elif other is S.One:
         return self
     return MatPow(self, other)
Exemple #4
0
    def __new__(cls, mat, **kwargs):

        if not mat.is_Matrix:
            return mat**(-1)

        if not mat.is_square:
            raise ShapeError("Inverse of non-square matrix %s" % mat)

        try:
            return mat._eval_inverse(**kwargs)
        except (AttributeError, NotImplementedError):
            return MatPow.__new__(cls, mat, -1)
Exemple #5
0
    def __new__(cls, mat, **kwargs):

        if not mat.is_Matrix:
            return mat**(-1)

        if not mat.is_square:
            raise ShapeError("Inverse of non-square matrix %s"%mat)

        try:
            return mat._eval_inverse(**kwargs)
        except (AttributeError, NotImplementedError):
            return MatPow.__new__(cls, mat, -1)
Exemple #6
0
    def __new__(cls, *args):

        # Check that the shape of the args is consistent
        matrices = [arg for arg in args if arg.is_Matrix]

        for i in range(len(matrices) - 1):
            A, B = matrices[i:i + 2]
            if A.m != B.n:
                raise ShapeError("Matrices %s and %s are not aligned" % (A, B))

        if any(arg.is_zero for arg in args):
            return ZeroMatrix(matrices[0].n, matrices[-1].m)

        expr = matrixify(Mul.__new__(cls, *args))
        if expr.is_Add:
            return MatAdd(*expr.args)
        if expr.is_Pow:
            return MatPow(*expr.args)
        if not expr.is_Mul:
            return expr

        if any(arg.is_Matrix and arg.is_ZeroMatrix for arg in expr.args):
            return ZeroMatrix(*expr.shape)

        # Clear out Identities
        nonmats = [M for M in expr.args if not M.is_Matrix]  # scalars
        mats = [M for M in expr.args if M.is_Matrix]  # matrices
        if any(M.is_Identity for M in mats):  # Any identities around?
            newmats = [M for M in mats if not M.is_Identity]  # clear out
            if len(newmats) == 0:  # Did we lose everything?
                newmats = [Identity(expr.n)]  # put just one back in

            if mats != newmats:  # Removed some I's but not everything?
                return MatMul(*(nonmats + newmats))  # Repeat with simpler expr

        return expr
Exemple #7
0
 def _eval_power(self, exp):
     return MatPow(self, exp)
Exemple #8
0
 def __pow__(self, other):
     if other == -S.One:
         return Inverse(self)
     return MatPow(self, other)