scope.tst 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. % Test SCOPE Package.
  2. % ==================
  3. % NOTE: The SCOPE, GHORNER, GSTRUCTR and GENTRAN packages must be loaded
  4. % to run these tests.
  5. % Further reading: SCOPE 1.5 manual Section 3, example 1;
  6. scope_switches$
  7. % Further reading: SCOPE 1.5 manual Section 3.1, examples 2,3,4 and 5.
  8. on priall$
  9. optimize z:=a^2*b^2+10*a^2*m^6+a^2*m^2+2*a*b*m^4+2*b^2*m^6+b^2*m^2
  10. iname s;
  11. off priall$
  12. on primat,acinfo$
  13. optimize
  14. ghorner <<z:=a^2*b^2+10*a^2*m^6+a^2*m^2+2*a*b*m^4+2*b^2*m^6+b^2*m^2>>
  15. vorder m
  16. iname s;
  17. off exp,primat,acinfo$
  18. q:=a+b$
  19. r:=q+a+b$
  20. optimize x:=a+b,q:=:q^2,p(q)::=:r iname s;
  21. on exp$
  22. clear q,r$
  23. % A similar example follows.
  24. % operator a$% Not necessary. Some differences between REDUCE 3.5 and REDUCE 3.6
  25. % when dealing with indices.
  26. on inputc$
  27. k:=j:=1$
  28. u:=c*x+d$
  29. v:=sin(u)$
  30. optimize {a(k,j):=v*(v^2*cos(u)^2+u),
  31. a(k,j)::=:v*(v^2*cos(u)^2+u)} iname s;
  32. off exp$
  33. optimize {a(k,j):=v*(v^2*cos(u)^2+u),
  34. a(k,j)::=:v*(v^2*cos(u)^2+u)} iname s;
  35. off inputc,period$
  36. optlang fortran$
  37. optimize z:=(6*a+18*b+9*c+3*d+6*j+18*f+6*g+5*h+5*k+3)^13 iname s;
  38. off ftch$
  39. optimize z:=(6*a+18*b+9*c+3*d+6*j+18*f+6*g+5*h+5*k+3)^13 iname s;
  40. optlang c$
  41. optimize z:=(6*a+18*b+9*c+3*d+6*j+18*f+6*g+5*h+5*k+3)^13 iname s;
  42. % Note: C code never contains exponentiations.
  43. on ftch$
  44. optimize {x:=3*a*p,y:=3*a*q,z:=6*a*r+2*b*p,u:=6*a*d+2*b*q,
  45. v:=9*a*c+4*b*d,w:=4*b} iname s;
  46. off ftch$
  47. optlang fortran$
  48. optimize {x:=3*a*p,y:=3*a*q,z:=6*a*r+2*b*p,u:=6*a*d+2*b*q,
  49. v:=9*a*c+4*b*d,w:=4*b} iname s;
  50. on ftch$
  51. setlength 2$
  52. optimize {x:=3*a*p,y:=3*a*q,z:=6*a*r+2*b*p,u:=6*a*d+2*b*q,
  53. v:=9*a*c+4*b*d,w:=4*b} iname s;
  54. resetlength$
  55. optlang nil$
  56. % Further reading: SCOPE 1.5 manual section 3.1, example 9 and section 3.2.
  57. u:=a*x+2*b$
  58. v:=sin(u)$
  59. w:=cos(u)$
  60. f:=v^2*w;
  61. off exp$
  62. optimize f:=:f,g:=:f^2+f iname s$
  63. alst:=aresults;
  64. restorables;
  65. f;
  66. arestore f;
  67. f;
  68. alst;
  69. optimize f:=:f,g:=:f^2+f iname s$
  70. alst:=aresults$
  71. optimize f:=:f,g:=:f^2+f iname s$
  72. restoreall$
  73. f;
  74. % Further reading: SCOPE 1.5 manual section 3.1, example 8.
  75. % See also section 5.
  76. % Also recommended: section 9.
  77. clear a$
  78. matrix a(2,2)$
  79. a(1,1):=x+y+z$
  80. a(1,2):=x*y$
  81. a(2,1):=(x+y)*x*y$
  82. a(2,2):=(x+2*y+3)^3-x$
  83. on exp$
  84. off fort,nat$
  85. optimize detexp:=:det(a) out "expfile" iname s$
  86. off exp$
  87. optimize detnexp:=:det(a) out "nexpfile" iname t$
  88. in expfile$
  89. in nexpfile$
  90. on nat$
  91. detexp-detnexp;
  92. system "rm expfile nexpfile"$
  93. % Further reading: SCOPE 1.5 manual section 4.2, example 15.
  94. % Although the output is similar, it is in general equivalent and
  95. % not identical when using REDUCE 3.6 in stead of REDUCE 3.5. This
  96. % is due to improvements in the simplification strategy.
  97. on acinfo$
  98. optimize
  99. gstructr<<a;aa:=(x+y)^2;b:=(x+y)*(y+z);c:=(x+2*y)*(y+z)*(z+x)^2>>
  100. name v iname s;
  101. alst:=
  102. algopt(algstructr({a,b=(x+y)^2,c=(x+y)*(y+z),d=(x+2*y)*(y+z)*(z+x)^2},v),s);
  103. off acinfo$
  104. % Further reading: SCOPE 1.5 manual section 4.3, example 16.
  105. clear a$
  106. procedure taylor(fx,x,x0,n);
  107. sub(x=x0,fx)+(for k:=1:n sum(sub(x=x0,df(fx,x,k))*(x-x0)^k/factorial(k)))$
  108. hlst:={f1=taylor(e^x,x,0,4),f2=taylor(cos x,x,0,6)}$
  109. on rounded$
  110. hlst:=hlst;
  111. optimize alghorner(hlst,{x}) iname g$
  112. off rounded$
  113. % Further reading: SCOPE 1.5 manual section 3.1, examples 6 and 7.
  114. optimize z:=:for j:=2:6 sum a^(1/j) iname s$
  115. optimize z1:=a+sqrt(sin(a^2+b^2)), z2:=b+sqrt(sin(a^2+b^2)),
  116. z3:=a+b+(a^2+b^2)^(1/2), z4:=sqroot(a^2+b^2)+(a^2+b^2)^3,
  117. z5:=a^2+b^2+cos(a^2+b^2), z6:=(a^2+b^2)^(1/3)+(a^2+b^2)^(1/6)
  118. iname s;
  119. % Further reading: SCOPE 1.5 manual section 6, examples 18 and 19.
  120. optlang fortran$
  121. optimize {x(i+1,i-1):=a(i+1,i-1)+b(i),y(i-1):=a(i-1,i+1)-b(i)} iname s
  122. declare <<x(4),a(4,4),y(5):real;b(5):integer>>;
  123. optlang c$
  124. optimize {x(i+1,i-1):=a(i+1,i-1)+b(i),y(i-1):=a(i-1,i+1)-b(i)} iname s
  125. declare <<x(4),a(4,4),y(5):real;b(5):integer>>;
  126. optlang pascal$
  127. optimize {x(i+1,i-1):=a(i+1,i-1)+b(i),y(i-1):=a(i-1,i+1)-b(i)} iname s
  128. declare <<x(4),a(4,4),y(5):real;b(5):integer>>;
  129. optlang ratfor$
  130. optimize {x(i+1,i-1):=a(i+1,i-1)+b(i),y(i-1):=a(i-1,i+1)-b(i)} iname s
  131. declare <<x(4),a(4,4),y(5):real;b(5):integer>>;
  132. precision 7$
  133. on rounded, double$
  134. optlang fortran$
  135. optimize x1:=2 *a + 10 *b,
  136. x2:=2.00001 *a + 10 *b,
  137. x3:=2 *a + 10.00001 *b,
  138. x4:=6 *a + 10 *b,
  139. x5:=2.0000001 *a + 10.000001 *b
  140. iname s
  141. declare << x1,x2,x3,x4,x5,a,b:real>>$
  142. % Further reading: SCOPE 1.5 manual section 7, example 20.
  143. % Notice the double role of e: In the lhs as identifier. In the rhs as
  144. % exponential function.
  145. % Further notice that a is expected to be declared operator. This is
  146. % due to lower level scope activities.
  147. optimize a(1,x+1) := g + h*r^f,
  148. b(y+1) := a(1,2*x+1)*(g+h*r^f),
  149. c1 := (h*r)/g*a(2,1+x),
  150. c2 := c1*a(1,x+1) + sin(d),
  151. a(1,x+1) := c1^(5/2),
  152. d := b(y+1)*a(1,x+1),
  153. a(1,1+2*x):= (a(1,x+1)*b(y+1)*c)/(d*g^2),
  154. b(y+1) := a(1,1+x)+b(y+1) + sin(d),
  155. a(1,x+1) := b(y+1)*c + h/(g + sin(d)),
  156. d := k*e + d*(a(1,1+x) + 3),
  157. e := d*(a(1,1+x) + 3) + sin(d),
  158. f := d*(3 + a(1,1+x)) + sin(d),
  159. g := d*(3 + a(1,1+x)) + f
  160. iname s
  161. declare << a(5,5),b(7),c,c1,d,e,f,g,h,r:real*8; x,y:integer>>$
  162. % Further reading: SCOPE 1.5 manual section 8, examples 21 and 22.
  163. % Also recommended: section 9.
  164. optlang nil$
  165. delaydecs$
  166. gentran declare <<a,b,c,d,q,w: real>>$
  167. gentran a:=b+c$
  168. gentran d:=b+c$
  169. gentran <<q:=b+c;w:=b+c>>$
  170. makedecs$
  171. on gentranopt$
  172. delaydecs$
  173. gentran declare <<a,b,c,d,q,w: real>>$
  174. gentran a:=b+c$
  175. gentran d:=b+c$
  176. gentran <<q:=b+c;w:=b+c>>$
  177. makedecs$
  178. off gentranopt$
  179. delayopts$
  180. gentran declare <<a,b,c,d,q,w: real>>$
  181. gentran a:=b+c$
  182. gentran d:=b+c$
  183. gentran <<q:=b+c;w:=b+c>>$
  184. makeopts$
  185. delaydecs$
  186. gentran declare <<a,b,c,d,q,w: real>>$
  187. delayopts$
  188. gentran a:=b+c$
  189. gentran d:=b+c$
  190. gentran <<q:=b+c;w:=b+c>>$
  191. makeopts$
  192. makedecs$
  193. clear a,b,c,d,q,w$
  194. matrix a(2,2)$
  195. a:=mat(((b+c)*(c+d),(b+c+2)*(c+d-3)),((c+b-3)*(d+b),(c+b)*(d+b+4)));
  196. gentranlang!*:='c$
  197. delayopts$
  198. gentran aa:=:a$
  199. makeopts$
  200. end;