|
Calling Sequence
|
|
optimize(expr)
|
|
Parameters
|
|
expr
|
-
|
expression, array, list of equations, or a procedure
|
|
|
|
|
Description
|
|
•
|
The input to optimize must be either a single algebraic expression, an array of algebraic expressions (for example a matrix of formulae), a list of equations of the form name = algebraic (meaning a "computation sequence"), or a Maple procedure.
|
•
|
If the input is not a Maple procedure, the output from optimize is a sequence of equations of the form name = algebraic representing an "optimized computation sequence" for the input expression. Each equation in the sequence corresponds to an assignment. The global variables are used for temporary (local) variables in the computation sequence.
|
•
|
If the input is a Maple procedure, the output is the optimized Maple procedure. Presently optimize can only optimize simple procedures which are "computation sequences" i.e. consist of a sequence of simple assignment statements -- they contain no if statements or for loops.
|
•
|
If the input is a computation sequence of the form
|
|
then this understood to be equivalent to the Maple procedure
|
proc(x) global s1,s2,...,sn;
|
s1 := f1(x);
|
s2 := f2(x,s1);
|
...
|
sn := fn(x,s1,s2,...,sn);
|
end proc
|
|
|
|
So the names in the computation sequence are regarded as global variables which cannot be removed from the computation sequence. The optional arguments globals=[g1,g2,...,gm], parameters=[p1,p2,...,pm], and locals=[l1,l2,...,lm] may be used to specify otherwise.
|
•
|
The optimize function is designed to optimize only computation sequences
|
|
where x does not include any of . It is advised that the optimize function be used only when this condition is true; otherwise, unexpected results may be produced.
|
•
|
The optimize function makes use of Maple's option remember facility to identify common subexpressions in linear time and space. This means, however, that only those subexpressions which Maple has simplified to be identical are found. For example, the expression is not recognized as being common to . That is, optimize performs mainly "syntactic" optimizations.
|
•
|
If tryhard is given as an optional argument, a different algorithm is used which will give a better optimized output but will consume more time and memory.
|
•
|
The routine codegen[makeproc] can be used to create a Maple procedure from a single formula, an array of formulae, or a computation sequence.
|
•
|
The routine codegen[cost] can be used to give an operation count for the unoptimized and optimized input.
|
•
|
The command with(codegen,optimize) allows the use of the abbreviated form of this command.
|
|
|
Examples
|
|
>
|
|
| (2) |
>
|
|
| (3) |
| (4) |
| (5) |
| (6) |
| (7) |
| (8) |
| (9) |
>
|
|
| (10) |
>
|
|
| (11) |
| (12) |
| (13) |
>
|
|
| (15) |
>
|
g := proc(x,A::array(1..2)) A[1] := y*sin(x); A[2] := x^2*sin(x); end proc;
|
| (16) |
| (17) |
>
|
h := proc(x) local s,t,r; s := 1; t := x; r := t^2; r*s end proc;
|
| (18) |
| (19) |
>
|
s := proc(a,b,c) local r,s; r := a*b+a*c; s := b*c+c^2;r-s;end proc;
|
| (20) |
| (21) |
>
|
|
| (22) |
>
|
p := proc(x,m,n)
local i;
add( i * f(x-i), i = m .. n ) / add( f(x-i), i = m .. n )
end proc:
|
| (23) |
>
|
|
| (24) |
|
|
|