def xxinv(mul): """ Y * X * X.I -> Y """ from sympy.matrices.expressions import Inverse factor, matrices = mul.as_coeff_matrices() for i, (X, Y) in enumerate(zip(matrices[:-1], matrices[1:])): try: if X.is_square and Y.is_square and X == Y.inverse(): I = Identity(X.rows) return newmul(factor, *(matrices[:i] + [I] + matrices[i + 2:])) except ValueError: # Y might not be invertible pass return mul
def xxinv(mul): """ Y * X * X.I -> Y """ from sympy.matrices.expressions import Inverse factor, matrices = mul.as_coeff_matrices() for i, (X, Y) in enumerate(zip(matrices[:-1], matrices[1:])): try: if X.is_square and Y.is_square and X == Y.inverse(): I = Identity(X.rows) return newmul(factor, *(matrices[:i] + [I] + matrices[i+2:])) except ValueError: # Y might not be invertible pass return mul
def factor_in_front(mul): factor, matrices = mul.as_coeff_matrices() if factor != 1: return newmul(factor, *matrices) return mul