alg.rlg 16 KB

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