MathGroup Archive 2008

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

Search the Archive

Re: OptimizeExpression

  • To: mathgroup at smc.vnet.net
  • Subject: [mg86193] Re: OptimizeExpression
  • From: dh <dh at metrohm.ch>
  • Date: Wed, 5 Mar 2008 03:41:14 -0500 (EST)
  • References: <fqisvi$mct$1@smc.vnet.net>


Hi Evan,

the problem your are facing is to prevent evaluation of a semicolon 

separated list.

I see two approaches to this, either you replace the semicolons by 

commas or you wrap the expression in Hold[..]. As C also uses 

semicolons, we will try the second approach. By ReplaceRepeated you can 

extract the lines of code and wrap them by Hold. Assume t1 is your 

expression Experimental`OptimizeExpression[...], then:

t2=t1/.{HoldPattern[_[Block[{__}, x___]]]->Hold[x]}

this gives you the lines of code wrapped in Hold. To get C code:

t2 // CForm

now you only need to remove Hold.

hope this helps, Daniel





Evan Drumwright wrote:

> Hi everyone,

> 

> I'm trying to send a very large (200+MB) expression to a C file using CForm, which, as you might imagine, crashes my compiler.  From other discussions on this list, I have seen that you can use the OptimizeExpression[] function to eliminate common subexpressions, and I think that this will work.  However, I don't understand how to process the resulting compound expression.  For instance:

> 

> 

> Experimental`OptimizeExpression[(3+3*a^2 + Sqrt[5 + 6*a + 5*a^2] + a*(4 + Sqrt[5 + 6*a + 5*a^2]))/6]

> 

> generates

> 

> Out[1]= Experimental`OptimizedExpression[Block[{$$11, $$13, $$14, $$15,

> 

>                       2

>>      $$16}, $$11 = a ; $$13 = 6 a; $$14 = 5 $$11; $$15 = 5 + $$13 + $$14;

> 

>                           3 + 3 $$11 + $$16 + a (4 + $$16)

>>      $$16 = Sqrt[$$15]; --------------------------------]]

>                                          6

> 

> Now, I'd like to get out the individual set of statements:

> 

> $$11 = a;

> $$13 = 6a;

> $$14 = 5 $$11;

> $$15 = 5 + $$13 + $$14;

> $$16 = Sqrt[$$15];

> (3 + 3$$11 + $$16 + a(4+$$16)/6

> 

> 

> But, if I were to look at the function in the optimized expression, I get the original function:

> 

> In[2]:= %[[1]]

> 

>                2                     2                             2

>         3 + 3 a  + Sqrt[5 + 6 a + 5 a ] + a (4 + Sqrt[5 + 6 a + 5 a ])

> Out[2]= --------------------------------------------------------------

>                                       6

> 

> 

> I suppose part of the difficulty is that I don't see a way to treat a CompoundExpression as a list of statements to be CForm'd.  Can anyone tell me how I'd do this?

> 

> 

> Thanks,

> Evan

> 

> 




  • Prev by Date: Bold Script Characters in 6.0.2
  • Next by Date: Re: Mathematica 6 obtains imaginary eigenvalues for a Hermitian
  • Previous by thread: OptimizeExpression
  • Next by thread: Re: OptimizeExpression