codegen - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : codegen Package : codegen/optimize

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

f:=x4x3

(1)

optimizef

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

(2)

fxlnx+2x2lnx+3x3lnx

f:=xlnx+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);

F:=procxx*lnx+2*x^2*lnx+3*x^3*lnxend proc

(8)

optimizeF

procxlocalt1,t3;t1:=lnx;t3:=x^2;3*t1*t3*x+2*t1*t3+t1*xend proc

(9)

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

optimizecs

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

(10)

optimizecs,locals=t

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

(11)

F := makeproc(cs,x);

F:=procxlocalt;t:=x;r[1]:=t*xy^2;r[2]:=2*yx^2end proc

(12)

optimizeF

procxlocalt1,t2,t4;t1:=xy;t2:=t1^2;r[1]:=x*t2;t4:=t1^2;r[2]:=2*t4end proc

(13)

Aarrayx3,x2,x2,x4

A:=x3x2x2x4

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

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

(16)

optimizeg

procx,A::array1..2localt1,t2;t1:=sinx;A[1]:=y*t1;t2:=x^2;A[2]:=t1*t2end proc

(17)

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

(18)

optimizeh

procxlocalr;r:=x^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;

s:=proca,b,clocalr,s;r:=a*b+a*c;s:=b*c+c^2;rsend proc

(20)

optimizes

proca,b,clocalt4;t4:=c^2;a*b+a*cb*ct4end proc

(21)

optimizes,'tryhard'

proca,b,clocalresult,t1;t1:=b+c;result:=a*t1c*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

q:=procx,m,nlocali,i1,i2,s1,s2,t1,t2;s1:=0;fori1frommtondos1:=s1+i1*fxi1end do;s2:=0;fori2frommtondos2:=s2+fxi2end do;s1/s2end proc

(23)

optimizeq,'tryhard'

procx,m,nlocali,i1,i2,s1,s2,t1,t2;s1:=0;fori1frommtondos1:=s1+i1*fxi1end do;s2:=0;fori2frommtondos2:=s2+fxi2end do;s1/s2end proc

(24)

See Also

codegen[cost]

codegen[makeproc]

codegen[prep2trans]

 


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam