reduce.tst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. showtime;
  2. comment some examples of the FOR statement;
  3. comment summing the squares of the even positive integers
  4. through 50;
  5. for i:=2 step 2 until 50 sum i**2;
  6. comment to set w to the factorial of 10;
  7. w := for i:=1:10 product i;
  8. comment alternatively, we could set the elements a(i) of the
  9. array a to the factorial of i by the statements;
  10. array a(10);
  11. a(0):=1$
  12. for i:=1:10 do a(i):=i*a(i-1);
  13. comment the above version of the FOR statement does not return
  14. an algebraic value, but we can now use these array
  15. elements as factorials in expressions, e. g.;
  16. 1+a(5);
  17. comment we could have printed the values of each a(i)
  18. as they were computed by writing the FOR statement as;
  19. for i:=1:10 do write a(i):= i*a(i-1);
  20. comment another way to use factorials would be to introduce an
  21. operator FAC by an integer procedure as follows;
  22. integer procedure fac (n);
  23. begin integer m;
  24. m:=1;
  25. l1: if n=0 then return m;
  26. m:=m*n;
  27. n:=n-1;
  28. go to l1
  29. end;
  30. comment we can now use fac as an operator in expressions, e. g.;
  31. z**2+fac(4)-2*fac 2*y;
  32. comment note in the above example that the parentheses around
  33. the arguments of FAC may be omitted since it is a unary operator;
  34. comment the following examples illustrate the solution of some
  35. complete problems;
  36. comment the f and g series (ref Sconzo, P., Leschack, A. R. and
  37. Tobey, R. G., Astronomical Journal, Vol 70 (May 1965);
  38. deps:= -sig*(mu+2*eps)$
  39. dmu:= -3*mu*sig$
  40. dsig:= eps-2*sig**2$
  41. f1:= 1$
  42. g1:= 0$
  43. for i:= 1:8 do
  44. <<f2:= -mu*g1 + deps*df(f1,eps) + dmu*df(f1,mu) + dsig*df(f1,sig);
  45. write "F(",i,") := ",f2;
  46. g2:= f1 + deps*df(g1,eps) + dmu*df(g1,mu) + dsig*df(g1,sig);
  47. write "G(",i,") := ",g2;
  48. f1:=f2;
  49. g1:=g2>>;
  50. comment a problem in Fourier analysis;
  51. factor cos,sin;
  52. on list;
  53. (a1*cos(wt) + a3*cos(3*wt) + b1*sin(wt) + b3*sin(3*wt))**3
  54. where {cos(~x)*cos(~y) => (cos(x+y)+cos(x-y))/2,
  55. cos(~x)*sin(~y) => (sin(x+y)-sin(x-y))/2,
  56. sin(~x)*sin(~y) => (cos(x-y)-cos(x+y))/2,
  57. cos(~x)**2 => (1+cos(2*x))/2,
  58. sin(~x)**2 => (1-cos(2*x))/2};
  59. remfac cos,sin;
  60. off list;
  61. comment end of Fourier analysis example;
  62. comment the following program, written in collaboration with David
  63. Barton and John Fitch, solves a problem in general relativity. it
  64. will compute the Einstein tensor from any given metric;
  65. on nero;
  66. comment here we introduce the covariant and contravariant metrics;
  67. operator p1,q1,x;
  68. array gg(3,3),h(3,3);
  69. gg(0,0):=e**(q1(x(1)))$
  70. gg(1,1):=-e**(p1(x(1)))$
  71. gg(2,2):=-x(1)**2$
  72. gg(3,3):=-x(1)**2*sin(x(2))**2$
  73. for i:=0:3 do h(i,i):=1/gg(i,i);
  74. comment generate Christoffel symbols and store in arrays cs1 and cs2;
  75. array cs1(3,3,3),cs2(3,3,3);
  76. for i:=0:3 do for j:=i:3 do
  77. <<for k:=0:3 do
  78. cs1(j,i,k) := cs1(i,j,k):=(df(gg(i,k),x(j))+df(gg(j,k),x(i))
  79. -df(gg(i,j),x(k)))/2;
  80. for k:=0:3 do cs2(j,i,k):= cs2(i,j,k) := for p := 0:3
  81. sum h(k,p)*cs1(i,j,p)>>;
  82. comment now compute the Riemann tensor and store in r(i,j,k,l);
  83. array r(3,3,3,3);
  84. for i:=0:3 do for j:=i+1:3 do for k:=i:3 do
  85. for l:=k+1:if k=i then j else 3 do
  86. <<r(j,i,l,k) := r(i,j,k,l) := for q := 0:3
  87. sum gg(i,q)*(df(cs2(k,j,q),x(l))-df(cs2(j,l,q),x(k))
  88. + for p:=0:3 sum (cs2(p,l,q)*cs2(k,j,p)
  89. -cs2(p,k,q)*cs2(l,j,p)));
  90. r(i,j,l,k) := -r(i,j,k,l);
  91. r(j,i,k,l) := -r(i,j,k,l);
  92. if i neq k or j>l
  93. then <<r(k,l,i,j) := r(l,k,j,i) := r(i,j,k,l);
  94. r(l,k,i,j) := -r(i,j,k,l);
  95. r(k,l,j,i) := -r(i,j,k,l)>>>>;
  96. comment now compute and print the Ricci tensor;
  97. array ricci(3,3);
  98. for i:=0:3 do for j:=0:3 do
  99. write ricci(j,i) := ricci(i,j) := for p := 0:3 sum for q := 0:3
  100. sum h(p,q)*r(q,i,p,j);
  101. comment now compute and print the Ricci scalar;
  102. rs := for i:= 0:3 sum for j:= 0:3 sum h(i,j)*ricci(i,j);
  103. comment finally compute and print the Einstein tensor;
  104. array einstein(3,3);
  105. for i:=0:3 do for j:=0:3 do
  106. write einstein(i,j):=ricci(i,j)-rs*gg(i,j)/2;
  107. comment end of Einstein tensor program;
  108. clear gg,h,cs1,cs2,r,ricci,einstein;
  109. comment an example using the matrix facility;
  110. matrix xx,yy,zz;
  111. let xx= mat((a11,a12),(a21,a22)),
  112. yy= mat((y1),(y2));
  113. 2*det xx - 3*w;
  114. zz:= xx**(-1)*yy;
  115. 1/xx**2;
  116. comment end of matrix examples;
  117. comment a physics example;
  118. on div; comment this gives us output in same form as Bjorken and Drell;
  119. mass ki= 0, kf= 0, p1= m, pf= m;
  120. vector ei,ef;
  121. mshell ki,kf,p1,pf;
  122. let p1.ei= 0, p1.ef= 0, p1.pf= m**2+ki.kf, p1.ki= m*k,p1.kf=
  123. m*kp, pf.ei= -kf.ei, pf.ef= ki.ef, pf.ki= m*kp, pf.kf=
  124. m*k, ki.ei= 0, ki.kf= m*(k-kp), kf.ef= 0, ei.ei= -1, ef.ef=
  125. -1;
  126. operator gp;
  127. for all p let gp(p)= g(l,p)+m;
  128. comment this is just to save us a lot of writing;
  129. gp(pf)*(g(l,ef,ei,ki)/(2*ki.p1) + g(l,ei,ef,kf)/(2*kf.p1))
  130. * gp(p1)*(g(l,ki,ei,ef)/(2*ki.p1) + g(l,kf,ef,ei)/(2*kf.p1))$
  131. write "The Compton cross-section is ",ws;
  132. comment end of first physics example;
  133. off div;
  134. comment another physics example;
  135. index ix,iy,iz;
  136. mass p1=mm,p2=mm,p3= mm,p4= mm,k1=0;
  137. mshell p1,p2,p3,p4,k1;
  138. vector qi,q2;
  139. factor mm,p1.p3;
  140. operator ga,gb;
  141. for all p let ga(p)=g(la,p)+mm, gb(p)= g(lb,p)+mm;
  142. ga(-p2)*g(la,ix)*ga(-p4)*g(la,iy)* (gb(p3)*g(lb,ix)*gb(qi)
  143. *g(lb,iz)*gb(p1)*g(lb,iy)*gb(q2)*g(lb,iz) + gb(p3)
  144. *g(lb,iz)*gb(q2)*g(lb,ix)*gb(p1)*g(lb,iz)*gb(qi)*g(lb,iy))$
  145. let qi=p1-k1, q2=p3+k1;
  146. comment it is usually faster to make such substitutions after all the
  147. trace algebra is done;
  148. write "CXN =",ws;
  149. comment end of second physics example;
  150. showtime;
  151. end;