sort.f 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. PROGRAM PARTITION
  2. IMPLICIT NONE
  3. INTEGER NMAX
  4. PARAMETER (NMAX = 10000)
  5. INTEGER N, NP, PART (NMAX, NMAX)
  6. INTEGER PATTERNS(NMAX,NMAX)
  7. INTEGER PATT(NMAX), CONF(NMAX)
  8. INTEGER LST(NMAX)
  9. INTEGER NCONFIG ( NMAX ), PCONFIG ( NMAX ), OCC, POW, VAL
  10. INTEGER TAB( NMAX ) , NTAB ( NMAX )
  11. INTEGER CODE, IY(NMAX), CONFCODE
  12. INTEGER CODES ( NMAX ), IPERM( NMAX ), IER
  13. CHARACTER (LEN = 10) STR
  14. INTEGER IARG
  15. INTEGER I,J,K, INC
  16. DOUBLE PRECISION X (NMAX)
  17. INTEGER NFIRM,NOUTLET,NNOUTLET, NM
  18. INTEGER OCCUR
  19. INTEGER ITEN
  20. CHARACTER (LEN=16) FFILE
  21. CHARACTER (LEN=25) FOMT
  22. COMMON /IVAR/ ITEN
  23. CALL GETARG (1, FFILE)
  24. OPEN ( UNIT = 1 , FILE = FFILE )
  25. READ (1,*)
  26. READ (1,*) NFIRM, NOUTLET, NNOUTLET
  27. NM = NFIRM * NOUTLET
  28. C PRINT*, NFIRM, NOUTLET, NM
  29. WRITE(STR,'(I5)') NM
  30. FOMT = "(I5,F8.2,"//ADJUSTR(STR)//"I5)"
  31. C PRINT*, STR,FOMT,NNOUTLET
  32. DO 10, I = 1, NNOUTLET
  33. READ( 1, FOMT )
  34. & K, X(I), (PATTERNS(I,J), J=1,NM)
  35. C PRINT '(I5,F6.2,15I4)', I, X(I), (PATTERNS(I,J), J=1,NM)
  36. C DO 5, J=1, NM
  37. C5 PATT(J) = PATTERNS(I,J)
  38. C DO 6, J = 1, N
  39. C CONF(J) = OCCUR(NM, PATT,J
  40. C PRINT '(I5,A3,9I5)', I," | ",(PATTERNS(I,J),J=1,NM)
  41. 10 CONTINUE
  42. CLOSE(1)
  43. C get the unique partitions of NM
  44. CALL AUP (NOUTLET, PART, NP)
  45. C PRINT*, "------"
  46. PRINT*, NM, NP, NNOUTLET
  47. DO 20, I = 1, NP
  48. NCONFIG(I) = 0
  49. DO 22, J = 1, NOUTLET
  50. 22 NCONFIG(I) = NCONFIG(I) + PART(I,J) * 10 ** (NOUTLET-J)
  51. C20 PRINT*, (PART(I,J), J=1,NOUTLET), NCONFIG(I)
  52. 20 CONTINUE
  53. C PRINT*, "------"
  54. ITEN = 1
  55. 25 CONTINUE
  56. ITEN = ITEN * 10
  57. IF(ITEN.LT.NP) GOTO 25
  58. PRINT*, ITEN
  59. DO 30, I = 1, NNOUTLET
  60. DO 31, J = 1, NM
  61. 31 LST(J) = PATTERNS(I,J)
  62. DO 32, J = 1, NFIRM
  63. DO 33, K = 1, NOUTLET
  64. OCC = OCCUR(NM,LST,J,K)
  65. TAB(K) = OCC
  66. 33 CONTINUE
  67. NTAB(J) = CODE(NOUTLET, TAB, NCONFIG, NP)
  68. 32 CONTINUE
  69. CALL ISORT ( NTAB, IY, NFIRM, -1)
  70. C PRINT*, "----->",(NTAB(J), J=1, NFIRM)
  71. C PRINT*,
  72. CODES (I) = CONFCODE(NTAB,NFIRM)
  73. C PRINT '(I5,A3,9I5,A3,I7)', I," | ",(PATTERNS(I,J),J=1,NM),
  74. C & " | ", CODES ( I )
  75. C PRINT *
  76. 30 CONTINUE
  77. CALL IPSORT(CODES, NNOUTLET, IPERM, 1, IER)
  78. DO 50, I = 1 , NNOUTLET
  79. PRINT '(I5,F8.2,A3,'//ADJUSTL(STR)//'I5,A3,I7)', I, X(IPERM(I)),
  80. & " | ", (PATTERNS(IPERM(I),J),J=1,NM), " | ", CODES ( IPERM(I) )
  81. 50 CONTINUE
  82. END
  83. FUNCTION CONFCODE ( LST, N )
  84. IMPLICIT NONE
  85. INTEGER CONFCODE
  86. INTEGER N
  87. INTEGER LST(N)
  88. INTEGER I, VAL
  89. INTEGER ITEN
  90. COMMON /IVAR/ ITEN
  91. VAL = 0
  92. DO 10, I = 1, N
  93. 10 VAL = VAL + LST(I) * ITEN ** ( N - I )
  94. CONFCODE = VAL
  95. RETURN
  96. END
  97. FUNCTION CODE ( N, TAB , NCONFIG, NP)
  98. IMPLICIT NONE
  99. INTEGER CODE
  100. INTEGER N, NP
  101. INTEGER TAB ( N ), NCONFIG( NP )
  102. INTEGER I, J, K, VAL, POW
  103. VAL = 0
  104. POW = 0
  105. DO 10, I = 1, N
  106. DO 10, J = 1, TAB(I)
  107. VAL = VAL + I * 10 ** POW
  108. POW = POW + 1
  109. 10 CONTINUE
  110. 11 IF ( VAL.LT.10**(N-1) ) THEN
  111. VAL = 10 * VAL
  112. GOTO 11
  113. ENDIF
  114. I = 0
  115. 20 CONTINUE
  116. I = I + 1
  117. IF ( VAL.NE.NCONFIG(I).AND.I.LE.NP ) GOTO 20
  118. CODE = NP - I + 1
  119. RETURN
  120. END
  121. FUNCTION OCCUR ( N, P, F, K)
  122. C Compute the number of occurence of pattern PAT in
  123. C list P
  124. C INPUT
  125. C N : length of configuration P
  126. C P : the configuration to be examined
  127. C F : the number identifying the firm
  128. C K : the number of adjacent outlets
  129. C OUTPUT
  130. C OCCUR : the number of time the pattern is observed in P
  131. INTEGER OCCUR
  132. INTEGER N, F, K, L
  133. INTEGER P(N), PP(N)
  134. INTEGER BLIST(N), I, J, IDX
  135. INTEGER INC
  136. INC = 0
  137. 1 CONTINUE
  138. CALL PADLIST ( P, PP, N, INC )
  139. INC = INC + 1
  140. IF ( PP(N).EQ.F ) GOTO 1
  141. L = 0
  142. I = 0
  143. 10 CONTINUE
  144. I = I + 1
  145. IF ( PP(I).NE.F.AND.I.LE.N ) GOTO 10
  146. IF(I.GT.N) GOTO 30
  147. J=I
  148. 20 CONTINUE
  149. J = J + 1
  150. IF ( PP(J).EQ.F.AND.J.LT.N ) GOTO 20
  151. IF (J - I .EQ. K ) L = L + 1
  152. I = J
  153. IF( I.LT.N ) GOTO 10
  154. 30 CONTINUE
  155. OCCUR = L
  156. RETURN
  157. END
  158. SUBROUTINE PADLIST ( L1, L2, N, INC )
  159. IMPLICIT NONE
  160. INTEGER N, INC, I
  161. INTEGER L1 ( N ), L2 ( N )
  162. IF (INC.LE.-1) GOTO 50
  163. IF (ABS(INC).GE.N) THEN
  164. PRINT*, "Incorrect value of INC, try smaller value"
  165. GOTO 100
  166. ENDIF
  167. DO 10, I=1, N
  168. IF(I+INC.LE.N) THEN
  169. L2(I+INC) = L1(I)
  170. ELSE
  171. L2(I+INC-N) = L1(I)
  172. ENDIF
  173. 10 CONTINUE
  174. GOTO 100
  175. 50 CONTINUE
  176. DO 11, I=1, N
  177. IF(I+INC.GE.1) THEN
  178. L2(I+INC) = L1(I)
  179. ELSE
  180. L2(I+INC+N) = L1(I)
  181. ENDIF
  182. 11 CONTINUE
  183. 100 CONTINUE
  184. RETURN
  185. END
  186. SUBROUTINE AUP ( N , PART, NP )
  187. IMPLICIT NONE
  188. INTEGER NMAX
  189. PARAMETER (NMAX = 10000)
  190. INTEGER P (NMAX)
  191. INTEGER N, NP
  192. INTEGER PART(NMAX,NMAX)
  193. INTEGER K, I
  194. INTEGER R
  195. INTEGER IT
  196. NP = 0
  197. K = 1
  198. P(K) = N
  199. 1 CONTINUE
  200. NP = NP + 1
  201. DO 5, I=1, K
  202. 5 PART(NP,I) = P(I)
  203. C PRINT*, (P(I), I=1,K)
  204. R = 0
  205. 2 CONTINUE
  206. IF ( K .GE. 1 .AND. P(K).EQ.1 ) THEN
  207. R = R + P(K)
  208. K = K - 1
  209. GOTO 2
  210. ENDIF
  211. IF ( K .LT. 1 ) GOTO 10
  212. P(K) = P(K) - 1
  213. R = R + 1
  214. 3 CONTINUE
  215. IF ( R.GT.P(K) ) THEN
  216. P(K+1) = P(K)
  217. R = R - P(K)
  218. K = K + 1
  219. GOTO 3
  220. ENDIF
  221. P(K+1) = R
  222. K = K + 1
  223. C PRINT* , K, R
  224. GOTO 1
  225. 10 CONTINUE
  226. RETURN
  227. END