parameters: (expr,prefix="SubExp")
return_statement: return repl
docstring: Creates a copy of expr where each reference to a common subexpression is
      replaced with a TaggedExpression.  If there are no common subexpressions,
      expr is returned.
docstring_summary: Creates a copy of expr where each reference to a common subexpression is
      replaced with a TaggedExpression.  If there are no common subexpressions,
      expr is returned.
docstring_tokens: Creates a copy of expr where each reference to a common subexpression is replaced with a TaggedExpression . If there are no common subexpressions expr is returned .
function: def _make_tagged(expr,prefix="SubExp"):
      """Creates a copy of expr where each reference to a common subexpression is
      replaced with a TaggedExpression.  If there are no common subexpressions,
      expr is returned."""
      def _refspre(node):
          if 'refs' in node._cache:
              node._cache['refs'] += 1
              return (False,True,node._cache['refs'])
          node._cache['refs'] = 1
          return (True,True,None)
      expr._traverse(pre=_refspre,cache=False)
      #all the cache values are now the number of references to a subexpression
  
      def _hassubexpr_pre(node):
          if node._cache['refs'] > 1:
              #print "Node",node.functionInfo.name,"is a repeated subexpression"
              node._cache['hassubexpr'] = True
              return (False,True,True)
          return (True,True,None)
      def _hassubexpr_post(node,cvals):
          if len(cvals) == 0:
              return (True,False)
          res = any(cvals)
          #print "Child of",node.functionInfo.name,"has repeated subexpression"
          node._cache['hassubexpr'] = res
          if res: return (True,True)
          return (True,False)
      if not expr._traverse(pre=_hassubexpr_pre,post=_hassubexpr_post,cache=False):
          #print "***Expression has no subexpressions***"
          expr._clearCache('refs')
          expr._clearCache('hassubexpr')
          return expr
  
      assert expr._cache.get('hassubexpr',False) == True
      expr._clearCache('refs')
      #print "***Expression has subexpressions***"
      subexprs = dict()
      def replace(node):
          if not node._cache.get('hassubexpr',False): return node
          if 'refs' in node._cache:
              if 'id' not in node._cache:
                  #new detected subexpression, not added yet
                  tag = prefix+str(len(subexprs)+1)
                  node._cache['id'] = tag
                  subexprs[tag] = _TaggedExpression(tag)
              node._cache['refs'] += 1
              #print "Reference",node._cache['refs'],"to",node.functionInfo.name
              return subexprs[node._cache['id']]
          node._cache['refs'] = 1
          if node._children is None:
              return node
          else:
              assert isinstance(node,OperatorExpression)
              #print "New reference to",node.functionInfo.name
              creps = [replace(c) for c in node._children]
              if any(cr is not c for (cr,c) in zip(creps,node._children)):
                  return OperatorExpression(node.functionInfo,creps,node.op)
              else:
                  return node
      repl = replace(expr)
      expr._clearCache('refs')
      expr._clearCache('hassubexpr')
      #NEED TO CLEAR 'id' from cache after repl is used
      return repl
function_tokens: def _make_tagged ( expr , prefix = "SubExp" ) : def _refspre ( node ) : if 'refs' in node . _cache : node . _cache [ 'refs' ] += 1 return ( False , True , node . _cache [ 'refs' ] ) node . _cache [ 'refs' ] = 1 return ( True , True , None ) expr . _traverse ( pre = _refspre , cache = False ) #all the cache values are now the number of references to a subexpression def _hassubexpr_pre ( node ) : if node . _cache [ 'refs' ] > 1 : #print "Node",node.functionInfo.name,"is a repeated subexpression" node . _cache [ 'hassubexpr' ] = True return ( False , True , True ) return ( True , True , None ) def _hassubexpr_post ( node , cvals ) : if len ( cvals ) == 0 : return ( True , False ) res = any ( cvals ) #print "Child of",node.functionInfo.name,"has repeated subexpression" node . _cache [ 'hassubexpr' ] = res if res : return ( True , True ) return ( True , False ) if not expr . _traverse ( pre = _hassubexpr_pre , post = _hassubexpr_post , cache = False ) : #print "***Expression has no subexpressions***" expr . _clearCache ( 'refs' ) expr . _clearCache ( 'hassubexpr' ) return expr assert expr . _cache . get ( 'hassubexpr' , False ) == True expr . _clearCache ( 'refs' ) #print "***Expression has subexpressions***" subexprs = dict ( ) def replace ( node ) : if not node . _cache . get ( 'hassubexpr' , False ) : return node if 'refs' in node . _cache : if 'id' not in node . _cache : #new detected subexpression, not added yet tag = prefix + str ( len ( subexprs ) + 1 ) node . _cache [ 'id' ] = tag subexprs [ tag ] = _TaggedExpression ( tag ) node . _cache [ 'refs' ] += 1 #print "Reference",node._cache['refs'],"to",node.functionInfo.name return subexprs [ node . _cache [ 'id' ] ] node . _cache [ 'refs' ] = 1 if node . _children is None : return node else : assert isinstance ( node , OperatorExpression ) #print "New reference to",node.functionInfo.name creps = [ replace ( c ) for c in node . _children ] if any ( cr is not c for ( cr , c ) in zip ( creps , node . _children ) ) : return OperatorExpression ( node . functionInfo , creps , node . op ) else : return node repl = replace ( expr ) expr . _clearCache ( 'refs' ) expr . _clearCache ( 'hassubexpr' ) #NEED TO CLEAR 'id' from cache after repl is used return repl