alg.rlg 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. Sun Aug 18 15:48:44 2002 run on Windows
  2. Comment This is a standard test file for REDUCE that has been used for
  3. many years. It only tests a limited number of facilities in the
  4. current system. In particular, it does not test floating point
  5. arithmetic, or any of the more advanced packages that have been made
  6. available since REDUCE 3.0 was released. It does however test more
  7. than just the alg package in which it is now stored. It has been used
  8. for a long time to benchmark the performance of REDUCE. A description
  9. of this benchmarking with statistics for REDUCE 3.2 was reported in Jed
  10. B. Marti and Anthony C. Hearn, "REDUCE as a Lisp Benchmark", SIGSAM
  11. Bull. 19 (1985) 8-16. That paper also gives information on the the
  12. parts of the system exercised by the test file. Updated statistics may
  13. be found in the "timings" file in the REDUCE Network Library;
  14. showtime;
  15. Time: 10 ms
  16. comment some examples of the FOR statement;
  17. comment summing the squares of the even positive integers
  18. through 50;
  19. for i:=2 step 2 until 50 sum i**2;
  20. 22100
  21. comment to set w to the factorial of 10;
  22. w := for i:=1:10 product i;
  23. w := 3628800
  24. comment alternatively, we could set the elements a(i) of the
  25. array a to the factorial of i by the statements;
  26. array a(10);
  27. a(0):=1$
  28. for i:=1:10 do a(i):=i*a(i-1);
  29. comment the above version of the FOR statement does not return
  30. an algebraic value, but we can now use these array
  31. elements as factorials in expressions, e. g.;
  32. 1+a(5);
  33. 121
  34. comment we could have printed the values of each a(i)
  35. as they were computed by writing the FOR statement as;
  36. for i:=1:10 do write a(i):= i*a(i-1);
  37. a(1) := 1
  38. a(2) := 2
  39. a(3) := 6
  40. a(4) := 24
  41. a(5) := 120
  42. a(6) := 720
  43. a(7) := 5040
  44. a(8) := 40320
  45. a(9) := 362880
  46. a(10) := 3628800
  47. comment another way to use factorials would be to introduce an
  48. operator FAC by an integer procedure as follows;
  49. integer procedure fac (n);
  50. begin integer m;
  51. m:=1;
  52. l1: if n=0 then return m;
  53. m:=m*n;
  54. n:=n-1;
  55. go to l1
  56. end;
  57. fac
  58. comment we can now use fac as an operator in expressions, e. g.;
  59. z**2+fac(4)-2*fac 2*y;
  60. 2
  61. - 4*y + z + 24
  62. comment note in the above example that the parentheses around
  63. the arguments of FAC may be omitted since it is a unary operator;
  64. comment the following examples illustrate the solution of some
  65. complete problems;
  66. comment the f and g series (ref Sconzo, P., Leschack, A. R. and
  67. Tobey, R. G., Astronomical Journal, Vol 70 (May 1965);
  68. deps:= -sigma*(mu+2*epsilon)$
  69. dmu:= -3*mu*sigma$
  70. dsig:= epsilon-2*sigma**2$
  71. f1:= 1$
  72. g1:= 0$
  73. for i:= 1:8 do
  74. <<f2:=-mu*g1 + deps*df(f1,epsilon) + dmu*df(f1,mu) + dsig*df(f1,sigma);
  75. write "F(",i,") := ",f2;
  76. g2:= f1 + deps*df(g1,epsilon) + dmu*df(g1,mu) + dsig*df(g1,sigma);
  77. write "G(",i,") := ",g2;
  78. f1:=f2;
  79. g1:=g2>>;
  80. F(1) := 0
  81. G(1) := 1
  82. F(2) := - mu
  83. G(2) := 0
  84. F(3) := 3*mu*sigma
  85. G(3) := - mu
  86. 2
  87. F(4) := mu*(3*epsilon + mu - 15*sigma )
  88. G(4) := 6*mu*sigma
  89. 2
  90. F(5) := 15*mu*sigma*( - 3*epsilon - mu + 7*sigma )
  91. 2
  92. G(5) := mu*(9*epsilon + mu - 45*sigma )
  93. 2 2 2
  94. F(6) := mu*( - 45*epsilon - 24*epsilon*mu + 630*epsilon*sigma - mu
  95. 2 4
  96. + 210*mu*sigma - 945*sigma )
  97. 2
  98. G(6) := 30*mu*sigma*( - 6*epsilon - mu + 14*sigma )
  99. 2 2 2
  100. F(7) := 63*mu*sigma*(25*epsilon + 14*epsilon*mu - 150*epsilon*sigma + mu
  101. 2 4
  102. - 50*mu*sigma + 165*sigma )
  103. 2 2 2
  104. G(7) := mu*( - 225*epsilon - 54*epsilon*mu + 3150*epsilon*sigma - mu
  105. 2 4
  106. + 630*mu*sigma - 4725*sigma )
  107. 3 2 2 2
  108. F(8) := mu*(1575*epsilon + 1107*epsilon *mu - 42525*epsilon *sigma
  109. 2 2 4
  110. + 117*epsilon*mu - 24570*epsilon*mu*sigma + 155925*epsilon*sigma
  111. 3 2 2 4 6
  112. + mu - 2205*mu *sigma + 51975*mu*sigma - 135135*sigma )
  113. 2 2 2
  114. G(8) := 126*mu*sigma*(75*epsilon + 24*epsilon*mu - 450*epsilon*sigma + mu
  115. 2 4
  116. - 100*mu*sigma + 495*sigma )
  117. comment a problem in Fourier analysis;
  118. factor cos,sin;
  119. on list;
  120. (a1*cos(omega*t) + a3*cos(3*omega*t) + b1*sin(omega*t)
  121. + b3*sin(3*omega*t))**3
  122. where {cos(~x)*cos(~y) => (cos(x+y)+cos(x-y))/2,
  123. cos(~x)*sin(~y) => (sin(x+y)-sin(x-y))/2,
  124. sin(~x)*sin(~y) => (cos(x-y)-cos(x+y))/2,
  125. cos(~x)**2 => (1+cos(2*x))/2,
  126. sin(~x)**2 => (1-cos(2*x))/2};
  127. 3
  128. (3*cos(omega*t)*(a1
  129. 2
  130. +a1 *a3
  131. 2
  132. +2*a1*a3
  133. 2
  134. +a1*b1
  135. +2*a1*b1*b3
  136. 2
  137. +2*a1*b3
  138. 2
  139. -a3*b1 )
  140. 2
  141. +cos(9*omega*t)*a3*(a3
  142. 2
  143. -3*b3 )
  144. 2
  145. +3*cos(7*omega*t)*(a1*a3
  146. 2
  147. -a1*b3
  148. -2*a3*b1*b3)
  149. 2
  150. +3*cos(5*omega*t)*(a1 *a3
  151. 2
  152. +a1*a3
  153. -2*a1*b1*b3
  154. 2
  155. -a1*b3
  156. 2
  157. -a3*b1
  158. +2*a3*b1*b3)
  159. 3
  160. +cos(3*omega*t)*(a1
  161. 2
  162. +6*a1 *a3
  163. 2
  164. -3*a1*b1
  165. 3
  166. +3*a3
  167. 2
  168. +6*a3*b1
  169. 2
  170. +3*a3*b3 )
  171. 2
  172. +3*sin(omega*t)*(a1 *b1
  173. 2
  174. +a1 *b3
  175. -2*a1*a3*b1
  176. 2
  177. +2*a3 *b1
  178. 3
  179. +b1
  180. 2
  181. -b1 *b3
  182. 2
  183. +2*b1*b3 )
  184. 2
  185. +sin(9*omega*t)*b3*(3*a3
  186. 2
  187. -b3 )
  188. +3*sin(7*omega*t)*(2*a1*a3*b3
  189. 2
  190. +a3 *b1
  191. 2
  192. -b1*b3 )
  193. 2
  194. +3*sin(5*omega*t)*(a1 *b3
  195. +2*a1*a3*b1
  196. +2*a1*a3*b3
  197. 2
  198. -a3 *b1
  199. 2
  200. -b1 *b3
  201. 2
  202. +b1*b3 )
  203. 2
  204. +sin(3*omega*t)*(3*a1 *b1
  205. 2
  206. +6*a1 *b3
  207. 2
  208. +3*a3 *b3
  209. 3
  210. -b1
  211. 2
  212. +6*b1 *b3
  213. 3
  214. +3*b3 ))/4
  215. remfac cos,sin;
  216. off list;
  217. comment end of Fourier analysis example;
  218. comment the following program, written in collaboration with David
  219. Barton and John Fitch, solves a problem in general relativity. it
  220. will compute the Einstein tensor from any given metric;
  221. on nero;
  222. comment here we introduce the covariant and contravariant metrics;
  223. operator p1,q1,x;
  224. array gg(3,3),h(3,3);
  225. gg(0,0):=e**(q1(x(1)))$
  226. gg(1,1):=-e**(p1(x(1)))$
  227. gg(2,2):=-x(1)**2$
  228. gg(3,3):=-x(1)**2*sin(x(2))**2$
  229. for i:=0:3 do h(i,i):=1/gg(i,i);
  230. comment generate Christoffel symbols and store in arrays cs1 and cs2;
  231. array cs1(3,3,3),cs2(3,3,3);
  232. for i:=0:3 do for j:=i:3 do
  233. <<for k:=0:3 do
  234. cs1(j,i,k) := cs1(i,j,k):=(df(gg(i,k),x(j))+df(gg(j,k),x(i))
  235. -df(gg(i,j),x(k)))/2;
  236. for k:=0:3 do cs2(j,i,k):= cs2(i,j,k) := for p := 0:3
  237. sum h(k,p)*cs1(i,j,p)>>;
  238. comment now compute the Riemann tensor and store in r(i,j,k,l);
  239. array r(3,3,3,3);
  240. for i:=0:3 do for j:=i+1:3 do for k:=i:3 do
  241. for l:=k+1:if k=i then j else 3 do
  242. <<r(j,i,l,k) := r(i,j,k,l) := for q := 0:3
  243. sum gg(i,q)*(df(cs2(k,j,q),x(l))-df(cs2(j,l,q),x(k))
  244. + for p:=0:3 sum (cs2(p,l,q)*cs2(k,j,p)
  245. -cs2(p,k,q)*cs2(l,j,p)));
  246. r(i,j,l,k) := -r(i,j,k,l);
  247. r(j,i,k,l) := -r(i,j,k,l);
  248. if i neq k or j>l
  249. then <<r(k,l,i,j) := r(l,k,j,i) := r(i,j,k,l);
  250. r(l,k,i,j) := -r(i,j,k,l);
  251. r(k,l,j,i) := -r(i,j,k,l)>>>>;
  252. comment now compute and print the Ricci tensor;
  253. array ricci(3,3);
  254. for i:=0:3 do for j:=0:3 do
  255. write ricci(j,i) := ricci(i,j) := for p := 0:3 sum for q := 0:3
  256. sum h(p,q)*r(q,i,p,j);
  257. q1(x(1))
  258. ricci(0,0) := ricci(0,0) := (e *(df(p1(x(1)),x(1))*df(q1(x(1)),x(1))*x(1)
  259. 2
  260. - 2*df(q1(x(1)),x(1),2)*x(1) - df(q1(x(1)),x(1)) *x(1)
  261. p1(x(1))
  262. - 4*df(q1(x(1)),x(1))))/(4*e *x(1))
  263. ricci(1,1) := ricci(1,1) := ( - df(p1(x(1)),x(1))*df(q1(x(1)),x(1))*x(1)
  264. 2
  265. - 4*df(p1(x(1)),x(1)) + 2*df(q1(x(1)),x(1),2)*x(1) + df(q1(x(1)),x(1)) *x(1)
  266. )/(4*x(1))
  267. ricci(2,2) := ricci(2,2) :=
  268. p1(x(1))
  269. - df(p1(x(1)),x(1))*x(1) + df(q1(x(1)),x(1))*x(1) - 2*e + 2
  270. ----------------------------------------------------------------------
  271. p1(x(1))
  272. 2*e
  273. 2
  274. ricci(3,3) := ricci(3,3) := (sin(x(2))
  275. p1(x(1))
  276. *( - df(p1(x(1)),x(1))*x(1) + df(q1(x(1)),x(1))*x(1) - 2*e + 2))/(2
  277. p1(x(1))
  278. *e )
  279. comment now compute and print the Ricci scalar;
  280. rs := for i:= 0:3 sum for j:= 0:3 sum h(i,j)*ricci(i,j);
  281. 2
  282. rs := (df(p1(x(1)),x(1))*df(q1(x(1)),x(1))*x(1) + 4*df(p1(x(1)),x(1))*x(1)
  283. 2 2 2
  284. - 2*df(q1(x(1)),x(1),2)*x(1) - df(q1(x(1)),x(1)) *x(1)
  285. p1(x(1)) p1(x(1)) 2
  286. - 4*df(q1(x(1)),x(1))*x(1) + 4*e - 4)/(2*e *x(1) )
  287. comment finally compute and print the Einstein tensor;
  288. array einstein(3,3);
  289. for i:=0:3 do for j:=0:3 do
  290. write einstein(i,j):=ricci(i,j)-rs*gg(i,j)/2;
  291. q1(x(1)) p1(x(1))
  292. e *( - df(p1(x(1)),x(1))*x(1) - e + 1)
  293. einstein(0,0) := -------------------------------------------------------
  294. p1(x(1)) 2
  295. e *x(1)
  296. p1(x(1))
  297. - df(q1(x(1)),x(1))*x(1) + e - 1
  298. einstein(1,1) := -------------------------------------------
  299. 2
  300. x(1)
  301. einstein(2,2) := (x(1)*(df(p1(x(1)),x(1))*df(q1(x(1)),x(1))*x(1)
  302. + 2*df(p1(x(1)),x(1)) - 2*df(q1(x(1)),x(1),2)*x(1)
  303. 2
  304. - df(q1(x(1)),x(1)) *x(1) - 2*df(q1(x(1)),x(1))))/(4
  305. p1(x(1))
  306. *e )
  307. 2
  308. einstein(3,3) := (sin(x(2)) *x(1)*(df(p1(x(1)),x(1))*df(q1(x(1)),x(1))*x(1)
  309. + 2*df(p1(x(1)),x(1)) - 2*df(q1(x(1)),x(1),2)*x(1)
  310. 2
  311. - df(q1(x(1)),x(1)) *x(1) - 2*df(q1(x(1)),x(1))))/(4
  312. p1(x(1))
  313. *e )
  314. comment end of Einstein tensor program;
  315. clear gg,h,cs1,cs2,r,ricci,einstein;
  316. comment an example using the matrix facility;
  317. matrix xx,yy,zz;
  318. let xx= mat((a11,a12),(a21,a22)),
  319. yy= mat((y1),(y2));
  320. 2*det xx - 3*w;
  321. 2*(a11*a22 - a12*a21 - 5443200)
  322. zz:= xx**(-1)*yy;
  323. [ - a12*y2 + a22*y1 ]
  324. [--------------------]
  325. [ a11*a22 - a12*a21 ]
  326. zz := [ ]
  327. [ a11*y2 - a21*y1 ]
  328. [------------------- ]
  329. [ a11*a22 - a12*a21 ]
  330. 1/xx**2;
  331. 2
  332. a12*a21 + a22
  333. mat((-------------------------------------------,
  334. 2 2 2 2
  335. a11 *a22 - 2*a11*a12*a21*a22 + a12 *a21
  336. - a12*(a11 + a22)
  337. -------------------------------------------),
  338. 2 2 2 2
  339. a11 *a22 - 2*a11*a12*a21*a22 + a12 *a21
  340. - a21*(a11 + a22)
  341. (-------------------------------------------,
  342. 2 2 2 2
  343. a11 *a22 - 2*a11*a12*a21*a22 + a12 *a21
  344. 2
  345. a11 + a12*a21
  346. -------------------------------------------))
  347. 2 2 2 2
  348. a11 *a22 - 2*a11*a12*a21*a22 + a12 *a21
  349. comment end of matrix examples;
  350. comment a physics example;
  351. on div;
  352. comment this gives us output in same form as Bjorken and Drell;
  353. mass ki= 0, kf= 0, p1= m, pf= m;
  354. vector eei,ef;
  355. mshell ki,kf,p1,pf;
  356. let p1.eei= 0, p1.ef= 0, p1.pf= m**2+ki.kf, p1.ki= m*k,p1.kf=
  357. m*kp, pf.eei= -kf.eei, pf.ef= ki.ef, pf.ki= m*kp, pf.kf=
  358. m*k, ki.eei= 0, ki.kf= m*(k-kp), kf.ef= 0, eei.eei= -1, ef.ef=
  359. -1;
  360. operator gp;
  361. for all p let gp(p)= g(l,p)+m;
  362. comment this is just to save us a lot of writing;
  363. gp(pf)*(g(l,ef,eei,ki)/(2*ki.p1) + g(l,eei,ef,kf)/(2*kf.p1))
  364. * gp(p1)*(g(l,ki,eei,ef)/(2*ki.p1) + g(l,kf,ef,eei)/(2*kf.p1))$
  365. write "The Compton cross-section is ",ws;
  366. 2 1 -1 1 -1
  367. The Compton cross-section is 2*eei.ef + ---*k*kp + ---*k *kp - 1
  368. 2 2
  369. comment end of first physics example;
  370. off div;
  371. comment another physics example;
  372. index ix,iy,iz;
  373. mass p1=mm,p2=mm,p3= mm,p4= mm,k1=0;
  374. mshell p1,p2,p3,p4,k1;
  375. vector qi,q2;
  376. factor mm,p1.p3;
  377. order mm;
  378. operator ga,gb;
  379. for all p let ga(p)=g(la,p)+mm, gb(p)= g(lb,p)+mm;
  380. ga(-p2)*g(la,ix)*ga(-p4)*g(la,iy)* (gb(p3)*g(lb,ix)*gb(qi)*
  381. g(lb,iz)*gb(p1)*g(lb,iy)*gb(q2)*g(lb,iz) + gb(p3)*
  382. g(lb,iz)*gb(q2)*g(lb,ix)*gb(p1)*g(lb,iz)*gb(qi)*g(lb,iy))$
  383. let qi=p1-k1, q2=p3+k1;
  384. comment it is usually faster to make such substitutions after all the
  385. trace algebra is done;
  386. write "CXN =",ws;
  387. 4 4 2 2
  388. CXN =32*mm *p1.p3 + 8*mm *(k1.p1 - k1.p3) - 16*mm *p1.p3
  389. 2
  390. + 16*mm *p1.p3*( - k1.p1 + k1.p3 - p2.p4)
  391. 2
  392. + 8*mm *( - k1.p1*p2.p4 - 2*k1.p2*k1.p4 + k1.p3*p2.p4) + 8*p1.p3*(
  393. k1.p2*p1.p4 - k1.p2*p3.p4 + k1.p4*p1.p2 - k1.p4*p2.p3 + 2*p1.p2*p3.p4
  394. + 2*p1.p4*p2.p3) + 8*(k1.p1*p1.p2*p3.p4 + k1.p1*p1.p4*p2.p3
  395. + 2*k1.p1*p2.p3*p3.p4 - 2*k1.p3*p1.p2*p1.p4 - k1.p3*p1.p2*p3.p4
  396. - k1.p3*p1.p4*p2.p3)
  397. comment end of second physics example;
  398. showtime;
  399. Time: 1862 ms plus GC time: 210 ms
  400. end;
  401. Time for test: 1982 ms, plus GC time: 210 ms