def block_collapse(expr):
 hasbm = (lambda expr: (isinstance(expr, MatrixExpr) and expr.has(BlockMatrix)))
 rule = exhaust(bottom_up(exhaust(condition(hasbm, typed({MatAdd: do_one(bc_matadd, bc_block_plus_ident), MatMul: do_one(bc_matmul, bc_dist), Transpose: bc_transpose, Inverse: bc_inverse, BlockMatrix: do_one(bc_unpack, deblock)})))))
 result = rule(expr)
 try:
  return result.doit()
 except AttributeError:
  return result

def block_collapse ( expr ) : hasbm = ( lambda expr : ( isinstance ( expr , MatrixExpr ) and expr . has ( BlockMatrix ) ) ) rule = exhaust ( bottom_up ( exhaust ( condition ( hasbm , typed ( { MatAdd : do_one ( bc_matadd , bc_block_plus_ident ) , MatMul : do_one ( bc_matmul , bc_dist ) , Transpose : bc_transpose , Inverse : bc_inverse , BlockMatrix : do_one ( bc_unpack , deblock ) } ) ) ) ) ) result = rule ( expr ) try : return result . doit ( ) except AttributeError : return result
evaluates a block matrix expression .