optimize - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


codegen

  

optimize

  

common subexpression optimization

 

Calling Sequence

Parameters

Description

Examples

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 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=f1x,s2=f2x,s1,...,sn=fnx,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

s1=f1x,s2=f2x,s1,...,sn=fnx,s1,...,sn

  

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.

Examples

withcodegen,optimize,makeproc,cost,prep2trans:

fx4x3

fx4x3

(1)

optimizef

t1=x2,t2=t12,t4=t1x+t2

(2)

fxlnx+2x2lnx+3x3lnx

fxlnx+2x2lnx+3x3lnx

(3)

optimizef

t1=lnx,t3=x2,t9=3t1t3x+2t1t3+t1x

(4)

costf

2additions+8multiplications+3functions

(5)

costoptimizef

functions+3assignments+7multiplications+2additions

(6)

costfcostoptimizef

multiplications+2functions3assignments

(7)

F := makeproc(f,x);

Fprocxx*lnx+2*x^2*lnx+3*x^3*lnxend proc

(8)

optimizeF

procxlocalt1,t3;t1lnx;t3x^2;3*t1*t3*x+2*t3*t1+t1*xend proc

(9)

cst=x,r1=txy2,r2=2yx2:

optimizecs

t=x,t1=xy,t2=t12,r1=t2x,t4=t12,r2=2t4

(10)

optimizecs,locals=t

t1=xy,t2=t12,r1=t2x,t4=t12,r2=2t4

(11)

F := makeproc(cs,x);

Fprocxlocalt;tx;r[1]t*xy^2;r[2]2*yx^2end proc

(12)

optimizeF

procxlocalt1,t2,t4;t1xy;t2t1^2;r[1]t2*x;t4t1^2;r[2]2*t4end proc

(13)

Aarrayx3,x2,x2,x4

Ax3x2x2x4

(14)

optimizeA

t1=x2,t3=t12,A1,1=t1x,A1,2=t1,A2,1=t1,A2,2=t3

(15)

g := proc(x,A::array(1..2)) A[1] := y*sin(x); A[2] := x^2*sin(x); end proc;

gprocx,A::array1..2A[1]y*sinx;A[2]x^2*sinxend proc

(16)

optimizeg

procx,A::array1..2localt1,t2;t1sinx;A[1]t1*y;t2x^2;A[2]t2*t1end proc

(17)

h := proc(x) local s,t,r; s := 1; t := x; r := t^2; r*s end proc;

hprocxlocals,t,r;s1;tx;rt^2;r*send proc

(18)

optimizeh

procxlocalr;rx^2;rend proc

(19)

s := proc(a,b,c) local r,s; r := a*b+a*c; s := b*c+c^2;r-s;end proc;

sproca,b,clocalr,s;rb*a+a*c;sb*c+c^2;rsend proc

(20)

optimizes

proca,b,clocalt4;t4c^2;b*a+a*cb*ct4end proc

(21)

optimizes,tryhard

proca,b,clocalresult,t1;t1b+c;resultt1*ac*t1end proc

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

qprep2transp

qprocx,m,nlocali,i1,i2,s1,s2,t1,t2;s10;fori1frommtondos1s1+i1*fxi1end do;s20;fori2frommtondos2s2+fxi2end do;s1/s2end proc

(23)

optimizeq,tryhard

procx,m,nlocali,i1,i2,s1,s2,t1,t2;s10;fori1frommtondos1s1+i1*fxi1end do;s20;fori2frommtondos2s2+fxi2end do;s1/s2end proc

(24)

See Also

codegen[cost]

codegen[makeproc]

codegen[prep2trans]