pm.rlg 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. Tue Feb 10 12:28:23 2004 run on Linux
  2. *** ~ already defined as operator
  3. % Tests of PM.
  4. % TESTS OF BASIC CONSTRUCTS.
  5. operator f, h$
  6. % A "literal" template.
  7. m(f(a),f(a));
  8. t
  9. % Not literally equal.
  10. m(f(a),f(b));
  11. %Nested operators.
  12. m(f(a,h(b)),f(a,h(b)));
  13. t
  14. % A "generic" template.
  15. m(f(a,b),f(a,?a));
  16. {?a->b}
  17. m(f(a,b),f(?a,?b));
  18. {?a->a,?b->b}
  19. % ??a takes "rest" of arguments.
  20. m(f(a,b),f(??a));
  21. {??a->[a,b]}
  22. % But ?a does not.
  23. m(f(a,b),f(?a));
  24. % Conditional matches.
  25. m(f(a,b),f(?a,?b _=(?a=?b)));
  26. m(f(a,a),f(?a,?b _=(?a=?b)));
  27. {?a->a,?b->a}
  28. % "plus" is symmetric.
  29. m(a+b+c,c+?a+?b);
  30. {?a->a,?b->b}
  31. %It is also associative.
  32. m(a+b+c,c+?a);
  33. {?a->a + b}
  34. % Note the effect of using multi-generic symbol is different.
  35. m(a+b+c,c+??c);
  36. {??c->[a,b]}
  37. %Flag h as associative.
  38. flag('(h),'assoc);
  39. m(h(a,b,d,e),h(?a,d,?b));
  40. {?a->h(a,b),?b->e}
  41. % Substitution tests.
  42. s(f(a,b),f(a,?b)->?b^2);
  43. 2
  44. b
  45. s(a+b,a+b->a*b);
  46. a*b
  47. % "associativity" is used to group a+b+c in to (a+b) + c.
  48. s(a+b+c,a+b->a*b);
  49. a*b + c
  50. % Only substitute top at top level.
  51. s(a+b+f(a+b),a+b->a*b,inf,0);
  52. f(a + b) + a*b
  53. % SIMPLE OPERATOR DEFINITIONS.
  54. % Numerical factorial.
  55. operator nfac$
  56. s(nfac(3),{nfac(0)->1,nfac(?x)->?x*nfac(?x-1)},1);
  57. 3*nfac(2)
  58. s(nfac(3),{nfac(0)->1,nfac(?x)->?x*nfac(?x-1)},2);
  59. 6*nfac(1)
  60. si(nfac(3),{nfac(0)->1,nfac(?x)->?x*nfac(?x-1)});
  61. 6
  62. % General factorial.
  63. operator gamma,fac;
  64. fac(?x _=Natp(?x)) ::- ?x*fac(?x-1);
  65. hold(?x*fac(?x - 1))
  66. fac(0) :- 1;
  67. 1
  68. fac(?x) :- Gamma(?x+1);
  69. gamma(?x + 1)
  70. fac(3);
  71. 6
  72. fac(3/2);
  73. 5
  74. gamma(---)
  75. 2
  76. % Legendre polynomials in ?x of order ?n, ?n a natural number.
  77. operator legp;
  78. legp(?x,0) :- 1;
  79. 1
  80. legp(?x,1) :- ?x;
  81. ?x
  82. legp(?x,?n _=natp(?n))
  83. ::- ((2*?n-1)*?x*legp(?x,?n-1)-(?n-1)*legp(?x,?n-2))/?n;
  84. (2*?n - 1)*?x*legp(?x,?n - 1) - (?n - 1)*legp(?x,?n - 2)
  85. hold(----------------------------------------------------------)
  86. ?n
  87. legp(z,5);
  88. 4 2
  89. z*(63*z - 70*z + 15)
  90. ------------------------
  91. 8
  92. legp(a+b,3);
  93. 3 2 2 3
  94. 5*a + 15*a *b + 15*a*b - 3*a + 5*b - 3*b
  95. ---------------------------------------------
  96. 2
  97. legp(x,y);
  98. legp(x,y)
  99. % TESTS OF EXTENSIONS TO BASIC PATTERN MATCHER.
  100. comment *: MSet[?exprn,?val] or ?exprn ::: ?val
  101. assigns the value ?val to the projection ?exprn in such a way
  102. as to store explicitly each form of ?exprn requested. *;
  103. Nosimp('mset,(t t));
  104. Newtok '((!: !: !: !-) Mset);
  105. infix :::-;
  106. precedence Mset,RSetd;
  107. ?exprn :::- ?val ::- (?exprn ::- (?exprn :- ?val ));
  108. hold(?exprn::-(?exprn:-?val))
  109. scs := sin(?x)^2 + Cos(?x)^2 -> 1;
  110. 2 2
  111. scs := cos(?x) + sin(?x) ->1
  112. % The following pattern substitutes the rule sin^2 + cos^2 into a sum of
  113. % such terms. For 2n terms (ie n sin and n cos) the pattern has a worst
  114. % case complexity of O(n^3).
  115. operator trig,u;
  116. trig(?i) :::- Ap(+, Ar(?i,sin(u(?1))^2+Cos(u(?1))^2));
  117. 2 2
  118. hold(trig(?i):-ap(plus,ar(?i,sin(u(?1)) + cos(u(?1)) )))
  119. if si(trig 1,scs) = 1 then write("Pm ok") else Write("PM failed");
  120. Pm ok
  121. if si(trig 10,scs) = 10 then write("Pm ok") else Write("PM failed");
  122. Pm ok
  123. % The next one takes about 70 seconds on an HP 9000/350, calling UNIFY
  124. % 1927 times.
  125. % if si(trig 50,scs) = 50 then write("Pm ok") else Write("PM failed");
  126. % Hypergeometric Function simplification.
  127. newtok '((!#) !#);
  128. *** # redefined
  129. flag('(#), 'symmetric);
  130. operator #,@,ghg;
  131. xx := ghg(4,3,@(a,b,c,d),@(d,1+a-b,1+a-c),1);
  132. xx := ghg(4,3,@(a,b,c,d),@(d,a - b + 1,a - c + 1),1)
  133. S(xx,sghg(3));
  134. *** sghg declared operator
  135. ghg(4,3,@(a,b,c,d),@(d,a - b + 1,a - c + 1),1)
  136. s(ws,sghg(2));
  137. ghg(4,3,@(a,b,c,d),@(d,a - b + 1,a - c + 1),1)
  138. yy := ghg(3,2,@(a-1,b,c/2),@((a+b)/2,c),1);
  139. c a + b
  140. yy := ghg(3,2,@(a - 1,b,---),@(-------,c),1)
  141. 2 2
  142. S(yy,sghg(1));
  143. c a + b
  144. ghg(3,2,@(a - 1,b,---),@(-------,c),1)
  145. 2 2
  146. yy := ghg(3,2,@(a-1,b,c/2),@(a/2+b/2,c),1);
  147. c a + b
  148. yy := ghg(3,2,@(a - 1,b,---),@(-------,c),1)
  149. 2 2
  150. S(yy,sghg(1));
  151. c a + b
  152. ghg(3,2,@(a - 1,b,---),@(-------,c),1)
  153. 2 2
  154. % Some Ghg theorems.
  155. flag('(@), 'symmetric);
  156. % Watson's Theorem.
  157. SGhg(1) := Ghg(3,2,@(?a,?b,?c),@(?d _=?d=(1+?a+?b)/2,?e _=?e=2*?c),1) ->
  158. Gamma(1/2)*Gamma(?c+1/2)*Gamma((1+?a+?b)/2)*Gamma((1-?a-?b)/2+?c)/
  159. (Gamma((1+?a)/2)*Gamma((1+?b)/2)*Gamma((1-?a)/2+?c)
  160. *Gamma((1-?b)/2+?c));
  161. 1 + ?a + ?b
  162. sghg(1) := ghg(3,2,@(?a,?b,?c),@(?d _= ?d=-------------,?e _= ?e=2*?c),1)->(
  163. 2
  164. - ?a - ?b + 2*?c + 1 2*?c + 1
  165. gamma(-----------------------)*gamma(----------)
  166. 2 2
  167. ?a + ?b + 1 1 - ?a + 2*?c + 1
  168. *gamma(-------------)*gamma(---))/(gamma(------------------)
  169. 2 2 2
  170. - ?b + 2*?c + 1 ?a + 1 ?b + 1
  171. *gamma(------------------)*gamma(--------)*gamma(--------))
  172. 2 2 2
  173. % Dixon's theorem.
  174. SGhg(2) := Ghg(3,2,@(?a,?b,?c),@(?d _=?d=1+?a-?b,?e _=?e=1+?a-?c),1) ->
  175. Gamma(1+?a/2)*Gamma(1+?a-?b)*Gamma(1+?a-?c)*Gamma(1+?a/2-?b-?c)/
  176. (Gamma(1+?a)*Gamma(1+?a/2-?b)*Gamma(1+?a/2-?c)*Gamma(1+?a-?b-?c));
  177. sghg(2) := ghg(3,2,@(?a,?b,?c),@(?d _= ?d=1 + ?a - ?b,?e _= ?e=1 + ?a - ?c),1)->
  178. ?a - 2*?b - 2*?c + 2
  179. (gamma(?a - ?b + 1)*gamma(?a - ?c + 1)*gamma(----------------------)
  180. 2
  181. ?a + 2
  182. *gamma(--------))/(gamma(?a - ?b - ?c + 1)*gamma(?a + 1)
  183. 2
  184. ?a - 2*?b + 2 ?a - 2*?c + 2
  185. *gamma(---------------)*gamma(---------------))
  186. 2 2
  187. SGhg(3) := Ghg(?p,?q,@(?a,??b),@(?a,??c),?z)
  188. -> Ghg(?p-1,?q-1,@(??b),@(??c),?z);
  189. sghg(3) :=
  190. ghg(?p,?q,@(??b,?a),@(??c,?a),?z)->ghg(?p - 1,?q - 1,@(??b),@(??c),?z)
  191. SGhg(9) := Ghg(1,0,@(?a),?b,?z ) -> (1-?z)^(-?a);
  192. 1
  193. sghg(9) := ghg(1,0,@(?a),?b,?z)->---------------
  194. ?a
  195. ( - ?z + 1)
  196. SGhg(10) := Ghg(0,0,?a,?b,?z) -> E^?z;
  197. ?z
  198. sghg(10) := ghg(0,0,?a,?b,?z)->e
  199. SGhg(11) := Ghg(?p,?q,@(??t),@(??b),0) -> 1;
  200. sghg(11) := ghg(?p,?q,@(??t),@(??b),0)->1
  201. % If one of the bottom parameters is zero or a negative integer the
  202. % hypergeometric functions may be singular, so the presence of a
  203. % functions of this type causes a warning message to be printed.
  204. % Note it seems to have an off by one level spec., so this may need
  205. % changing in future.
  206. %
  207. % Reference: AS 15.1; Slater, Generalized Hypergeometric Functions,
  208. % Cambridge University Press,1966.
  209. s(Ghg(3,2,@(a,b,c),@(b,c),z),SGhg(3));
  210. ghg(2,1,@(a,b),@(b),z)
  211. si(Ghg(3,2,@(a,b,c),@(b,c),z),{SGhg(3),Sghg(9)});
  212. 1
  213. -------------
  214. a
  215. ( - z + 1)
  216. S(Ghg(3,2,@(a-1,b,c),@(a-b,a-c),1),sghg 2);
  217. a - 2*b - 2*c + 1 a + 1
  218. gamma(a - b)*gamma(a - c)*gamma(-------------------)*gamma(-------)
  219. 2 2
  220. ---------------------------------------------------------------------
  221. a - 2*b + 1 a - 2*c + 1
  222. gamma(a - b - c)*gamma(-------------)*gamma(-------------)*gamma(a)
  223. 2 2
  224. end;
  225. Time for test: 30 ms