Re: Optimize evaluation of symbolic expressions

• To: mathgroup at smc.vnet.net
• Subject: [mg101995] Re: [mg101993] Optimize evaluation of symbolic expressions
• From: Bob Hanlon <hanlonr at cox.net>
• Date: Sun, 26 Jul 2009 03:53:35 -0400 (EDT)

```You need to help it

expr = {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)]};

Note that if x and y are Real, this can be further simplified

Simplify[expr, Element[{x, y}, Reals]]

{ArcTan[-x^2 - y^2 + 1, 2*Sqrt[x^2 + y^2]], ArcTan[x, y]}

In the general case,

Simplify[expr, t == x^2 + y^2 + 1]

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

Manipulate[Module[{t = x^2 + y^2 + 1},
{ArcTan[2/t - 1, 2*Sqrt[(t - 1)/t^2]],
ArcTan[x/t, y/t]}],
{x, -1, 1, .01, Appearance -> "Labeled"},
{y, -1, 1, .01, Appearance -> "Labeled"}]

Bob Hanlon

---- 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

```

• Prev by Date: JavaView and contourplots
• Next by Date: list of arbitrary length n from user input
• Previous by thread: Re: Optimize evaluation of symbolic expressions
• Next by thread: Re: Optimize evaluation of symbolic expressions