MathGroup Archive 2009

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

Search the Archive

Re: Optimize evaluation of symbolic expressions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg102011] Re: [mg101993] Optimize evaluation of symbolic expressions
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Sun, 26 Jul 2009 03:56:30 -0400 (EDT)
  • References: <200907250818.EAA29767@smc.vnet.net>

This has been frequently asked and answered, usually (or maybe always)  
by Daniel Lichtblau. His latest post on this topic was, I think,

http://groups.google.com/group/comp.soft-sys.math.mathematica/msg/10fed4b8bd60b762

Here is a brief supplementary comment. The function  
OptimizeExpression, which is in the Experimantal context (so it can be  
modified in the future or even disappear altogether) will attempt to  
do what you wish. However, it is really intended for efficient  
compiling of numerical functions and may not be convenient to use for  
other purposes.

Here is how it works in the case of your example:

Define a compiled function by:

g = Compile[{x, y},
   Evaluate[
    Experimental`OptimizeExpression[{ArcTan[-1 + 2/(1 + x^2 + y^2),
       2 Sqrt[(x^2 + y^2)/(1 + x^2 + y^2)^2]],
      ArcTan[x/(1 + x^2 + y^2), y/(1 + x^2 + y^2)]}, OptimizationLevel  
-> 2]]]

evaluate the above and look at the output, you will see how the  
expression became optimized. Now you can use it for efficient  
computation:

g[1, 2]
  {2.30052, 1.10715}


Andrzej Kozlowski

On 25 Jul 2009, at 17:18, Harrie Kraai wrote:

> Hello all,
>
> With a specific example I countered today I want to introduce a  
> question
> that I have had for a longer time.
>
> From some composition of functions (and a subsequent FullSimplify) I
> get the following expression:
>
> {ArcTan[-1 + 2/(1 + x^2 + y^2), 2 Sqrt[(x^2 + y^2)/(1 + x^2 +  
> y^2)^2]],
>  ArcTan[x/(1 + x^2 + y^2), y/(1 + x^2 + y^2)]}
>
> which I need to evaluate many times inside a Manipulate.
>
> Now, how does Mathematica evaluate this?
>
> If I would program this myself, I would, of course, evaluate the
> subexpression (1 + x^2 + y^2) first and use this result 4 times in the
> subsequent calculations.
>
> It looks to me like Mathematica is not able to or at least does not  
> make
> this optimization. (Find out using 'Trace').  Perhaps this is  
> difficult
> to do in a general sense because any subexpression may have
> side-effects. However, in this case it should be possible to instruct
> Mathematica to collect common subexpressions first. It would surely  
> make
> calculations much faster.
> The symbolic calculations have led to an expression that is
> "Simplify"-ed  in terms of reading (perhaps) but not in terms of
> evaluation. Is there, or should there be a function that translates
> expressions to a form that is optimized for evaluation?
>
> This appears to me as a frequently encountered problem. How do we go
> about it?
>
>
> Thanks for your advice and/or thoughts.
>
> Harrie
>



  • Prev by Date: Re: Logarithmic ticks in SectorChart
  • Next by Date: Mathematica Animations by High School Students
  • Previous by thread: Re: Optimize evaluation of symbolic expressions
  • Next by thread: Re: Optimize evaluation of symbolic expressions