codegen
optimize
common subexpression optimization
Calling Sequence
Parameters
Description
Examples
optimize(expr)
expr
-
expression, array, list of equations, or a procedure
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 t0,t1,t2,… 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
s1=f1⁡x,s2=f2⁡x,s1,...,sn=fn⁡x,s1,...,sn
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 s1,s2,...,sn 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 s1,s2,...,sn. 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 x+y is not recognized as being common to x+y+z. 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.
Procedures containing calls to sum, add or piecewise must first be prepared for translation by calling codegen[prep2trans] and running optimize on the output.
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.
with⁡codegen,optimize,makeproc,cost,prep2trans:
f≔x4−x3
optimize⁡f
t1=x2,t2=t12,t4=−t1⁢x+t2
f≔x⁢ln⁡x+2⁢x2⁢ln⁡x+3⁢x3⁢ln⁡x
t1=ln⁡x,t3=x2,t9=3⁢t1⁢t3⁢x+2⁢t1⁢t3+t1⁢x
cost⁡f
2⁢additions+8⁢multiplications+3⁢functions
cost⁡optimize⁡f
functions+3⁢assignments+7⁢multiplications+2⁢additions
cost⁡f−cost⁡optimize⁡f
multiplications+2⁢functions−3⁢assignments
F := makeproc(f,x);
F ≔ procxx*ln⁡x+2*x^2*ln⁡x+3*x^3*ln⁡xend proc
optimize⁡F
procxlocalt1,t3;t1 ≔ ln⁡x;t3 ≔ x^2;3*t1*t3*x+2*t3*t1+t1*xend proc
cs≔t=x,r1=t⁢x−y2,r2=2⁢y−x2:
optimize⁡cs
t=x,t1=x−y,t2=t12,r1=t2⁢x,t4=t12,r2=2⁢t4
optimize⁡cs,locals=t
t1=x−y,t2=t12,r1=t2⁢x,t4=t12,r2=2⁢t4
F := makeproc(cs,x);
F ≔ procxlocalt;t ≔ x;r[1] ≔ t*x − y^2;r[2] ≔ 2*y − x^2end proc
procxlocalt1,t2,t4;t1 ≔ x − y;t2 ≔ t1^2;r[1] ≔ t2*x;t4 ≔ t1^2;r[2] ≔ 2*t4end proc
A≔array⁡x3,x2,x2,x4
A≔x3x2x2x4
optimize⁡A
t1=x2,t3=t12,A1,1=t1⁢x,A1,2=t1,A2,1=t1,A2,2=t3
g := proc(x,A::array(1..2)) A[1] := y*sin(x); A[2] := x^2*sin(x); end proc;
g ≔ procx,A::array⁡1..2A[1] ≔ y*sin⁡x;A[2] ≔ x^2*sin⁡xend proc
optimize⁡g
procx,A::array⁡1..2localt1,t2;t1 ≔ sin⁡x;A[1] ≔ t1*y;t2 ≔ x^2;A[2] ≔ t2*t1end proc
h := proc(x) local s,t,r; s := 1; t := x; r := t^2; r*s end proc;
h ≔ procxlocals,t,r;s ≔ 1;t ≔ x;r ≔ t^2;r*send proc
optimize⁡h
procxlocalr;r ≔ x^2;rend proc
s := proc(a,b,c) local r,s; r := a*b+a*c; s := b*c+c^2;r-s;end proc;
s ≔ proca,b,clocalr,s;r ≔ b*a+a*c;s ≔ b*c+c^2;r − send proc
optimize⁡s
proca,b,clocalt4;t4 ≔ c^2;b*a+a*c − b*c − t4end proc
optimize⁡s,tryhard
proca,b,clocalresult,t1;t1 ≔ b+c;result ≔ t1*a − c*t1end proc
p := proc(x,m,n) local i; add( i * f(x-i), i = m .. n ) / add( f(x-i), i = m .. n ) end proc:
q≔prep2trans⁡p
q ≔ procx,m,nlocali,i1,i2,s1,s2,t1,t2;s1 ≔ 0;fori1frommtondos1 ≔ s1+i1*f⁡x − i1end do;s2 ≔ 0;fori2frommtondos2 ≔ s2+f⁡x − i2end do;s1/s2end proc
optimize⁡q,tryhard
procx,m,nlocali,i1,i2,s1,s2,t1,t2;s1 ≔ 0;fori1frommtondos1 ≔ s1+i1*f⁡x − i1end do;s2 ≔ 0;fori2frommtondos2 ≔ s2+f⁡x − i2end do;s1/s2end proc
See Also
codegen[cost]
codegen[makeproc]
codegen[prep2trans]
Download Help Document