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
Define something like
f[x_,y_]:=
({ArcTan[-1+2/#, 2 Sqrt[(#-1)/#^2], ArcTan[x/#,y/#]}&)[1+x^2+y^2]
but for an interpreting language the gain will be moderate if more than
this single function only has to be evaluated during an inner loop.
--
Roland Franzius
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 <hak...@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?
{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
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
> ...
> 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?
>...
I have read that internally, Mathematica recognizes common
subexpressions and evaluates them only once. That doesn't help me when
I use Mathematica to create C code, so I wrote an explicit common
subexpression hoister that rewrites an expression into one with
variables holding multiply-used parts. It's not perfect, but it works
for me.
<http://stoney.sb.org/wordpress/2009/06/converting-symbolic-
mathematica-expressions-to-c-code/>
Maybe this would be useful to you too.
- Stoney