       Re: Optimize evaluation of symbolic expressions

• To: mathgroup at smc.vnet.net
• Subject: [mg101999] Re: [mg101993] Optimize evaluation of symbolic expressions
• From: DrMajorBob <btreat1 at austin.rr.com>
• Date: Sun, 26 Jul 2009 03:54:18 -0400 (EDT)
• References: <200907250818.EAA29767@smc.vnet.net>

```expr1 = {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)]};
expr2 = expr1 /. (1 + x^2 + y^2 -> s)

{ArcTan[-1 + 2/s, 2 Sqrt[(x^2 + y^2)/s^2]], ArcTan[x/s, y/s]}

f[x_, y_] = Block[{s = 1 + x^2 + y^2}, expr2];
f[a, b]

{ArcTan[-1 + 2/(1 + a^2 + b^2),
2 Sqrt[(a^2 + b^2)/(1 + a^2 + b^2)^2]],
ArcTan[a/(1 + a^2 + b^2), b/(1 + a^2 + b^2)]}

Bobby

On Sat, 25 Jul 2009 03:18:57 -0500, Harrie Kraai <hakraai at xs4all.nl> 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
>
>
>
> Harrie
>

--
DrMajorBob at bigfoot.com

```

• Prev by Date: Re: Re: Re: Re: Thoughts on a Wolfram|Alpha
• Next by Date: Re: model for early cosmological symmetry breaking as a 3d surface
• Previous by thread: Optimize evaluation of symbolic expressions
• Next by thread: Re: Optimize evaluation of symbolic expressions