Ejemplo n.º 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)
Ejemplo n.º 2
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)