def simplify_mul(tree):
 (neg, inputs) = tree
 if isinstance(inputs, list):
  s_inputs = []
  for s_i in imap(simplify_mul, inputs):
   if (s_i[1] is None):
    neg ^= s_i[0]
   else:
    s_inputs.append(s_i)
  if (not s_inputs):
   rval = [neg, None]
  elif (len(s_inputs) == 1):
   s_inputs[0][0] ^= neg
   rval = s_inputs[0]
  else:
   rval = [neg, s_inputs]
 else:
  rval = tree
 return rval

def simplify_mul ( tree ) : ( neg , inputs ) = tree if isinstance ( inputs , list ) : s_inputs = [ ] for s_i in imap ( simplify_mul , inputs ) : if ( s_i [ 1 ] is None ) : neg ^= s_i [ 0 ] else : s_inputs . append ( s_i ) if ( not s_inputs ) : rval = [ neg , None ] elif ( len ( s_inputs ) == 1 ) : s_inputs [ 0 ] [ 0 ] ^= neg rval = s_inputs [ 0 ] else : rval = [ neg , s_inputs ] else : rval = tree return rval
simplify a multiplication tree .