Rename *ll* and *ul* to ll and ul in easy-subs
[maxima.git] / share / misc / seqopt.dem
blob8e3b344f3e8378d66d4722951107b90060c2f079
1 /*-*-MACSYMA-*-*/
2 if properties(sequence_optimize) = [] then load(seqopt)$
3 /* To illustate the exponent gathering capabilities of SEQUENCE_OPTIMIZE,
4    we start with a product of terms with complicated exponents, EXP1:       */
5 exp1:a^(tan(th)/r+3)*b^(tan(th)/r+300)*f^(tan(th)/r+2)*g^4*h;
6 sequence_optimize(exp1);
7 /* The next expression, EXP2, is a simple product of terms raised to integer
8    powers:                                                                  */
9 exp2:a^40*b^35*f^21*g^2;
10 /* Notice how the integer exponents are gathered: */
11 sequence_optimize(exp2);
12 /* Note that the result is an expression which is not a standard simplified
13    form.  To see this clearly, we reevaluate the expression:                */
14 ev(%);
15 /* For this reason, it is recommended that the optimization be performed only
16    as the last step before code generation(FORTRAN, for example).
18    Next, with EXP3, we examine a case where the exponents are not integers. */
19 exp3:(a*b*k*q*r*s*t)^(4*sin(u))*(v*w*x*y*z)^(5*sin(u));
20 sequence_optimize(exp3);
21 /* Note that whereas m^4*n^5 would be cheaper as (m*n)^4*n, the expense of
22    computing the analogous form of the result of SEQUENCE_OPTIMIZE(EXP3)
23    (where "m" and "n" are now complicated expressions) prevents it from
24    being returned.
26    Now, consider EXP4, where the exponents are yet more complicated. */
27 exp4:a^(4/sqrt(u))*b^(5/4/sqrt(u))*c^(s*sqrt(u));
28 sequence_optimize(exp4);
29 /* So, the exponent gathering capability of SEQUENCE_OPTIMIZE is substantial.
31    Next, lets look at some common subexpression extraction.  First, an
32    example of a single expression that OPTIMIZE could handle equally well. */
33 exp5:r*(u-v)*log(s*sqrt(u-v))/(t-a*(u-v));
34 sequence_optimize(exp5);
35 /* SEQUENCE_OPTIMIZE already does more, however, because it informs you of
36    the number of times a given subexpression occurred.
38    Next, in the list of equations, EQNS1, we observe the fact that
39    SEQUENCE_OPTIMIZE can deal with a number of expressions simultaneously. */
40 eqns1:[r1[i]=b[i]^3*q[i]^4*z-p[i],
41        r2[i]=sqrt(b[i]*q[i])*(u*v)^(2/3),
42        r3[i]=(u*v)^(5/3)-t[i],
43        r4[i]=g[i]+b[i]^6*q[i]^6/p[i]];
44 sequence_optimize(eqns1);
45 /* Note that the output is in the form of a list, since the input was in
46    that form; also, there was exponent gathering in this example.
48    Now turn to the case of saving the optimization information.  To
49    demonstrate this, we will turn on the flag to save this list in
50    OPTIM_EQUIVS :                                                      */
51 save_optim_info:true$
52 /* Since we wish to target these equations to a vectorizing DO-LOOP, we
53    must give each optimized temporary value found the loop index as a
54    suffix.  Say we have chosen that index to be "k".  Then, the appropriate
55    command is:                                                             */
56 sequence_optim_suffix:?"(K)";
57 /* The first sequence to be optimized will provide the values for
58    OPTIM_EQUIVS. */
59 eqns2:[t\(k\)=a\(k\)*(b1\(i\,j\)+exb1\(i\,j\))-exp(-hnu/ti\(i\,j\)),
60        u\(k\)=b\(k\)*(b2\(i\,j\)+exb2\(i\,j\))*(b3\(i\,j\)+exb3\(i\,j\)),
61        v\(k\)=(b1\(i\,j\)+exb1\(i\,j\))*(b3\(i\,j\)+exb3\(i\,j\))^2,
62        w\(k\)=(b2\(i\,j\)+exb2\(i\,j\))/sqrt((b1\(i\,j\)+exb1\(i\,j\))^2+
63                (b2\(i\,j\)+exb2\(i\,j\))^2+(b3\(i\,j\)+exb3\(i\,j\))^2),
64        x\(k\)=sqrt((b1\(i\,j\)+exb1\(i\,j\))^2+
65                (b2\(i\,j\)+exb2\(i\,j\))^2+(b3\(i\,j\)+exb3\(i\,j\))^2)
66               *sqrt(b1\(i\,j\)+exb1\(i\,j\))-exp(-hnu/ti\(i\,j\))];
67 sequence_optim_prefix:'optb;
68 sequence_optim_counter:1$
69 sequence_optimize(eqns2);
70 /* Since SAVE_OPTIM_INFO was true, lets look at OPTIM_EQUIVS :  */
71 optim_equivs;
72 /* This optimization will be useful in optimizing the next code
73    sequence, EQNS3. */
74 eqns3:[a1\(k\)=q*b1\(i\,j\)*(exb1\(i\,j\)+b1\(i\,j\))^3
75                -sqrt(t-v\(k\))*exp(-hnu/ti\(i\,j\)),
76        a2\(k\)=-hnu*exp(-hnu/ti\(i\,j\)),
77        a3\(k\)=r^2*b2\(i\,j\)*(exb2\(i\,j\)+b2\(i\,j\))^2*b3\(i\,j\),
78        a4\(k\)=t*rr\(k\)*b3\(i\,j\)^4*(exb3\(i\,j\)+b3\(i\,j\))^4,
79        a5\(k\)=(t-v\(k\))*sqrt((exb1\(i\,j\)+b1\(i\,j\))^2+
80                                (exb2\(i\,j\)+b2\(i\,j\))^2+
81                                (exb3\(i\,j\)+b3\(i\,j\))^2)
82                          /sqrt(b1\(i\,j\)*b2\(i\,j\)*b3\(i\,j\)),
83         a6\(k\)=hnu*ti\(i\,j\)^(t-v\(k\))];
84 /* Before optimizing it, lets change the prefix for intermediate labels. */
85 sequence_optim_prefix:'optim;
86 sequence_optim_counter:1$
87 sequence_optimize(eqns3);
88 /* Notice that SEQUENCE_OPTIMIZE was able to use the multi-level information
89    in OPTIM_EQUIVS("optb6(k)" at top level, with "optb1(k)", etc. embedded one
90    level).  Note also the new value of OPTIM_EQUIVS: */
91 optim_equivs;
92 /* Had it been useful to retain the previous value of OPTIM_EQUIVS, we
93    should have done SAVE_OPTIM_INFO:FALSE$  prior to the last
94    SEQUENCE_OPTIMIZE command.                                         */
95 "end of demo"$