def common_subexpression(expr, *exprs):
 if (not exprs):
  return expr
 exprs = ((expr,) + exprs)
 all_leaves = [expr._leaves() for expr in exprs]
 leaves = set.intersection(*map(set, all_leaves))
 if (not leaves):
  raise ValueError(('No common leaves found in expressions %s' % list(exprs)))
 pathlist = [list(path(expr, leaf)) for expr in exprs for leaf in leaves]
 common = reduce(ordered_intersect, pathlist)
 if (not common):
  raise ValueError(('No common subexpression found in paths to leaf: %s' % list(map(set, pathlist))))
 return first(common)

def common_subexpression ( expr , * exprs ) : if ( not exprs ) : return expr exprs = ( ( expr , ) + exprs ) all_leaves = [ expr . _leaves ( ) for expr in exprs ] leaves = set . intersection ( * map ( set , all_leaves ) ) if ( not leaves ) : raise ValueError ( ( 'No common leaves found in expressions %s' % list ( exprs ) ) ) pathlist = [ list ( path ( expr , leaf ) ) for expr in exprs for leaf in leaves ] common = reduce ( ordered_intersect , pathlist ) if ( not common ) : raise ValueError ( ( 'No common subexpression found in paths to leaf: %s' % list ( map ( set , pathlist ) ) ) ) return first ( common )
common sub expression between subexpressions examples .