perm1.red 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. %======================================================
  2. % Name: PERM1 - permutation package
  3. % Author: A.Kryukov (kryukov@theory.npi.msu.su)
  4. % Copyright: (C), 1993-1996, A.Kryukov
  5. % Version: 2.32
  6. % Release: Nov. 12, 1993
  7. % Mar. 28, 1996 PFIND: add error msg.
  8. %======================================================
  9. module perm1$
  10. global '(!*ppacked)$
  11. !*ppacked:=t$
  12. %-------------------------------------------------------
  13. % Generator of permutations.
  14. % Version 1.2.1 Nov. 18, 1994
  15. %
  16. %-------------------------------------------------------
  17. procedure GPerm n$ % order of symmetric group.
  18. % Return all pertmutation of S(n).
  19. begin scalar l$
  20. % if n>9 then rederr list('GPerm,": ",n," is too high order (<=9).")$
  21. while n>0 do << l:=n . l$ n:=n-1 >>$
  22. return for each x in GPerm0 l collect pkp x$
  23. end$
  24. procedure GPerm0(OLst)$
  25. % OLst - list of objects.
  26. % Return - list of permutation of these objects.
  27. if null OLst then nil
  28. else GPerm3(cdr OLst,list list car OLst)$
  29. procedure GPerm3(OList,Res)$
  30. % OList - list of objects,
  31. % Res - list of perm. of objects.
  32. if null OList then Res
  33. else GPerm3(cdr OList,GPerm2(Res,car OList,nil))$
  34. procedure GPerm2(PLst,Obj,Res)$
  35. % Obj - object,
  36. % PLst - permutation list,
  37. % Res - list of perm. included Obj.
  38. if null PLst then Res
  39. else GPerm2(cdr PLst,Obj,GPerm1(Rev(car PLst,nil),Obj,nil,Res))$
  40. procedure GPerm1(L,Obj,R,Res)$
  41. % Obj - object,
  42. % L,R - left(reverse form) and right(direct form) part of
  43. % permutation.
  44. % Res - list of permutation.
  45. if null L then (Obj . R) . Res
  46. else GPerm1(cdr L,Obj,car L . R,Rev(L,Obj . R) . Res)$
  47. procedure Rev(Lst,RLst)$
  48. if null Lst then RLst
  49. else Rev(cdr Lst, car Lst . RLst)$
  50. %-------------------------------------------------------
  51. symbolic procedure mkunitp k$
  52. begin scalar p$
  53. for i:=1:k do p:=i . p$
  54. return pkp reversip p$
  55. end$
  56. symbolic procedure pfind(l1,l2)$
  57. % l1,l2 - (paked) lists of indices.
  58. begin scalar p,z$
  59. integer m$
  60. l1:=unpkp l1$
  61. l2:=unpkp l2$
  62. m:=length l2 + 1$
  63. l2:=for each x in l2 collect x$
  64. for each x in l1 do <<
  65. z:=member(x,l2)$
  66. if null z
  67. then rederr list("PFIND: No index",x,"in",l2)$ %+ AK 28/03/96
  68. p:=(m - length z) . p$
  69. rplaca(z,'nil!*)$
  70. >>$
  71. return pkp reversip p$
  72. end$
  73. symbolic procedure prev(f)$
  74. begin scalar p,w$
  75. integer i,j,l$
  76. f:=unpkp f$
  77. l:=length f$
  78. for i:=1:l do <<
  79. w:=f$
  80. j:=1$
  81. while not(car w = i) do << j:=j+1$ w:=cdr w >>$
  82. p:=j . p$
  83. >>$
  84. return pkp reversip p$
  85. end$
  86. symbolic procedure psign(f)$
  87. begin integer s,i,j,n,k$
  88. scalar new0,new,wnew,f0,wf$
  89. s:=1$
  90. f:=unpkp f$
  91. n:=length f$
  92. f0:=f$
  93. new0:=for each x in f collect t$
  94. new:=new0$
  95. for i:=1:n do <<
  96. if car new then % find cycle contained i
  97. << j:=car f$
  98. while not(j = i) do <<
  99. wnew:=new0$
  100. wf:=f0$
  101. for k:=1:j-1 do << wnew:=cdr wnew$ wf:=cdr wf >>$
  102. rplaca(wnew,nil)$
  103. s:=-s$
  104. j:=car wf$
  105. >>$
  106. >>$
  107. new:=cdr new$
  108. f:=cdr f$
  109. >>$ % for i
  110. return s$
  111. end$
  112. symbolic procedure pmult(f,g)$
  113. begin scalar p,w,ok$
  114. integer i$
  115. f:=unpkp f$
  116. g:=unpkp g$
  117. while g do <<
  118. w:=f$
  119. for i:=1:(car g - 1) do w:=cdr w$
  120. p:=car w . p$
  121. g:=cdr g$
  122. >>$
  123. return pkp reversip p$
  124. end$
  125. symbolic procedure pappl(p,l)$
  126. begin scalar l1,w$
  127. integer i$
  128. p:=unpkp p$
  129. while p do <<
  130. w:=l$
  131. for i:=1:(car p - 1) do w:=cdr w$
  132. l1:=car w . l1$
  133. p:=cdr p$
  134. >>$
  135. return reversip l1$
  136. end$
  137. symbolic procedure pappl0(p1,p2)$
  138. pkp pappl(p1,unpkp p2)$
  139. symbolic procedure pupright(p,d)$
  140. begin scalar w,i,k$
  141. p:=unpkp p$
  142. k:=(length p + 1)$
  143. d:=k+d-1$
  144. for i:=k:d do w:=i . w$
  145. return pkp append(p,reversip w)$
  146. end$
  147. symbolic procedure pupleft(p,d)$
  148. begin scalar w,i$
  149. p:=unpkp p$
  150. p:=for each x in p collect (x+d)$
  151. for i:=1:d do w:=i . w$
  152. return pkp append(reversip w,p)$
  153. end$
  154. symbolic procedure pappend(p1,p2)$
  155. begin scalar l;
  156. p1:=unpkp p1;
  157. l:=length p1;
  158. p2:=unpkp p2;
  159. p2:=for each x in p2 collect (x + l)$
  160. return pkp append(p1,p2)$
  161. end$
  162. %--------------------------------------------------------
  163. global '(diglist!*)$
  164. diglist!*:='((!1 . 1) (!2 . 2) (!3 . 3) (!4 . 4) (!5 . 5)
  165. (!6 . 6) (!7 . 7) (!8 . 8) (!9 . 9) (!0 . 0))$
  166. symbolic procedure dssoc(x,u)$
  167. if null u then nil
  168. else if x=cdar u then car u
  169. else dssoc(x,cdr u)$
  170. %symbolic procedure hugerank()$ 3$
  171. symbolic procedure pkp p$
  172. begin scalar w,huge,z$
  173. if atom p or null !*ppacked then return p$
  174. huge:=(length p >= 10)$
  175. for each x in p do
  176. if huge then <<
  177. if x<10 then w := car dssoc(x,diglist!*) . '!0 . w
  178. else << z:=divide(x,10)$
  179. w := car dssoc(car z,diglist!*) . w$
  180. w := car dssoc(cdr z,diglist!*) . w$
  181. >>$
  182. >>
  183. else w:=car dssoc(x,diglist!*) . w$
  184. return compress reversip w$
  185. end$
  186. symbolic procedure unpkp p$
  187. begin scalar w,huge,z$
  188. if null atom p then return p$
  189. p:=explode p$
  190. huge:=(length p >=10)$
  191. if huge and null evenp length p then p := '!0 . p$
  192. while p do <<
  193. if huge then <<
  194. z:=cdr assoc(car p,diglist!*)$
  195. p:=cdr p$
  196. w:= (z*10+cdr assoc(car p,diglist!*)) . w$
  197. >>
  198. else w:=cdr assoc(car p,diglist!*) . w$
  199. p:=cdr p$
  200. >>$
  201. return reversip w$
  202. end$
  203. symbolic procedure porder p $
  204. length unpkp p$
  205. symbolic procedure hugep p$
  206. <<
  207. p:=unpkp p$
  208. if length p >= 10 then list p else nil
  209. >>$
  210. endmodule;
  211. end;