def xxinv(mul):
 (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:
   pass
 return mul

def xxinv ( mul ) : ( 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 : pass return mul
y * x * x .