MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Locating common subexpressions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69471] Re: [mg69429] Locating common subexpressions
  • From: Daniel Lichtblau <danl at wolfram.com>
  • Date: Wed, 13 Sep 2006 04:02:56 -0400 (EDT)
  • References: <200609121052.GAA04126@smc.vnet.net>

carlos at colorado.edu wrote:
> The strangest thing about the behvior of Simplify in my
> previous post can be seen at a glance:
> 
>   R = (3 + 3*a^2 + Sqrt[5 + 6*a + 5*a^2] + a*(4 + Sqrt[5 + 6*a +
> 5*a^2]))/6
>   R = Simplify[R,a>=0]
>   (3 + 3*a^2 + Sqrt[5 + 6*a + 5*a^2] + a*(4 + Sqrt[5 + 6*a + 5*a^2]))/6
> 
> The subexpression Sqrt[5 + 6*a + 5*a^2] is not located. Ideally
> Simplify should right away replace that by a temp, say t$, and
> get to work on
> 
>   (3 + 3*a^2 + t$ + a*(4 + t$))/6
> 
> where from the assumptions, t$>0 and real. Of course the leaf count
> of t$ should influence subsequent transformations.
> This initial pass is useful in complicated expressions that come, eg,
> from an equation solver since messy subexpressions like the
> discriminant may appear in several places. I had some of those
> with leaf counts in the tens of thousands.
> 
> Locating common subs is of course an key task of optimizing
> compilers. Simplification and compilation share some objectives,
> although compilers have to deal with timing and side effects. In fact
> I wouldnt mind at all if Simplify would return a compound expression:
> 
> Block [{t$}, t$=Sqrt[5 + 6*a + 5*a^2];  (3 + 4*a + 3*a^2 + (1 +
> a)*t$)/6 ]
> 
> since this is perfect for documentation, or conversion to low-level
> code.

If you want CSE you can use OptimizeExpression in Experimental` context.

In[8]:= InputForm[Experimental`OptimizeExpression[(3 + 3*a^2 + Sqrt[5 + 
6*a + 5*a^2] + a*(4 + Sqrt[5 + 6*a + 5*a^2]))/6]]

Out[8]//InputForm=
Experimental`OptimizedExpression[Block[{$$25, $$27, $$28, $$29, $$30},
   $$25 = a^2; $$27 = 6*a; $$28 = 5*$$25; $$29 = 5 + $$27 + $$28;
    $$30 = Sqrt[$$29]; (3 + 3*$$25 + $$30 + a*(4 + $$30))/6]]

This sort of thing is not a part of the Simplify "charter", but is very 
much the intent of OptimizeExpression. As you might suspect, this is 
meant for reforming expressions prior to conversion to lower level 
languages such as C or the byte code of Mathematica's Compile virtual 
machine.


Daniel Lichtblau
Wolfram Research


  • Prev by Date: sampled points by Method->Oscillatory
  • Next by Date: Re: Evaluating a Meijer G-function
  • Previous by thread: Locating common subexpressions
  • Next by thread: Re: Re: Locating common subexpressions