#NEWLINE# def _replace_mul_fpowxgpow( expr , f , g , rexp , h , rexph ) : #NEWLINE# #INDENT# 'Helper for _match_div_rewrite.\n\n    Replace f(b_)**c_*g(b_)**(rexp(c_)) with h(b)**rexph(c) if f(b_)\n    and g(b_) are both positive or if c_ is an integer.\n    ' #NEWLINE# fargs = defaultdict ( int ) #NEWLINE# gargs = defaultdict ( int ) #NEWLINE# args = [ ] #NEWLINE# for x in expr . args : #NEWLINE# #INDENT# if ( x . is_Pow or ( x . func in ( f , g ) ) ) : #NEWLINE# #INDENT# ( b , e ) = x . as_base_exp ( ) #NEWLINE# if ( b . is_positive or e . is_integer ) : #NEWLINE# #INDENT# if ( b . func == f ) : #NEWLINE# #INDENT# fargs [ b . args [ 0 ] ] += e #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( b . func == g ) : #NEWLINE# #INDENT# gargs [ common . args [ 0 ] ] += e #NEWLINE# continue #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# args . append ( x ) #NEWLINE# #UNINDENT# common = ( set ( fargs ) & set ( gargs ) ) #NEWLINE# hit = False #NEWLINE# while common : #NEWLINE# #INDENT# key = common . pop ( ) #NEWLINE# fe = fargs . pop ( key ) #NEWLINE# ge = gargs . pop ( key ) #NEWLINE# if ( fe == rexp ( ge ) ) : #NEWLINE# #INDENT# args . append ( ( h ( key ) ** rexph ( fe ) ) ) #NEWLINE# hit = True #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# fargs [ key ] = fe #NEWLINE# gargs [ key ] = ge #NEWLINE# #UNINDENT# #UNINDENT# if ( not hit ) : #NEWLINE# #INDENT# return expr #NEWLINE# #UNINDENT# while fargs : #NEWLINE# #INDENT# ( key , e ) = fargs . popitem ( ) #NEWLINE# args . append ( ( f ( key ) ** e ) ) #NEWLINE# #UNINDENT# while gargs : #NEWLINE# #INDENT# ( key , e ) = gargs . popitem ( ) #NEWLINE# args . append ( ( g ( key ) ** e ) ) #NEWLINE# #UNINDENT# return Mul ( * args )