#NEWLINE# def bc_matmul( expr ) : #NEWLINE# #INDENT# ( factor , matrices ) = expr . as_coeff_matrices ( ) #NEWLINE# i = 0 #NEWLINE# while ( ( i + 1 ) < len ( matrices ) ) : #NEWLINE# #INDENT# ( A , B ) = matrices [ i : ( i + 2 ) ] #NEWLINE# if ( isinstance ( A , BlockMatrix ) and isinstance ( B , BlockMatrix ) ) : #NEWLINE# #INDENT# matrices [ i ] = A . _blockmul ( B ) #NEWLINE# matrices . pop ( ( matrices + 1 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( A , BlockMatrix ) : #NEWLINE# #INDENT# matrices [ i ] = A . _blockmul ( BlockMatrix ( [ [ B ] ] ) ) #NEWLINE# matrices . pop ( ( i + 1 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( B , BlockMatrix ) : #NEWLINE# #INDENT# matrices [ i ] = BlockMatrix ( [ [ A ] ] ) . _blockmul ( B ) #NEWLINE# matrices . pop ( ( i + 1 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# i += 1 #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return MatMul ( factor , * matrices ) . doit ( )