       Optimize evaluation of symbolic expressions

• To: mathgroup at smc.vnet.net
• Subject: [mg101993] Optimize evaluation of symbolic expressions
• From: Harrie Kraai <hakraai at xs4all.nl>
• Date: Sat, 25 Jul 2009 04:18:57 -0400 (EDT)

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