permute.sl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. % {DSK}PERMUTE.PSL;1 5-FEB-83 15:53:01
  2. (GLISPOBJECTS
  3. (HISTOGRAM (LISTOBJECT (MIN INTEGER)
  4. (MAX INTEGER)
  5. (TOTAL INTEGER)
  6. (COUNTS (LISTOF INTEGER)))
  7. PROP ((PEAKS HISTO-PEAKS))
  8. MSG ((CREATE HISTO-CREATE)
  9. (+ HISTO-ADD)))
  10. (PERMUTATION (LISTOF INTEGER)
  11. PROP ((LENGTH LENGTH)
  12. (INVERSE PERM-INVERSE RESULT PERMUTATION))
  13. MSG ((* COMPOSEBITSHUFFLES RESULT PERMUTATION)))
  14. )
  15. (SETQ PERM3S '((7 3 5 1 6 2 4 0)
  16. (7 5 3 1 6 4 2 0)
  17. (7 3 6 2 5 1 4 0)
  18. (7 5 6 4 3 1 2 0)
  19. (7 6 3 2 5 4 1 0)))
  20. (SETQ FOLD3S '((3 2 1 0 7 6 5 4)
  21. (5 4 7 6 1 0 3 2)
  22. (6 7 4 5 2 3 0 1)))
  23. (SETQ PERM4S '((15 7 11 3 13 5 9 1 14 6 10 2 12 4 8 0)
  24. (15 11 7 3 13 9 5 1 14 10 6 2 12 8 4 0)
  25. (15 7 13 5 11 3 9 1 14 6 12 4 10 2 8 0)
  26. (15 11 13 9 7 3 5 1 14 10 12 8 6 2 4 0)
  27. (15 13 7 5 11 9 3 1 14 12 6 4 10 8 2 0)
  28. (15 13 11 9 7 5 3 1 14 12 10 8 6 4 2 0)
  29. (15 7 11 3 14 6 10 2 13 5 9 1 12 4 8 0)
  30. (15 11 7 3 14 10 6 2 13 9 5 1 12 8 4 0)
  31. (15 7 13 5 14 6 12 4 11 3 9 1 10 2 8 0)
  32. (15 11 13 9 14 10 12 8 7 3 5 1 6 2 4 0)
  33. (15 13 7 5 14 12 6 4 11 9 3 1 10 8 2 0)
  34. (15 13 11 9 14 12 10 8 7 5 3 1 6 4 2 0)
  35. (15 7 14 6 11 3 10 2 13 5 12 4 9 1 8 0)
  36. (15 11 14 10 7 3 6 2 13 9 12 8 5 1 4 0)
  37. (15 7 14 6 13 5 12 4 11 3 10 2 9 1 8 0)
  38. (15 11 14 10 13 9 12 8 7 3 6 2 5 1 4 0)
  39. (15 13 14 12 7 5 6 4 11 9 10 8 3 1 2 0)
  40. (15 13 14 12 11 9 10 8 7 5 6 4 3 1 2 0)
  41. (15 14 7 6 11 10 3 2 13 12 5 4 9 8 1 0)
  42. (15 14 11 10 7 6 3 2 13 12 9 8 5 4 1 0)
  43. (15 14 7 6 13 12 5 4 11 10 3 2 9 8 1 0)
  44. (15 14 11 10 13 12 9 8 7 6 3 2 5 4 1 0)
  45. (15 14 13 12 7 6 5 4 11 10 9 8 3 2 1 0)))
  46. (SETQ FOLD4S '((7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8)
  47. (11 10 9 8 15 14 13 12 3 2 1 0 7 6 5 4)
  48. (13 12 15 14 9 8 11 10 5 4 7 6 1 0 3 2)
  49. (14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1)))
  50. % edited: 27-DEC-82 15:36
  51. % Generate a list of all permutations of length N. The identity
  52. % permutation is always the first member of the list.
  53. (DG ALLPERMS (N:INTEGER)
  54. (RESULT (LISTOF PERMUTATION))
  55. % (SPECVARS LST)
  56. (PROG (LST)
  57. (IF N>5 (ERROR 0 "TOO MANY PERMUTATIONS!"))
  58. (GENPERMS NIL (IDPERM N))
  59. (RETURN LST)))
  60. % edited: 28-DEC-82 11:26
  61. % Convert N to a list of bit values.
  62. (DG BINLIST (N,NBITS:INTEGER)
  63. (RESULT (LISTOF INTEGER))(PROG (L I BIT)
  64. (I_0)
  65. (BIT_1)
  66. (WHILE I<NBITS DO
  67. (L+_ (IF (LOGAND N BIT)
  68. =0 THEN 0 ELSE 1))
  69. (I_+1)
  70. (BIT_+BIT))
  71. (RETURN L)))
  72. % edited: 6-MAY-82 16:33
  73. % Compute a bit-shuffle of the input according to the specification
  74. % list LST. LST gives, for each output bit in order, the input bit
  75. % from which it comes.
  76. (DE BITSHUFFLE (INPUT LST)
  77. (PROG (RES)
  78. (SETQ RES 0)
  79. (MAPC LST (FUNCTION (LAMBDA (X)
  80. (SETQ RES (PLUS (PLUS RES RES)
  81. (COND
  82. ((NULL X)
  83. 0)
  84. ((NOT (NUMBERP X))
  85. 1)
  86. ((ZEROP (LOGAND INPUT
  87. (BITPICK X)))
  88. 0)
  89. (T 1)))))))
  90. (RETURN RES)))
  91. % edited: 23-JUN-82 15:17
  92. % Compose two bitshuffles to produce a single bitshuffle which is
  93. % equivalent.
  94. (DE COMPOSEBITSHUFFLES (FIRST SECOND)
  95. (PROG (L)
  96. (COND ((NOT (EQUAL (SETQ L (LENGTH FIRST))
  97. (LENGTH SECOND)))
  98. (ERROR 0 NIL)))
  99. (RETURN (MAPCAR SECOND (FUNCTION (LAMBDA (X)
  100. (COND
  101. ((FIXP X)
  102. (CAR (PNth FIRST
  103. (DIFFERENCE L X))))
  104. (T X))))))))
  105. % edited: 27-DEC-82 15:44
  106. (DE DOBITSHUFFLE (INT PERM)
  107. (BITSHUFFLE INT PERM))
  108. % edited: 27-DEC-82 15:38
  109. % Generate all permutations consisting of the list PREV followed by
  110. % all permutations of the list L. The permutations which are
  111. % generated are added to the global LST. Called by ALLPERMS.
  112. (DG GENPERMS (PREV,L: (LISTOF INTEGER))
  113. (GLOBAL LST: (LISTOF PERMUTATION))(PROG (I TMP N)
  114. (IF ~L THEN LST+_PREV (RETURN NIL))
  115. (N_ (LENGTH L))
  116. (I_0)
  117. (WHILE (I_+1)
  118. <=N DO
  119. (TMP_ (CAR (PNth L I)))
  120. (GENPERMS (PREV+TMP)
  121. (L - TMP)))))
  122. % edited: 30-DEC-82 13:26
  123. (DG HISTO-ADD (H:HISTOGRAM N:INTEGER)
  124. (IF N>MAX OR N<MIN THEN (ERROR 0 NIL)
  125. ELSE TOTAL_+1 (CAR (PNth COUNTS (N - MIN + 1)))
  126. _+1)H)
  127. % edited: 2-JAN-83 14:14
  128. (DG HISTO-CREATE (H:HISTOGRAM)
  129. (RESULT HISTOGRAM)% Initialize a histogram.
  130. (TOTAL_0)(COUNTS_ (LISTOFC 0 (MAX - MIN + 1)))H)
  131. % edited: 2-JAN-83 14:10
  132. (DG HISTO-PEAKS (H:HISTOGRAM)
  133. (PROG (THRESH L MX N)
  134. (MX_0)
  135. (FOR X IN COUNTS (IF X>MX MX_X))
  136. (THRESH_MX/2)
  137. (N_MIN)
  138. (FOR X IN COUNTS DO (IF X>=THRESH L+_N)
  139. N_+1)
  140. (RETURN (REVERSIP L))))
  141. % edited: 28-DEC-82 11:23
  142. % Produce an identity permutation of length N.
  143. (DG IDPERM (N:INTEGER)
  144. (RESULT PERMUTATION)(PROG (L I)
  145. (SETQ I 0)
  146. (WHILE I<N L+_I I_+1)
  147. (RETURN L)))
  148. % edited: 28-DEC-82 11:23
  149. % Make a list of N copies of the constant C.
  150. (DG LISTOFC (C N:INTEGER)
  151. (RESULT (LISTOF ATOM))(PROG (I L)
  152. (I_0)
  153. (WHILE (I_+1)
  154. <=N DO L+_C)
  155. (RETURN L)))
  156. % edited: 28-DEC-82 11:07
  157. % Log to the base 2 of an integer, rounded up.
  158. (DG LOG2 (N:INTEGER)
  159. (RESULT INTEGER)(PROG (I M)
  160. (SETQ I 0)
  161. (SETQ M 1)
  162. (WHILE M<N DO I_+1 M_+M)
  163. (RETURN I)))
  164. % edited: 28-DEC-82 11:03
  165. % Compute the permutation to be applied to the output of a boolean
  166. % function of N inputs to account for negating the Mth input.
  167. (DG NEGINPPERM (N,M:INTEGER)
  168. (RESULT PERMUTATION)(PROG (TWON TWOM I L)
  169. (SETQ I 0)
  170. (TWON_2^N)
  171. (TWOM_2^M)
  172. (WHILE I<TWON L+_ (IF (LOGAND I TWOM)
  173. ~=0 THEN I - TWOM ELSE I+TWOM)
  174. I_+1)
  175. (RETURN L)))
  176. % edited: 28-DEC-82 11:02
  177. % Create the set of permutations of the set of 2^N outputs
  178. % corresponding to isomorphisms, i.e., renamings of the N inputs of
  179. % a boolean function. The identity isomorphism is omitted.
  180. (DG OUTPERMS (N:INTEGER)
  181. (RESULT (LISTOF PERMUTATION))(PROG (I TMP RES TWON)
  182. (TWON_2^N)
  183. (FOR X IN (CDR (ALLPERMS N))
  184. DO
  185. (I_0)
  186. (TMP_NIL)
  187. (WHILE I<TWON DO
  188. (TMP+_ (DOBITSHUFFLE I X))
  189. (I_+1))
  190. (RES+_TMP))
  191. (RETURN RES)))
  192. % edited: 2-SEP-82 10:47
  193. (DG PERM-INVERSE (P:PERMUTATION)
  194. (RESULT PERMUTATION)% edited: 2-SEP-82 10:44
  195. % Compute the inverse of a permutation.
  196. (PROG (LST N M I J PP TMP)
  197. (SETQ I 0)
  198. (N_P:LENGTH)
  199. (WHILE I<N DO (J _ N - 1)
  200. (PP_P)
  201. (WHILE PP DO (IF (CAR PP)
  202. =I THEN LST+_J PP_NIL ELSE TMP-_PP J_-1
  203. (IF ~PP (ERROR 0 NIL))))
  204. (I_+1))
  205. (RETURN LST)))
  206. (PUT 'BITSHUFFLE
  207. 'GLRESULTTYPE
  208. 'INTEGER)
  209. (PUT 'DOBITSHUFFLE
  210. 'GLRESULTTYPE
  211. 'INTEGER)