C
C************************************************************************
C************************************************************************
C*                                                                     **
C*  PERFL_2006.FOR -- PROGRAM COMPUTES CONFIGURATION OF LEGISLATORS    **
C*                  AND CUTTING LINES THAT PERFECTLY CLASSIFY A BINARY **
C*                  CHOICE MATRIX                                      **
C*                                                                     **
C*                 THIS VERSION PERMUTES LEGISLATORS AT END IN         **
C*                 ONE DIMENSION TO INCREASE FIT                       **
C*                                                                     **
C*                           14 APRIL 2008                             **
C*                                                                     **
C************************************************************************
C************************************************************************
C
C *****ARRAY SIZES BELOW*****
C        LEGISLATORS     1899 -- LDATA(.,.) AND KNAME(.,.) ONLY
C        LEGISLATORS     1879 ALL OTHER ARRAYS
C        ROLL CALLS      21011  
C        DUAL USE ARRAYS 21555 > MAX(P,Q)
C        WS(.)           43011 > 2*MAX(P,Q)
C
C
      DIMENSION ZMAT(1879,1879),WVEC(1879),XX(1879),SUMX(1879),
     C          VCOV(1879,1879),BB(25,21011),LLL(21555),YY(1879),
     C          MDATA(1879,21011),FV1(1879),ZPT(21011),XZAP(50),
     C          XJCH(25),XJEH(25),XJCL(25),XJEL(25),
     C          XXX(21555),XXY(1879),UL(1879,1879),V(1879),
     C          XMAT(1879,1879),WVEC2(1879),ZMAT2(1879,1879),
     C          MM(1879),MVOTE(21555),WS(43011),YRANK(1879),
     C          LLV(21555),LLE(21555),LLVB(21555),LLEB(21555),
     C          ZS(21555),
     C          LERROR(1879,21011),ZZINC(1879),XMATSAVE(1879,25),
     C          ZVEC(21011,25),LLEGERR(1879,2),
     C          XPROJ(1879,21011),FV2(1879),XONE(21555),XPT(21555),
     C          MCUTS(21011,2),ZVECSAVE(21011,25),
     C          KCUTTER(21011),LCUTTER(21011),
     C          XTRUE(1879,25),XSAVE(1879,25),
     C          FV3(21011),KKINC(1879),KK50(1879)
      CHARACTER*1 KNAME
      CHARACTER*9 MONTH(100)
      CHARACTER*64 FNAME,FMT,TITLE,FMT2
      INTEGER*2 ITIM1,ITIM2,ITIM3,ITIM4,JTIM1,JTIM2,JTIM3,JTIM4,
     C          IYEAR,IMONTH,IDAY
      COMMON /XXCOM/ LDATA(1899,21011)
      COMMON /MINE/ NS,NRCALL,NP
      COMMON /VOTES/ KAV(21011),KAY(21011),KAN(21011),KNAME(1899,99),JNE
      COMMON /XIME/ NTIME(1200),NITR,MSEC
      COMMON /WORKSP/ RWKSP
      REAL RWKSP(61913)
      OPEN(4,FILE='PERFSTRT_2006.DAT',STATUS='OLD')
      OPEN(21,FILE='PERF21.DAT')
      OPEN(23,FILE='PERF23.DAT')
      OPEN(25,FILE='PERF25.DAT')
C
C
      CALL IWKIN(61913)
C
      CALL GETDAT(IYEAR,IMONTH,IDAY)
      CALL GETTIM(ITIM1,ITIM2,ITIM3,ITIM4)
C
      MONTH(1)= 'JANUARY  '
      MONTH(2)= 'FEBRUARY '
      MONTH(3)= 'MARCH    '
      MONTH(4)= 'APRIL    '
      MONTH(5)= 'MAY      '
      MONTH(6)= 'JUNE     '
      MONTH(7)= 'JULY     '
      MONTH(8)= 'AUGUST   '
      MONTH(9)= 'SEPTEMBER'
      MONTH(10)='OCTOBER  '
      MONTH(11)='NOVEMBER '
      MONTH(12)='DECEMBER '
C
      WRITE(*,209)IDAY,MONTH(IMONTH),IYEAR,ITIM1,ITIM2,ITIM3,ITIM4
      WRITE(21,209)IDAY,MONTH(IMONTH),IYEAR,ITIM1,ITIM2,ITIM3,ITIM4
      WRITE(23,209)IDAY,MONTH(IMONTH),IYEAR,ITIM1,ITIM2,ITIM3,ITIM4
      WRITE(25,209)IDAY,MONTH(IMONTH),IYEAR,ITIM1,ITIM2,ITIM3,ITIM4
C
  209 FORMAT(1X,I2.2,1X,A9,1X,I4,2X,4(I2.2,'.'))
  210 FORMAT(I5,10F12.3)
  301 FORMAT(' COORDINATE MEANS',25F7.3)
  302 FORMAT(I4,25F7.3)
  303 FORMAT(5I4,25F7.3)
  304 FORMAT(1X,78('*'))
  777 FORMAT(5X,2A1,3X,A1,2X,5A1,2X,11A1,1600I1)
 1000 FORMAT(22X,4(I2.2,'.'))
 1001 FORMAT(' ELAPSED TIME OF JOB  ',4(I2.2,'.'))
 1002 FORMAT(8I5)
 1003 FORMAT(7I5,F6.3,I5)
 1007 FORMAT(' RANDOM NUMBER SEED',I10)
 1008 FORMAT(' DATA     ',I6,I7)
 1009 FORMAT(' CLEAN    ',I6,I7)
 1010 FORMAT(' EIGENV   ',I6,I7)
 1012 FORMAT(' DECOMPOSITION OF LEGISLATOR MATRIX',3I4)
 1030 FORMAT(' MAXIMUM SQUARED',F7.3)
 1076 FORMAT(I5,1X,19A1,2I5,25F7.3)
 1077 FORMAT(I5,1X,19A1,2I5,25F8.3)
 1078 FORMAT(2I5,3I4,F7.3,2I2,F10.3)
 1082 FORMAT(A64)
 1087 FORMAT(I3,' ROLL CALLS ',I3,2I8,3F9.5,9X,F9.5)
 1088 FORMAT(3X,' LEGISLATORS',I3,2I8,4F9.5)
 1089 FORMAT(' ERROR LEGISLATORS     ',I3,I4,2I8,4F10.5)
 1091 FORMAT(' INVERSE MATRIX ERROR',I4,I5,F10.4)
 1093 FORMAT(' CLASSIFICATION RCS    ',I3,I4,4I8)
 1094 FORMAT(' ERROR ROLL CALLS      ',I3,I4,2I8,4F10.5)
 1095 FORMAT(' L-PERMUTATIONS',I3,2I8,8F9.5)
 1096 FORMAT(' R-SQUARED BEFORE & AFTER EDITH',F7.3)
 1097 FORMAT(2I5,5I4,2I2,25F7.3)
 1098 FORMAT(' TOTAL CLASSIFICATION ERRORS',I8,F10.4)
 1099 FORMAT(' MACHINE PREC. ',I3,2I8,3F9.5)
 1105 FORMAT(' SHARPEN       ',I3,3I8)
 1106 FORMAT(' L-PERMUTATIONS',I3,2I8,8F7.3)
 1107 FORMAT(' TOTAL NONMISSING AND MISSING OBSERVATIONS',2I8)
 1108 FORMAT(' ERROR ON CLASSIFICATION CHECKS',2I8)
 1109 FORMAT(' ERROR ON CLASSIFICATION CHECKS - LEGS',I3,I4,2I8)
 1111 FORMAT(' ERROR FROM DISC CHECKS',4I8)
 1112 FORMAT(' DISC NOT MAXIMUM!!!!',I4,I5,3F7.3)
 1114 FORMAT(' GRID SEARCH IMPROVEMENT',5I4,90F7.3)
 1115 FORMAT(' ERROR IN LEGISLATOR CLASSIFICATION',I5,2I7)
 1116 FORMAT(' ERROR2 IN LEGISLATOR CLASSIFICATION',I5,2I7)
 1118 FORMAT(3I4,F7.3,3I4)
 1119 FORMAT(' ERROR IN ROLL CALL CLASS. (PRECISION)',I5,5I7)
 1120 FORMAT(4I4,50F7.3)
 1121 FORMAT(4I4,4I2,50F7.3)
 1122 FORMAT(' ERROR ROLL CALLS-SRCH ',I3,I4,2I8,3F10.5)
 1127 FORMAT(' MEAN VOLUME LEG.',7F9.4,I5)
 1128 FORMAT(' MEAN WIGGLE CUTTING PLANES    ',2F10.4)
C
      CALL GETTIM(ITIM1,ITIM2,ITIM3,ITIM4)
      MSEC=ITIM1*60*60+ITIM2*60+ITIM3
      ISEED=1000*ITIM2+100*ITIM2+1000*ITIM3+200*ITIM4
C
C
      CALL RNSET(ISEED)
      WRITE(21,1007)ISEED
C
C
C  CPUTIM RETURNS THE TIME IN SECONDS SINCE THE BEGINNING OF THE PROGRAM
C
      CALL CPUTIM(KTIM1)
C
C
      READ(4,1082)FNAME 
      WRITE(21,1082)FNAME
      WRITE(*,1082)FNAME
      OPEN(40,FILE=FNAME,STATUS='OLD')
      READ(4,'(A)')TITLE
      WRITE(21,'(A)')TITLE
      WRITE(*,'(A)')TITLE
      READ(4,1003)NS,NRCALL,NLAST,JNE,NLEFT,NUP2,KVMIN,XVMIN,NPERMMAX
C
C  ERROR CATCH FOR NLEFT, NUP2, KVMIN, XVMIN, AND NPERMMAX
C
      IF(NLEFT.LE.0)NLEFT=1
      IF(NUP2.LE.0)NUP2=1
      IF(KVMIN.LE.0)KVMIN=10
      IF(XVMIN.LT.0.005)XVMIN=.005
      IF(NPERMMAX.LE.0)NPERMMAX=5
C
      WRITE(21,1003)NS,NRCALL,NLAST,JNE,NLEFT,NUP2,KVMIN,XVMIN,NPERMMAX
      WRITE(*,1003)NS,NRCALL,NLAST,JNE,NLEFT,NUP2,KVMIN,XVMIN,NPERMMAX
      READ(4,'(A)')FMT
      WRITE(21,'(A)')FMT
      WRITE(*,'(A)')FMT
      READ(4,'(A)')FMT2
      WRITE(21,'(A)')FMT2
      WRITE(*,'(A)')FMT2
      WRITE(21,304)
C
      NSMAX=NS
C      XVMIN=0.0250
C      XVMIN=0.005
C      KVMIN=10
C
      DO 1 I=1,1879
      SUMX(I)=0.0
      XTRUE(I,1)=0.0
      DO 2 J=1,1879
      VCOV(I,J)=0.0
  2   CONTINUE
  1   CONTINUE
C
C
C  XVMIN IS THE CUTOFF VALUE FOR MINORITY PERCENTAGE OF THE ROLL CALL (USUALLY
C    .05 OR .025) AND KVMIN IS THE MINIMUM NUMBER OF VOTES FOR A LEGISLATOR
C    TO BE INCLUDED IN THE ANALYSIS
C
C
      NOB=0
C
C  READ IN ROLL CALL DATA FROM UNIT 3 USING FORMAT STATEMENT READ IN
C     ABOVE.  NOTE THAT JNE = # A1'S IN FORMAT STATEMENT
C
C     KNAME DESCRIBES NAME, PARTY, STATE ETC. OF THE LEGISLATOR
C     LDATA IS ROLL CALL DATA.
C
  10  CONTINUE
      READ(40,FMT,END=20)(KNAME(NOB+1,J),J=1,JNE),
     C                   (LDATA(NOB+1,J),J=1,NRCALL)
      NOB=NOB+1
C
      GO TO 10
  20  NP=NOB 
      WRITE(23,1002)NP,NRCALL
      CALL CPUTIM(KTIM1)
      WRITE(*,1008)KTIM1,KTIM1
      LTIME=KTIM1      
C
C  SUBROUTINE CLEAN--THROWS OUT ALL VOTES WITH LESS THAN XVMIN MAJ. AND
C    ALL LEGISLATORS VOTING LESS THAN KVMIN TIMES
C
      CALL CLEAN(XVMIN,KVMIN)
C
C      NRCALL=500
C
      CALL CPUTIM(KTIM1)
      MTIME=KTIM1-LTIME
      WRITE(*,1009)KTIM1,MTIME
      LTIME=KTIM1
C
C  CALL SUBROUTINE SNYDER -- PERFORMS THE HECKMAN-SNYDER DECOMPOSITION
C
      CALL SNYDER(NP,NRCALL,NS,ZMAT,WVEC)
C
      CALL CPUTIM(KTIM1)
      MTIME=KTIM1-LTIME
      WRITE(*,1010)KTIM1,MTIME
      LTIME=KTIM1
C
C  WRITE OUT COORDINATES OF LEGISLATORS
C
      DO 71 K=1,NS
      DO 72 I=1,NP
      XX(I)=ZMAT(I,NP+1-K)
      LLL(I)=I
C
      XMAT(I,K)=XX(I)
C
  72  CONTINUE
C      CALL RSORT(XX,NP,LLL)
C      AAMIN=XX(1)
C      AAMAX=XX(NP)
C      AA=(AAMIN+AAMAX)/2.0
C      BBAA=AAMAX-AA
  71  CONTINUE
      XLEFT=XMAT(NLEFT,1)
      XUP2=XMAT(NUP2,2)
      DO 69 I=1,NP
      IF(XLEFT.GT.0.0)THEN
         XMAT(I,1)=-XMAT(I,1)
         ZMAT(I,NP)=-ZMAT(I,NP)
      ENDIF
      IF(XUP2.LT.0.0)THEN
         XMAT(I,2)=-XMAT(I,2)
         ZMAT(I,NP-1)=-ZMAT(I,NP-1)
      ENDIF
      XONE(I)=XMAT(I,1)
      DO 74 K=1,NS
      SUMX(K)=SUMX(K)+XMAT(I,K)
      DO 75 L=1,NS
      VCOV(K,L)=VCOV(K,L)+XMAT(I,K)*XMAT(I,L)
  75  CONTINUE
  74  CONTINUE
C
  69  CONTINUE
      DO 73 K=1,NS
      SUMX(K)=SUMX(K)/FLOAT(NP)
  73  CONTINUE
      WRITE(*,301)(SUMX(K),K=1,NS)
      DO 76 I=1,NS
C      WRITE(*,302)I,(VCOV(I,J),J=1,NS)
      WRITE(23,302)I,(VCOV(I,J),J=1,NS)
  76  CONTINUE
C
C  FILL IN MISSING MATRIX ENTRIES
C
C  FIND 10 CLOSEST VOTERS
C      
C  IF MISSING VOTE IMPUTE LIKELIEST CHOICE USING NEAREST 5 DISTANCES
C
C
      NMISS=0
      DO 78 JX=1,NRCALL
      DO 788 I=1,NP
      IF(LDATA(I,JX).EQ.0)NMISS=NMISS+1
      MDATA(I,JX)=LDATA(I,JX)
      DO 79 II=1,NP
      SUM=0.0
      DO 80 K=1,2
      SUM=SUM+(ZMAT(I,NP+1-K)-ZMAT(II,NP+1-K))**2
  80  CONTINUE
      FV1(II)=SUM
      LLL(II)=II
  79  CONTINUE
      CALL RSORT(FV1,NP,LLL)
      MYES=0
      MNO=0
      MMISS=0
      DO 81 K=1,10
      IF(LDATA(LLL(K),JX).EQ.0)GO TO 81
      MMISS=MMISS+1
      IF(LDATA(LLL(K),JX).EQ.1)MYES=MYES+1
      IF(LDATA(LLL(K),JX).EQ.6)MNO =MNO +1
      IF(K.GT.5.AND.MMISS.GT.1)GO TO 82
  81  CONTINUE
  82  CONTINUE
      IF(MYES.GE.MNO)MDATA(I,JX)=1
      IF(MYES.LT.MNO)MDATA(I,JX)=6
      IF(LDATA(I,JX).NE.0)MDATA(I,JX)=LDATA(I,JX)
  788 CONTINUE
  78  CONTINUE
      KMISS=NP*NRCALL-NMISS
      WRITE(23,1107)KMISS,NMISS
      WRITE(*,1107)KMISS,NMISS
C
C    RUN EDITH TO GET OPTIMAL CLASSIFICATIONS FOR S=1
C
      CALL EDITH(NP,NRCALL,NS,XMAT,XONE,XPT,ZPT,XCLASS,KTOTC,
     C           KCUTTER,LCUTTER,LERROR,MSUM)
C
C  TOTAL CHOICES IN MINORITY
C
      MPRE=KTOTC-MSUM
C
C
C  COMPUTE R-SQUARE BETWEEN EDITH COORDINATES AND COORDINATES FROM 
C    MATRIX DECOMPOSITION
C
      ASUM=0.0
      BSUM=0.0
      CSUM=0.0
      DSUM=0.0
      ESUM=0.0
      DO 131 I=1,NP
      ASUM=ASUM+XMAT(I,1)
      BSUM=BSUM+XPT(I)
      CSUM=CSUM+XMAT(I,1)**2
      DSUM=DSUM+XPT(I)**2
      ESUM=ESUM+XMAT(I,1)*XPT(I)
C
      XMAT(I,1)=XPT(I)
C      
  131 CONTINUE
      AA=FLOAT(NP)*ESUM-ASUM*BSUM
      AB=FLOAT(NP)*CSUM-ASUM*ASUM
      AC=FLOAT(NP)*DSUM-BSUM*BSUM
      RSQR=(AA*AA)/(AB*AC)
C      WRITE(*,1096)RSQR
C      WRITE(21,1096)RSQR
      WRITE(23,1096)RSQR
      NOISE=66
      IF(NS.EQ.1.AND.NOISE.EQ.66)GO TO 6789
C
C
C
C
C
C ********** BEGINNING OF PERFECT VOTING LOOP **********
C
C
C
C
C      DO 9898 IJKL=2,NSMAX
      DO 9898 IJKL=2,2
      NS=NSMAX+2-IJKL
C
      DO 472 I=1,NP
      DO 471 K=1,NS
      XMAT(I,K)=ZMAT(I,NP+1-K)
  471 CONTINUE
  472 CONTINUE
C
      NSS=NS+1
C      NLAST=25
      DO 9797 IIII=1,NLAST
C
C  NORMALIZE CURRENT ESTIMATES OF LEGISLATORS TO BE WITHIN THE
C   UNIT HYPERSPHERE WITH CENTROID = 0
C
      IF(IIII.EQ.1)THEN
         DO 31 K=1,NS
         SUM=0.0
         DO 30 I=1,NP
  30     SUM=SUM+XMAT(I,K)
         DO 32 I=1,NP
         XMAT(I,K)=XMAT(I,K)-SUM/FLOAT(NP)
  32     CONTINUE
  31     CONTINUE
         BBB=-99.0
         DO 46 I=1,NP
         SUM=0.0
         DO 34 K=1,NS
         SUM=SUM+XMAT(I,K)**2
  34     CONTINUE
         BBB=AMAX1(BBB,SUM)
  46     CONTINUE
C         WRITE(*,1030)BBB
C         WRITE(21,1030)BBB
         WRITE(23,1030)BBB
         DO 35 I=1,NP
         DO 36 K=1,NS
         XMAT(I,K)=XMAT(I,K)*(1.0/SQRT(BBB))
  36     CONTINUE
  35     CONTINUE
      ENDIF
C
C
C    (X'X)
C
      DO 38 K=1,NS
      DO 39 L=1,NS
      SUM=0.0
      DO 40 I=1,NP
      SUM=SUM+XMAT(I,K)*XMAT(I,L)
  40  CONTINUE
      VCOV(K,L)=SUM
  39  CONTINUE
  38  CONTINUE
C
C  EIGENVECTOR-EIGENVALUE DECOMPOSITION OF LEGISLATOR COORDINATES
C
      CALL RS(1879,NS,VCOV,WVEC2,1,ZMAT2,FV1,FV2,IER)
C      WRITE(21,1012)NS,NP,IER
C      WRITE(*,1012)NS,NP,IER
C      DO 37 I=1,NS
C      WRITE(21,210)I,WVEC2(I)
C      WRITE(*,210)I,WVEC2(I)
C  37  CONTINUE
C
C  (X'X)-1
C
      DO 83 I=1,NS
      DO 83 K=1,NS
      SUM=0.0
      DO 84 J=1,NS
      IF(ABS(WVEC2(NS+1-J)).GT..0001)THEN
          SUM=SUM+ZMAT2(K,NS+1-J)*(1.0/WVEC2(NS+1-J))*ZMAT2(I,NS+1-J)
      ENDIF
  84  CONTINUE
  83  UL(I,K)=SUM
C
C  MATRIX INVERSION CHECK  (X'X)-1(X'X) = I
C 
      ASUM=0.0
      DO 933 I=1,NS
      DO 933 J=1,NS
      SUM=0.0
      DO 944 K=1,NS
      SUM=SUM+UL(J,K)*VCOV(K,I)
  944 CONTINUE
      IF(I.EQ.J)ASUM=ASUM+ABS(1.0-SUM)
      IF(I.NE.J)ASUM=ASUM+ABS(SUM)
  933 CONTINUE
      IF(ASUM.GT..01)WRITE(*,1091)IIII,JX,ASUM
      IF(ASUM.GT..01)WRITE(23,1091)IIII,JX,ASUM
C
C  (X'X)-1*X'
C
      DO 85 I=1,NP
      DO 85 J=1,NS
      SUM=0.0
      DO 86 JJ=1,NS
 86   SUM=SUM+UL(J,JJ)*XMAT(I,JJ)
 85   BB(J,I)=SUM
C
C
C
C   ESTIMATE PROJECTION VECTORS
C  
C
C
      KONE=0
      KTWO=0
      KTHREE=0
      KT=0
      KTT=0
      KTTSAVE=0
      KTSAVE=0
      KCHECK=0
      KCHECKX=0
      LXERROR=0
      DO 93 JX=1,NRCALL
      KYES=0
      KNO=0
      KKYES=0
      KKNO=0
      YBAR=0.0
C
C  GET Y-VECTOR -- YES'S AND NO'S SUCH THAT SUM OF Y = 0 -- ELIMINATES
C         THE INTERCEPT TERM -- THE IMPUTED YES/NO FROM ABOVE IS USED
C         FOR MISSING DATA SO THAT THE SAME X MATRIX CAN BE USED FOR
C         EVERY ROLL CALL
C
      DO 92 I=1,NP
      IF(MDATA(I,JX).EQ.1)KYES=KYES+1
      IF(MDATA(I,JX).EQ.6)KNO=KNO+1
      IF(LDATA(I,JX).EQ.1)KKYES=KKYES+1
      IF(LDATA(I,JX).EQ.6)KKNO=KKNO+1
  92  CONTINUE
      DO 110 I=1,NP
      IF(MDATA(I,JX).EQ.1)YY(I)=1.0/FLOAT(KYES)
      IF(MDATA(I,JX).EQ.6)YY(I)=-1.0/FLOAT(KNO)
  110 CONTINUE
C
C  BETA-HAT = V(.) = (X'X)-1*X'Y = A-1*U'Y
C
      DO 102 K=1,NS
      SUM=0.0
      DO 87 J=1,NP
  87  SUM=SUM+BB(K,J)*YY(J)
  102 V(K)=SUM            
C
C  NORMALIZE BETA VECTOR SO ITS OF UNIT LENGTH -- THIS DEFINES A POINT
C    ON THE UNIT HYPERSPHERE SO THAT THE BETA-VECTOR CAN BE USED DIRECTLY
C    TO CALCULATE THE PROJECTION VECTOR Y-HAT BELOW
C
      SUM=0.0
      SUM1=0.0
      DO 103 K=1,NS
      SUM=SUM+V(K)**2
      SUM1=SUM1+ZVEC(JX,K)**2
  103 CONTINUE
C
C  V(NS+.) CONTAINS THE CURRENT ESTIMATE OF THE MAPPING FOR THE PROJECTION
C          VECTOR
C  ZVEC(JX,.) CONTAINS THE FIRST ESTIMATE OF THE MAPPING
C
C
      DO 104 K=1,NS
      V(K+NS)=V(K)/SQRT(SUM)
      IF(IIII.GT.1)ZVEC(JX,K)=ZVEC(JX,K)/SQRT(SUM1)
C
C  SELECT VECTOR MAPPING SUCH THAT IT IS ON THE + HEMISPHERE
C
      IF(V(1).LT.0.0)V(K+NS)=V(K+NS)*(-1.0)
      IF(IIII.EQ.1)THEN
         ZVEC(JX,K)=V(K+NS)
      ENDIF
  104 CONTINUE
C
C
C  Y-HAT = BETA-HAT*Y = X(X'X)-1*X'Y
C
C          THE NORMALIZED PROJECTION VECTOR
C
C
      DO 89 I=1,NP
      SUM=0.0
      DO 90 K=1,NS
      SUM=SUM+XMAT(I,K)*ZVEC(JX,K)
  90  CONTINUE
C
C  SAVE PROJECTION VECTORS -- LEGISLATOR BY ROLL CALL MATRIX
C
      XPROJ(I,JX)=SUM
      XXY(I)=SUM
      LLL(I)=I
      XXX(I)=SUM
      MM(I)=LDATA(I,JX)
      IF(LDATA(I,JX).EQ.0)MM(I)=9
  89  CONTINUE
C
C  SORT PROJECTION VECTOR (Y-HAT)
C
      CALL RSORT(XXX,NP,LLL)
      DO 114 I=1,NP
      MVOTE(I)=MM(LLL(I))
  114 CONTINUE
C
C  
C  CALCULATE CLASSIFICATION ERRORS
C  
C
C  FIND OPTIMAL CUTTING POINT ON THE PROJECTION VECTOR -- THE Y-HAT VECTOR.
C  NOTE THAT BECAUSE BETA HAS BEEN NORMALIZED, THIS LITERALLY IS A PROJECTION
C   OF THE LEGISLATOR CONFIGURATION DOWN ONTO A LINE PASSING THROUGH THE
C   UNIT HYPERSPHERE WHICH EXITS THE HYPERSPHERE AT THE POINTS DEFINED BY
C   THE BETA-VECTOR AND -BETA-VECTOR
C
      JCH=0
      JEH=0
      JCL=0
      JEL=0
      IROTC=0
      CALL JAN1PT(NP,NRCALL,NS,JX,XMAT,XXX,MVOTE,WS,
     C            LLV,LLVB,LLE,LLEB,LERROR,
     C            ZS,JCH,JEH,JCL,JEL,IROTC,KCUT,LCUT,LLL,
     C            XJCH,XJEH,XJCL,XJEL)
C
      IF(JEH+JEL.EQ.0)THEN
         KT=KT+JCH+JEH+JCL+JEL
         KTSAVE=KTSAVE+JCH+JEH+JCL+JEL
         KITTY1=0
         IJUST=0
         GO TO 9377
      ENDIF
C
C
C  SET-UP FOR GRID SEARCH FOR BEST CUTTING LINE 
C
      NCUT=25
C
      CALL SEARCH(IIII,JX,NCUT,NS,NP,NRCALL,KCUT,LCUT,KTT,KT,
     C            XMAT,ZVEC,XPROJ,WS,XXY,
     C            KITTY1,KITTY2,KYES,KNO)
C
C
      KTTSAVE=KTTSAVE+KITTY1
      KTSAVE=KTSAVE+KITTY2
C
 9377 CONTINUE
C
C  STORE DIRECTIONALITY OF ROLL CALL
C
      MCUTS(JX,1)=KCUT
      MCUTS(JX,2)=LCUT
C
C
C  LOCATE ERRORS -- WS(.) CONTAINS THE OPTIMAL CUTTING POINT ON THE
C                   PROJECTION VECTOR -- IT CAN BE USED TO CALCULATE THE
C                   CLASSIFICATION ERRORS
C
      IF(IIII.NE.NLAST)THEN
         KSUM=0
         DO 108 I=1,NP
         LERROR(I,JX)=0
         XXX(I)=XXY(I)
         LLL(I)=I
         FV2(I)=ABS(XXY(I)-WS(JX))
         IF(LDATA(I,JX).EQ.0)GO TO 108
         IF(XXY(I).LT.WS(JX))THEN
            IF(LDATA(I,JX).NE.KCUT)THEN
               LERROR(I,JX)=1
               KCHECK=KCHECK+1
               KSUM=KSUM+1
            ENDIF
         ENDIF
         IF(XXY(I).GT.WS(JX))THEN
            IF(LDATA(I,JX).NE.LCUT)THEN
               LERROR(I,JX)=1
               KCHECK=KCHECK+1
               KSUM=KSUM+1
            ENDIF
         ENDIF
  108    CONTINUE
         JXERROR=KSUM
         IF(JXERROR.NE.KITTY1)THEN
            WRITE(*,1265)JX,KITTY1,KSUM
            STOP
 1265 FORMAT(' BIG TIME ERROR DUDE!',3I6)
         ENDIF
C
         CALL RSORT(FV2,NP,LLL)
         FV3(JX)=FV2(1)+FV2(2)
C
      ENDIF
C
      IF(IIII.EQ.NLAST)THEN
         CALL DISC(IIII,JX,XMAT,V,MDATA,JERROR,XERROR)
         KONE=KONE+JERROR
         DO 105 K=1,NS
         V(K)=V(NS+K)
         V(NS+K)=ZVEC(JX,K)
         FV2(K)=ZVEC(JX,K)
  105    CONTINUE
         CALL DISC(IIII,JX,XMAT,V,MDATA,JERROR,YERROR)
         KTWO=KTWO+JERROR
         JXERROR=JERROR
C         WRITE(38,1002)JX,JERROR,KTWO
C
C  SLIGHT ROTATION OF V
C
         V(3)=V(1)*.9848078 - V(2)*.1736482
         V(4)=V(1)*.1736482 + V(2)*.9848078
         CALL DISC(IIII,JX,XMAT,V,MDATA,JERROR,ZERROR)
         KTHREE=KTHREE+JERROR
         IF(XERROR.LT.YERROR.OR.XERROR.LT.ZERROR)THEN
C            WRITE(*,1112)IIII,JX,XERROR,YERROR,ZERROR
         ENDIF
         DO 508 I=1,NP
         SUMI=0.0
         DO 194 K=1,NS
         SUMI=SUMI+XMAT(I,K)*ZVEC(JX,K)
  194    CONTINUE
         FV2(I)=ABS(SUMI-WS(JX))
         LLL(I)=I
         IF(LDATA(I,JX).EQ.0)GO TO 508
         KCUT=MCUTS(JX,1)
         LCUT=MCUTS(JX,2)
  508    CONTINUE
C
         CALL RSORT(FV2,NP,LLL)
         FV3(JX)=FV2(1)+FV2(2)
C       
      ENDIF
C
C  DO SMALL LOCAL SEARCH TO ESTABLISH PRECISION OF NORMAL VECTOR
C
         NCUT=25
         XINC=.2
         KXERROR=JXERROR
         SAVEWS=WS(JX)
C         DO 930 JJJ=1,20
         DO 930 JJJ=1,1
         CALL RSEARCH(XINC,JX,NCUT,KPCUT,LPCUT,
     C                 XMAT,ZVEC,WS,KDOWN,KEQUAL,KUP,JXERROR,
     C                 WSNEW)
C         WRITE(38,1118)JX,JJJ,JXERROR,XINC,KDOWN,KEQUAL,KUP
C
         IF(JXERROR.EQ.KXERROR)THEN
            WS(JX)=SAVEWS
         ENDIF
C
C  RESET LERROR(,)
C
         IF(JXERROR.LT.KXERROR)THEN
            KTTSAVE=KTTSAVE-KITTY1+JXERROR
            WS(JX)=WSNEW
            SAVEWS=WS(JX)
            MCUTS(JX,1)=KPCUT
            MCUTS(JX,2)=LPCUT
            KCHECK3=0
            DO 191 I=1,NP  
            SUMI=0.0
            DO 192 K=1,NS
            SUMI=SUMI+XMAT(I,K)*ZVEC(JX,K)
  192       CONTINUE
            KCUT=MCUTS(JX,1)
            LCUT=MCUTS(JX,2)
            LERROR(I,JX)=0
            IF(LDATA(I,JX).EQ.0)GO TO 191
            IF(SUMI.LT.WS(JX))THEN
               IF(LDATA(I,JX).NE.KCUT)THEN
                  LERROR(I,JX)=1
                  KCHECK3=KCHECK3+1
               ENDIF
            ENDIF
            IF(SUMI.GT.WS(JX))THEN
               IF(LDATA(I,JX).NE.LCUT)THEN
                  LERROR(I,JX)=1
                  KCHECK3=KCHECK3+1
               ENDIF
            ENDIF
  191       CONTINUE
            WRITE(21,1120)JX,JJJ,JXERROR,KXERROR,(ZVEC(JX,K),K=1,NS),
     C                        (FV2(K),K=1,NS),WS(JX)
            IF(KCHECK3.NE.JXERROR)THEN
               WRITE(23,1119)JX,JXERROR,KCHECK3
C               STOP
            ENDIF
            KXERROR=JXERROR
         ENDIF
         IF(JXERROR.GE.KXERROR)THEN
            KCHECK33=0
            DO 391 I=1,NP  
            SUMI=0.0
            DO 392 K=1,NS
            SUMI=SUMI+XMAT(I,K)*ZVEC(JX,K)
  392       CONTINUE
            KCUT=MCUTS(JX,1)
            LCUT=MCUTS(JX,2)
            LERROR(I,JX)=0
            IF(LDATA(I,JX).EQ.0)GO TO 391
            IF(SUMI.LT.WS(JX))THEN
               IF(LDATA(I,JX).NE.KCUT)THEN
                  LERROR(I,JX)=1
                  KCHECK33=KCHECK33+1
               ENDIF
            ENDIF
            IF(SUMI.GT.WS(JX))THEN
               IF(LDATA(I,JX).NE.LCUT)THEN
                  LERROR(I,JX)=1
                  KCHECK33=KCHECK33+1
               ENDIF
            ENDIF
  391       CONTINUE
         ENDIF
         XINC=XINC-.01
  930    CONTINUE
         LXERROR=LXERROR+KXERROR
C         WRITE(38,1002)JX,JXERROR,KXERROR,LXERROR
C
C  RESET THE ERROR COUNTERS
C
         DO 408 I=1,NP
         LERROR(I,JX)=0
         SUMI=0.0
         DO 193 K=1,NS
         SUMI=SUMI+XMAT(I,K)*ZVEC(JX,K)
  193    CONTINUE
         IF(LDATA(I,JX).EQ.0)GO TO 408
         KCUT=MCUTS(JX,1)
         LCUT=MCUTS(JX,2)
         IF(SUMI.LT.WS(JX))THEN
            IF(LDATA(I,JX).NE.KCUT)THEN
               LERROR(I,JX)=1
               KCHECKX=KCHECKX+1
            ENDIF
         ENDIF
         IF(SUMI.GT.WS(JX))THEN
            IF(LDATA(I,JX).NE.LCUT)THEN
               LERROR(I,JX)=1
               KCHECKX=KCHECKX+1
            ENDIF
         ENDIF
  408    CONTINUE
C
C
  93  CONTINUE
C
C  COMPUTE COSINES BETWEEN ITERATIONS
C
      SUMCOS=0.0
      IF(IIII.GT.1)THEN
         KJK=0
         DO 590 JX=1,NRCALL
         KYESNO=AMIN0(KAY(JX),KAN(JX))
         XMARG=FLOAT(KYESNO)/FLOAT(KAY(JX)+KAN(JX))
         IF(XMARG.GT..10)THEN
            KJK=KJK+1
            SUM=0.0
            DO 589 K=1,NS
            SUM=SUM+ZVEC(JX,K)*ZVECSAVE(JX,K)
  589       CONTINUE
            SUMCOS=SUMCOS+ABS(SUM)
         ENDIF
  590    CONTINUE
         SUMCOS=SUMCOS/FLOAT(KJK)
      ENDIF
      SUM=0.0
      DO 593 JX=1,NRCALL
      DO 592 K=1,NS
      ZVECSAVE(JX,K)=ZVEC(JX,K)
  592 CONTINUE
      SUM=SUM+FV3(JX)
  593 CONTINUE
      SUM=SUM/FLOAT(NRCALL)
      SUM1=0.0
      DO 594 JX=1,NRCALL
      SUM1=SUM1+(FV3(JX)-SUM)**2
  594 CONTINUE
      SUM1=SQRT(SUM1/FLOAT(NRCALL))
C      WRITE(21,1128)SUM,SUM1
      WRITE(23,1128)SUM,SUM1
C      WRITE(*,1128)SUM,SUM1
C
      IF(IIII.EQ.NLAST)THEN
         KTTSAVE=KCHECKX
         WRITE(23,1111)KONE,KTWO,KTHREE,LXERROR
C         WRITE(*,1111)KONE,KTWO,KTHREE,LXERROR
      ENDIF
C
      KT=KTSAVE
      KTT=KTTSAVE
      KCHECK2=0
      DO 281 JX=1,NRCALL
      DO 282 I=1,NP
      KCHECK2=KCHECK2+LERROR(I,JX)
  282 CONTINUE
  281 CONTINUE
      IF(KCHECK2.NE.KCHECKX)THEN
         WRITE(*,1108)KCHECK,KCHECK2
         STOP
      ENDIF
C      WRITE(21,1093)NS,IIII,KCHECK,KCHECKX
      WRITE(23,1093)NS,IIII,KCHECK,KCHECKX
C      WRITE(* ,1093)NS,IIII,KCHECK,KCHECKX
      KCHECK=KCHECKX
      IF(KT.GT.0)THEN
C        XERROR=FLOAT(KTT)/FLOAT(KT)
        XERROR=FLOAT(KCHECK)/FLOAT(KT)
        YERROR=1.0-XERROR
        XPRE=FLOAT(MPRE-KTT)/FLOAT(MPRE)
      ENDIF
      WRITE(21,1087)IIII,NS,KTT,KT,XERROR,YERROR,XPRE,SUMCOS
      WRITE(23,1094)IIII,NS,KTT,KT,XERROR,YERROR,XPRE,SUMCOS
      WRITE(* ,1087)IIII,NS,KTT,KT,XERROR,YERROR,XPRE,SUMCOS
C
C
C
C  FOR EVERY LEGISLATOR, DETERMINE THE POINT OF INTERSECTION OF THE
C    PLANE THROUGH THE NS-1 LEGISLATOR POINT AND THE CUTTING LINE.
C    THIS PRODUCES A VECTOR OF PROJECTED ROLL CALL MIDPOINTS ON A
C    LINE PERPENDICULAR TO THE NS-1 LEGISLATOR POINT.  SEARCH FOR
C    LEGISLATOR COORDINATE ON THE S'TH DIMENSION WHICH MINIMIZES
C    CLASSIFICATION ERRORS
C
      CALL ZVECINV(NRCALL,NS,ZVEC,BB)
C
C
C
      LTOTAL=0
      LWRONG=0
      MWRONG=0
      MMWRONG=0
      DO 111 I=1,NP
      IVOT=I
      CALL KTPXI(IVOT,NRCALL,NS,MCUTS,BB,XMAT,ZVEC,WS,
     C            LERROR,KTOTAL,KWRONG)
      MMWRONG=MMWRONG+KWRONG
      IF(IIII.EQ.NLAST)THEN
C
C  DO SMALL LOCAL SEARCH
C
         XINC=0.2
         DO 799 JJ10=1,20
         CALL STNDERR(IVOT,20,XMAT,ZVEC,MCUTS,WS,KCHECK,XINC,
     C                    KDOWN,KEQUAL,KUP)
         KCHECK2=KCHECK
C         KWRONG=KCHECK
         XINC=XINC-.01
C
C         IF(KCHECK.NE.KWRONG)THEN
C            WRITE(*,1115)IVOT,KWRONG,KCHECK
C            STOP
C         ENDIF
         IF(KCHECK2.LT.KWRONG)THEN
            KWRONG=KCHECK2
C            WRITE(21,1114)IVOT,KDOWN,KHAT,KCHECK,KCHECK2,XINC+.01,
C     C                           (XMAT(IVOT,K),K=1,NS),
C     C                           (XZAP(K),K=1,NS)
C            DO 790 K=1,NS
C            XMAT(IVOT,K)=XZAP(K)
C  790       CONTINUE
C
C  RESET LERROR(,)
C
            KCHECK3=0
            DO 791 JX=1,NRCALL
            SUMI=0.0
            DO 792 K=1,NS
            SUMI=SUMI+XMAT(I,K)*ZVEC(JX,K)
  792       CONTINUE

            KCUT=MCUTS(JX,1)
            LCUT=MCUTS(JX,2)

            LERROR(I,JX)=0
            IF(LDATA(I,JX).EQ.0)GO TO 791
            IF(SUMI.LT.WS(JX))THEN
               IF(LDATA(I,JX).NE.KCUT)THEN
                  LERROR(I,JX)=1
                  KCHECK3=KCHECK3+1
               ENDIF
            ENDIF
            IF(SUMI.GT.WS(JX))THEN
               IF(LDATA(I,JX).NE.LCUT)THEN
                  LERROR(I,JX)=1
                  KCHECK3=KCHECK3+1
               ENDIF
            ENDIF
  791       CONTINUE
            IF(KCHECK3.NE.KWRONG)THEN
               WRITE(*,1116)IVOT,KWRONG,KCHECK3
C               STOP
            ENDIF
         ENDIF
  799    CONTINUE
      ENDIF
      MWRONG=MWRONG+KWRONG
      LTOTAL=LTOTAL+KTOTAL
      LLEGERR(I,1)=0
      LLEGERR(I,2)=KTOTAL
C
  111 CONTINUE
C
C
C  COMPUTE CORRELATION BETWEEN ITERATIONS
C
      SUMCOS=0.0
      SUMCOS1=0.0
      SUMCOS2=0.0
      IF(IIII.GT.1)THEN
         DO 588 I=1,NP
         DO 587 K=1,NS
         SUMCOS=SUMCOS+XMAT(I,K)*XMATSAVE(I,K)
         SUMCOS1=SUMCOS1+XMAT(I,K)**2
         SUMCOS2=SUMCOS2+XMATSAVE(I,K)**2
  587    CONTINUE
  588    CONTINUE
         SUMCOS=SUMCOS/SQRT(SUMCOS1*SUMCOS2)
      ENDIF
      SUM=0.0
      DO 586 I=1,NP
      DO 585 K=1,NS
      XMATSAVE(I,K)=XMAT(I,K)
  585 CONTINUE
  586 CONTINUE
C
      XERROR=FLOAT(MMWRONG)/FLOAT(LTOTAL)
      YERROR=1.0-(FLOAT(MMWRONG)/FLOAT(LTOTAL))
      XPRE=FLOAT(MPRE-MMWRONG)/FLOAT(MPRE)
      WRITE(21,1088)NS,MMWRONG,LTOTAL,XERROR,YERROR,XPRE,SUMCOS
      WRITE(23,1089)IIII,NS,MMWRONG,LTOTAL,XERROR,YERROR,XPRE,SUMCOS
      WRITE(* ,1088)NS,MMWRONG,LTOTAL,XERROR,YERROR,XPRE,SUMCOS
C
      KCHECK3=0
      KCHECK33=0
      DO 881 JX=1,NRCALL
      DO 882 I=1,NP
      KCHECK3=KCHECK3+LERROR(I,JX)
      LLEGERR(I,1)=LLEGERR(I,1)+LERROR(I,JX)
      SUMI=0.0
      DO 892 K=1,NS
      SUMI=SUMI+XMAT(I,K)*ZVEC(JX,K)
  892 CONTINUE
      KCUT=MCUTS(JX,1)
      LCUT=MCUTS(JX,2)
      IF(LDATA(I,JX).EQ.0)GO TO 882
      IF(SUMI.LT.WS(JX))THEN
         IF(LDATA(I,JX).NE.KCUT)THEN
            IF(ABS(SUMI-WS(JX)).GT..001)KCHECK33=KCHECK33+1
         ENDIF
      ENDIF
      IF(SUMI.GT.WS(JX))THEN
         IF(LDATA(I,JX).NE.LCUT)THEN
            IF(ABS(SUMI-WS(JX)).GT..001)KCHECK33=KCHECK33+1
         ENDIF
      ENDIF
  882 CONTINUE
  881 CONTINUE
      WRITE(23,1109)NS,IIII,KCHECK33,MWRONG
C      WRITE(* ,1109)NS,IIII,KCHECK33,MWRONG
C
      IF(KCHECK3.EQ.0)GO TO 9796
C
C
 9797 CONTINUE
C
 9796 CONTINUE
C
C  END OF PERFECT VOTING LOOP CURRENT DIMENSIONALITY
C
 9898 CONTINUE
C
C
C ********** END OF PERFECT VOTING LOOP ALL DIMENSIONS **********
C
C
C  CALCULATE VOLUME FOR LEGISLATORS
C
C
      K50=0
      KCHECKY=0
      DO 785 I=1,NP
      IX=I
      SUM=0.0
      DO 781 K=1,NS
      SUM=SUM+XMAT(I,K)**2
  781 CONTINUE
      SUM=SQRT(SUM)
      FV3(I)=SUM
C
      CALL VOLUME2(IX,XMAT,ZVEC,WS,BMAX,KBMAX)
C
      BPER=FLOAT(LLEGERR(I,2)-LLEGERR(I,1))/FLOAT(LLEGERR(I,2))
      WRITE(25,FMT2)I,(KNAME(I,J),J=1,JNE),LLEGERR(I,1),LLEGERR(I,2),
     C                             BPER,BMAX,(XMAT(I,K),K=1,NS)
      ZZINC(I)=BMAX
      KKINC(I)=KBMAX
      IF(SUM.LT..794)THEN
         K50=K50+1
         KK50(K50)=KBMAX
      ENDIF
C
  785 CONTINUE
C
      ASUM=0.0
      BSUM=0.0
      CSUM=0.0
      DSUM=0.0
      ESUM=0.0
      DO 279 I=1,NP
      AAA=FLOAT(KKINC(I))
      BBB=FV3(I)
      ASUM=ASUM+AAA
      BSUM=BSUM+BBB
      CSUM=CSUM+AAA*AAA
      DSUM=DSUM+BBB*BBB
      ESUM=ESUM+AAA*BBB
  279 CONTINUE
      AAA=FLOAT(NP)*ESUM-ASUM*BSUM
      BBB=FLOAT(NP)*CSUM-ASUM*ASUM
      CCC=FLOAT(NP)*DSUM-BSUM*BSUM
      RRR=AAA/SQRT(BBB*CCC)
C
      KSUM50=0
      DO 780 I=1,K50
      KSUM50=KSUM50+KK50(I)
  780 CONTINUE
      XKSUM50=FLOAT(KSUM50)/FLOAT(K50)
      XXK50=0.0
      DO 779 I=1,K50
      XXK50=XXK50+(FLOAT(KK50(I))-XKSUM50)**2
  779 CONTINUE
      XXK50=SQRT(XXK50/FLOAT(K50))
      SUM=0.0
      KSUM=0
      DO 784 I=1,NP
      SUM=SUM+ZZINC(I)
      KSUM=KSUM+KKINC(I)
  784 CONTINUE
      SUM2=0.0
      SUM=SUM/FLOAT(NP)
      XKSUM=FLOAT(KSUM)/FLOAT(NP)
      XXKSUM=0.0
      DO 783 I=1,NP
      SUM2=SUM2+(ZZINC(I)-SUM)**2
      XXKSUM=XXKSUM+(FLOAT(KKINC(I))-XKSUM)**2
  783 CONTINUE
      SUM2=SQRT(SUM2/FLOAT(NP))
      XXKSUM=SQRT(XXKSUM/FLOAT(NP))
C
C  SUM    = AVERAGE MAXIMUM DISTANCE TO A POLYTOPE BOUNDARY
C  SUM2   = STANDARD DEVIATION OF MAXIMUM DISTANCE TO BOUNDARY
C  XKSUM  = AVERAGE OF MAXIMUM NUMBER ITERATIONS TO GET TO POLYTOPE BOUNDARY
C  XXKSUM = STANDARD DEVIATION OF MAXIMUM NUMBER OF ITERATIONS
C  RRR    = PEARSON CORRELATION OF LEGISLATOR'S DISTANCE FROM CENTER OF 
C            HYPERSPHERE AND THE MAXIMUM NUMBER OF ITERATIONS TO FIND
C            A BOUNDARY
C
      WRITE(21,1127)SUM,SUM2
      WRITE(23,1127)SUM,SUM2,XKSUM,XXKSUM,RRR
      WRITE(*,1127)SUM,SUM2
C      WRITE(21,1127)RRR,SUM,SUM2,XKSUM,XXKSUM,XKSUM50,XXK50,K50
C      WRITE(23,1127)RRR,SUM,SUM2,XKSUM,XXKSUM,XKSUM50,XXK50,K50
C      WRITE(*,1127)RRR,SUM,SUM2,XKSUM,XXKSUM,XKSUM50,XXK50,K50
C
      KCHECK4=0
      DO 441 JX=1,NRCALL
      KCUT=MCUTS(JX,1)
      LCUT=MCUTS(JX,2)
      KSUM=0
      KYES=0
      KNO=0
      KLOW=0
      KHIGH=0
      DO 442 I=1,NP
      IF(LDATA(I,JX).EQ.1)KYES=KYES+1
      IF(LDATA(I,JX).EQ.6)KNO=KNO+1
      IF(LERROR(I,JX).EQ.1)KSUM=KSUM+1
C
C      CALCULATE PREDICTED MARGIN
C
      SUM=0.0
      DO 909 K=1,NS
      SUM=SUM+XMAT(I,K)*ZVEC(JX,K)
  909 CONTINUE
      IF(LDATA(I,JX).EQ.0)GO TO 992
      IF(ABS(SUM-WS(JX)).LE..00001)THEN
         GO TO 992
      ENDIF
      IF(SUM.LT.WS(JX))THEN
         IF(LDATA(I,JX).NE.KCUT)THEN
            KCHECK4=KCHECK4+1
         ENDIF
      ENDIF
      IF(SUM.GT.WS(JX))THEN
         IF(LDATA(I,JX).NE.LCUT)THEN
            KCHECK4=KCHECK4+1
         ENDIF
      ENDIF
  992 CONTINUE
C
      IF(SUM.LT.WS(JX))THEN
         KLOW=KLOW+1
      ENDIF
      IF(SUM.GT.WS(JX))THEN
         KHIGH=KHIGH+1
      ENDIF
  442 CONTINUE
      KYESNO=AMIN0(KYES,KNO)
      XPRE=FLOAT(KYESNO-KSUM)/FLOAT(KYESNO)
C
C      WRITE(25,1097)JX,KAV(JX),KYES,KNO,KSUM,KLOW,KHIGH,
C     C                KCUT,LCUT,XPRE,WS(JX),
C     C                (ZVEC(JX,K),K=1,NS)
  441 CONTINUE
C
      XERROR=FLOAT(KCHECK4)/FLOAT(KTSAVE)
      YERROR=1.0-XERROR
      XPRE=FLOAT(MPRE-KCHECK4)/FLOAT(MPRE)
      WRITE(21,1099)NS,KCHECK4,KTSAVE,XERROR,YERROR,XPRE
      WRITE(23,1099)NS,KCHECK4,KTSAVE,XERROR,YERROR,XPRE
      WRITE(* ,1099)NS,KCHECK4,KTSAVE,XERROR,YERROR,XPRE
C
C  PLACE NORMAL VECTOR IN POSITIVE SIDE OF HYPERSPHERE 
C    AND COMPUTE REDUNDANCY CHECK
C
      KCHECK4=0
      DO 438 JX=1,NRCALL
      KCUT=MCUTS(JX,1)
      LCUT=MCUTS(JX,2)
C
C  CHECK SIGN HERE
C
      IF(ZVEC(JX,1).LT.0.0)THEN
         DO 995 K=1,NS
         ZVEC(JX,K)=-ZVEC(JX,K)
  995    CONTINUE
         WS(JX)=-WS(JX)
         KCUT=MCUTS(JX,2)
         LCUT=MCUTS(JX,1)
         MCUTS(JX,1)=KCUT
         MCUTS(JX,2)=LCUT
      ENDIF
C
      KSUM=0
      KYES=0
      KNO=0
      KLOW=0
      KHIGH=0
      DO 439 I=1,NP
      LERROR(I,JX)=0
      IF(LDATA(I,JX).EQ.1)KYES=KYES+1
      IF(LDATA(I,JX).EQ.6)KNO=KNO+1
C
C      CALCULATE PREDICTED MARGIN
C
      SUM=0.0
      DO 994 K=1,NS
      SUM=SUM+XMAT(I,K)*ZVEC(JX,K)
  994 CONTINUE
      IF(LDATA(I,JX).EQ.0)GO TO 993
      IF(ABS(SUM-WS(JX)).LE..00001)THEN
C         IF(LDATA(I,JX).EQ.KCUT)KTIELOW=KTIELOW+1
C         IF(LDATA(I,JX).EQ.LCUT)KTIEHIGH=KTIEHIGH+1
         GO TO 993
      ENDIF
      IF(SUM.LT.WS(JX))THEN
         IF(LDATA(I,JX).NE.KCUT)THEN
            LERROR(I,JX)=1
            KCHECK4=KCHECK4+1
            KSUM=KSUM+1
         ENDIF
      ENDIF
      IF(SUM.GT.WS(JX))THEN
         IF(LDATA(I,JX).NE.LCUT)THEN
            LERROR(I,JX)=1
            KCHECK4=KCHECK4+1
            KSUM=KSUM+1
         ENDIF
      ENDIF
  993 CONTINUE
C
      IF(SUM.LT.WS(JX))THEN
         KLOW=KLOW+1
      ENDIF
      IF(SUM.GT.WS(JX))THEN
         KHIGH=KHIGH+1
      ENDIF
  439 CONTINUE
      KYESNO=AMIN0(KYES,KNO)
      XPRE=FLOAT(KYESNO-KSUM)/FLOAT(KYESNO)
C
      WRITE(25,1097)JX,KAV(JX),KYES,KNO,KSUM,KLOW,KHIGH,
     C                KCUT,LCUT,XPRE,WS(JX),
     C                (ZVEC(JX,K),K=1,NS)
  438 CONTINUE
C
      XERROR=FLOAT(KCHECK4)/FLOAT(KTSAVE)
      YERROR=1.0-XERROR
      XPRE=FLOAT(MPRE-KCHECK4)/FLOAT(MPRE)
      WRITE(21,1099)NS,KCHECK4,KTSAVE,XERROR,YERROR,XPRE
      WRITE(23,1099)NS,KCHECK4,KTSAVE,XERROR,YERROR,XPRE
      WRITE(* ,1099)NS,KCHECK4,KTSAVE,XERROR,YERROR,XPRE
C
C  CALCULATE R-SQUARE BETWEEN ESTIMATED LEGISLATOR COORDINATES AND
C    TRUE COORDINATES
C
 6789 CONTINUE
      IF(NS.EQ.1)THEN
C
C  DO PERMUTATIONS OF ALL 5-ADJACENT LEGISLATOR BLOCKS TO SHARPEN
C       RANK ORDERING
C
          DO 1515 J=1,NRCALL
          WS(J)=ZPT(J)
 1515     CONTINUE
C
C         NPERMMAX=5
C
         DO 9009 NNPERM=2,NPERMMAX
         CALL SHARPEN(NNPERM,KCUTTER,LCUTTER,XMAT,
     C                 WS,LERROR)
C
C  CHECK ERROR
C
         KINITIAL=0
         KEITHE=0
         DO 19 J=1,NRCALL
C
C  TRANSFER WS(.) TO ZPT(.)
C
         ZPT(J)=WS(J)
         DO 19 I=1,NP
         KINITIAL=KINITIAL+LERROR(I,J)
C
C  CHECK RC # 338
C
         IF(J.EQ.338)WRITE(98,9938)I,J,XMAT(I,1),ZPT(J),LDATA(I,J),
     C                             LERROR(I,J)
 9938 FORMAT(2I4,2F8.3,2I2)
C
C  CHECK ERROR DIRECTLY
C
         IF(LDATA(I,J).EQ.0)GO TO 9933
         IF(XMAT(I,1).LT.WS(J))THEN
            IF(LDATA(I,J).NE.KCUTTER(J))KEITHE=KEITHE+1
         ENDIF
         IF(XMAT(I,1).GT.WS(J))THEN
            IF(LDATA(I,J).NE.LCUTTER(J))KEITHE=KEITHE+1
         ENDIF
 9933    CONTINUE
  19     CONTINUE
C         WRITE(*,1105)NNPERM,KINITIAL,KEITHE,NP*NRCALL-NMISS
C         WRITE(21,1105)NNPERM,KINITIAL,KEITHE,NP*NRCALL-NMISS
         WRITE(*,1105)NNPERM,KINITIAL,NP*NRCALL-NMISS
         WRITE(21,1105)NNPERM,KINITIAL,NP*NRCALL-NMISS
 9009    CONTINUE         
         NNPERM=NPERMMAX
C
C  CALCULATE ERRORS
C
         DO 6999 I=1,NP
         KITOT=0
         KIERR=0
         DO 6998 JX=1,NRCALL
         IF(LDATA(I,JX).NE.0)KITOT=KITOT+1
         IF(LERROR(I,JX).EQ.1)KIERR=KIERR+1
C
C  CHECK FEINGOLD
C
         IF(I.EQ.99)THEN
            IF(LERROR(I,JX).EQ.1)WRITE(99,7668)I,JX,KCUTTER(JX),
     C          LCUTTER(JX),LDATA(I,JX),XMAT(I,1),ZPT(JX)
 7668 FORMAT(2I4,3I2,2F8.3)
         ENDIF
 6998    CONTINUE
         LLEGERR(I,1)=KIERR
         LLEGERR(I,2)=KITOT
 6999    CONTINUE
C
C  RANK ORDER THE LEGISLATORS
C
         DO 6701 I=1,NP
         XXX(I)=XMAT(I,1)
         LLL(I)=I
 6701    CONTINUE
         CALL RSORT(XXX,NP,LLL)
C
         DO 6702 I=1,NP
         YRANK(I)=0.0
         KK=0
         LL=0
         JJ=0
         DO 6703 J=1,NP
         JJ=JJ+1
         IF(ABS(XXX(I)-XXX(J)).LE..00001)THEN
            KK=KK+1
            LL=LL+JJ
         ENDIF
 6703    CONTINUE
         YRANK(I)=FLOAT(LL)/FLOAT(KK)
 6702    CONTINUE
C
C
C  WRITE OUT LEGISLATOR COORDINATES -- SORTED AND UNSORTED BY RANK
C
         DO 887 I=1,NP
         FV1(LLL(I))=YRANK(I)
         BPER=FLOAT(LLEGERR(LLL(I),2)-LLEGERR(LLL(I),1))/
     C                                   FLOAT(LLEGERR(LLL(I),2))
         WRITE(25,FMT2)I,(KNAME(LLL(I),J),J=1,JNE),
     C                   LLEGERR(LLL(I),1),LLEGERR(LLL(I),2),
     C                            BPER,YRANK(I)
  887    CONTINUE
         WRITE(25,304)
         DO 888 I=1,NP
         BPER=FLOAT(LLEGERR(I,2)-LLEGERR(I,1))/
     C                                   FLOAT(LLEGERR(I,2))
         WRITE(25,FMT2)I,(KNAME(I,J),J=1,JNE),
     C                   LLEGERR(I,1),LLEGERR(I,2),
     C                            BPER,FV1(I)
  888    CONTINUE
C
C  WRITE OUT ROLL CALL CLASSIFICATIONS
C
         WRITE(25,304)
         KTSUM=0
         DO 811 J=1,NRCALL
         KSUM=0
         KYES=0
         KNO=0
         DO 8111 I=1,NP
         IF(LDATA(I,J).EQ.1)KYES=KYES+1
         IF(LDATA(I,J).EQ.6)KNO=KNO+1
         IF(LERROR(I,J).EQ.1)KSUM=KSUM+1
 8111    CONTINUE
         NSAVEI=0
         DO 812 I=1,NP
         IF(I.LT.NP)THEN
            IF(ZPT(J).GT.XXX(I).AND.ZPT(J).LT.XXX(I+1))THEN
               ZDOWN=YRANK(I)
               ZUP=YRANK(I+1)
               NSAVEI=I
               GO TO 8122
            ENDIF
         ENDIF
         IF(ZPT(J).LT.XXX(1))THEN
            ZDOWN=YRANK(1)
            ZUP=YRANK(1)
            NSAVEI=I
            GO TO 8122
         ENDIF
         IF(ZPT(J).GT.XXX(NP))THEN
            ZDOWN=YRANK(NP)
            ZUP=YRANK(NP)
            NSAVEI=I
            GO TO 8122
         ENDIF
  812    CONTINUE
 8122    CONTINUE
C
C  TAKE CARE OF TIED RANKS/PRECISION PROBLEMS
C
         IF(NSAVEI.EQ.0)THEN
            DO 8123 I=1,NP
            IF(ABS(XXX(I)-ZPT(J)).LE..00001)THEN
               ZDOWN=YRANK(I)
               ZUP=YRANK(I)
               NSAVEI=I
               GO TO 8124
            ENDIF
 8123       CONTINUE
         ENDIF
 8124    CONTINUE
         IF(J.EQ.338)THEN
         WRITE(99,2047)NSAVEI,J,LDATA(NSAVEI,J),ZPT(J),ZDOWN,ZUP,
     C                 XXX(NSAVEI),XXX(NSAVEI+1),
     C                 YRANK(NSAVEI),YRANK(NSAVEI+1),YRANK(1),YRANK(NP)
 2047    FORMAT(2I5,I2,3F8.3/12X,6F8.3)
         ENDIF
         KTSUM=KTSUM+KSUM
         KYESNO=AMIN0(KYES,KNO)
         XPRE=FLOAT(KYESNO-KSUM)/FLOAT(KYESNO)
C
C  COMPUTE CUTTING POINT ON NORMAL VECTOR
C
C         WRITE(25,1097)J,KAV(J),KYES,KNO,KSUM,XPRE,ZPT(J),ZDOWN,ZUP
         WRITE(25,1078)J,KAV(J),KYES,KNO,KSUM,XPRE,
     C                 KCUTTER(J),LCUTTER(J),(ZDOWN+ZUP)/2.0
         LLL(J)=J
         MDATA(1,J)=KYES
         MDATA(2,J)=KNO
         MDATA(3,J)=KSUM
         XPROJ(1,J)=XPRE
         ZS(J)=(ZDOWN+ZUP)/2.0
  811    CONTINUE
C
         KEITHE=0
         DO 1919 J=1,NRCALL
         DO 1919 I=1,NP
C
C  CHECK ERROR DIRECTLY
C
         IF(LDATA(I,J).EQ.0)GO TO 9944
         IF(FV1(I).LT.ZS(J))THEN
            IF(LDATA(I,J).EQ.LCUTTER(J))KEITHE=KEITHE+1
         ENDIF
         IF(FV1(I).GT.ZS(J))THEN
            IF(LDATA(I,J).EQ.KCUTTER(J))KEITHE=KEITHE+1
         ENDIF
 9944    CONTINUE
 1919    CONTINUE
         WRITE(*,1616)KEITHE
         WRITE(21,1616)KEITHE
 1616    FORMAT(' DIRECT CHECK ON ERROR USING RANKS',I8)
C
         CALL RSORT(ZS,NRCALL,LLL)
         WRITE(25,304)
         DO 814 J=1,NRCALL
         WRITE(25,1078)LLL(J),KAV(LLL(J)),MDATA(1,LLL(J)),
     C                 MDATA(2,LLL(J)),MDATA(3,LLL(J)),
     C                 XPROJ(1,LLL(J)),
     C                 KCUTTER(LLL(J)),LCUTTER(LLL(J)),ZS(J)
  814    CONTINUE
         XHECK=1.0-(FLOAT(KTSUM)/FLOAT(NP*NRCALL-NMISS))
         XPRE=FLOAT(MPRE-KTSUM)/FLOAT(MPRE)
         WRITE(21,1095)NNPERM,KTSUM,
     C                 NP*NRCALL-NMISS,1.0-XHECK,XHECK,XPRE
         WRITE(23,1095)NNPERM,KTSUM,
     C                 NP*NRCALL-NMISS,1.0-XHECK,XHECK,XPRE
         WRITE(*,1106)NNPERM,KTSUM,
     C                 NP*NRCALL-NMISS,1.0-XHECK,XHECK,XPRE
      ENDIF
C
C
      CALL GETTIM(JTIM1,JTIM2,JTIM3,JTIM4)
      WRITE(*,1000)ITIM1,ITIM2,ITIM3,ITIM4
      WRITE(*,1001)JTIM1,JTIM2,JTIM3,JTIM4
      WRITE(21,1000)ITIM1,ITIM2,ITIM3,ITIM4
      WRITE(21,1001)JTIM1,JTIM2,JTIM3,JTIM4
      WRITE(23,1000)ITIM1,ITIM2,ITIM3,ITIM4
      WRITE(23,1001)JTIM1,JTIM2,JTIM3,JTIM4
      STOP
      END
C
C **************************************************************************
C  SUBROUTINE VOLUME2 -- CALCULATES VOLUME OF REGION CONTAINING LEGISLATOR
C                           POINT
C                       
C **************************************************************************
C
      SUBROUTINE VOLUME2(IX,XMAT,ZVEC,WS,BBSAVE,KKSAVE)
      DIMENSION XMAT(1879,1879),XMAT2(21011,33),
     C          XXY(21011),WS(43011),XXX(21011),
     C          ZVEC(21011,25),TVEC(21011,25)
      COMMON /XXCOM/ LDATA(1899,21011)
      COMMON /MINE/ NS,NRCALL,NP
 1000 FORMAT(3I4,20F7.3)
 1001 FORMAT(' MAX ITER',3I5,F6.3,2X,100F7.3)
C
C      CALL GETTIM(ITIM1,ITIM2,ITIM3,ITIM4)
C      ISEED=1000*ITIM2+100*ITIM2+1000*ITIM3+200*ITIM4
C
C      CALL RNSET(ISEED)
C
      KKSAVE=-99 
      BBSAVE=-999.0
C
C PUT POINTS ON CUTTING PLANES CLOSEST TO XI
C
      DO 777 LM=1,100
      SUM=0.0
      DO 59 K=1,NS
C      TVEC(LM,K)=(URAND(ISEED)-.50)
      TVEC(LM,K)=(RNUNF()-.50)
      SUM=SUM+TVEC(LM,K)**2
  59  CONTINUE
      DO 60 K=1,NS
      TVEC(LM,K)=TVEC(LM,K)/SQRT(SUM)
  60  CONTINUE
      XINC=.002
C
      DO 776 KLM=1,500
      SUMBIG=0.0
      DO 61 K=1,NS
      XMAT2(LM,K)=XMAT(IX,K)+XINC*TVEC(LM,K)
      SUMBIG=SUMBIG+XMAT2(LM,K)**2
  61  CONTINUE
C
C  CHECK IF POINT MOVES OUTSIDE HYPERSPHERE
C
      IF(SUMBIG.GE.1.0)GO TO 933
      DO 93 JX=1,NRCALL
      SUM=0.0
      SUM1=0.0
      DO 90 K=1,NS
      SUM= SUM+ XMAT(IX,K)*ZVEC(JX,K)
      SUM1=SUM1+XMAT2(LM,K)*ZVEC(JX,K)
  90  CONTINUE
      XXY(JX)=SUM
      XXX(JX)=SUM1
C
C  CHECK IF POINT MOVES OUTSIDE POLYTOPE
C
      IF(LDATA(IX,JX).EQ.0)GO TO 108
      IF(XXY(JX).LT.WS(JX))THEN
         IF(XXX(JX).GT.WS(JX))GO TO 933
      ENDIF
      IF(XXY(JX).GT.WS(JX))THEN
         IF(XXX(JX).LT.WS(JX))GO TO 933
      ENDIF
  108 CONTINUE
  93  CONTINUE
      XINC=XINC+.001
  776 CONTINUE
      WRITE(23,1001)IX,KLM,LM,XINC,(XMAT2(LM,K),K=1,NS),
     C                (TVEC(LM,K),K=1,NS),(XMAT(IX,K),K=1,NS)
  933 CONTINUE
      SUM=0.0
      DO 81 K=1,NS
      SUM=SUM+(XMAT2(LM,K)-XMAT(IX,K))**2
  81  CONTINUE
      SUM=SQRT(SUM)
      BBSAVE=AMAX1(BBSAVE,SUM)
      KKSAVE=AMAX0(KKSAVE,KLM)
C
  777 CONTINUE
      RETURN
      END
C
C  ************************************************************************
C    SUBROUTINE RSEARCH -- DOES LOCAL SEARCH ON NORMAL VECTORS
C
C  ************************************************************************
C
      SUBROUTINE RSEARCH(XINC,JX,NCUT,KPCUT,LPCUT,
     C                XMAT,ZVEC,WS,KDOWN,KEQUAL,KUP,JXERROR,
     C                WSNEW)
      DIMENSION XMAT(1879,1879),ZVEC(21011,25),ZZZ(1879),
     C          WS(43011),XXX(21555),MM(1879),
     C          LLL(21555),MVOTE(21555),
     C          LLV(21555),
     C          LLVB(21555),LLE(21555),LLEB(21555),LERROR(1879,21011),
     C          XJCH(25),XJEH(25),XJCL(25),XJEL(25),
     C          ZS(21555),UUU(1879,25),
     C          ZVEC2(21011,25)
      COMMON /XXCOM/ LDATA(1899,21011)
      COMMON /MINE/ NS,NRCALL,NP
  210 FORMAT(I5,10F12.3)
 1091 FORMAT(' INVERSE MATRIX ERROR',I4,I5,I8,2F10.4)
 1099 FORMAT(I3,I5,I3,2I4)
 1103 FORMAT(' MIDPOINT DECOMPOSITION',5I6)
 1212 FORMAT(I3,I5,7I4)
 3909 FORMAT(I5,I3,6I4,2I8,5I5)
C
C      XINC=0.05
C
      KDOWN=0
      KEQUAL=0
      KUP=0
      DO 998 IJL=1,NCUT
      KQUIT=IJL
C
C  SET-UP FOR PHASE 2
C
      SUM=0.0
      DO 3 K=1,NS
C      ZZZ(K)=(URAND(ISEED)-.50)*0.4 + ZVEC(JX,K)
      ZZZ(K)=(RNUNF()-.50)*0.4 + ZVEC(JX,K)
      SUM=SUM+ZZZ(K)**2
  3   CONTINUE
      SUM2=0.0
      DO 4 K=1,NS
      ZZZ(K)=ZZZ(K)/SQRT(SUM)
      SUM2=SUM2+(ZVEC(JX,K)-ZZZ(K))**2
  4   CONTINUE
      SUM2=SQRT(SUM2)
      SUM3=0.0
      DO 5 K=1,NS
      ZVEC2(JX,K)=ZVEC(JX,K)+(XINC/SUM2)*(ZZZ(K)-ZVEC(JX,K))
      ZVEC2(JX,K)=(RNUNF()-.50)
      SUM3=SUM3+ZVEC2(JX,K)**2
  5   CONTINUE
      DO 6 K=1,NS
      ZVEC2(JX,K)=ZVEC2(JX,K)/SQRT(SUM3)
  6   CONTINUE
C
      DO 488 K=1,NS
      UUU(IJL,K)=ZVEC2(JX,K)
  488 CONTINUE
      DO 489 I=1,NP
      SUM=0.0
      DO 490 K=1,NS
      SUM=SUM+XMAT(I,K)*ZVEC2(JX,K)
  490 CONTINUE
C
C  SAVE PROJECTION VECTORS -- LEGISLATOR BY ROLL CALL MATRIX
C
      LLL(I)=I
      XXX(I)=SUM
      MM(I)=LDATA(I,JX)
      IF(LDATA(I,JX).EQ.0)MM(I)=9
  489 CONTINUE
C
C  SORT PROJECTION VECTOR (Y-HAT)
C
C
      CALL RSORT(XXX,NP,LLL)
      DO 414 I=1,NP
      MVOTE(I)=MM(LLL(I))
  414 CONTINUE
C
C  
C  CALCULATE CLASSIFICATION ERRORS FOR BEST SOLUTION FROM PHASE 1
C  
C
      JCH=0
      JEH=0
      JCL=0
      JEL=0
      IROTC=0
      CALL JAN1PT(NP,NRCALL,NS,JX,XMAT,XXX,MVOTE,WS,
     C            LLV,LLVB,LLE,LLEB,LERROR,
     C            ZS,JCH,JEH,JCL,JEL,IROTC,KCUT,LCUT,LLL,
     C            XJCH,XJEH,XJCL,XJEL)
C
      IF(JEH+JEL.LT.JXERROR)THEN
         KDOWN=KDOWN+1
         JXERROR=JEH+JEL
         DO 997 K=1,NS
         ZVEC(JX,K)=ZVEC2(JX,K)
  997    CONTINUE
         WSNEW=WS(JX)
         KPCUT=KCUT
         LPCUT=LCUT
         GO TO 998
      ENDIF
      IF(JEH+JEL.EQ.JXERROR)KEQUAL=KEQUAL+1
      IF(JEH+JEL.GT.JXERROR)KUP=KUP+1
C
  998 CONTINUE
C
      RETURN
      END
C
C **************************************************************************
C  SUBROUTINE STNDERR -- CALCULATES "STANDARD ERRORS" FOR LEGISLATOR
C                           COORDINATES
C                       
C **************************************************************************
C
      SUBROUTINE STNDERR(IX,NTRIAL,XMAT,ZVEC,MCUTS,WS,KCHECK1,XINC,
     C                        KDOWN,KEQUAL,KUP)
      DIMENSION XMAT(1879,1879),XMAT2(1879,1879),LCHECK(1000),
     C          XXY(21011),WS(43011),XXX(21011),MCHECK(1000),
     C          MCUTS(21011,2),ZVEC(21011,25)
      COMMON /XXCOM/ LDATA(1899,21011)
      COMMON /MINE/ NS,NRCALL,NP
 1000 FORMAT(' STNDERR CHECK',4I4,90F7.3)
 1001 FORMAT(I4,4I6,10F7.3)
C
      CALL GETTIM(ITIM1,ITIM2,ITIM3,ITIM4)
      ISEED=1000*ITIM2+100*ITIM2+1000*ITIM3+200*ITIM4
C
C      NTRIAL=100
C
      DO 5 I=1,1000
      LCHECK(I)=0
      MCHECK(I)=0
  5   CONTINUE
      DO 2 K=1,NS
      XMAT2(IX,K)=XMAT(IX,K)
  2   CONTINUE
C
      KUP=0
      KEQUAL=0
      KDOWN=0
      DO 1 III=1,NTRIAL+1
      IF(III.GT.1)THEN
         SUM=0.0
         DO 3 K=1,NS
C         XXX(K)=(URAND(ISEED)-.50)*2.0
         XXX(K)=(RNUNF()-.50)*2.0
         SUM=SUM+XXX(K)**2
  3      CONTINUE
         DO 4 K=1,NS
         XXX(K)=XXX(K)/SQRT(SUM)
         XXX(K)=XXX(K)*XINC
         XMAT2(IX,K)=XMAT(IX,K)+XXX(K)
  4      CONTINUE
      ENDIF
      KCHECK=0
      DO 93 JX=1,NRCALL
      SUM=0.0
      DO 90 K=1,NS
      SUM=SUM+XMAT2(IX,K)*ZVEC(JX,K)
  90  CONTINUE
      XXY(IX)=SUM
C
C  LOCATE ERRORS -- WS(.) CONTAINS THE OPTIMAL CUTTING POINT ON THE
C                   PROJECTION VECTOR -- IT CAN BE USED TO CALCULATE THE
C                   CLASSIFICATION ERRORS
C
      KCUT=MCUTS(JX,1)
      LCUT=MCUTS(JX,2)
C
      IF(LDATA(IX,JX).EQ.0)GO TO 108
      IF(XXY(IX).LT.WS(JX))THEN
         IF(LDATA(IX,JX).NE.KCUT)THEN
            KCHECK=KCHECK+1
            KSUM=KSUM+1
         ENDIF
      ENDIF
      IF(XXY(IX).GT.WS(JX))THEN
         IF(LDATA(IX,JX).NE.LCUT)THEN
            KCHECK=KCHECK+1
         ENDIF
      ENDIF
  108 CONTINUE
  93  CONTINUE
C
      IF(III.GT.1)THEN
         IF(KCHECK.GT.KCHECK1)KUP=KUP+1
         IF(KCHECK.EQ.KCHECK1)KEQUAL=KEQUAL+1
         IF(KCHECK.LT.KCHECK1)THEN
            KDOWN=KDOWN+1
            WRITE(23,1000)IX,III,KCHECK,KCHECK1,
     C               XINC,(XMAT(IX,K),K=1,NS),(XMAT2(IX,K),K=1,NS)
C            WRITE(*,1000)IX,III,KCHECK,KCHECK1,
C     C               XINC,(XMAT(IX,K),K=1,NS),(XMAT2(IX,K),K=1,NS)
            KCHECK1=KCHECK
            DO 33 K=1,NS
            XMAT(IX,K)=XMAT2(IX,K)
  33        CONTINUE
         ENDIF
      ENDIF
      IF(III.EQ.1)KCHECK1=KCHECK
C
  1   CONTINUE
      XDOWN=FLOAT(KDOWN)/FLOAT(NTRIAL)
      XEQUAL=FLOAT(KEQUAL)/FLOAT(NTRIAL)
      XUP=FLOAT(KUP)/FLOAT(NTRIAL)
      RETURN
      END
C
C **************************************************************************
C  SUBROUTINE DISC -- CALCULATES WITHIN AND BETWEEN SUM OF SQUARES
C                       
C **************************************************************************
C
      SUBROUTINE DISC(IIII,JX,XMAT,V,MDATA,JERROR,XERROR)
      DIMENSION XMAT(1879,1879),V(1879),MDATA(1879,21011),LLL(21555),
     C          XXX(21555),MM(1879),MVOTE(21555),WS(43011),
     C          XJCH(25),XJEH(25),XJCL(25),XJEL(25),
     C          LLV(21555),LLE(21555),LLVB(21555),LLEB(21555),
     C          ZS(21555),LERROR(1879,21011)
      COMMON /XXCOM/ LDATA(1899,21011)
      COMMON /MINE/ NS,NRCALL,NP
C
 1110 FORMAT(7I4,3F7.3)
C
      KYES=0
      KNO=0
      KKYES=0
      KKNO=0
C
      DO 92 I=1,NP
      IF(MDATA(I,JX).EQ.1)KYES=KYES+1
      IF(MDATA(I,JX).EQ.6)KNO=KNO+1
      IF(LDATA(I,JX).EQ.1)KKYES=KKYES+1
      IF(LDATA(I,JX).EQ.6)KKNO=KKNO+1
  92  CONTINUE
C
C  CHECK WITHIN AND BETWEEN SUM OF SQUARES
C
      SUMB=0.0
      SUMW=0.0
      SUMY=0.0
      SUMN=0.0
      DO 105 I=1,NP
      SUM=0.0
      DO 106 K=1,NS
      SUM=SUM+XMAT(I,K)*V(K+NS)
  106 CONTINUE
      IF(MDATA(I,JX).EQ.1)THEN
         SUMY=SUMY+SUM
      ENDIF
      IF(MDATA(I,JX).EQ.6)THEN
         SUMN=SUMN+SUM
      ENDIF
  105 CONTINUE
      SUMY=SUMY/FLOAT(KYES)
      SUMN=SUMN/FLOAT(KNO)
      DO 107 I=1,NP
      SUM=0.0
      DO 109 K=1,NS
      SUM=SUM+XMAT(I,K)*V(K+NS)
  109 CONTINUE
      IF(MDATA(I,JX).EQ.1)THEN
         SUMW=SUMW+(SUM-SUMY)**2
      ENDIF
      IF(MDATA(I,JX).EQ.6)THEN
         SUMW=SUMW+(SUM-SUMN)**2
      ENDIF
  107 CONTINUE
      SUMW=SUMW/FLOAT(NP)
      SUMB=(SUMY-SUMN)**2
C
C
      DO 89 I=1,NP
      SUM=0.0
      DO 90 K=1,NS
      SUM=SUM+XMAT(I,K)*V(K+NS)
  90  CONTINUE
C
C  SAVE PROJECTION VECTORS -- LEGISLATOR BY ROLL CALL MATRIX
C
      LLL(I)=I
      XXX(I)=SUM
      MM(I)=LDATA(I,JX)
      IF(LDATA(I,JX).EQ.0)MM(I)=9
  89  CONTINUE
C
C  SORT PROJECTION VECTOR (Y-HAT)
C
      CALL RSORT(XXX,NP,LLL)
      DO 114 I=1,NP
      MVOTE(I)=MM(LLL(I))
  114 CONTINUE
C  
C  CALCULATE CLASSIFICATION ERRORS
C
      JCH=0
      JEH=0
      JCL=0
      JEL=0
      IROTC=0
      CALL JAN1PT(NP,NRCALL,NS,JX,XMAT,XXX,MVOTE,WS,
     C            LLV,LLVB,LLE,LLEB,LERROR,
     C            ZS,JCH,JEH,JCL,JEL,IROTC,KCUT,LCUT,LLL,
     C            XJCH,XJEH,XJCL,XJEL)
C
      JERROR=JEH+JEL
      XERROR=SUMB/SUMW
      RETURN
      END
C
C **************************************************************************
C  SUBROUTINE ZVECINV -- CALCULATES (Z'Z)-1Z' WHERE Z IS THE NORMAL PLANE
C                        MATRIX
C **************************************************************************
C
      SUBROUTINE ZVECINV(NRCALL,NS,ZVEC,BB)
      DIMENSION VVV(25,25),FV1(1879),FV2(1879),
     C          VCOV(25,25),WVEC(25),UL(25,25),
     C          BB(25,21011),
     C          ZVEC(21011,25)
C
 1011 FORMAT(7F10.4)
 1012 FORMAT(' DECOMPOSITION OF NORMAL VECTOR MATRIX',3I4)
 1091 FORMAT(' INVERSE MATRIX ERROR',F10.4)
C
C
C    (X'X)
C
      DO 38 K=1,NS
      DO 39 L=1,NS
      SUM=0.0
      DO 40 I=1,NRCALL
      SUM=SUM+ZVEC(I,K)*ZVEC(I,L)
  40  CONTINUE
      VCOV(K,L)=SUM
  39  CONTINUE
  38  CONTINUE
C
C  EIGENVECTOR-EIGENVALUE DECOMPOSITION OF NORMAL VECTOR MATRIX
C
      CALL RS(25,NS,VCOV,WVEC,1,VVV,FV1,FV2,IER)
C      WRITE(21,1012)NS,NRCALL,IER
C      WRITE(*,1012)NS,NRCALL,IER
C      WRITE(21,1011)(WVEC(K),K=1,NS)
C
C  (X'X)-1
C
      DO 83 I=1,NS
      DO 83 K=1,NS
      SUM=0.0
      DO 84 J=1,NS
      IF(ABS(WVEC(NS+1-J)).GT..0001)THEN
          SUM=SUM+VVV(K,NS+1-J)*(1.0/WVEC(NS+1-J))*VVV(I,NS+1-J)
      ENDIF
  84  CONTINUE
  83  UL(I,K)=SUM
C
C
C  MATRIX INVERSION CHECK  (X'X)-1(X'X) = I
C 
      ASUM=0.0
      DO 933 I=1,NS
      DO 933 J=1,NS
      SUM=0.0
      DO 944 K=1,NS
      SUM=SUM+UL(J,K)*VCOV(K,I)
  944 CONTINUE
      IF(I.EQ.J)ASUM=ASUM+ABS(1.0-SUM)
      IF(I.NE.J)ASUM=ASUM+ABS(SUM)
  933 CONTINUE
      IF(ASUM.GT..01)WRITE(*,1091)ASUM
      IF(ASUM.GT..01)WRITE(23,1091)ASUM
C
C  (X'X)-1*X'
C
      DO 85 I=1,NRCALL
      DO 85 J=1,NS
      SUM=0.0
      DO 86 JJ=1,NS
      SUM=SUM+UL(J,JJ)*ZVEC(I,JJ)
  86  CONTINUE
  85  BB(J,I)=SUM
C
      RETURN
      END
C
C **************************************************************************
C  SUBROUTINE KTPXI -- PERFORMS LEGISLATOR FITTING
C **************************************************************************
C
      SUBROUTINE KTPXI(ILEG,NRCALL,NS,MCUTS,BB,XMAT,ZVEC,WS,
     C                                   LERROR,KTOTAL,KWRONG)
      DIMENSION BB(25,21011),XMAT(1879,1879),WS(43011),
     C          ZVEC(21011,25),MCUTS(21011,2),LERROR(1879,21011),
     C          ZWRONG(50),XDAT(1879,1879),XXZ(21011),LL(21011),
     C          YWRONG(21011,25),YYWRONG(21011,25),LSAVE(21011,25),
     C          XMAT2(25),XXY(21011),YYYWRONG(21011,25)
      COMMON /XXCOM/ LDATA(1899,21011)
C
 1001 FORMAT(I4,2X,I6,I4)
 1011 FORMAT(I4,2I3,I4,12F7.3)
 1012 FORMAT(14X,12F7.3)
 1013 FORMAT(' LEG',2I3,I4,12F7.3)
 1014 FORMAT(' WHOA DUDE',I4,I3,I4)
 1015 FORMAT(' HEY THESE ARE NOT EQUAL!')
 1016 FORMAT(I4,2I3,I4,12F7.3)
 1017 FORMAT(14X,I4,12F7.3)
 1018 FORMAT(10X,2I4,12F7.3)
 1019 FORMAT(16X,12F7.3)
 1020 FORMAT(2I4,3F10.4)
 1021 FORMAT(4I4,12F7.3)
 1022 FORMAT(' TWO  ',3I4,12F7.3)
 1023 FORMAT(' THREE',3I4,12F7.3)
 1024 FORMAT(' FOUR ',3I4,12F7.3)
 1025 FORMAT(4X,4X,12X,12F7.3)
 1026 FORMAT(I4,25F7.3)
C
      NTRIAL2=15
      MZZZ=5
C
C      WRITE(21,1019)(XMAT(ILEG,K),K=1,NS)
      DO 203 K=1,NS
      XDAT(20,K)=XMAT(ILEG,K)
  203 CONTINUE
C
      DO 9887 IIII=1,2
      SUM=0.0
      DO 101 K=1,NS
      XDAT(1,K)=0.0
C      IF(IIII.EQ.2)XDAT(1,K)=0.1
      IF(IIII.EQ.2)XDAT(1,K)=XDAT(20,K)
      XDAT(2,K)=XDAT(1,K)
  101 CONTINUE
C
C      WRITE(21,1019)(XDAT(1,K),K=1,NS)
      KWRONG=0
      K3WRONG=0
C
      DO 9888 III=1,MZZZ
C
      DO 988 II=1,NTRIAL2
      NII=II
      DO 987 KL=1,NS
C
      K3WRONG=KWRONG
      XDAT(2,KL)=0.01
C
C
C  CALCULATE FEASIBLE ALPHA VALUE
C
      ASUM=0.0
      AAA=0.0
      BBB=0.0
      DO 902 ML=1,NS
      ASUM=ASUM+XDAT(1,ML)**2
      AAA=AAA+(XDAT(2,ML)-XDAT(1,ML))**2
      BBB=BBB+2.0*(XDAT(2,ML)-XDAT(1,ML))*XDAT(1,ML)
  902 CONTINUE
      CCC=ASUM - 1.0
      RAD=BBB*BBB-4.0*AAA*CCC
      RADSQ=SQRT(ABS(RAD))
      ROOT1=0.0
      ROOT2=0.0
      IF(AAA.GT..00001)THEN
         ROOT1=(-BBB+RADSQ)/(2.0*AAA)
         ROOT2=(-BBB-RADSQ)/(2.0*AAA)
      ENDIF
C
      CALL KTPXIXJ(NII,ILEG,NRCALL,NS,MCUTS,BB,XDAT,ZVEC,WS,
     C                XXZ,WSSY,XMAT2,ZWRONG,YYWRONG,LERROR,KTOTAL,
     C                KWRONG,ROOT1,ROOT2)
C
      LSAVE(II,KL)=KWRONG
      IF(KWRONG.EQ.0)THEN
         LL(1)=II
         DO 668 K=1,NS
         XMAT(ILEG,K)=XMAT2(K)
  668    CONTINUE
         KKNII=NII
         K3WRONG=KWRONG
C         WRITE(21,1021)ILEG,III,II,KWRONG,(XMAT(ILEG,K),K=1,NS)
         GO TO 996
      ENDIF
C
      SUM=0.0
      DO 133 K=1,NS
      SUM=SUM+XMAT2(K)**2
      YWRONG(II,K)=XMAT2(K)
      YYYWRONG(KL,K)=XMAT2(K)
      XDAT(3,K)=XDAT(1,K)
      XDAT(1,K)=XMAT2(K)
      XDAT(2,K)=XMAT2(K)
      XMAT(ILEG,K)=XMAT2(K)
  133 CONTINUE
C
C      WRITE(21,1011)ILEG,II,KL,KWRONG,(XDAT(3,K),K=1,NS)
C      WRITE(21,1012)(XMAT2(K),K=1,NS),(ZWRONG(K),K=1,NS)
C                                      
  987 CONTINUE
C
      SUM=0.0
      KSUM=0
      DO 132 K=1,NS
      IF(II.GT.1)THEN
         SUM=SUM+(YWRONG(II,K)-YWRONG(II-1,K))**2
         KSUM=KSUM+ABS(LSAVE(II,K)-LSAVE(II-1,K))
      ENDIF
  132 CONTINUE
      IF(II.EQ.1)GO TO 988
      IF(SUM.LE..000001)GO TO 986
      IF(II.GT.5.AND.KSUM.EQ.0)GO TO 986
  988 CONTINUE
C
  986 CONTINUE
C      WRITE(21,1021)ILEG,III,II,KWRONG,(XMAT(ILEG,K),K=1,NS)
      KKKNII=NII
      K3WRONG=KWRONG
C
C
C  CALCULATE FEASIBLE ALPHA VALUE
C
      ASUM=0.0
      AAA=0.0
      BBB=0.0
      DO 903 ML=1,NS
      XDAT(1,ML)=XMAT(ILEG,ML)
      XDAT(2,ML)=ZWRONG(K)
      ASUM=ASUM+XDAT(1,ML)**2
      AAA=AAA+(XDAT(2,ML)-XDAT(1,ML))**2
      BBB=BBB+2.0*(XDAT(2,ML)-XDAT(1,ML))*XDAT(1,ML)
  903 CONTINUE
      CCC=ASUM - 1.0
      RAD=BBB*BBB-4.0*AAA*CCC
      RADSQ=SQRT(ABS(RAD))
      ROOT1=0.0
      ROOT2=0.0
      IF(AAA.GT..00001)THEN
         ROOT1=(-BBB+RADSQ)/(2.0*AAA)
         ROOT2=(-BBB-RADSQ)/(2.0*AAA)
      ENDIF
C      ROOT1=(-BBB+RADSQ)/(2.0*AAA)
C      ROOT2=(-BBB-RADSQ)/(2.0*AAA)
C
      CALL KTPXIXJ(NII,ILEG,NRCALL,NS,MCUTS,BB,XDAT,ZVEC,WS,
     C                XXZ,WSSY,XMAT2,ZWRONG,YYWRONG,LERROR,KTOTAL,
     C                KWRONG,ROOT1,ROOT2)
C
C      WRITE(21,1021)ILEG,III,II,KWRONG,(XMAT2(K),K=1,NS),WSSY
C
      DO 44 K=1,NS
      XMAT(ILEG,K)=XMAT2(K)
      XDAT(1,K)=XMAT2(K)
      XDAT(2,K)=XMAT2(K)
      IF(IIII.EQ.1)XDAT(10,K)=XMAT2(K)
  44  CONTINUE
      IF(KWRONG.EQ.0)GO TO 996
 9888 CONTINUE
      IF(IIII.EQ.1)KLWRONG=KWRONG
      IF(IIII.EQ.2)THEN
C
C  CALCULATE FEASIBLE ALPHA VALUE
C
         ASUM=0.0
         AAA=0.0
         BBB=0.0
         DO 905 ML=1,NS
         XDAT(1,ML)=XMAT(ILEG,ML)
         XDAT(2,ML)=XDAT(10,ML)
         ASUM=ASUM+XDAT(1,ML)**2
         AAA=AAA+(XDAT(2,ML)-XDAT(1,ML))**2
         BBB=BBB+2.0*(XDAT(2,ML)-XDAT(1,ML))*XDAT(1,ML)
  905    CONTINUE
         CCC=ASUM - 1.0
         RAD=BBB*BBB-4.0*AAA*CCC
         RADSQ=SQRT(ABS(RAD))
         ROOT1=0.0
         ROOT2=0.0
         IF(AAA.GT..00001)THEN
            ROOT1=(-BBB+RADSQ)/(2.0*AAA)
            ROOT2=(-BBB-RADSQ)/(2.0*AAA)
         ENDIF
C         ROOT1=(-BBB+RADSQ)/(2.0*AAA)
C         ROOT2=(-BBB-RADSQ)/(2.0*AAA)
C
         CALL KTPXIXJ(NII,ILEG,NRCALL,NS,MCUTS,BB,XDAT,ZVEC,WS,
     C                XXZ,WSSY,XMAT2,ZWRONG,YYWRONG,LERROR,KTOTAL,
     C                KWRONG,ROOT1,ROOT2)
C
C         WRITE(21,1021)ILEG,III,II,KWRONG,(XMAT2(K),K=1,NS),WSSY
C   
         DO 48 K=1,NS
         XMAT(ILEG,K)=XMAT2(K)
  48     CONTINUE
         IF(KWRONG.EQ.0)GO TO 996
      ENDIF
C
 9887 CONTINUE
C
  996 CONTINUE
C
      SUM=0.0
      DO 12 K=1,NS
      SUM=SUM+XMAT(ILEG,K)**2
  12  CONTINUE
      IF(SUM.GT.1.0)THEN
         DO 13 K=1,NS
         XMAT(ILEG,K)=XMAT(ILEG,K)/SQRT(SUM)
  13     CONTINUE
      ENDIF
C
      KKRITE=0
      KKWRONG=0
      KTOTAL=0
      DO 31 JX=1,NRCALL
      LERROR(ILEG,JX)=0
      SUM=0.0
      DO 32 K=1,NS
      SUM=SUM+XMAT(ILEG,K)*ZVEC(JX,K)
  32  CONTINUE
      XXY(JX)=SUM
      DB2B1=WS(JX)-XXY(JX)
C
C  CALCULATE CLASSIFICATION ERROR
C
      IF(LDATA(ILEG,JX).NE.0)THEN
         KTOTAL=KTOTAL+1
         IF(XXY(JX).LT.WS(JX))THEN
C            KTOTAL=KTOTAL+1
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,1))THEN
               KKRITE=KKRITE+1
            ENDIF
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,2))THEN
               LERROR(ILEG,JX)=1
               KKWRONG=KKWRONG+1
            ENDIF
         ENDIF
         IF(XXY(JX).GT.WS(JX))THEN
C            KTOTAL=KTOTAL+1
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,2))THEN
               KKRITE=KKRITE+1
            ENDIF
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,1))THEN
               LERROR(ILEG,JX)=1
               KKWRONG=KKWRONG+1
            ENDIF
         ENDIF
      ENDIF
  31  CONTINUE
C      WRITE(21,1001)ILEG,KKRITE,KKWRONG
      IF(KKWRONG.NE.KWRONG)THEN
         WRITE(21,1015)
      ENDIF
      SUM=0.0
      DO 204 K=1,NS
      SUM=SUM+(XDAT(20,K)-XMAT(ILEG,K))**2
  204 CONTINUE
      SUM=SQRT(SUM)
C      WRITE(23,1026)ILEG,SUM
      RETURN
      END
C
C **************************************************************************
C  SUBROUTINE KTPXIXJ -- PERFORMS LEGISLATOR FITTING
C **************************************************************************
C
      SUBROUTINE KTPXIXJ(NII,ILEG,NRCALL,NS,MCUTS,BB,XDAT,ZVEC,WS,
     C                      XXZ,WSSY,XMAT2,ZWRONG,YWRONG,LERROR,KTOTAL,
     C                      KKWRONG,ROOT1,ROOT2)
      DIMENSION BB(25,21011),XDAT(1879,1879),WS(43011),
     C          ZVEC(21011,25),MCUTS(21011,2),XXY(21011),XXZ(21011),
     C          XSAVE(21011,100),MRITE(21011,1000),
     C          ZWRONG(50),YWRONG(21011,25),LERROR(1879,21011),
     C          XALL(43011),KALL(43011),XMAT2(25),
     C          YALL(43011),LALL(43011)
      COMMON /XXCOM/ LDATA(1899,21011)
C
 1012 FORMAT(I4,3I6,7F7.3)
C 1013 FORMAT(2I4,3I6,F12.6,6F7.3/8X,8F7.3/8X,8F7.3)
 1013 FORMAT(2I4,4I6,3F7.3)
 1014 FORMAT(15X,I6,13X,4F7.3)
 1015 FORMAT(I5,4F7.3)
 1016 FORMAT(I5,2F7.3,I3,F7.3)
 1017 FORMAT(I5,2F10.4,3F7.3)
 1018 FORMAT(4X,6I5,F10.4)
 1019 FORMAT(2I4,4I6)
 1020 FORMAT(' INITIAL ERROR',I4,3I6,7F7.3)
 1021 FORMAT(I3,2I5,15F7.3)
 1023 FORMAT(3I4,F15.10)
C
      KWED1=NII
C
C  INITIALIZE AT ZERO
C
      NTRY=2
C
      DO 99 III=1,NTRY
      KTOTAL=0
      KRITE=0
      KWRONG=0
      DO 11 JX=1,NRCALL
      SUM=0.0
      DO 52 K=1,NS
      SUM=SUM+XDAT(III,K)*ZVEC(JX,K)
  52  CONTINUE
      XXY(JX)=SUM
      XSAVE(JX,III)=SUM
      MRITE(JX,III)=0
      DB2B1=WS(JX)-XXY(JX)
      IF(LDATA(ILEG,JX).NE.0)THEN
         KTOTAL=KTOTAL+1
C
C  CALCULATE CLASSIFICATION ERROR
C
         IF(XXY(JX).LT.WS(JX))THEN
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,1))THEN
               KRITE=KRITE+1
               MRITE(JX,III)=1
            ENDIF
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,2))THEN
               KWRONG=KWRONG+1
            ENDIF
         ENDIF
         IF(XXY(JX).GT.WS(JX))THEN
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,1))THEN
               KWRONG=KWRONG+1
            ENDIF
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,2))THEN
               KRITE=KRITE+1
               MRITE(JX,III)=1
            ENDIF
         ENDIF
      ENDIF
  11  CONTINUE
C      WRITE(21,1012)III,KRITE,KWRONG,KTOTAL,(XDAT(III,K),K=1,NS)
  99  CONTINUE
C
C
C  CONSTRUCT PROJECTION VECTOR
C
      JJJ=1
      III=2
      ITOT=0
      KRITE=0
      KWRONG=0
      KTOTAL=0
      XERR=0.0
      XKMAX=+99999.0
      XKMIN=-99999.0
      DO 1 JX=1,NRCALL
C
      DB2B1=WS(JX)-XSAVE(JX,III)
      DENOM=XSAVE(JX,III)-XSAVE(JX,JJJ)
      IF(ABS(DENOM).LE.0.00001)THEN
C         WRITE(23,1023)NII,ILEG,JX,DENOM
         GO TO 1
      ENDIF
      XNUM1=+1.0-XSAVE(JX,JJJ)
      XNUM2=-1.0-XSAVE(JX,JJJ)
      XNUM3=WS(JX)-XSAVE(JX,JJJ)
C
C  CALCULATE CLASSIFICATION ERROR
C
      IF(LDATA(ILEG,JX).NE.0)THEN
         IF(XSAVE(JX,III).LT.WS(JX))THEN
            KTOTAL=KTOTAL+1
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,1))THEN
               KRITE=KRITE+1
C
C  CORRECT TO CORRECT (CASES 3 AND 4)
C
               IF(MRITE(JX,JJJ).EQ.1)THEN
C  CASE 3
                  IF(XSAVE(JX,III).LT.XSAVE(JX,JJJ))THEN
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM2/DENOM
                     KALL(ITOT)=6
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM3/DENOM
                     KALL(ITOT)=1
                  ENDIF
C  CASE 4
                  IF(XSAVE(JX,III).GT.XSAVE(JX,JJJ))THEN
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM3/DENOM
                     KALL(ITOT)=6
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM2/DENOM
                     KALL(ITOT)=1
                  ENDIF
               ENDIF
C
C  INCORRECT TO CORRECT (CASE 11)
C
               IF(MRITE(JX,JJJ).EQ.0)THEN
                  ITOT=ITOT+1
                  XALL(ITOT)=XNUM2/DENOM
                  KALL(ITOT)=6
                  ITOT=ITOT+1
                  XALL(ITOT)=XNUM3/DENOM
                  KALL(ITOT)=1
               ENDIF
            ENDIF
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,2))THEN
               KWRONG=KWRONG+1
C
C  CORRECT TO INCORRECT (CASE 9)
C
               IF(MRITE(JX,JJJ).EQ.1)THEN
                  ITOT=ITOT+1
                  XALL(ITOT)=XNUM3/DENOM
                  KALL(ITOT)=6
                  ITOT=ITOT+1
                  XALL(ITOT)=XNUM1/DENOM
                  KALL(ITOT)=1
               ENDIF
C
C  INCORRECT TO INCORRECT (CASES 7 AND 8)
C
               IF(MRITE(JX,JJJ).EQ.0)THEN
C  CASE 7
                  IF(XSAVE(JX,III).LT.XSAVE(JX,JJJ))THEN
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM3/DENOM
                     KALL(ITOT)=6
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM1/DENOM
                     KALL(ITOT)=1
                  ENDIF
C  CASE 8
                  IF(XSAVE(JX,III).GT.XSAVE(JX,JJJ))THEN
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM1/DENOM
                     KALL(ITOT)=6
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM3/DENOM
                     KALL(ITOT)=1
                  ENDIF
               ENDIF
            ENDIF
         ENDIF
C
C
C
         IF(XSAVE(JX,III).GT.WS(JX))THEN
            KTOTAL=KTOTAL+1
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,2))THEN
               KRITE=KRITE+1
C
C  CORRECT TO CORRECT (CASES 1 AND 2)
C
               IF(MRITE(JX,JJJ).EQ.1)THEN
C  CASE 2
                  IF(XSAVE(JX,III).LT.XSAVE(JX,JJJ))THEN
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM3/DENOM
                     KALL(ITOT)=6
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM1/DENOM
                     KALL(ITOT)=1
                  ENDIF
C  CASE 1
                  IF(XSAVE(JX,III).GT.XSAVE(JX,JJJ))THEN
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM1/DENOM
                     KALL(ITOT)=6
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM3/DENOM
                     KALL(ITOT)=1
                  ENDIF
               ENDIF
C
C  INCORRECT TO CORRECT (CASE 12)
C
               IF(MRITE(JX,JJJ).EQ.0)THEN
                  ITOT=ITOT+1
                  XALL(ITOT)=XNUM1/DENOM
                  KALL(ITOT)=6
                  ITOT=ITOT+1
                  XALL(ITOT)=XNUM3/DENOM
                  KALL(ITOT)=1
               ENDIF
            ENDIF
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,1))THEN
               KWRONG=KWRONG+1
C
C  CORRECT TO INCORRECT (CASE 10)
C
               IF(MRITE(JX,JJJ).EQ.1)THEN
                  ITOT=ITOT+1
                  XALL(ITOT)=XNUM3/DENOM
                  KALL(ITOT)=6
                  ITOT=ITOT+1
                  XALL(ITOT)=XNUM2/DENOM
                  KALL(ITOT)=1
               ENDIF
C
C  INCORRECT TO INCORRECT (CASES 5 AND 6)
C
               IF(MRITE(JX,JJJ).EQ.0)THEN
C  CASE 6
                  IF(XSAVE(JX,III).LT.XSAVE(JX,JJJ))THEN
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM2/DENOM
                     KALL(ITOT)=6
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM3/DENOM
                     KALL(ITOT)=1
                  ENDIF
C  CASE 5
                  IF(XSAVE(JX,III).GT.XSAVE(JX,JJJ))THEN
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM3/DENOM
                     KALL(ITOT)=6
                     ITOT=ITOT+1
                     XALL(ITOT)=XNUM2/DENOM
                     KALL(ITOT)=1
                  ENDIF
               ENDIF
            ENDIF
         ENDIF
      ENDIF
  1   CONTINUE
C
C  IMPOSE RANGE CONSTRAINTS ON ALPHA VECTOR -- ROOT2 IS THE LOWER BOUND
C                                              ROOT1 IS THE UPPER BOUND
C
      KK=0
      DO 363 I=1,ITOT
      IF(XALL(I).GT.ROOT2.AND.XALL(I).LT.ROOT1)THEN
         KK=KK+1
         YALL(KK)=XALL(I)
         LALL(KK)=KALL(I)
      ENDIF
  363 CONTINUE
C
      WSSY=0.0
      IF(KK.GT.0)THEN
         CALL RSORT(YALL,KK,LALL)
         IROTC=1
         CALL JAN0PT(KK,YALL,LALL,WSSY,JCH,JEH,JCL,JEL,IROTC)
      ENDIF
C
C  (Z'Z)-1*Z'W-HAT
C
C
      DO 3 K=1,NS
      ZWRONG(K)=0.0
      SUMWS=0.0
      DO 4 JJ=1,NRCALL
      SUMWS=SUMWS+BB(K,JJ)*(XSAVE(JJ,JJJ)+
     C                WSSY*(XSAVE(JJ,III)-XSAVE(JJ,JJJ)))
   4  CONTINUE
C
      XMAT2(K)=SUMWS
   3  CONTINUE
C
      KKRITE=0
      KKWRONG=0
      KTOTAL=0
      XERR2=0.0
      DO 31 JX=1,NRCALL
      LERROR(ILEG,JX)=0
      SUM=0.0
      DO 32 K=1,NS
      SUM=SUM+XMAT2(K)*ZVEC(JX,K)
  32  CONTINUE
      XXY(JX)=SUM
      XXZ(JX)=SUM
      DB2B1=WS(JX)-XXY(JX)
C
C  CALCULATE CLASSIFICATION ERROR
C
      IF(LDATA(ILEG,JX).NE.0)THEN
         IF(XXY(JX).LT.WS(JX))THEN
            KTOTAL=KTOTAL+1
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,1))THEN
               KKRITE=KKRITE+1
            ENDIF
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,2))THEN
               LERROR(ILEG,JX)=1
               KKWRONG=KKWRONG+1
               XERR2=XERR2+(XXY(JX)-WS(JX))**2
               XXZ(JX)=+1.0
               DO 62 K=1,NS
               YWRONG(KKWRONG,K)=XMAT2(K)+1.000*DB2B1*ZVEC(JX,K)
               ZWRONG(K)=ZWRONG(K)+XMAT2(K)+
     C                   1.5000*DB2B1*ZVEC(JX,K)
  62           CONTINUE
            ENDIF
         ENDIF
         IF(XXY(JX).GT.WS(JX))THEN
            KTOTAL=KTOTAL+1
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,2))THEN
               KKRITE=KKRITE+1
            ENDIF
            IF(LDATA(ILEG,JX).EQ.MCUTS(JX,1))THEN
               LERROR(ILEG,JX)=1
               KKWRONG=KKWRONG+1
               XERR2=XERR2+(XXY(JX)-WS(JX))**2
               XXZ(JX)=-1.0
               DO 63 K=1,NS
               YWRONG(KKWRONG,K)=XMAT2(K)+1.000*DB2B1*ZVEC(JX,K)
               ZWRONG(K)=ZWRONG(K)+XMAT2(K)+
     C                   1.5000*DB2B1*ZVEC(JX,K)
  63           CONTINUE
            ENDIF
         ENDIF
      ENDIF
  31  CONTINUE
C
      DO 5 K=1,NS
      IF(KKWRONG.GT.0)ZWRONG(K)=ZWRONG(K)/FLOAT(KKWRONG)
  5   CONTINUE
C      IF(ILEG.EQ.5)THEN
C      WRITE(21,1013)NII,ILEG,KKRITE,KKWRONG,JEH+JEL,KTOTAL,
C     C                  (XMAT2(K),K=1,3)
C      ENDIF
C
      RETURN
      END
C
C  **************************************************************************
C    SUBROUTINE JAN0PT -- FINDS OPTIMAL CUTTING POINT FOR ONE DIMENSION
C  **************************************************************************
C
      SUBROUTINE JAN0PT(NP,YSS,KA,WSSY,JCH,JEH,JCL,JEL,IROTC)
     C                   
      DIMENSION YSS(43011),KA(43011),LV(43011),LVB(43011),
     C          LEB(43011),Z(43011),Y(43011),
     C          LE(43011),LJEP(43011),
     C          AAJEP(101),
     C          ABJEP(101),LAJEP(101),LBJEP(101),LCJEP(101),
     C          LDJEP(101),ABABJEP(101),MJEP(101)
C
      NPN=NP+1
      NPP=NP-1
      KCUT=1
      LCUT=6
      NOTE=1
      AA1=0.0
      AB1=0.0
      LA1=0
      LB1=0
      LC1=0
      LD1=0
      AA2=999.0
      AB2=0.0
      LA2=0
      LB2=0
      LC2=0
      LD2=0
      DO 999 III=1,1
      IF(III.EQ.2)THEN
         KCUT=6
         LCUT=1
      ENDIF
C
C  CHECK ALL POSSIBLE INTERIOR CUT POINTS  --  THE NP INPUT POINTS
C      ARE HELD FIXED.  THERE ARE NP POSSIBLE CUT-POINTS BEGINNING
C      WITH CUT-POINT 1 WHICH IS .001 UNITS TO THE LEFT OF POINT 1.
C      CUT-POINT 2 IS BETWEEN POINTS 1 AND 2, ETC.
C
C     1   2   3   4   5   6   7   8   9   10   11 ...... NP-1   NP
C    *  *   *   *   *   *   *   *   *   *    *                *
C    1  2   3   4   5   6   7   8   9  10   11  ...........  NP
C
C  IF KCUT=1 AND LCUT=6, THE FOLLOWING NP PATTERNS ARE TESTED
C
C PATTERN
C   1         6666666666666666666666
C   2         1666666666666666666666
C   3         1166666666666666666666
C   4         1116666666666666666666
C   5         1111666666666666666666
C   6         1111166666666666666666
C   7         1111116666666666666666
C   .           .....
C   .           .....
C   .           .....
C  NP-1       1111111111111111111166
C   NP        1111111111111111111116
C
C  BECAUSE THE PROGRAM TRIES BOTH KCUT=1/LCUT=6 AND KCUT=6/LCUT=1, THIS
C  WILL ALSO TEST THE ONE MISSING PATTERN ABOVE, VIZ., ALL "1"S.
C
C
      KSE=0
      KSV=0
      LSV=0
      LSE=0
      KMARK=1
      I=0
  10  I=I+1
      IF(I-NP-1)61,12,12
  61  Z(I)=999.0
      IF(I.EQ.1)THEN
         Y(I)=YSS(1)-.001
      ENDIF
      IF(I.GT.1)THEN
         Y(I)=(YSS(I)+YSS(I-1))/2.0
      ENDIF
C      IF(KA(I).EQ.9)GO TO 10
      IF(KMARK.EQ.1)THEN
         DO 3 J=I,NP
         IF(KA(J).EQ.9)GO TO 3
         IF(LCUT-KA(J))33,5,33
  33     IF(KCUT-KA(J))3,6,3
  5      LSV=LSV+1
         GO TO 3
  6      LSE=LSE+1
  3      CONTINUE
         KMARK=0
         GO TO 31
      ENDIF
      IF(KA(I-1).EQ.KCUT)THEN
         KSV=KSV+1
         LSE=LSE-1
      ENDIF
      IF(KA(I-1).EQ.LCUT)THEN
         KSE=KSE+1
         LSV=LSV-1
      ENDIF
C
  31  CONTINUE
      LJEP(I)=I
      LV(I)=KSV
      LVB(I)=LSV
      LE(I)=KSE
      LEB(I)=LSE
      KT=LV(I)+LE(I)+LVB(I)+LEB(I)
      Z(I)=FLOAT(LE(I)+LEB(I))/FLOAT(KT)
      GO TO 10
  12  CONTINUE
C
C  FIND BEST CUT POINT
C
      CALL RSORT(Z,NP,LJEP)
      KIN=1
      MJEP(1)=1
      AAJEP(KIN)=Z(1)
      ABJEP(KIN)=Y(LJEP(1))
      ABABJEP(KIN)=ABS(ABJEP(KIN))
      LAJEP(KIN)=LV(LJEP(1))
      LBJEP(KIN)=LE(LJEP(1))
      LCJEP(KIN)=LVB(LJEP(1))
      LDJEP(KIN)=LEB(LJEP(1))
C
C  CHECK IF THERE ARE MULTIPLE CUT-POINTS WITH SAME CLASSIFICATION AND
C    SELECT THAT CUT-POINT CLOSEST TO THE INTERIOR OF THE SPACE
C
      DO 63 I=2,NP
      IF(ABS(Z(1)-Z(I)).LE..000001)THEN
C      IF(Z(1).EQ.Z(I))THEN
         KIN=KIN+1
         MJEP(KIN)=KIN
         AAJEP(KIN)=Z(I)
         ABJEP(KIN)=Y(LJEP(I))
         ABABJEP(KIN)=ABS(ABJEP(KIN))
         LAJEP(KIN)=LV(LJEP(I))
         LBJEP(KIN)=LE(LJEP(I))
         LCJEP(KIN)=LVB(LJEP(I))
         LDJEP(KIN)=LEB(LJEP(I))
         IF(KIN.GT.100)GO TO 633
         GO TO 63
      ENDIF
      IF(Z(1).LT.Z(I))GO TO 633
  63  CONTINUE
  633 CONTINUE
      IF(KIN.EQ.1)THEN
         AA=AAJEP(1)
         AB=ABJEP(1)
         LA=LAJEP(1)
         LB=LBJEP(1)
         LC=LCJEP(1)
         LD=LDJEP(1)
      ENDIF
      IF(KIN.GT.1)THEN
         CALL RSORT(ABABJEP,KIN,MJEP)
         AA=AAJEP(MJEP(1))
         AB=ABJEP(MJEP(1))
         LA=LAJEP(MJEP(1))
         LB=LBJEP(MJEP(1))
         LC=LCJEP(MJEP(1))
         LD=LDJEP(MJEP(1))
      ENDIF
C
      IF(III.EQ.1)THEN
         AA1=AA
         AB1=AB
         LA1=LA
         LB1=LB
         LC1=LC
         LD1=LD
      ENDIF
      IF(III.EQ.2)THEN
         AA2=AA
         AB2=AB
         LA2=LA
         LB2=LB
         LC2=LC
         LD2=LD
      ENDIF
C
  999 CONTINUE
C
      IF(AA1.LE.AA2)THEN
         KCCUT=1
         LCCUT=6
         AA=AA1
         AB=AB1
         LA=LA1
         LB=LB1
         LC=LC1
         LD=LD1
      ENDIF
      IF(AA1.GT.AA2)THEN
         KCCUT=6
         LCCUT=1
         AA=AA2
         AB=AB2
         LA=LA2
         LB=LB2
         LC=LC2
         LD=LD2
      ENDIF
      IF(IROTC.EQ.1)THEN
         KCCUT=1
         LCCUT=6
         AA=AA1
         AB=AB1
         LA=LA1
         LB=LB1
         LC=LC1
         LD=LD1
      ENDIF
      WSSY=AB
      JCL=LA
      JEL=LB
      JCH=LC
      JEH=LD
C
C
      RETURN
      END
C
C  ************************************************************************
C    SUBROUTINE SEARCH
C  ************************************************************************
C
      SUBROUTINE SEARCH(IIII,JX,NCUT,NS,NP,NRCALL,KCUT,LCUT,KTT,KT,
     C                  XMAT,ZVEC,XPROJ,WS,XXY,
     C                  KITTY1,KITTY2,KYES,KNO)
      DIMENSION XMAT(1879,1879),ZVEC(21011,25),
     C          XPROJ(1879,21011),WS(43011),XXY(1879),XXX(21555),
     C          LLL(21555),MVOTE(21555),LLM(1879),LLN(1879),
     C          KKKCUT(1879),LLLCUT(1879),LLV(21555),MM(1879),
     C          LLVB(21555),LLE(21555),LLEB(21555),LERROR(1879,21011),
     C          XJCH(25),XJEH(25),XJCL(25),XJEL(25),
     C          ZS(21555),UUU(1879,25),Y16MIDP(1879,25),
     C          FV1(1879),FV2(1879),SUMX(1879),X16MIDP(1879,25),
     C          YHAT(1879),LWRONG(1879),W16MIDP(1879,25),
     C          VVV(25,25)
      COMMON /XXCOM/ LDATA(1899,21011)
  210 FORMAT(I5,10F12.3)
 1091 FORMAT(' INVERSE MATRIX ERROR',I4,I5,I8,2F10.4)
 1099 FORMAT(I3,I5,I3,2I4)
 1103 FORMAT(' MIDPOINT DECOMPOSITION',5I6)
 1212 FORMAT(I3,I5,7I4)
 3909 FORMAT(I8,I3,6I4,2I8,5I5)
C
      DO 1 I=1,50
      SUMX(I)=0.0
  1   CONTINUE
C
C  PHASE 2
C
C      NCUT2=20
C
      DO 999 IJL=1,NCUT
C
C  SET-UP FOR PHASE 2
C
C
      DO 388 K=1,NS
      UUU(IJL,K)=ZVEC(JX,K)
  388 CONTINUE
      DO 389 I=1,NP
      SUM=0.0
      DO 390 K=1,NS
      SUM=SUM+XMAT(I,K)*ZVEC(JX,K)
  390 CONTINUE
C
C  SAVE PROJECTION VECTORS -- LEGISLATOR BY ROLL CALL MATRIX
C
      XPROJ(I,JX)=SUM
      XXY(I)=SUM
      LLL(I)=I
      XXX(I)=SUM
      MM(I)=LDATA(I,JX)
      IF(LDATA(I,JX).EQ.0)MM(I)=9
  389 CONTINUE
C
C  SORT PROJECTION VECTOR (Y-HAT)
C
C
      CALL RSORT(XXX,NP,LLL)
      DO 314 I=1,NP
      MVOTE(I)=MM(LLL(I))
  314 CONTINUE
C
C  
C  CALCULATE CLASSIFICATION ERRORS FOR BEST SOLUTION FROM PHASE 1
C  
C
      JCH=0
      JEH=0
      JCL=0
      JEL=0
      IROTC=0
      CALL JAN1PT(NP,NRCALL,NS,JX,XMAT,XXX,MVOTE,WS,
     C            LLV,LLVB,LLE,LLEB,LERROR,
     C            ZS,JCH,JEH,JCL,JEL,IROTC,KCUT,LCUT,LLL,
     C            XJCH,XJEH,XJCL,XJEL)
C
C      WRITE(21,3909)JX,IJL,KYES,KNO,JCH,JCL,JEH,JEL
C
      LLM(IJL)=IJL
      LLN(IJL)=JEH+JEL
      FV1(IJL)=FLOAT(JEH+JEL)
      FV2(IJL)=WS(JX)
      KKKCUT(IJL)=KCUT
      LLLCUT(IJL)=LCUT
C
      IF(JEH+JEL.EQ.0)THEN
         KT=KT+JCH+JCL+JEH+JEL
         KITTY1=0
         KITTY2=JCH+JCL+JEH+JEL
         IJUST=2
         RETURN
      ENDIF
C
C
      KASTRO=4*(JEH+JEL)
      IF(KASTRO.GT.NP)KASTRO=NP
      IF(KASTRO.LT.4*NS)KASTRO=4*NS
C
      DO 108 I=1,NP
      LWRONG(I)=0
      DB2B1=WS(JX)-XXY(I)
      IF(XXY(I).LT.WS(JX))THEN
C
C  IF CORRECT PLACE LEGISLATOR POINT ON THE CURRENT CUTTING PLANE
C
         IF(LDATA(I,JX).EQ.KCUT)THEN
            DO 109 K=1,NS
            Y16MIDP(I,K)=XMAT(I,K)+DB2B1*ZVEC(JX,K)
  109       CONTINUE
         ENDIF
C
C  IF INCORRECT PUT ACTUAL POINT INTO THE CUTTING CLOUD
C
         IF(LDATA(I,JX).EQ.LCUT)THEN
            LWRONG(I)=1
            DO 110 K=1,NS
            Y16MIDP(I,K)=XMAT(I,K)
  110       CONTINUE
         ENDIF
C
C  IF NOT-VOTING PUT LEGISLATOR POINT ON THE CURRRENT CUTTING PLANE
C
         IF(LDATA(I,JX).EQ.0)THEN
            DO 111 K=1,NS
            Y16MIDP(I,K)=XMAT(I,K)+DB2B1*ZVEC(JX,K)
  111       CONTINUE
         ENDIF
      ENDIF
      IF(XXY(I).GT.WS(JX))THEN
C
C  IF CORRECT PLACE LEGISLATOR POINT ON THE CURRENT CUTTING PLANE
C
         IF(LDATA(I,JX).EQ.LCUT)THEN
            DO 112 K=1,NS
            Y16MIDP(I,K)=XMAT(I,K)+DB2B1*ZVEC(JX,K)
  112       CONTINUE
         ENDIF
C
C  IF INCORRECT PUT ACTUAL POINT INTO THE CUTTING CLOUD
C
         IF(LDATA(I,JX).EQ.KCUT)THEN
            LWRONG(I)=1
            DO 113 K=1,NS
            Y16MIDP(I,K)=XMAT(I,K)
  113       CONTINUE
         ENDIF
C
C  IF NOT-VOTING PUT LEGISLATOR POINT ON THE CURRRENT CUTTING PLANE
C
         IF(LDATA(I,JX).EQ.0)THEN
            DO 214 K=1,NS
            Y16MIDP(I,K)=XMAT(I,K)+DB2B1*ZVEC(JX,K)
  214       CONTINUE
         ENDIF
      ENDIF
C
  108 CONTINUE
C
C  MASS CENTER THE CUTTING PLANE MATRIX (Y16MIDP(,) HAS ALL POINTS)
C
      DO 215 K=1,NS
      SUM=0.0
      DO 216 I=1,NP
      SUM=SUM+Y16MIDP(I,K)
  216 CONTINUE
      DO 217 I=1,NP
      Y16MIDP(I,K)=Y16MIDP(I,K)-SUM/FLOAT(NP)
      W16MIDP(I,K)=Y16MIDP(I,K)
      SUMX(K)=SUMX(K)+Y16MIDP(I,K)**2
  217 CONTINUE
      SUMX(K)=SUMX(K)/FLOAT(NP)
  215 CONTINUE
C
C  CONSTRUCT PARTIAL CUTTING PLANE MATRIX (X16MIDP(,))
C
      KK=0
      KHIT=0
C
      DO 316 I=1,NP
      IF(LWRONG(I).EQ.1)THEN
         KK=KK+1
         DO 317 K=1,NS
         X16MIDP(KK,K)=Y16MIDP(I,K)
  317    CONTINUE
      ENDIF
  316 CONTINUE
      DO 201 I=1,NP
      IF(LWRONG(I).EQ.0)THEN
         KK=KK+1
         DO 219 K=1,NS
         X16MIDP(KK,K)=Y16MIDP(I,K)
  219    CONTINUE
         IF(KK.EQ.KASTRO)GO TO 203
      ENDIF
  201 CONTINUE
  203 CONTINUE
C
C  MASS CENTER THE PARTIAL CUTTING PLANE MATRIX
C
      DO 815 K=1,NS
      SUM=0.0
      DO 816 I=1,KASTRO
      SUM=SUM+X16MIDP(I,K)
  816 CONTINUE
      DO 817 I=1,KASTRO
      X16MIDP(I,K)=X16MIDP(I,K)-SUM/FLOAT(KASTRO)
      SUMX(K+NS)=SUMX(K+NS)+X16MIDP(I,K)**2
  817 CONTINUE
      SUMX(K+NS)=SUMX(K+NS)/FLOAT(KASTRO)
  815 CONTINUE
C
C  RUN REGRESSION TO ELIMINATE DIMENSION WITH LEAST VARIANCE
C
C
C  CALL SINGULAR VALUE DECOMPOSITION ROUTINE
C
      XTOL=.001
      CALL LSVRR(NP,NS,Y16MIDP,1879,21,XTOL,IRANK,YHAT,Y16MIDP,
     C           1879,VVV,25)
C
C      SUM2=0.0
C      DO 10 I=1,NP
C      DO 11 J=1,NS
C      SUM=0.0
C      DO 12 K=1,NS
C      SUM=SUM+Y16MIDP(I,K)*YHAT(K)*VVV(J,K)
C  12  CONTINUE
C      SUM2=SUM2+(W16MIDP(I,J)-SUM)**2
C  11  CONTINUE
C  10  CONTINUE
C
      DO 115 K=1,NS
      SUMX(K)=SUMX(K+NS)
C
C  WRONG WAY
C
C      ZVEC(JX,K)=VVV(NS,K)
C
C  RIGHT WAY
C
      ZVEC(JX,K)=VVV(K,NS)
C
  115 CONTINUE
C
C
C  RUN REGRESSION TO ELIMINATE DIMENSION WITH LEAST VARIANCE
C
      CALL LSVRR(KASTRO,NS,X16MIDP,1879,21,XTOL,IRANK,YHAT,X16MIDP,
     C           1879,VVV,25)
C
      IF(IJL.GT.25)THEN
         DO 114 K=1,NS
C
C  WRONG WAY
C
C         ZVEC(JX,K)=VVV(NS,K)
C
C  RIGHT WAY
C
         ZVEC(JX,K)=VVV(K,NS)
  114    CONTINUE
      ENDIF
C
  999 CONTINUE
C
      CALL RSORT(FV1,NCUT,LLM)
C
      DO 281 JJ=1,NCUT
      IF(FV1(1).LT.FV1(JJ))GO TO 282
  281 CONTINUE
  282 KIN=JJ-1
      LLM(1)=LLM(KIN)
C
      DO 387 K=1,NS
      ZVEC(JX,K)=UUU(LLM(1),K)
  387 CONTINUE
      WS(JX)=FV2(LLM(1))
      KCUT=KKKCUT(LLM(1))
      LCUT=LLLCUT(LLM(1))
      DO 137 I=1,NP
      SUM=0.0
      DO 138 K=1,NS
      SUM=SUM+XMAT(I,K)*ZVEC(JX,K)
  138 CONTINUE
      XPROJ(I,JX)=SUM
      XXY(I)=SUM
  137 CONTINUE      
      KTT=KTT+LLN(LLM(1))
      KITTY1=LLN(LLM(1))
      IJUST=3
      KT=KT+JCH+JCL+JEH+JEL
      KITTY2=JCH+JCL+JEH+JEL
      RETURN
      END
C
C **************************************************************************
C  SUBROUTINE SNYDER -- PERFORMS THE HECKMAN-SNYDER DECOMPOSITION
C **************************************************************************
C
      SUBROUTINE SNYDER(NP,NRCALL,NS,ZMAT,WVEC)
      DIMENSION XCOL(43011),XROW(43011),KROW(43011),XCENTER(1879,21011),
     C          XCOV(1879,1879),ZMAT(1879,1879),FV1(1879),FV2(1879),
     C          WVEC(1879),XAGREE(1879,1879),ROWMEAN(1879),
     C          YCENTER(1879,1879),WVEC2(1879),ZMAT2(1879,1879)
      COMMON /AREA1/ JR,LR 
      COMMON /XXCOM/ LDATA(1899,21011)
C
  101 FORMAT(' PERFORMANCE INDEX EIGENVALUE/VECTOR ROUTINE='
     C       ,3I5,I6)
  102 FORMAT(I4,6F10.4)
  103 FORMAT(I4,F10.4)
 1000 FORMAT(3F10.4)
 1011 FORMAT(' BIG ERROR',3I5)
      DO 3 I=1,43011
      XCOL(I)=0.0
      XROW(I)=0.0
      KROW(I)=0
  3   CONTINUE
      DO 30 I=1,1879
      WVEC(I)=0.0
      ROWMEAN(I)=0.0
      DO 31 J=1,1879
      ZMAT(I,J)=0.0
  31  CONTINUE
  30  CONTINUE
C
C  PERFORM THE PSEUDO-DOUBLE CENTER
C
C  CALCULATE THE ROLL CALL MEANS (#YEAS/#VOTING)
C
      XMAT=0.0
      KMAT=0
      XCHK1=0.0
      DO 1 J=1,NRCALL
      SUM=0.0
      KK=0
      DO 2 I=1,NP
      IF(LDATA(I,J).NE.0)THEN
         KK=KK+1
         KROW(I)=KROW(I)+1
      ENDIF
      IF(LDATA(I,J).EQ.1)THEN
         SUM=SUM+1.0
         XROW(I)=XROW(I)+1.0
      ENDIF
  2   CONTINUE
      XMAT=XMAT+SUM
      KMAT=KMAT+KK
      XCOL(J)=SUM/FLOAT(KK)
      XCHK1=XCHK1+XCOL(J)
  1   CONTINUE
      XCHK2=0.0
      DO 4 I=1,NP
      XROW(I)=XROW(I)/FLOAT(KROW(I))
      XCHK2=XCHK2+XROW(I)
  4   CONTINUE
C
C  MATRIX MEAN
C
      XCHK1=XCHK1/FLOAT(NRCALL)
      XCHK2=XCHK2/FLOAT(NP)
      XMAT=XMAT/FLOAT(KMAT)
      WRITE(* ,1000)XCHK1,XCHK2,XMAT
      WRITE(23,1000)XCHK1,XCHK2,XMAT
C
C  COMPUTE HECKMAN-SNYDER PSEUDO DOUBLE-CENTER
C
      DO 5 I=1,NP
      DO 6 J=1,NRCALL
      XCENTER(I,J)=0.0
      IF(LDATA(I,J).EQ.0)THEN
         XCENTER(I,J)=XMAT-XCOL(J)
         GO TO 6
      ENDIF
      IF(LDATA(I,J).EQ.1)THEN
         XCENTER(I,J)=1.0-XCOL(J)
      ENDIF
      IF(LDATA(I,J).EQ.6)THEN
         XCENTER(I,J)=0.0-XCOL(J)
      ENDIF
  6   CONTINUE
  5   CONTINUE
C
C
C  COMPUTE HECKMAN-SNYDER COVARIANCE MATRIX
C
      ALLMEAN=0.0
      KZERO=0
      DO 7 I=1,NP
      RSUM=0.0
      DO 8 J=1,NP
      SUM=0.0
      KK=0
      KKK=0
      DO 9 K=1,NRCALL
      IF(LDATA(I,K).EQ.0)GO TO 9
      IF(LDATA(J,K).EQ.0)GO TO 9
      KK=KK+1
C
C  SET UP SYMMETRIC MATRIX OF AGREEMENT SCORES
C
      IF(LDATA(I,K).EQ.LDATA(J,K))THEN
         KKK=KKK+1
      ENDIF
      SUM=SUM+XCENTER(I,K)*XCENTER(J,K)
  9   CONTINUE
      IF(KK.EQ.0)THEN
         ASUM=0.0
         DO 32 KP=1,NRCALL
         ASUM=ASUM+XCENTER(I,KP)*XCENTER(J,KP)
  32     CONTINUE
         XCOV(I,J)=ASUM/FLOAT(NRCALL)
         XAGREE(I,J)=0.25
         GO TO 88
      ENDIF
      XCOV(I,J)=SUM/FLOAT(KK)
      XAGREE(I,J)=(1.0 - (FLOAT(KKK)/FLOAT(KK)))**2
  88  CONTINUE
      RSUM=RSUM+XAGREE(I,J)
  8   CONTINUE
      ROWMEAN(I)=RSUM/FLOAT(NP)
      ALLMEAN=ALLMEAN+ROWMEAN(I)
  7   CONTINUE
      ALLMEAN=ALLMEAN/FLOAT(NP)
C
C  SETUP DOUBLE-CENTERED AGREEMENT SCORE MATRIX
C
      DO 33 I=1,NP
      DO 34 J=1,NP
      YCENTER(I,J)=(XAGREE(I,J)-ROWMEAN(I)-ROWMEAN(J)+ALLMEAN)/(-2.0)
  34  CONTINUE
  33  CONTINUE
C
C  EIGENVECTOR-EIGENVALUE DECOMPOSITION HECKMAN-SNYDER
C
      CALL RS(1879,NP,XCOV,WVEC2,1,ZMAT2,FV1,FV2,IER)
      WRITE(23,101)NS,NP,IER,KZERO
      WRITE(*,101)NS,NP,IER,KZERO
C
C
C  EIGENVECTOR-EIGENVALUE DECOMPOSITION DOUBLE-CENTERED AGREEMENT
C     SCORE MATRIX
C
      CALL RS(1879,NP,YCENTER,WVEC,1,ZMAT,FV1,FV2,IER)
      WRITE(23,101)NS,NP,IER,KZERO
      WRITE(*,101)NS,NP,IER,KZERO
      SUM=0.0
      SUM2=0.0
      DO 20 I=1,NP
      SUM=SUM+ABS(WVEC(I))
      SUM2=SUM2+ABS(WVEC2(I))
  20  CONTINUE
      YPER=0.0
      YPER2=0.0
      DO 10 I=1,20
      XPER=(WVEC(NP+1-I)/SUM)*100.0
      YPER=YPER+XPER
      XPER2=(WVEC2(NP+1-I)/SUM2)*100.0
      YPER2=YPER2+XPER2
      WRITE(23,102)I,WVEC(NP+1-I),XPER,YPER,
     C                WVEC2(NP+1-I),XPER2,YPER2
      WRITE(* ,102)I,WVEC(NP+1-I),XPER,YPER,
     C                WVEC2(NP+1-I),XPER2,YPER2
  10  CONTINUE
      RETURN
      END
C
C  ************************************************************************
C    EDITH SUBROUTINE--PERFORMS ONE DIMENSION NOMINAL UNFOLDING -- FINDS
C                      OPTIMAL CLASSIFICATIONS FOR MATRIX OF SENATE
C                      PAIRS
C  ************************************************************************
C
      SUBROUTINE EDITH(NP,NV,NS,XMAT,X,XPT,ZPT,AB,KTOTC,
     C                 KCUTTER,LCUTTER,LERROR,MSUM)
      DIMENSION X(21555),XYM(21011),XNM(21011),YSS(21555),
     C          KA(21555),WS(43011),L(21555),LL(21555),
     C          LERROR(1879,21011),XMAT(1879,1879),ZS(21555),
     C          LLV(21555),LLE(21555),LLVB(21555),LLEB(21555),
     C          KKA(21555),KYES(21011),KNO(21011),XCTL(50),
     C          LV(21555),MM(21555),ZPT(21011),XPT(21555),
     C          YS(21555),KCUTTER(21011),LCUTTER(21011),
     C          XJCH(25),XJEH(25),XJCL(25),XJEL(25),
     C          YSJAVA(21555),MMJAVA(21555),LJAVA(21555)
      COMMON /XXCOM/ LDATA(1899,21011)
  330 FORMAT(' WARNING--MISMATCH ON NUMBER OF VOTES READ',2I5)
  340 FORMAT(' WARNING--MISMATCH ON ERROR PERCENTAGES',I5,2F7.3)
  825 FORMAT(I4,I5,2I8,6F7.3)
 8250 FORMAT(I3,' ROLL CALLS ',I3,2I8,5F7.3)
 8251 FORMAT(I3,' ROLL CALLS ',I3,2I8,5F9.5)
 8252 FORMAT(I3,' LEGISLATORS',I3,2I8,8F9.5)
 8253 FORMAT(I3,' LEGISLATORS',I3,2I8,6F7.3)
      KSTOPR=1
      KSTOP=14
      DO 1 I=1,NP
      YSS(I)=X(I)
      XPT(I)=X(I)
      L(I)=I
      LL(I)=I
  1   CONTINUE
C
      CALL RSORT(YSS,NP,LL)
C
C
      ITR=0
  121 CONTINUE
      ITR=ITR+1
      XLOW=YSS(1)
      XHIGH=YSS(NP)
      KT=0
      KTT=0
      JCH=0
      JEH=0
      JCL=0
      JEL=0
      MSUM=0
      NSUM=0
      IVOT=0
      NUNAM=0
C
C
C  ROLL CALL LOOP -- ESTIMATE BEST CUTTING POINT GIVEN FIXED LEGISLATOR
C                    CONFIGURATION
C
      DO 600 IIII=1,NV
      IVOT=IVOT+1
      ESUM=0.0
      FSUM=0.0
      KSUM=0
      LSUM=0
C
C  RECODE AND COMPUTE YES AND NO MEANS 
C
      DO 16 J=1,NP
      LV(J)=LDATA(J,IVOT)
      IF(LV(J).EQ.1.OR.LV(J).EQ.2.OR.LV(J).EQ.3)KK=1
      IF(LV(J).EQ.4.OR.LV(J).EQ.5.OR.LV(J).EQ.6)KK=6
      IF(LV(J).EQ.0.OR.LV(J).EQ.7.OR.LV(J).EQ.8.OR.LV(J).EQ.9)KK=9
      KKA(J)=KK
      IF(KK.EQ.1)ESUM=ESUM+XPT(J)
      IF(KK.EQ.1)KSUM=KSUM+1
      IF(KK.EQ.6)LSUM=LSUM+1
      IF(KK.EQ.6)FSUM=FSUM+XPT(J)
  16  CONTINUE
      DO 255 J=1,NP
  255 KA(J)=KKA(LL(J))
      KYES(IVOT)=KSUM
      KNO(IVOT)=LSUM
C
C  CHECK FOR UNANIMOUS VOTES AND DISCARD THEM
C
      IF(KSUM.EQ.0.OR.LSUM.EQ.0)XYM(IVOT)=99.0
      IF(KSUM.EQ.0.OR.LSUM.EQ.0)XNM(IVOT)=99.0
      IF(KSUM.EQ.0.OR.LSUM.EQ.0)NUNAM=NUNAM+1
      IF(KSUM.EQ.0.OR.LSUM.EQ.0)GO TO 600
C
C  STORE YES AND NO MEANS
C
      XYM(IVOT)=ESUM/FLOAT(KSUM)
      XNM(IVOT)=FSUM/FLOAT(LSUM)
C
C  CUMULATIVE SUM OF MAJORITY VOTE BY ROLL CALL
C
      IF(KSUM-LSUM)201,202,202
  202 MSUM=MSUM+KSUM
      GO TO 203
  201 MSUM=MSUM+LSUM
  203 CONTINUE
C
C  BEGIN JANICE LOOP FOR ROLL CALLS
C
C  GET ONE POINT RESULTS
C
      JCH=0
      JEH=0
      JCL=0
      JEL=0
      KJAVA=0
      DO 1663 JAVA=1,NP
      IF(KA(JAVA).NE.9)THEN
        KJAVA=KJAVA+1
        YSJAVA(KJAVA)=YSS(JAVA)
        MMJAVA(KJAVA)=KA(JAVA)
        LJAVA(KJAVA)=LL(JAVA)
      ENDIF
 1663 CONTINUE
      YSJAVA(KJAVA+1)=YSJAVA(KJAVA)+.01
      MMJAVA(KJAVA+1)=9
      LJAVA(KJAVA+1)=NP+1
      NV1=KJAVA+1
      IROTC=0
      CALL JAN1PT(NV1,NV,NS,IVOT,XMAT,YSJAVA,MMJAVA,WS,
     C            LLV,LLVB,LLE,LLEB,LERROR,
     C            ZS,JCH,JEH,JCL,JEL,IROTC,KCUT,LCUT,LJAVA,
     C            XJCH,XJEH,XJCL,XJEL)
      KT=KT+JCH+JCL+JEH+JEL
      KTT=KTT+JEH+JEL
      KTOTC=KT
      KCUTTER(IVOT)=KCUT
      LCUTTER(IVOT)=LCUT
C
C  END OF ROLL CALL LOOP
C
  600 CONTINUE
C
C
      XT=FLOAT(KT)
      XTT=FLOAT(KTT)
      AB=1.0-(XTT/XT)
      XSUM=FLOAT(MSUM)
      XSUM=XSUM/XT
      YSUM=AB-XSUM
      PRE1=YSUM/(1.0-XSUM)
C
C  WRITE OUT: ITERATION #; NUMBER OF VOTES; 
C             TOTAL CHOICES; TOTAL ERROR CHOICES; PERCENT CORRECT; 
C             PERCENT MAJORITY MODEL; PROPORTIONAL REDUCTION IN ERROR
C
      IF(NS.GT.1)THEN
         WRITE(*,825)ITR,IVOT,KT,KTT,AB,XSUM,PRE1,XLOW,XHIGH
         WRITE(23,825)ITR,IVOT,KT,KTT,AB,XSUM,PRE1,XLOW,XHIGH
      ENDIF
      IF(NS.EQ.1)THEN
         WRITE(21,8251)ITR,NS,KTT,KT,1.0-AB,AB,PRE1
         WRITE(23,8251)ITR,NS,KTT,KT,1.0-AB,AB,XSUM,PRE1
         WRITE(*,8250)ITR,NS,KTT,KT,1.0-AB,AB,PRE1
      ENDIF
      IF(IVOT.NE.NV)THEN
         WRITE(*,330)IVOT,NV
         STOP
      ENDIF
      XCTL(ITR)=AB
C
C
C
C  WRITE ROLL CALL MARGINS AND ERRORS TO DISK
C
      DO 130 I=1,NV
      IF(KYES(I).LE.KNO(I))KMIN=KYES(I)
      IF(KYES(I).GT.KNO(I))KMIN=KNO(I)
      KRITE=LLV(I)+LLVB(I)
      KWRONG=LLE(I)+LLEB(I)
      IF((KRITE+KWRONG).GT.0.0)XBAD=FLOAT(KWRONG)/FLOAT(KRITE+KWRONG)
      IF(KYES(I).EQ.0.OR.KNO(I).EQ.0)XBAD=0.0
      IF(KYES(I).EQ.0.OR.KNO(I).EQ.0)ZS(I)=0.0
      XMIN=0.0
      IF((KYES(I)+KNO(I)).GT.0)XMIN=FLOAT(KMIN)/FLOAT(KYES(I)+KNO(I))
      IF(ABS(XBAD-ZS(I)).GT..001)THEN
         WRITE(*,340)I,XBAD,ZS(I)
         STOP
      ENDIF
      AAA=ABS(WS(I)-XLOW)
      BBB=ABS(WS(I)-XHIGH)
      IF(XBAD.GT.XMIN.AND.AAA.LT.BBB)WS(I)=XLOW
      IF(XBAD.GT.XMIN.AND.AAA.GE.BBB)WS(I)=XHIGH
      IF(XBAD.GT.XMIN)XBAD=XMIN
      IF(KYES(I).EQ.0.OR.KNO(I).EQ.0)WS(I)=0.0
      IF(KSTOPR.EQ.1)GO TO 130
C
C  WRITE OUT CONGRESS #, VOTE #, YES #, NO #, PROPORTION IN MINORITY,
C            PROPORTION ERROR, CUT POINT
C
C      WRITE(24,800)I,KYES(I),KNO(I),XMIN,XBAD,WS(I),XYM(I),XNM(I)
  800 FORMAT(I5,2I4,5F7.3)
  130 CONTINUE
C
C
C
C  BEGIN JANICE LOOP FOR LEGISLATORS
C
C
C
      ITR=ITR+1
      DO 124 I=1,NV
      YS(I)=WS(I)
      ZPT(I)=WS(I)
  124 L(I)=I
C
C  SORT ROLL CALL MIDPOINTS
C
      CALL RSORT(YS,NV,L)
C
      KT=0
      KTT=0
      JCH=0
      JCL=0
      JEH=0
      JEL=0
      DO 9999 I=1,NP
      IVOT=I
C
C
      DO 166 J=1,NV
      LV(J)=LDATA(IVOT,J)
      IF(LV(J).EQ.1.OR.LV(J).EQ.2.OR.LV(J).EQ.3)KK=1
      IF(LV(J).EQ.4.OR.LV(J).EQ.5.OR.LV(J).EQ.6)KK=6
      IF(LV(J).EQ.0.OR.LV(J).EQ.7.OR.LV(J).EQ.8.OR.LV(J).EQ.9)KK=9
      IF(XYM(J).EQ.99.0.OR.XNM(J).EQ.99.0)KK=9
      KA(J)=KK
  166 CONTINUE
C
C  DETERMINE DIRECTIONALITY FOR EACH MIDPOINT -- STORE IN MM VECTOR
C
C
C  CUMULATIVE SUM OF MAJORITY VOTE BY INDIVIDUALS
C
      DO 161 J=1,NV
C      IF(XYM(L(J))-XNM(L(J)))163,162,162
C  163 KCUT=1
C      LCUT=6
C      GO TO 164
C  162 KCUT=6
C      LCUT=1
       KCUT=KCUTTER(L(J))
       LCUT=LCUTTER(L(J))
C
C  NOTE THAT THIS CODING OF MM(.) PRODUCES THE PATTERNS
C
C          6666666666...
C          1666666666...
C          1166666666...
C          1116666666...
C          1111666666...
C          1111166666...
C
C  ETC., IF THE LEGISLATOR IS VOTING PERFECTLY
C
  164 IF(KA(L(J)).EQ.KCUT)MM(J)=6
      IF(KA(L(J)).EQ.LCUT)MM(J)=1
      IF(KA(L(J)).EQ.9)MM(J)=9
      IF(KYES(L(J)).LT.KNO(L(J)).AND.KA(L(J)).EQ.6)NSUM=NSUM+1
      IF(KYES(L(J)).GE.KNO(L(J)).AND.KA(L(J)).EQ.1)NSUM=NSUM+1
  161 CONTINUE
C
C  GET ONE POINT RESULTS
C
      JCH=0
      JEH=0
      JCL=0
      JEL=0
      KJAVA=0
      DO 1661 JAVA=1,NV
      IF(MM(JAVA).NE.9)THEN
        KJAVA=KJAVA+1
        YSJAVA(KJAVA)=YS(JAVA)
        MMJAVA(KJAVA)=MM(JAVA)
        LJAVA(KJAVA)=L(JAVA)
      ENDIF
 1661 CONTINUE
C
C  TRICK JANICE TO FIX ROLL CALL PROBLEM
C
      YSJAVA(KJAVA+1)=YSJAVA(KJAVA)+.01
      MMJAVA(KJAVA+1)=9
      LJAVA(KJAVA+1)=NV+1
      NV1=KJAVA+1
      IROTC=1
      CALL JAN1PT(NV1,NP,NS,IVOT,XMAT,YSJAVA,MMJAVA,WS,LLV,LLVB,
     C          LLE,LLEB,LERROR,
     C            ZS,JCH,JEH,JCL,JEL,IROTC,KCUT,LCUT,LJAVA,
     C            XJCH,XJEH,XJCL,XJEL)
      KT=KT+JCH+JCL+JEH+JEL
      KTT=KTT+JEH+JEL
C
C  END OF LEGISLATOR LOOP
C
 9999 CONTINUE
C
C  CALCULATE REGRESSION OF PREVIOUS LEGISLATOR CONFIGURATION ON 
C        CURRENT LEGISLATOR CONFIGURATION
C
      ASUM=0.0
      BSUM=0.0
      CSUM=0.0
      DSUM=0.0
      ESUM=0.0
      KKNP=0
      DO 125 I=1,NP
      LL(I)=I
C
      YSS(I)=WS(I)
      KKNP=KKNP+1
      ASUM=ASUM+XPT(I)*WS(I+NP)
      BSUM=BSUM+XPT(I)
      CSUM=CSUM+WS(I+NP)
      DSUM=DSUM+XPT(I)*XPT(I)
      ESUM=ESUM+WS(I+NP)*WS(I+NP)
      XPT(I)=WS(I)
  125 CONTINUE
      AA=FLOAT(KKNP)*ASUM-BSUM*CSUM
      BB=FLOAT(KKNP)*DSUM-BSUM*BSUM
      CC=FLOAT(KKNP)*ESUM-CSUM*CSUM
      IF(BB*CC.GT.0.0)RR=(AA*AA)/(BB*CC)
      BETA=AA/CC
      ALFA=(BSUM-BETA*CSUM)/FLOAT(KKNP)
      XT=FLOAT(KT)
      XTT=FLOAT(KTT)
      AB=1.0-(XTT/XT)
      XSUM=FLOAT(NSUM)/FLOAT(KT)
      YSUM=AB-XSUM
      PRE=YSUM/(1.0-XSUM)
      NUNP=NP-KKNP
C
C  WRITE OUT: ITERATION #; NUMBER OF LEGISLATORS; 
C             TOTAL CHOICES; TOTAL ERROR CHOICES; PERCENT CORRECT; 
C             PERCENT MAJORITY MODEL; PROPORTIONAL REDUCTION IN ERROR
C
      IF(NS.GT.1)THEN
         WRITE(* ,825)ITR,NP,KT,KTT,AB,XSUM,PRE,ALFA,BETA,RR
         WRITE(23,825)ITR,NP,KT,KTT,AB,XSUM,PRE,ALFA,BETA,RR
      ENDIF
      IF(NS.EQ.1)THEN
         WRITE(21,8252)ITR,NS,KTT,KT,1.0-AB,AB,PRE,SQRT(RR)
         WRITE(23,8252)ITR,NS,KTT,KT,1.0-AB,AB,XSUM,PRE,ALFA,BETA,RR
         WRITE(*,8253)ITR,NS,KTT,KT,1.0-AB,AB,PRE,SQRT(RR)
      ENDIF
      XCTL(ITR)=AB
C      NOISY=69
C      IF(NOISY.EQ.69)STOP
C
C  WRITE LEGISLATOR MARGINS TO DISK
C
      DO 128 I=1,NP
C      WS(I+NP)=WS(I+NP)*BETA+ALFA
C      YSS(I)=YSS(I)*BETA+ALFA
      KHIGH=LLV(I)+LLE(I)
      KLOW=LLVB(I)+LLEB(I)
      IF(KHIGH.LE.KLOW)XMIN=FLOAT(KHIGH)/FLOAT(KHIGH+KLOW)
      IF(KHIGH.GT.KLOW)XMIN=FLOAT(KLOW)/FLOAT(KHIGH+KLOW)
      XBAD=FLOAT(LLE(I)+LLEB(I))/FLOAT(KHIGH+KLOW)
      AAA=ABS(WS(I)-XLOW)
      BBB=ABS(WS(I)-XHIGH)
      IF(XBAD.GT.XMIN.AND.AAA.LT.BBB)WS(I)=XLOW
      IF(XBAD.GT.XMIN.AND.AAA.GE.BBB)WS(I)=XHIGH
      IF(XBAD.GT.XMIN)XBAD=XMIN
  729 CONTINUE
      IF(KSTOPR.EQ.1)GO TO 128
C      WRITE(23,800)I,KHIGH,KLOW,XMIN,XBAD,WS(I)
  128 CONTINUE
      CALL RSORT(YSS,NP,LL)
      AA=(YSS(1)+YSS(NP))/2.0
      BB=YSS(NP)-AA
      DO 730 I=1,NP
      YSS(I)=(YSS(I)-AA)/BB
      XPT(I)=(XPT(I)-AA)/BB
      WS(I)=(WS(I)-AA)/BB
      WS(I+NP)=WS(I)
  730 CONTINUE
      IF(ITR.LE.5)GO TO 121
      IF(KSTOPR.EQ.0)RETURN
      IF((XCTL(ITR)-XCTL(ITR-2)).LE..0001)KSTOPR=0
      IF(ITR.LE.KSTOP)GO TO 121
      RETURN
      END
C
C  **************************************************************************
C    SUBROUTINE JAN1PT -- FINDS OPTIMAL CUTTING POINT FOR ONE DIMENSION
C  **************************************************************************
C
      SUBROUTINE JAN1PT(NP,NV,NS,IVOT,XMAT,YSS,KA,WS,LLV,LLVB,LLE,LLEB,
     C                  LERROR,ZS,JCH,JEH,JCL,JEL,IROTC,KCCUT,LCCUT,
     C                  LLL,XJCH,XJEH,XJCL,XJEL)
      DIMENSION YSS(21555),KA(21555),WS(43011),LV(21555),LVB(21555),
     C          LEB(21555),Z(21555),Y(21555),LLV(21555),LLE(21555),
     C          LLVB(21555),LE(21555),LERROR(1879,21011),LJEP(21555),
     C          LLEB(21555),ZS(21555),LLL(21555),XMAT(1879,1879),
     C          XJCH(25),XJEH(25),XJCL(25),XJEL(25),AAJEP(101),
     C          ABJEP(101),LAJEP(101),LBJEP(101),LCJEP(101),
     C          LDJEP(101),ABABJEP(101),MJEP(101)
C
      JROTC=1
      IF(IROTC.EQ.2)THEN
         JROTC=0
         IROTC=1
      ENDIF
      NPN=NP+1
      NPP=NP-1
      KCUT=1
      LCUT=6
      NOTE=2
      IF(IROTC.EQ.1)THEN
         NOTE=1
      ENDIF
      AA1=0.0
      AB1=0.0
      LA1=0
      LB1=0
      LC1=0
      LD1=0
      AA2=999.0
      AB2=0.0
      LA2=0
      LB2=0
      LC2=0
      LD2=0
      DO 999 III=1,NOTE
      IF(III.EQ.2)THEN
         KCUT=6
         LCUT=1
      ENDIF
C
C  CHECK ALL POSSIBLE INTERIOR CUT POINTS  --  THE NP INPUT POINTS
C      ARE HELD FIXED.  THERE ARE NP POSSIBLE CUT-POINTS BEGINNING
C      WITH CUT-POINT 1 WHICH IS .001 UNITS TO THE LEFT OF POINT 1.
C      CUT-POINT 2 IS BETWEEN POINTS 1 AND 2, ETC.
C
C     1   2   3   4   5   6   7   8   9   10   11 ...... NP-1   NP
C    *  *   *   *   *   *   *   *   *   *    *                *
C    1  2   3   4   5   6   7   8   9  10   11  ...........  NP
C
C  IF KCUT=1 AND LCUT=6, THE FOLLOWING NP PATTERNS ARE TESTED
C
C PATTERN
C   1         6666666666666666666666
C   2         1666666666666666666666
C   3         1166666666666666666666
C   4         1116666666666666666666
C   5         1111666666666666666666
C   6         1111166666666666666666
C   7         1111116666666666666666
C   .           .....
C   .           .....
C   .           .....
C  NP-1       1111111111111111111166
C   NP        1111111111111111111116
C
C  BECAUSE THE PROGRAM TRIES BOTH KCUT=1/LCUT=6 AND KCUT=6/LCUT=1, THIS
C  WILL ALSO TEST THE ONE MISSING PATTERN ABOVE, VIZ., ALL "1"S.
C
C
      KSE=0
      KSV=0
      LSV=0
      LSE=0
      KMARK=1
      I=0
  10  I=I+1
      IF(I-NP-1)61,12,12
  61  Z(I)=999.0
      IF(I.EQ.1)THEN
         Y(I)=YSS(1)-.001
      ENDIF
      IF(I.GT.1)THEN
         Y(I)=(YSS(I)+YSS(I-1))/2.0
      ENDIF
C      IF(KA(I).EQ.9)GO TO 10
      IF(KMARK.EQ.1)THEN
         DO 3 J=I,NP
         IF(KA(J).EQ.9)GO TO 3
         IF(LCUT-KA(J))33,5,33
  33     IF(KCUT-KA(J))3,6,3
  5      LSV=LSV+1
         GO TO 3
  6      LSE=LSE+1
  3      CONTINUE
         KMARK=0
         GO TO 31
      ENDIF
      IF(KA(I-1).EQ.KCUT)THEN
         KSV=KSV+1
         LSE=LSE-1
      ENDIF
      IF(KA(I-1).EQ.LCUT)THEN
         KSE=KSE+1
         LSV=LSV-1
      ENDIF
C
  31  CONTINUE
      LJEP(I)=I
      LV(I)=KSV
      LVB(I)=LSV
      LE(I)=KSE
      LEB(I)=LSE
      KT=LV(I)+LE(I)+LVB(I)+LEB(I)
      Z(I)=FLOAT(LE(I)+LEB(I))/FLOAT(KT)
C
      IF(JROTC.EQ.0)THEN
         ZS(I)=Y(I)
         LLV(I)=LV(I)
         LLE(I)=LE(I)
         LLVB(I)=LVB(I)
         LLEB(I)=LEB(I)
      ENDIF
      GO TO 10
  12  CONTINUE
C
C  FIND BEST CUT POINT
C
      CALL RSORT(Z,NP,LJEP)
      KIN=1
      MJEP(1)=1
      AAJEP(KIN)=Z(1)
      ABJEP(KIN)=Y(LJEP(1))
      ABABJEP(KIN)=ABS(ABJEP(KIN))
      LAJEP(KIN)=LV(LJEP(1))
      LBJEP(KIN)=LE(LJEP(1))
      LCJEP(KIN)=LVB(LJEP(1))
      LDJEP(KIN)=LEB(LJEP(1))
C
C  CHECK IF THERE ARE MULTIPLE CUT-POINTS WITH SAME CLASSIFICATION AND
C    SELECT THAT CUT-POINT CLOSEST TO THE INTERIOR OF THE SPACE
C
      DO 63 I=2,NP
      IF(ABS(Z(1)-Z(I)).LE..00001)THEN
         KIN=KIN+1
         MJEP(KIN)=KIN
         AAJEP(KIN)=Z(I)
         ABJEP(KIN)=Y(LJEP(I))
         ABABJEP(KIN)=ABS(ABJEP(KIN))
         LAJEP(KIN)=LV(LJEP(I))
         LBJEP(KIN)=LE(LJEP(I))
         LCJEP(KIN)=LVB(LJEP(I))
         LDJEP(KIN)=LEB(LJEP(I))
         IF(KIN.GT.100)GO TO 633
         GO TO 63
      ENDIF
      IF(Z(1).LT.Z(I))GO TO 633
  63  CONTINUE
  633 CONTINUE
      IF(KIN.EQ.1)THEN
         AA=AAJEP(1)
         AB=ABJEP(1)
         LA=LAJEP(1)
         LB=LBJEP(1)
         LC=LCJEP(1)
         LD=LDJEP(1)
      ENDIF
      IF(KIN.GT.1)THEN
         CALL RSORT(ABABJEP,KIN,MJEP)
         AA=AAJEP(MJEP(1))
         AB=ABJEP(MJEP(1))
         LA=LAJEP(MJEP(1))
         LB=LBJEP(MJEP(1))
         LC=LCJEP(MJEP(1))
         LD=LDJEP(MJEP(1))
      ENDIF
C
      IF(III.EQ.1)THEN
         AA1=AA
         AB1=AB
         LA1=LA
         LB1=LB
         LC1=LC
         LD1=LD
      ENDIF
      IF(III.EQ.2)THEN
         AA2=AA
         AB2=AB
         LA2=LA
         LB2=LB
         LC2=LC
         LD2=LD
      ENDIF
C
  999 CONTINUE
C
      IF(AA1.LE.AA2)THEN
         KCCUT=1
         LCCUT=6
         AA=AA1
         AB=AB1
         LA=LA1
         LB=LB1
         LC=LC1
         LD=LD1
      ENDIF
      IF(AA1.GT.AA2)THEN
         KCCUT=6
         LCCUT=1
         AA=AA2
         AB=AB2
         LA=LA2
         LB=LB2
         LC=LC2
         LD=LD2
      ENDIF
      IF(IROTC.EQ.1)THEN
         KCCUT=1
         LCCUT=6
         AA=AA1
         AB=AB1
         LA=LA1
         LB=LB1
         LC=LC1
         LD=LD1
      ENDIF
      WS(IVOT)=AB
      IF(IROTC.EQ.1)WS(IVOT+NV)=AB
      IF(JROTC.EQ.1)THEN
         ZS(IVOT)=AA
         LLV(IVOT)=LA
         LLE(IVOT)=LB
         LLVB(IVOT)=LC
         LLEB(IVOT)=LD
      ENDIF
      JCL=LA
      JEL=LB
      JCH=LC
      JEH=LD
C
      IF(IROTC.EQ.0)THEN
         DO 71 K=1,NS
         XJCH(K)=0.0
         XJEH(K)=0.0
         XJCL(K)=0.0
         XJEL(K)=0.0
  71     CONTINUE
         DO 64 I=1,NP
         LERROR(LLL(I),IVOT)=0
         IF(KA(I).EQ.9)GO TO 64
         IF(YSS(I).LT.AB)THEN
            IF(KA(I).EQ.KCCUT)THEN
               LERROR(LLL(I),IVOT)=0
               DO 70 K=1,NS
               XJCL(K)=XJCL(K)+XMAT(LLL(I),K)
   70          CONTINUE
            ENDIF
            IF(KA(I).EQ.LCCUT)THEN
               LERROR(LLL(I),IVOT)=1
               DO 72 K=1,NS
               XJEL(K)=XJEL(K)+XMAT(LLL(I),K)
   72          CONTINUE
            ENDIF
         ENDIF
         IF(YSS(I).GT.AB)THEN
            IF(KA(I).EQ.LCCUT)THEN
               LERROR(LLL(I),IVOT)=0
               DO 73 K=1,NS
               XJCH(K)=XJCH(K)+XMAT(LLL(I),K)
   73          CONTINUE
            ENDIF
            IF(KA(I).EQ.KCCUT)THEN
               LERROR(LLL(I),IVOT)=1
               DO 74 K=1,NS
               XJEH(K)=XJEH(K)+XMAT(LLL(I),K)
   74          CONTINUE
            ENDIF
         ENDIF
  64     CONTINUE
         DO 75 K=1,NS
         IF(JCL.GT.0)XJCL(K)=XJCL(K)/FLOAT(JCL)
         IF(JEL.GT.0)XJEL(K)=XJEL(K)/FLOAT(JEL)
         IF(JCH.GT.0)XJCH(K)=XJCH(K)/FLOAT(JCH)
         IF(JEH.GT.0)XJEH(K)=XJEH(K)/FLOAT(JEH)
  75     CONTINUE
      ENDIF
      IF(IROTC.EQ.1)THEN
         DO 65 I=1,NP
         IF(I.LE.NP-1)LERROR(IVOT,LLL(I))=0
         IF(KA(I).EQ.9)GO TO 65
         IF(YSS(I).LT.AB)THEN
            IF(KA(I).EQ.KCCUT)LERROR(IVOT,LLL(I))=0
            IF(KA(I).EQ.LCCUT)LERROR(IVOT,LLL(I))=1
         ENDIF
         IF(YSS(I).GT.AB)THEN
            IF(KA(I).EQ.LCCUT)LERROR(IVOT,LLL(I))=0
            IF(KA(I).EQ.KCCUT)LERROR(IVOT,LLL(I))=1
         ENDIF
  65     CONTINUE
      ENDIF
      RETURN
      END
C
C ****************************************************************************
C  SUBROUTINE CLEAN----CALLED BY MAIN.  THROWS OUT ALL ROLLCALLS BELOW A
C     CUTOFF POINT (XVMIN)--USUALLY .05 OR .025.  THROWS OUT ALL LEGISLATORS
C     WHO HAVE VOTED KVMIN TIMES OR LESS (KVMIN=10 USUALLY).  KNAME(,)--
C     CHARACTER ARRAY HOLDING INFORMATION ON LEGISLATORS IS REARRANGED TO
C     HOLD ONLY INCLUDED LEGISLATORS.  LDATA( , ) IS THEN FILLED WITH THE
C     INCLUDED ROLL CALL VOTES.
C ****************************************************************************
C
      SUBROUTINE CLEAN(XVMIN,KVMIN)
      DIMENSION KKSUM(21011),LLSUM(21011),MMSUM(1879),NNSUM(1879),
     C          KD(21011),KMARG(1879)
      CHARACTER*1 KNAME
      CHARACTER*10 LMARG(10)
      COMMON /XXCOM/ LDATA(1899,21011)
      COMMON /MINE/ NS,NRCALL,NP
      COMMON /VOTES/ KAV(21011),KAY(21011),KAN(21011),KNAME(1899,99),JNE
      LMARG(1)= '50 - 55   '
      LMARG(2)= '56 - 60   '
      LMARG(3)= '61 - 65   '
      LMARG(4)= '66 - 70   '
      LMARG(5)= '71 - 75   '
      LMARG(6)= '76 - 80   '
      LMARG(7)= '81 - 85   '
      LMARG(8)= '86 - 90   '
      LMARG(9)= '91 - 95   '
      LMARG(10)='96 - 99.5 '
      DO 34 I=1,21011
      KKSUM(I)=0
      LLSUM(I)=0
  34  CONTINUE
      NAS=0
      NRS=0
      NOB=0
      NMOB=0
      DO 1 I=1,NP
      KMARG(I)=0
      ISUM=0
      JSUM=0
      DO 2 J=1,NRCALL
      KD(J)=LDATA(I,J)
      IF(KD(J).EQ.1.OR.KD(J).EQ.2.OR.KD(J).EQ.3)KKSUM(J)=KKSUM(J)+1
      IF(KD(J).EQ.1.OR.KD(J).EQ.2.OR.KD(J).EQ.3)ISUM=ISUM+1
      IF(KD(J).EQ.4.OR.KD(J).EQ.5.OR.KD(J).EQ.6)LLSUM(J)=LLSUM(J)+1
  2   IF(KD(J).EQ.4.OR.KD(J).EQ.5.OR.KD(J).EQ.6)JSUM=JSUM+1
      MMSUM(I)=ISUM
  1   NNSUM(I)=JSUM
      DO 3 I=1,NP
      LL=MMSUM(I)+NNSUM(I)
      IF(LL.GT.KVMIN)NOB=NOB+1
      IF(LL.LE.KVMIN)NMOB=NMOB+1
      IF(LL.LE.KVMIN)GO TO 3
      DO 333 IJ=1,JNE
  333 KNAME(NOB,IJ)=KNAME(I,IJ)
      NAS=0
      NRS=0
      DO 33 J=1,NRCALL
      KD(J)=LDATA(I,J)
      KK=KKSUM(J)+LLSUM(J)
      LL=MIN0(KKSUM(J),LLSUM(J))
      AA=0.0
      IF(KK.GT.0)AA=FLOAT(LL)/FLOAT(KK)
      IF(AA.GT.XVMIN)NAS=NAS+1
      IF(AA.LE.XVMIN)NRS=NRS+1
      IF(AA.GT.XVMIN)KAV(NAS)=J
      IF(AA.GT.XVMIN)KAY(NAS)=KKSUM(J)
      IF(AA.GT.XVMIN)KAN(NAS)=LLSUM(J)
      IF(AA.LE.XVMIN)GO TO 33
      IF(KD(J).EQ.2.OR.KD(J).EQ.3)KD(J)=1
      IF(KD(J).EQ.4.OR.KD(J).EQ.5)KD(J)=6
      IF(KD(J).EQ.7.OR.KD(J).EQ.8.OR.KD(J).EQ.9)KD(J)=0
      LDATA(NOB,NAS)=KD(J)
 33   CONTINUE
  3   CONTINUE
      WRITE(23,1000)NRCALL,NRS,NAS,XVMIN
 1000 FORMAT(' ROLL-CALLS READ=',I8,2X,'NUMBER REJECTED=',I8,2X,
     C'NUMBER ACCEPTED=',I8,2X,'CUTOFF=',F6.3)
      WRITE(23,1001)NP,NMOB,NOB,KVMIN
 1001 FORMAT(' LEGISLATORS READ=',I4,2X,'NUMBER REJECTED=',I4,2X,
     C'NUMBER ACCEPTED=',I4,2X,'CUTOFF=',I4)
      NRCALL=NAS
      NP=NOB
C
C   CALCULATE DISTRIBUTION OF ROLL CALL MARGINS
C
      DO 40 J=1,NRCALL
      KAY(J)=0
      KAN(J)=0
      DO 41 I=1,NP
      IF(LDATA(I,J).EQ.1)KAY(J)=KAY(J)+1
      IF(LDATA(I,J).EQ.6)KAN(J)=KAN(J)+1
  41  CONTINUE
  40  CONTINUE
      LLTOT=0
      LLALL=0
      DO 4 J=1,NRCALL
      LL=MAX0(KAY(J),KAN(J))
      LLTOT=LLTOT+LL
      AA=FLOAT(LL)/FLOAT(KAY(J)+KAN(J))
      LLALL=LLALL+KAY(J)+KAN(J)
      IF(AA.GE..50.AND.AA.LE..55)KMARG(1)=KMARG(1)+1
      IF(AA.GT..55.AND.AA.LE..60)KMARG(2)=KMARG(2)+1
      IF(AA.GT..60.AND.AA.LE..65)KMARG(3)=KMARG(3)+1
      IF(AA.GT..65.AND.AA.LE..70)KMARG(4)=KMARG(4)+1
      IF(AA.GT..70.AND.AA.LE..75)KMARG(5)=KMARG(5)+1
      IF(AA.GT..75.AND.AA.LE..80)KMARG(6)=KMARG(6)+1
      IF(AA.GT..80.AND.AA.LE..85)KMARG(7)=KMARG(7)+1
      IF(AA.GT..85.AND.AA.LE..90)KMARG(8)=KMARG(8)+1
      IF(AA.GT..90.AND.AA.LE..95)KMARG(9)=KMARG(9)+1
      IF(AA.GT..95.AND.AA.LE..995)KMARG(10)=KMARG(10)+1
  4   CONTINUE
      WRITE(23,1003)
 1003 FORMAT('  DISTRIBUTION OF SCALABLE ROLL CALLS')
      DO 5 I=1,10
      AA=FLOAT(KMARG(I))/FLOAT(NRCALL)
      WRITE(23,1002)I,LMARG(I),KMARG(I),AA
  5   CONTINUE
 1002 FORMAT(I4,1X,A10,I5,F7.3)
      XSUM=FLOAT(LLTOT)/FLOAT(LLALL)
      WRITE(23,1004)LLTOT,LLALL,XSUM
      WRITE(*,1004)LLTOT,LLALL,XSUM
 1004 FORMAT(' AVERAGE MAJORITY MARGIN= ',2I8,F9.5)
      RETURN
      END
C
C
C  ************************************************************************
C    SORT SUBROUTINE--SORTS A VECTOR 'A' OF REAL ELEMENTS INTO ASCENDING
C    ORDER.  'LA' IS THE NUMBER OF ELEMENTS TO BE SORTED AND 'IR' IS A
C    VECTOR OF INTEGERS THAT RECORDS THE PERMUTATIONS--USUALLY SET TO
C    1,2,3,4,...
C  ************************************************************************
C
C
      SUBROUTINE RSORT(A,LA,IR)
      DIMENSION A(LA),IU(21),IL(21),IR(LA)
      IF (LA.LE.0) RETURN
      M = 1
      I = 1
      J = LA
      R = .375
    5 IF (I.EQ.J) GO TO 45
      IF (R.GT..5898437) GO TO 10
      R = R+3.90625E-2
      GO TO 15 
   10 R = R-.21875 
   15 K = I 
C
C SELECT A CENTRAL ELEMENT OF THE  
C ARRAY AND SAVE IT IN LOCATION T  
C
      IJ = I+(J-I)*R 
      T = A(IJ)   
      IT = IR(IJ) 
C
C FIRST ELEMENT OF ARRAY IS GREATER
C THAN T, INTERCHANGE WITH T       
C
      IF (A(I).LE.T) GO TO 20  
      A(IJ) = A(I) 
      A(I) = T     
      T = A(IJ) 
      IR(IJ) = IR(I)  
      IR(I) = IT
      IT = IR(IJ) 
   20 L = J
C
C IF LAST ELEMENT OF ARRAY IS LESS THAN
C T, INTERCHANGE WITH T
C
      IF (A(J).GE.T) GO TO 30
      A(IJ) = A(J)
      A(J) = T
      T = A(IJ)
      IR(IJ) = IR(J)
      IR(J) = IT
      IT = IR(IJ)
C
C IF FIRST ELEMENT OF ARRAY IS GREATER
C THAN T, INTERCHANGE WITH T
C
      IF (A(I).LE.T) GO TO 30
      A(IJ) = A(I)
      A(I) = T
      T = A(IJ)
      IR(IJ) = IR(I)
      IR(I) = IT
      IT = IR(IJ)
      GO TO 30
   25 IF (A(L).EQ.A(K)) GO TO 30
      TT = A(L)
      A(L) = A(K)
      A(K) = TT
      ITT = IR(L)
      IR(L) = IR(K)
      IR(K) = ITT
C
C FIND AN ELEMENT IN THE SECOND HALF OF
C THE ARRAY WHICH IS SMALLER THAN T
C
   30 L = L-1
      IF (A(L).GT.T) GO TO 30
C
C FIND AN ELEMENT IN THE FIRST HALF OF
C THE ARRAY WHICH IS GREATER THAN T
C
   35 K = K+1
      IF (A(K).LT.T) GO TO 35
C
C INTERCHANGE THESE ELEMENTS
C
      IF (K.LE.L) GO TO 25
C
C SAVE UPPER AND LOWER SUBSCRIPTS OF
C THE ARRAY YET TO BE SORTED
C
      IF (L-I.LE.J-K) GO TO 40
      IL(M) = I
      IU(M) = L
      I = K
      M = M+1
      GO TO 50
   40 IL(M) = K
      IU(M) = J
      J = L
      M = M+1
      GO TO 50
C
C BEGIN AGAIN ON ANOTHER PORTION OF
C THE UNSORTED ARRAY
C
   45 M = M-1
      IF (M.EQ.0) RETURN
      I = IL(M)
      J = IU(M)
   50 IF (J-I.GE.11) GO TO 15
      IF (I.EQ.1) GO TO 5
      I = I-1
   55 I = I+1
      IF (I.EQ.J) GO TO 45
      T = A(I+1)
      IT = IR(I+1)
      IF (A(I).LE.T) GO TO 55
      K = I
   60 A(K+1) = A(K)
      IR(K+1) = IR(K)
      K = K-1
      IF (T.LT.A(K)) GO TO 60
      A(K+1) = T
      IR(K+1) = IT
      GO TO 55
      END
C
C
C  **************************************************************************
C    EIGENVECTOR/EIGENVALUE DECOMPOSITION SUBROUTINES FOR A SYMMETRIC MATRIX
C    SUBROUTINES ARE FROM EISPACK
C  **************************************************************************
C
      SUBROUTINE RS(NM,N,A,W,MATZ,Z,FV1,FV2,IERR)
C
      INTEGER N,NM,IERR,MATZ
      REAL A(NM,N),W(N),Z(NM,N),FV1(N),FV2(N)
C
C     THIS SUBROUTINE CALLS THE RECOMMENDED SEQUENCE OF
C     SUBROUTINES FROM THE EIGENSYSTEM SUBROUTINE PACKAGE (EISPACK)
C     TO FIND THE EIGENVALUES AND EIGENVECTORS (IF DESIRED)
C     OF A REAL SYMMETRIC MATRIX.
C
C     ON INPUT
C
C        NM  MUST BE SET TO THE ROW DIMENSION OF THE TWO-DIMENSIONAL
C        ARRAY PARAMETERS AS DECLARED IN THE CALLING PROGRAM
C        DIMENSION STATEMENT.
C
C        N  IS THE ORDER OF THE MATRIX  A.
C
C        A  CONTAINS THE REAL SYMMETRIC MATRIX.
C
C        MATZ  IS AN INTEGER VARIABLE SET EQUAL TO ZERO IF
C        ONLY EIGENVALUES ARE DESIRED.  OTHERWISE IT IS SET TO
C        ANY NON-ZERO INTEGER FOR BOTH EIGENVALUES AND EIGENVECTORS.
C
C     ON OUTPUT
C
C        W  CONTAINS THE EIGENVALUES IN ASCENDING ORDER.
C
C        Z  CONTAINS THE EIGENVECTORS IF MATZ IS NOT ZERO.
C
C        IERR  IS AN INTEGER OUTPUT VARIABLE SET EQUAL TO AN ERROR
C           COMPLETION CODE DESCRIBED IN THE DOCUMENTATION FOR TQLRAT
C           AND TQL2.  THE NORMAL COMPLETION CODE IS ZERO.
C
C        FV1  AND  FV2  ARE TEMPORARY STORAGE ARRAYS.
C
C     QUESTIONS AND COMMENTS SHOULD BE DIRECTED TO BURTON S. GARBOW,
C     MATHEMATICS AND COMPUTER SCIENCE DIV, ARGONNE NATIONAL LABORATORY
C
C     THIS VERSION DATED AUGUST 1983.
C
C     ------------------------------------------------------------------
C
      IF (N .LE. NM) GO TO 10
      IERR = 10 * N
      GO TO 50
C
   10 IF (MATZ .NE. 0) GO TO 20
C     .......... FIND EIGENVALUES ONLY ..........
      CALL  TRED1(NM,N,A,W,FV1,FV2)
      CALL  TQLRAT(N,W,FV2,IERR)
      GO TO 50
C     .......... FIND BOTH EIGENVALUES AND EIGENVECTORS ..........
   20 CALL  TRED2(NM,N,A,W,FV1,Z)
      CALL  TQL2(NM,N,W,FV1,Z,IERR)
   50 RETURN
      END
      SUBROUTINE TRED1(NM,N,A,D,E,E2)
C
      INTEGER I,J,K,L,N,II,NM,JP1
      REAL A(NM,N),D(N),E(N),E2(N)
      REAL F,G,H,SCALE
C
C     THIS SUBROUTINE IS A TRANSLATION OF THE ALGOL PROCEDURE TRED1,
C     NUM. MATH. 11, 181-195(1968) BY MARTIN, REINSCH, AND WILKINSON.
C     HANDBOOK FOR AUTO. COMP., VOL.II-LINEAR ALGEBRA, 212-226(1971).
C
C     THIS SUBROUTINE REDUCES A REAL SYMMETRIC MATRIX
C     TO A SYMMETRIC TRIDIAGONAL MATRIX USING
C     ORTHOGONAL SIMILARITY TRANSFORMATIONS.
C
C     ON INPUT
C
C        NM MUST BE SET TO THE ROW DIMENSION OF TWO-DIMENSIONAL
C          ARRAY PARAMETERS AS DECLARED IN THE CALLING PROGRAM
C          DIMENSION STATEMENT.
C
C        N IS THE ORDER OF THE MATRIX.
C
C        A CONTAINS THE REAL SYMMETRIC INPUT MATRIX.  ONLY THE
C          LOWER TRIANGLE OF THE MATRIX NEED BE SUPPLIED.
C
C     ON OUTPUT
C
C        A CONTAINS INFORMATION ABOUT THE ORTHOGONAL TRANS-
C          FORMATIONS USED IN THE REDUCTION IN ITS STRICT LOWER
C          TRIANGLE.  THE FULL UPPER TRIANGLE OF A IS UNALTERED.
C
C        D CONTAINS THE DIAGONAL ELEMENTS OF THE TRIDIAGONAL MATRIX.
C
C        E CONTAINS THE SUBDIAGONAL ELEMENTS OF THE TRIDIAGONAL
C          MATRIX IN ITS LAST N-1 POSITIONS.  E(1) IS SET TO ZERO.
C
C        E2 CONTAINS THE SQUARES OF THE CORRESPONDING ELEMENTS OF E.
C          E2 MAY COINCIDE WITH E IF THE SQUARES ARE NOT NEEDED.
C
C     QUESTIONS AND COMMENTS SHOULD BE DIRECTED TO BURTON S. GARBOW,
C     MATHEMATICS AND COMPUTER SCIENCE DIV, ARGONNE NATIONAL LABORATORY
C
C     THIS VERSION DATED AUGUST 1983.
C
C     ------------------------------------------------------------------
C
      DO 100 I = 1, N
         D(I) = A(N,I)
         A(N,I) = A(I,I)
  100 CONTINUE
C     .......... FOR I=N STEP -1 UNTIL 1 DO -- ..........
      DO 300 II = 1, N
         I = N + 1 - II
         L = I - 1
         H = 0.0E0
         SCALE = 0.0E0
         IF (L .LT. 1) GO TO 130
C     .......... SCALE ROW (ALGOL TOL THEN NOT NEEDED) ..........
         DO 120 K = 1, L
  120    SCALE = SCALE + ABS(D(K))
C
         IF (SCALE .NE. 0.0E0) GO TO 140
C
         DO 125 J = 1, L
            D(J) = A(L,J)
            A(L,J) = A(I,J)
            A(I,J) = 0.0E0
  125    CONTINUE
C
  130    E(I) = 0.0E0
         E2(I) = 0.0E0
         GO TO 300
C
  140    DO 150 K = 1, L
            D(K) = D(K) / SCALE
            H = H + D(K) * D(K)
  150    CONTINUE
C
         E2(I) = SCALE * SCALE * H
         F = D(L)
         G = -SIGN(SQRT(H),F)
         E(I) = SCALE * G
         H = H - F * G
         D(L) = F - G
         IF (L .EQ. 1) GO TO 285
C     .......... FORM A*U ..........
         DO 170 J = 1, L
  170    E(J) = 0.0E0
C
         DO 240 J = 1, L
            F = D(J)
            G = E(J) + A(J,J) * F
            JP1 = J + 1
            IF (L .LT. JP1) GO TO 220
C
            DO 200 K = JP1, L
               G = G + A(K,J) * D(K)
               E(K) = E(K) + A(K,J) * F
  200       CONTINUE
C
  220       E(J) = G
  240    CONTINUE
C     .......... FORM P ..........
         F = 0.0E0
C
         DO 245 J = 1, L
            E(J) = E(J) / H
            F = F + E(J) * D(J)
  245    CONTINUE
C
         H = F / (H + H)
C     .......... FORM Q ..........
         DO 250 J = 1, L
  250    E(J) = E(J) - H * D(J)
C     .......... FORM REDUCED A ..........
         DO 280 J = 1, L
            F = D(J)
            G = E(J)
C
            DO 260 K = J, L
  260       A(K,J) = A(K,J) - F * E(K) - G * D(K)
C
  280    CONTINUE
C
  285    DO 290 J = 1, L
            F = D(J)
            D(J) = A(L,J)
            A(L,J) = A(I,J)
            A(I,J) = F * SCALE
  290    CONTINUE
C
  300 CONTINUE
C
      RETURN
      END
      SUBROUTINE TRED2(NM,N,A,D,E,Z)
C
      INTEGER I,J,K,L,N,II,NM,JP1
      REAL A(NM,N),D(N),E(N),Z(NM,N)
      REAL F,G,H,HH,SCALE
C
C     THIS SUBROUTINE IS A TRANSLATION OF THE ALGOL PROCEDURE TRED2,
C     NUM. MATH. 11, 181-195(1968) BY MARTIN, REINSCH, AND WILKINSON.
C     HANDBOOK FOR AUTO. COMP., VOL.II-LINEAR ALGEBRA, 212-226(1971).
C
C     THIS SUBROUTINE REDUCES A REAL SYMMETRIC MATRIX TO A
C     SYMMETRIC TRIDIAGONAL MATRIX USING AND ACCUMULATING
C     ORTHOGONAL SIMILARITY TRANSFORMATIONS.
C
C     ON INPUT
C
C        NM MUST BE SET TO THE ROW DIMENSION OF TWO-DIMENSIONAL
C          ARRAY PARAMETERS AS DECLARED IN THE CALLING PROGRAM
C          DIMENSION STATEMENT.
C
C        N IS THE ORDER OF THE MATRIX.
C
C        A CONTAINS THE REAL SYMMETRIC INPUT MATRIX.  ONLY THE
C          LOWER TRIANGLE OF THE MATRIX NEED BE SUPPLIED.
C
C     ON OUTPUT
C
C        D CONTAINS THE DIAGONAL ELEMENTS OF THE TRIDIAGONAL MATRIX.
C
C        E CONTAINS THE SUBDIAGONAL ELEMENTS OF THE TRIDIAGONAL
C          MATRIX IN ITS LAST N-1 POSITIONS.  E(1) IS SET TO ZERO.
C
C        Z CONTAINS THE ORTHOGONAL TRANSFORMATION MATRIX
C          PRODUCED IN THE REDUCTION.
C
C        A AND Z MAY COINCIDE.  IF DISTINCT, A IS UNALTERED.
C
C     QUESTIONS AND COMMENTS SHOULD BE DIRECTED TO BURTON S. GARBOW,
C     MATHEMATICS AND COMPUTER SCIENCE DIV, ARGONNE NATIONAL LABORATORY
C
C     THIS VERSION DATED AUGUST 1983.
C
C     ------------------------------------------------------------------
C
      DO 100 I = 1, N
C
         DO 80 J = I, N
   80    Z(J,I) = A(J,I)
C
         D(I) = A(N,I)
  100 CONTINUE
C
      IF (N .EQ. 1) GO TO 510
C     .......... FOR I=N STEP -1 UNTIL 2 DO -- ..........
      DO 300 II = 2, N
         I = N + 2 - II
         L = I - 1
         H = 0.0E0
         SCALE = 0.0E0
         IF (L .LT. 2) GO TO 130
C     .......... SCALE ROW (ALGOL TOL THEN NOT NEEDED) ..........
         DO 120 K = 1, L
  120    SCALE = SCALE + ABS(D(K))
C
         IF (SCALE .NE. 0.0E0) GO TO 140
  130    E(I) = D(L)
C
         DO 135 J = 1, L
            D(J) = Z(L,J)
            Z(I,J) = 0.0E0
            Z(J,I) = 0.0E0
  135    CONTINUE
C
         GO TO 290
C
  140    DO 150 K = 1, L
            D(K) = D(K) / SCALE
            H = H + D(K) * D(K)
  150    CONTINUE
C
         F = D(L)
         G = -SIGN(SQRT(H),F)
         E(I) = SCALE * G
         H = H - F * G
         D(L) = F - G
C     .......... FORM A*U ..........
         DO 170 J = 1, L
  170    E(J) = 0.0E0
C
         DO 240 J = 1, L
            F = D(J)
            Z(J,I) = F
            G = E(J) + Z(J,J) * F
            JP1 = J + 1
            IF (L .LT. JP1) GO TO 220
C
            DO 200 K = JP1, L
               G = G + Z(K,J) * D(K)
               E(K) = E(K) + Z(K,J) * F
  200       CONTINUE
C
  220       E(J) = G
  240    CONTINUE
C     .......... FORM P ..........
         F = 0.0E0
C
         DO 245 J = 1, L
            E(J) = E(J) / H
            F = F + E(J) * D(J)
  245    CONTINUE
C
         HH = F / (H + H)
C     .......... FORM Q ..........
         DO 250 J = 1, L
  250    E(J) = E(J) - HH * D(J)
C     .......... FORM REDUCED A ..........
         DO 280 J = 1, L
            F = D(J)
            G = E(J)
C
            DO 260 K = J, L
  260       Z(K,J) = Z(K,J) - F * E(K) - G * D(K)
C
            D(J) = Z(L,J)
            Z(I,J) = 0.0E0
  280    CONTINUE
C
  290    D(I) = H
  300 CONTINUE
C     .......... ACCUMULATION OF TRANSFORMATION MATRICES ..........
      DO 500 I = 2, N
         L = I - 1
         Z(N,L) = Z(L,L)
         Z(L,L) = 1.0E0
         H = D(I)
         IF (H .EQ. 0.0E0) GO TO 380
C
         DO 330 K = 1, L
  330    D(K) = Z(K,I) / H
C
         DO 360 J = 1, L
            G = 0.0E0
C
            DO 340 K = 1, L
  340       G = G + Z(K,I) * Z(K,J)
C
            DO 360 K = 1, L
               Z(K,J) = Z(K,J) - G * D(K)
  360    CONTINUE
C
  380    DO 400 K = 1, L
  400    Z(K,I) = 0.0E0
C
  500 CONTINUE
C
  510 DO 520 I = 1, N
         D(I) = Z(N,I)
         Z(N,I) = 0.0E0
  520 CONTINUE
C
      Z(N,N) = 1.0E0
      E(1) = 0.0E0
      RETURN
      END
      SUBROUTINE TQL2(NM,N,D,E,Z,IERR)
C
      INTEGER I,J,K,L,M,N,II,L1,L2,NM,MML,IERR
      REAL D(N),E(N),Z(NM,N)
      REAL C,C2,C3,DL1,EL1,F,G,H,P,R,S,S2,TST1,TST2,PYTHAG
C
C     THIS SUBROUTINE IS A TRANSLATION OF THE ALGOL PROCEDURE TQL2,
C     NUM. MATH. 11, 293-306(1968) BY BOWDLER, MARTIN, REINSCH, AND
C     WILKINSON.
C     HANDBOOK FOR AUTO. COMP., VOL.II-LINEAR ALGEBRA, 227-240(1971).
C
C     THIS SUBROUTINE FINDS THE EIGENVALUES AND EIGENVECTORS
C     OF A SYMMETRIC TRIDIAGONAL MATRIX BY THE QL METHOD.
C     THE EIGENVECTORS OF A FULL SYMMETRIC MATRIX CAN ALSO
C     BE FOUND IF  TRED2  HAS BEEN USED TO REDUCE THIS
C     FULL MATRIX TO TRIDIAGONAL FORM.
C
C     ON INPUT
C
C        NM MUST BE SET TO THE ROW DIMENSION OF TWO-DIMENSIONAL
C          ARRAY PARAMETERS AS DECLARED IN THE CALLING PROGRAM
C          DIMENSION STATEMENT.
C
C        N IS THE ORDER OF THE MATRIX.
C
C        D CONTAINS THE DIAGONAL ELEMENTS OF THE INPUT MATRIX.
C
C        E CONTAINS THE SUBDIAGONAL ELEMENTS OF THE INPUT MATRIX
C          IN ITS LAST N-1 POSITIONS.  E(1) IS ARBITRARY.
C
C        Z CONTAINS THE TRANSFORMATION MATRIX PRODUCED IN THE
C          REDUCTION BY  TRED2, IF PERFORMED.  IF THE EIGENVECTORS
C          OF THE TRIDIAGONAL MATRIX ARE DESIRED, Z MUST CONTAIN
C          THE IDENTITY MATRIX.
C
C      ON OUTPUT
C
C        D CONTAINS THE EIGENVALUES IN ASCENDING ORDER.  IF AN
C          ERROR EXIT IS MADE, THE EIGENVALUES ARE CORRECT BUT
C          UNORDERED FOR INDICES 1,2,...,IERR-1.
C
C        E HAS BEEN DESTROYED.
C
C        Z CONTAINS ORTHONORMAL EIGENVECTORS OF THE SYMMETRIC
C          TRIDIAGONAL (OR FULL) MATRIX.  IF AN ERROR EXIT IS MADE,
C          Z CONTAINS THE EIGENVECTORS ASSOCIATED WITH THE STORED
C          EIGENVALUES.
C
C        IERR IS SET TO
C          ZERO       FOR NORMAL RETURN,
C          J          IF THE J-TH EIGENVALUE HAS NOT BEEN
C                     DETERMINED AFTER 30 ITERATIONS.
C
C     CALLS PYTHAG FOR  SQRT(A*A + B*B) .
C
C     QUESTIONS AND COMMENTS SHOULD BE DIRECTED TO BURTON S. GARBOW,
C     MATHEMATICS AND COMPUTER SCIENCE DIV, ARGONNE NATIONAL LABORATORY
C
C     THIS VERSION DATED AUGUST 1983.
C
C     ------------------------------------------------------------------
C
      IERR = 0
      IF (N .EQ. 1) GO TO 1001
C
      DO 100 I = 2, N
  100 E(I-1) = E(I)
C
      F = 0.0E0
      TST1 = 0.0E0
      E(N) = 0.0E0
C
      DO 240 L = 1, N
         J = 0
         H = ABS(D(L)) + ABS(E(L))
         IF (TST1 .LT. H) TST1 = H
C     .......... LOOK FOR SMALL SUB-DIAGONAL ELEMENT ..........
         DO 110 M = L, N
            TST2 = TST1 + ABS(E(M))
            IF (TST2 .EQ. TST1) GO TO 120
C     .......... E(N) IS ALWAYS ZERO, SO THERE IS NO EXIT
C                THROUGH THE BOTTOM OF THE LOOP ..........
  110    CONTINUE
C
  120    IF (M .EQ. L) GO TO 220
  130    IF (J .EQ. 30) GO TO 1000
         J = J + 1
C     .......... FORM SHIFT ..........
         L1 = L + 1
         L2 = L1 + 1
         G = D(L)
         P = (D(L1) - G) / (2.0E0 * E(L))
         R = PYTHAG(P,1.0E0)
         D(L) = E(L) / (P + SIGN(R,P))
         D(L1) = E(L) * (P + SIGN(R,P))
         DL1 = D(L1)
         H = G - D(L)
         IF (L2 .GT. N) GO TO 145
C
         DO 140 I = L2, N
  140    D(I) = D(I) - H
C
  145    F = F + H
C     .......... QL TRANSFORMATION ..........
         P = D(M)
         C = 1.0E0
         C2 = C
         EL1 = E(L1)
         S = 0.0E0
         MML = M - L
C     .......... FOR I=M-1 STEP -1 UNTIL L DO -- ..........
         DO 200 II = 1, MML
            C3 = C2
            C2 = C
            S2 = S
            I = M - II
            G = C * E(I)
            H = C * P
            R = PYTHAG(P,E(I))
            E(I+1) = S * R
            S = E(I) / R
            C = P / R
            P = C * D(I) - S * G
            D(I+1) = H + S * (C * G + S * D(I))
C     .......... FORM VECTOR ..........
            DO 180 K = 1, N
               H = Z(K,I+1)
               Z(K,I+1) = S * Z(K,I) + C * H
               Z(K,I) = C * Z(K,I) - S * H
  180       CONTINUE
C
  200    CONTINUE
C
         P = -S * S2 * C3 * EL1 * E(L) / DL1
         E(L) = S * P
         D(L) = C * P
         TST2 = TST1 + ABS(E(L))
         IF (TST2 .GT. TST1) GO TO 130
  220    D(L) = D(L) + F
  240 CONTINUE
C     .......... ORDER EIGENVALUES AND EIGENVECTORS ..........
      DO 300 II = 2, N
         I = II - 1
         K = I
         P = D(I)
C
         DO 260 J = II, N
            IF (D(J) .GE. P) GO TO 260
            K = J
            P = D(J)
  260    CONTINUE
C
         IF (K .EQ. I) GO TO 300
         D(K) = D(I)
         D(I) = P
C
         DO 280 J = 1, N
            P = Z(J,I)
            Z(J,I) = Z(J,K)
            Z(J,K) = P
  280    CONTINUE
C
  300 CONTINUE
C
      GO TO 1001
C     .......... SET ERROR -- NO CONVERGENCE TO AN
C                EIGENVALUE AFTER 30 ITERATIONS ..........
 1000 IERR = L
 1001 RETURN
      END
      SUBROUTINE TQLRAT(N,D,E2,IERR)
C
      INTEGER I,J,L,M,N,II,L1,MML,IERR
      REAL D(N),E2(N)
      REAL B,C,F,G,H,P,R,S,T,EPSLON,PYTHAG
C
C     THIS SUBROUTINE IS A TRANSLATION OF THE ALGOL PROCEDURE TQLRAT,
C     ALGORITHM 464, COMM. ACM 16, 689(1973) BY REINSCH.
C
C     THIS SUBROUTINE FINDS THE EIGENVALUES OF A SYMMETRIC
C     TRIDIAGONAL MATRIX BY THE RATIONAL QL METHOD.
C
C     ON INPUT
C
C        N IS THE ORDER OF THE MATRIX.
C
C        D CONTAINS THE DIAGONAL ELEMENTS OF THE INPUT MATRIX.
C
C        E2 CONTAINS THE SQUARES OF THE SUBDIAGONAL ELEMENTS OF THE
C          INPUT MATRIX IN ITS LAST N-1 POSITIONS.  E2(1) IS ARBITRARY.
C
C      ON OUTPUT
C
C        D CONTAINS THE EIGENVALUES IN ASCENDING ORDER.  IF AN
C          ERROR EXIT IS MADE, THE EIGENVALUES ARE CORRECT AND
C          ORDERED FOR INDICES 1,2,...IERR-1, BUT MAY NOT BE
C          THE SMALLEST EIGENVALUES.
C
C        E2 HAS BEEN DESTROYED.
C
C        IERR IS SET TO
C          ZERO       FOR NORMAL RETURN,
C          J          IF THE J-TH EIGENVALUE HAS NOT BEEN
C                     DETERMINED AFTER 30 ITERATIONS.
C
C     CALLS PYTHAG FOR  SQRT(A*A + B*B) .
C
C     QUESTIONS AND COMMENTS SHOULD BE DIRECTED TO BURTON S. GARBOW,
C     MATHEMATICS AND COMPUTER SCIENCE DIV, ARGONNE NATIONAL LABORATORY
C
C     THIS VERSION DATED AUGUST 1983.
C
C     ------------------------------------------------------------------
C
      IERR = 0
      IF (N .EQ. 1) GO TO 1001
C
      DO 100 I = 2, N
  100 E2(I-1) = E2(I)
C
      F = 0.0E0
      T = 0.0E0
      E2(N) = 0.0E0
C
      DO 290 L = 1, N
         J = 0
         H = ABS(D(L)) + SQRT(E2(L))
         IF (T .GT. H) GO TO 105
         T = H
         B = EPSLON(T)
         C = B * B
C     .......... LOOK FOR SMALL SQUARED SUB-DIAGONAL ELEMENT ..........
  105    DO 110 M = L, N
            IF (E2(M) .LE. C) GO TO 120
C     .......... E2(N) IS ALWAYS ZERO, SO THERE IS NO EXIT
C                THROUGH THE BOTTOM OF THE LOOP ..........
  110    CONTINUE
C
  120    IF (M .EQ. L) GO TO 210
  130    IF (J .EQ. 30) GO TO 1000
         J = J + 1
C     .......... FORM SHIFT ..........
         L1 = L + 1
         S = SQRT(E2(L))
         G = D(L)
         P = (D(L1) - G) / (2.0E0 * S)
         R = PYTHAG(P,1.0E0)
         D(L) = S / (P + SIGN(R,P))
         H = G - D(L)
C
         DO 140 I = L1, N
  140    D(I) = D(I) - H
C
         F = F + H
C     .......... RATIONAL QL TRANSFORMATION ..........
         G = D(M)
         IF (G .EQ. 0.0E0) G = B
         H = G
         S = 0.0E0
         MML = M - L
C     .......... FOR I=M-1 STEP -1 UNTIL L DO -- ..........
         DO 200 II = 1, MML
            I = M - II
            P = G * H
            R = P + E2(I)
            E2(I+1) = S * R
            S = E2(I) / R
            D(I+1) = H + S * (H + D(I))
            G = D(I) - E2(I) / G
            IF (G .EQ. 0.0E0) G = B
            H = G * P / R
  200    CONTINUE
C
         E2(L) = S * G
         D(L) = H
C     .......... GUARD AGAINST UNDERFLOW IN CONVERGENCE TEST ..........
         IF (H .EQ. 0.0E0) GO TO 210
         IF (ABS(E2(L)) .LE. ABS(C/H)) GO TO 210
         E2(L) = H * E2(L)
         IF (E2(L) .NE. 0.0E0) GO TO 130
  210    P = D(L) + F
C     .......... ORDER EIGENVALUES ..........
         IF (L .EQ. 1) GO TO 250
C     .......... FOR I=L STEP -1 UNTIL 2 DO -- ..........
         DO 230 II = 2, L
            I = L + 2 - II
            IF (P .GE. D(I-1)) GO TO 270
            D(I) = D(I-1)
  230    CONTINUE
C
  250    I = 1
  270    D(I) = P
  290 CONTINUE
C
      GO TO 1001
C     .......... SET ERROR -- NO CONVERGENCE TO AN
C                EIGENVALUE AFTER 30 ITERATIONS ..........
 1000 IERR = L
 1001 RETURN
      END
      REAL FUNCTION PYTHAG(A,B)
      REAL A,B
C
C     FINDS SQRT(A**2+B**2) WITHOUT OVERFLOW OR DESTRUCTIVE UNDERFLOW
C
      REAL P,R,S,T,U
      P = AMAX1(ABS(A),ABS(B))
      IF (P .EQ. 0.0E0) GO TO 20
      R = (AMIN1(ABS(A),ABS(B))/P)**2
   10 CONTINUE
         T = 4.0E0 + R
         IF (T .EQ. 4.0E0) GO TO 20
         S = R/T
         U = 1.0E0 + 2.0E0*S
         P = U*P
         R = (S/U)**2 * R
      GO TO 10
   20 PYTHAG = P
      RETURN
      END
C
      REAL FUNCTION EPSLON (X)
      REAL X
      REAL A,B,C,EPS
      A = 4.0E0/3.0E0
  10  B = A - 1.0E0
      C = B + B + B
      EPS = ABS(C-1.0E0)
      IF(EPS .EQ. 0.0E0)GO TO 10
      EPSLON = EPS*ABS(X)
      RETURN
      END
C
C
      SUBROUTINE CPUTIM(KTIM1)
      INTEGER*2 ITIM1,ITIM2,ITIM3,ITIM4
      COMMON /XIME/ NTIME(1200),NITR,MSEC
C
      CALL GETTIM(ITIM1,ITIM2,ITIM3,ITIM4)
      MMSEC=ITIM1*60*60+ITIM2*60+ITIM3
      KTIM1=MMSEC-MSEC
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  LSVRR/DLSVRR (SINGLE/DOUBLE PRECISION VERSION)
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    JANUARY 1, 1985
C
C  PURPOSE:    COMPUTE THE SINGULAR VALUE DECOMPOSITION OF A REAL
C              MATRIX.
C
C  USAGE:      CALL LSVRR (NRA, NCA, A, LDA, IPATH, TOL, IRANK,
C                          S, U, LDU, V, LDV)
C
C  ARGUMENTS:
C     NRA    - NUMBER OF ROWS OF A.  (INPUT)
C     NCA    - NUMBER OF COLUMNS OF A.  (INPUT)
C     A      - NRA BY NCA MATRIX WHOSE SINGULAR VALUE DECOMPOSITION
C              IS TO BE COMPUTED.  (INPUT)
C     LDA    - LEADING DIMENSION OF A EXACTLY AS SPECIFIED IN THE
C              DIMENSION STATEMENT OF THE CALLING PROGRAM.  (INPUT)
C     IPATH  - FLAG USED TO CONTROL THE COMPUTATION OF THE SINGULAR
C              VECTORS.  (INPUT)
C              IPATH HAS THE DECIMAL EXPANSION IJ SUCH THAT:
C                 I = 0 MEANS DO NOT COMPUTE THE LEFT SINGULAR VECTORS,
C                 I = 1 MEANS RETURN THE NCA LEFT SINGULAR VECTORS IN U,
C                 I = 2 MEANS RETURN ONLY THE MIN(NRA,NCA) LEFT SINGULAR
C                       VECTORS IN U,
C                 J = 0 MEANS DO NOT COMPUTE THE RIGHT SINGULAR VECTORS,
C                 J = 1 MEANS RETURN THE RIGHT SINGULAR VECTORS IN V.
C              FOR EXAMPLE, IPATH = 20 MEANS I = 2 AND J = 0.
C     TOL    - SCALAR CONTAINING THE TOLERANCE USED TO DETERMINE WHEN A
C              SINGULAR VALUE IS NEGLIGIBLE.  (INPUT)
C              IF TOL IS POSITIVE THEN A SINGULAR VALUE SI IS CONSIDERED
C                 NEGLIGIBLE IF SI .LE. TOL.
C              IF TOL IS NEGATIVE THEN A SINGULAR VALUE SI IS CONSIDERED
C                 NEGLIGIBLE IF SI .LE. ABS(TOL)*(INFINITY NORM OF A).
C                 IN THIS CASE ABS(TOL) SHOULD GENERALLY CONTAIN AN
C                 ESTIMATE OF THE LEVEL OF RELATIVE ERROR IN THE DATA.
C     IRANK  - SCALAR CONTAINING AN ESTIMATE OF THE RANK OF A.  (OUTPUT)
C     S      - VECTOR OF LENGTH MIN(NRA+1,NCA) CONTAINING THE SINGULAR
C              VALUES OF A IN DESCENDING ORDER OF MAGNITUDE IN THE FIRST
C              MIN(NRA,NCA) POSITIONS.  (OUTPUT)
C     U      - NRA BY NCU MATRIX CONTAINING THE LEFT SINGULAR VECTORS OF
C              A.  (OUTPUT)
C              NCU MUST BE EQUAL TO NRA IF I IS EQUAL TO 1.
C              NCU MUST BE EQUAL TO MIN(NRA,NCA) IF I IS EQUAL TO 2.
C              U WILL NOT BE REFERENCED IF I IS EQUAL TO ZERO.  IF NRA
C              IS LESS THAN OR EQUAL TO NCU, THEN U CAN SHARE THE SAME
C              STORAGE LOCATIONS AS A.  SEE REMARKS.
C     LDU    - LEADING DIMENSION OF U EXACTLY AS SPECIFIED IN THE
C              DIMENSION STATEMENT OF THE CALLING PROGRAM.  (INPUT)
C     V      - NCA BY NCA MATRIX CONTAINING THE RIGHT SINGULAR VECTORS
C              OF A.  (OUTPUT)
C              V WILL NOT BE REFERENCED IF J IS EQUAL TO ZERO.  V CAN
C              SHARE THE SAME STORAGE LOCATION AS A; HOWEVER U AND V
C              CANNOT BOTH COINCIDE WITH A SIMULTANEOUSLY.
C     LDV    - LEADING DIMENSION OF V EXACTLY AS SPECIFIED IN THE
C              DIMENSION STATEMENT OF THE CALLING PROGRAM.  (INPUT)
C
C  REMARKS:
C  1. AUTOMATIC WORKSPACE USAGE IS
C              LSVRR    NRA*NCA + NRA + NCA + MAX(NRA,NCA) - 1 UNITS, OR
C              DLSVRR   2*(NRA*NCA + NRA + NCA + MAX(NRA,NCA) - 1)
C                       UNITS.
C     WORKSPACE MAY BE EXPLICITLY PROVIDED, IF DESIRED, BY USE OF
C     L2VRR/DL2VRR.  THE REFERENCE IS
C              CALL L2VRR (NRA, NCA, A, LDA, IPATH, TOL, IRANK,
C                          S, U, LDU, V, LDV, ACOPY, WK)
C     THE ADDITIONAL ARGUMENTS ARE AS FOLLOWS:
C     ACOPY  - WORK VECTOR OF LENGTH NRA*NCA FOR THE MATRIX A.  IF
C              A IS NOT NEEDED THEN A AND ACOPY MAY SHARE THE SAME
C              STORAGE LOCATIONS.
C     WK     - WORK VECTOR OF LENGTH NRA + NCA + MAX(NRA,NCA) - 1.
C
C  2. INFORMATIONAL ERROR
C     TYPE CODE
C       4   1  CONVERGENCE CANNOT BE ACHIEVED FOR ALL THE SINGULAR
C              VALUES AND THEIR CORRESPONDING SINGULAR VECTORS.
C
C  3. WHEN NRA IS MUCH GREATER THAN NCA, IT MIGHT NOT BE REASONABLE TO
C     STORE THE WHOLE MATRIX U.  IN THIS CASE IPATH WITH I = 2 ALLOWS
C     A SINGULAR VALUE FACTORIZATION OF A TO BE COMPUTED IN WHICH ONLY
C     THE FIRST NCA COLUMNS OF U ARE COMPUTED, AND IN MANY APPLICATIONS
C     THOSE ARE ALL THAT ARE NEEDED.
C
C  KEYWORDS:   LEAST SQUARES; COMPLETE ORTHOGONAL DECOMPOSITION;
C              RANK ESTIMATION
C
C  GAMS:       D6
C
C  CHAPTERS:   MATH/LIBRARY LINEAR SYSTEMS
C              STAT/LIBRARY MATHEMATICAL SUPPORT
C
C  COPYRIGHT:  1985 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE LSVRR (NRA, NCA, A, LDA, IPATH, TOL, IRANK, S, U,
     &                  LDU, V, LDV)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    NRA, NCA, LDA, IPATH, IRANK, LDU, LDV
      REAL       TOL, A(LDA,*), S(*), U(LDU,*), V(LDV,*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    INDA, INDW
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                                  SPECIFICATIONS FOR COMMON /WORKSP/
      REAL       RWKSP(61913)
      REAL       RDWKSP(5000)
      DOUBLE PRECISION DWKSP(2500)
      COMPLEX    CWKSP(2500)
      COMPLEX    CZWKSP(2500)
      COMPLEX    *16 ZWKSP(1250)
      INTEGER    IWKSP(5000)
      LOGICAL    LWKSP(5000)
      EQUIVALENCE (DWKSP(1), RWKSP(1))
      EQUIVALENCE (CWKSP(1), RWKSP(1)), (ZWKSP(1), RWKSP(1))
      EQUIVALENCE (IWKSP(1), RWKSP(1)), (LWKSP(1), RWKSP(1))
      EQUIVALENCE (RDWKSP(1), RWKSP(1)), (CZWKSP(1), RWKSP(1))
      COMMON     /WORKSP/ RWKSP
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  MAX0
      INTRINSIC  MAX0
      INTEGER    MAX0
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   E1MES, E1POP, E1PSH, E1STI, L2VRR
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   I1KGT, N1RCD
      INTEGER    I1KGT, N1RCD
C
      CALL E1PSH ('LSVRR ')
C
      IF (NRA.LE.0 .OR. NCA.LE.0) THEN
         CALL E1STI (1, NRA)
         CALL E1STI (2, NCA)
         CALL E1MES (5, 1, 'BOTH THE NUMBER OF ROWS AND THE '//
     &               'NUMBER OF COLUMNS OF THE INPUT MATRIX HAVE TO '//
     &               'BE POSITIVE WHILE NRA = %(I1) AND NCA = %(I2) '//
     &               'ARE GIVEN.')
      ELSE
         INDA = I1KGT(NRA*NCA,3)
         INDW = I1KGT(NRA+NCA+MAX0(NCA,NRA)-1,3)
         IF (N1RCD(0) .NE. 0) THEN
            CALL E1MES (5, 2, ' ')
            CALL E1STI (1, NRA)
            CALL E1STI (2, NCA)
            CALL E1MES (5, 2, 'THE WORKSPACE IS BASED ON NRA AND NCA '//
     &                  'WHERE NRA = %(I1) AND NCA = %(I2) ARE '//
     &                  'GIVEN.')
         ELSE
            CALL L2VRR (NRA, NCA, A, LDA, IPATH, TOL, IRANK, S, U,
     &                  LDU, V, LDV, RDWKSP(INDA), RDWKSP(INDW))
         END IF
      END IF
C
      CALL E1POP ('LSVRR ')
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  L2VRR/DL2VRR (SINGLE/DOUBLE PRECISION VERSION)
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    JANUARY 1, 1985
C
C  PURPOSE:    COMPUTE THE SINGULAR VALUE DECOMPOSITION OF A REAL
C              MATRIX.
C
C  USAGE:      CALL L2VRR (NRA, NCA, A, LDA, IPATH, TOL, IRANK,
C                          S, U, LDU, V, LDV, WKA, WK)
C
C  ARGUMENTS:  SEE LSVRR/DLSVRR.
C
C  REMARKS:    SEE LSVRR/DLSVRR.
C
C  CHAPTER:    MATH/LIBRARY LINEAR SYSTEMS
C
C  COPYRIGHT:  1985 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE L2VRR (NRA, NCA, A, LDA, IPATH, TOL, IRANK, S, U,
     &                  LDU, V, LDV, WKA, WK)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    NRA, NCA, LDA, IPATH, IRANK, LDU, LDV
      REAL       TOL, A(LDA,*), S(*), U(*), V(*), WKA(*), WK(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, INDE, INDW, JOBU, JOBV
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  MOD
      INTRINSIC  MOD
      INTEGER    MOD
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   E1MES, E1POP, E1PSH, E1STI, SCOPY, L3VRR
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   N1RCD
      INTEGER    N1RCD
C
      CALL E1PSH ('L2VRR   ')
C
      IF (NRA.LE.0 .OR. NCA.LE.0) THEN
         CALL E1STI (1, NRA)
         CALL E1STI (2, NCA)
         CALL E1MES (5, 1, 'BOTH THE NUMBER OF ROWS AND THE '//
     &               'NUMBER OF COLUMNS OF THE INPUT MATRIX HAVE TO '//
     &               'BE POSITIVE WHILE NRA = %(I1) AND NCA = %(I2) '//
     &               'ARE GIVEN.')
      ELSE
         IF (NRA .GT. LDA) THEN
            CALL E1STI (1, NRA)
            CALL E1STI (2, LDA)
            CALL E1MES (5, 2, 'THE NUMBER OF ROWS OF A MUST BE '//
     &                  'LESS THAN OR EQUAL TO ITS LEADING DIMENSION '//
     &                  'WHILE NRA = %(I1) AND LDA = %(I2) ARE '//
     &                  'GIVEN.')
         END IF
C
         IF (LDU .LE. 0) THEN
            CALL E1STI (1, LDU)
            CALL E1MES (5, 3, 'THE LEADING DIMENSION OF U MUST '//
     &                  'BE GREATER THAN ZERO.  LDU = %(I1) IS '//
     &                  'GIVEN.')
         END IF
C
         IF (LDV .LE. 0) THEN
            CALL E1STI (1, LDV)
            CALL E1MES (5, 4, 'THE LEADING DIMENSION OF V MUST '//
     &                  'BE GREATER THAN ZERO.  LDV = %(I1) IS '//
     &                  'GIVEN.')
         END IF
C
         IF (N1RCD(0) .EQ. 0) THEN
            JOBU = MOD(IPATH,100)/10
            JOBV = MOD(IPATH,10)
            IF ((JOBU.NE.0.AND.JOBU.NE.1.AND.JOBU.NE.2) .OR.
     &          (JOBV.NE.0.AND.JOBV.NE.1)) THEN
               CALL E1STI (1, JOBU)
               CALL E1STI (2, JOBV)
               CALL E1MES (5, 7, 'ERROR IN COMPUTATION CONTROL FLAG. '//
     &                     'THE IJ DECIMAL EXPANSION OF IPATH IS I = '//
     &                     '%(I1) AND J = %(I2).  I MUST BE EITHER '//
     &                     '0, 1 OR 2 AND J MUST BE EITHER 0 OR 1.')
            ELSE
C                                  MAKE A COPY OF A IN WKA AND WORK
C                                  WITH WKA ONLY.
               DO 10  I=1, NCA
                  CALL SCOPY (NRA, A(1,I), 1, WKA((I-1)*NRA+1), 1)
   10          CONTINUE
C
               INDE = 1
               INDW = NCA + 1
               CALL L3VRR (NRA, NCA, WKA, NRA, IPATH, TOL, IRANK, S,
     &                     U, LDU, V, LDV, WK(INDE), WK(INDW))
            END IF
         END IF
      END IF
C
      CALL E1POP ('L2VRR   ')
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  L3VRR/DL3VRR (SINGLE/DOUBLE PRECISION VERSION)
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    JANUARY 1, 1985
C
C  PURPOSE:    COMPUTE THE SINGULAR VALUE DECOMPOSITION OF A REAL
C              MATRIX.
C
C  USAGE:      CALL L3VRR (NRA, NCA, A, LDA, IPATH, TOL, IRANK,
C                          S, U, LDU, V, LDV, E, WORK)
C
C  ARGUMENTS:  SEE LSVRR/DLSVRR
C
C  REMARKS:    SEE LSVRR/DLSVRR.
C
C  CHAPTER:    MATH/LIBRARY LINEAR SYSTEMS
C
C  COPYRIGHT:  1985 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE L3VRR (NRA, NCA, A, LDA, IPATH, TOL, IRANK, S, U,
     &                  LDU, V, LDV, E, WORK)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    NRA, NCA, LDA, IPATH, IRANK, LDU, LDV
      REAL       TOL, A(LDA,*), S(*), U(LDU,*), V(LDV,*), E(*), WORK(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, IEND, INFO, ISTART, ITER, J, JOBU, K, KASE, KK, L,
     &           LL, LLS, LM1, LP1, LS, LU, M, MAXIT, MINMN, MM, MM1,
     &           MP1, NCT, NCTP1, NCU, NRT, NRTP1
      REAL       ANORM, B, C, CS, EL, EMM1, F, G, SCALE, SHIFT, SL,
     &           SM, SMM1, SN, STOL, T, T1, TEST, ZTEST
      LOGICAL    WANTU, WANTV
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  ABS,AMAX1,MAX0,MIN0,MOD,SIGN,SQRT
      INTRINSIC  ABS, AMAX1, MAX0, MIN0, MOD, SIGN, SQRT
      INTEGER    MAX0, MIN0, MOD
      REAL       ABS, AMAX1, SIGN, SQRT
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   E1MES, E1POP, E1PSH, E1STI, SCOPY, SGEMV, SGER, SROT,
     &           SROTG, SSCAL, SSET, SSWAP
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   SASUM, SNRM2
      REAL       SASUM, SNRM2
C
      CALL E1PSH ('L3VRR   ')
C                                  IF TOL.LT.0, COMPUTE THE INFINITY
C                                  NORM OF A AND SINGULAR VALUE
C                                  TOLERANCE FOR RANK ESTIMATION
      IF (TOL .LT. 0.0E0) THEN
         ANORM = 0.0E0
         DO 10  I=1, NRA
            ANORM = AMAX1(ANORM,SASUM(NCA,A(I,1),LDA))
   10    CONTINUE
         STOL = ABS(TOL)*ANORM
      ELSE
         STOL = TOL
      END IF
C                                  SET THE MAXIMUM NUMBER OF
C                                  ITERATIONS.
      MAXIT = 30
C                                  DETERMINE WHAT IS TO BE COMPUTED.
      WANTU = .FALSE.
      WANTV = .FALSE.
      JOBU = MOD(IPATH,100)/10
      NCU = NRA
      IF (JOBU .EQ. 2) NCU = MIN0(NRA,NCA)
      IF (JOBU .NE. 0) WANTU = .TRUE.
      IF (MOD(IPATH,10) .NE. 0) WANTV = .TRUE.
C                                  REDUCE A TO BIDIAGONAL FORM, STORING
C                                  THE DIAGONAL ELEMENTS IN S AND THE
C                                  SUPER-DIAGONAL ELEMENTS IN E.
      INFO = 0
      NCT = MIN0(NRA-1,NCA)
      NRT = MAX0(0,MIN0(NCA-2,NRA))
      LU = MAX0(NCT,NRT)
      IF (LU .GE. 1) THEN
         DO 20  L=1, LU
            LP1 = L + 1
            IF (L .LE. NCT) THEN
C                                  COMPUTE THE TRANSFORMATION FOR THE
C                                  L-TH NCA AND PLACE THE L-TH
C                                  DIAGONAL IN S(L).
               S(L) = SNRM2(NRA-L+1,A(L,L),1)
               IF (S(L) .NE. 0.0E0) THEN
                  IF (A(L,L) .NE. 0.0E0) S(L) = SIGN(S(L),A(L,L))
                  CALL SSCAL (NRA-L+1, 1.0E0/S(L), A(L,L), 1)
                  A(L,L) = 1.0E0 + A(L,L)
               END IF
               S(L) = -S(L)
            END IF
C
            IF (NCA .GE. LP1) THEN
               IF (L.LE.NCT .AND. S(L).NE.0.0E0) THEN
C                                  APPLY THE TRANSFORMATION.
                  CALL SGEMV ('T', NRA-L+1, NCA-LP1+1, -1.0E0/A(L,L),
     &                        A(L,LP1), LDA, A(L,L), 1, 0.0E0,
     &                        WORK(NRA+1), 1)
                  CALL SGER (NRA-L+1, NCA-LP1+1, 1.0E0, A(L,L), 1,
     &                       WORK(NRA+1), 1, A(L,LP1), LDA)
               END IF
C                                  PLACE THE L-TH ROW OF A INTO E FOR
C                                  THE SUBSEQUENT CALCULATION OF THE
C                                  ROW TRANSFORMATION.
               CALL SCOPY (NCA-LP1+1, A(L,LP1), LDA, E(LP1), 1)
            END IF
C                                  PLACE THE TRANSFORMATION IN U FOR
C                                  SUBSEQUENT BACK MULTIPLICATION.
            IF (WANTU .AND. L.LE.NCT) CALL SCOPY (NRA-L+1, A(L,L), 1,
     &          U(L,L), 1)
            IF (L .LE. NRT) THEN
C                                  COMPUTE THE L-TH ROW TRANSFORMATION
C                                  AND PLACE THE L-TH SUPER-DIAGONAL
C                                  IN E(L).
               E(L) = SNRM2(NCA-L,E(LP1),1)
               IF (E(L) .NE. 0.0E0) THEN
                  IF (E(LP1) .NE. 0.0E0) E(L) = SIGN(E(L),E(LP1))
                  CALL SSCAL (NCA-L, 1.0E0/E(L), E(LP1), 1)
                  E(LP1) = 1.0E0 + E(LP1)
               END IF
               E(L) = -E(L)
               IF (LP1.LE.NRA .AND. E(L).NE.0.0E0) THEN
C                                  APPLY THE TRANSFORMATION.
                  CALL SGEMV ('N', NRA-L, NCA-LP1+1, 1.0E0,
     &                        A(LP1,LP1), LDA, E(LP1), 1, 0.0E0,
     &                        WORK(LP1), 1)
                  CALL SGER (NRA-L, NCA-LP1+1, -1.0E0/E(LP1),
     &                       WORK(LP1), 1, E(LP1), 1, A(LP1,LP1), LDA)
               END IF
C                                  PLACE THE TRANSFORMATION IN V FOR
C                                  SUBSEQUENT BACK MULTIPLICATION.
               IF (WANTV) CALL SCOPY (NCA-LP1+1, E(LP1), 1, V(LP1,L),
     &             1)
            END IF
   20    CONTINUE
      END IF
C                                  SET UP THE FINAL BIDIAGONAL MATRIX
C                                  OR ORDER M.
      M = MIN0(NCA,NRA+1)
      NCTP1 = NCT + 1
      NRTP1 = NRT + 1
      IF (NCT .LT. NCA) S(NCTP1) = A(NCTP1,NCTP1)
      IF (NRA .LT. M) S(M) = 0.0E0
      IF (NRTP1 .LT. M) E(NRTP1) = A(NRTP1,M)
      E(M) = 0.0E0
C                                  IF REQUIRED, GENERATE U.
      IF (WANTU) THEN
         IF (NCU .GE. NCTP1) THEN
            DO 30  J=NCTP1, NCU
               CALL SSET (NRA, 0.0E0, U(1,J), 1)
               U(J,J) = 1.0E0
   30       CONTINUE
         END IF
         IF (NCT .GE. 1) THEN
            DO 40  LL=1, NCT
               L = NCT - LL + 1
               IF (S(L) .NE. 0.0E0) THEN
                  LP1 = L + 1
                  IF (NCU .GE. LP1) THEN
                     CALL SGEMV ('T', NRA-L+1, NCU-LP1+1,
     &                           -1.0E0/U(L,L), U(L,LP1), LDU, U(L,L),
     &                           1, 0.0E0, WORK(NRA+1), 1)
                     CALL SGER (NRA-L+1, NCU-LP1+1, 1.0E0, U(L,L), 1,
     &                          WORK(NRA+1), 1, U(L,LP1), LDU)
                  END IF
                  CALL SSCAL (NRA-L+1, -1.0E0, U(L,L), 1)
                  U(L,L) = 1.0E0 + U(L,L)
                  LM1 = L - 1
                  IF (LM1 .GE. 1) CALL SSET (LM1, 0.0E0, U(1,L), 1)
               ELSE
                  CALL SSET (NRA, 0.0E0, U(1,L), 1)
                  U(L,L) = 1.0E0
               END IF
   40       CONTINUE
         END IF
      END IF
C                                  IF IT IS REQUIRED, GENERATE V.
      IF (WANTV) THEN
         DO 50  LL=1, NCA
            L = NCA - LL + 1
            LP1 = L + 1
            IF (L.LE.NRT .AND. E(L).NE.0.0E0) THEN
               CALL SGEMV ('T', NCA-L, NCA-LP1+1, -1.0E0/V(LP1,L),
     &                     V(LP1,LP1), LDV, V(LP1,L), 1, 0.0E0,
     &                     WORK(NRA+1), 1)
               CALL SGER (NCA-L, NCA-LP1+1, 1.0E0, V(LP1,L), 1,
     &                    WORK(NRA+1), 1, V(LP1,LP1), LDV)
            END IF
            CALL SSET (NCA, 0.0E0, V(1,L), 1)
            V(L,L) = 1.0E0
   50    CONTINUE
      END IF
C                                  MAIN ITERATION LOOP FOR THE SINGULAR
C                                  VALUES.
      MM = M
      ITER = 0
   60 CONTINUE
C                                  QUIT IF ALL THE SINGULAR VALUES HAVE
C                                  BEEN FOUND.
      IF (M.NE.0 .AND. ITER.LE.MAXIT) THEN
C                                  THIS SECTION OF THE PROGRAM INSPECTS
C                                  FOR NEGLIGIBLE ELEMENTS IN THE S AND
C                                  E ARRAYS. ON COMPLETION THE VARIABLES
C                                  KASE AND L ARE SET AS FOLLOWS.
C
C                                  KASE = 1     IF S(M) AND E(L-1) ARE
C                                  NEGLIGIBLE AND L.LT.M
C
C                                  KASE = 2     IF S(L) IS NEGLIGIBLE
C                                  AND L.LT.M
C
C                                  KASE = 3     IF E(L-1) IS NEGLIGIBLE,
C                                  L.LT.M, AND S(L), ..., S(M) ARE NOT
C                                  NEGLIGIBLE (QR STEP).
C
C                                  KASE = 4     IF E(M-1) IS NEGLIGIBLE
C                                  (CONVERGENCE).
         DO 70  LL=1, M
            L = M - LL
            IF (L .EQ. 0) GO TO 80
            TEST = ABS(S(L)) + ABS(S(L+1))
            ZTEST = TEST + ABS(E(L))
            IF (ZTEST .EQ. TEST) THEN
               E(L) = 0.0E0
               GO TO 80
            END IF
   70    CONTINUE
   80    CONTINUE
         IF (L .EQ. M-1) THEN
            KASE = 4
         ELSE
            LP1 = L + 1
            MP1 = M + 1
            DO 90  LLS=LP1, MP1
               LS = M - LLS + LP1
               IF (LS .EQ. L) GO TO 100
               TEST = 0.0E0
               IF (LS .NE. M) TEST = TEST + ABS(E(LS))
               IF (LS .NE. L+1) TEST = TEST + ABS(E(LS-1))
               ZTEST = TEST + ABS(S(LS))
               IF (ZTEST .EQ. TEST) THEN
                  S(LS) = 0.0E0
                  GO TO 100
               END IF
   90       CONTINUE
  100       CONTINUE
            IF (LS .EQ. L) THEN
               KASE = 3
            ELSE IF (LS .EQ. M) THEN
               KASE = 1
            ELSE
               KASE = 2
               L = LS
            END IF
         END IF
         L = L + 1
C                                  PERFORM THE TASK INDICATED BY KASE.
         IF (KASE .EQ. 1) THEN
C                                  DEFLATE NEGLIGIBLE S(M).
            MM1 = M - 1
            F = E(M-1)
            E(M-1) = 0.0E0
            DO 110  KK=L, MM1
               K = MM1 - KK + L
               T1 = S(K)
               CALL SROTG (T1, F, CS, SN)
               S(K) = T1
               IF (K .NE. L) THEN
                  F = -SN*E(K-1)
                  E(K-1) = CS*E(K-1)
               END IF
               IF (WANTV) CALL SROT (NCA, V(1,K), 1, V(1,M), 1, CS, SN)
  110       CONTINUE
C                                  SPLIT AT NEGLIGIBLE S(L).
         ELSE IF (KASE .EQ. 2) THEN
            F = E(L-1)
            E(L-1) = 0.0E0
            DO 120  K=L, M
               T1 = S(K)
               CALL SROTG (T1, F, CS, SN)
               S(K) = T1
               F = -SN*E(K)
               E(K) = CS*E(K)
               IF (WANTU) CALL SROT (NRA, U(1,K), 1, U(1,L-1), 1, CS,
     &             SN)
  120       CONTINUE
C                                  PERFORM ONE QR STEP.
         ELSE IF (KASE .EQ. 3) THEN
C                                  CALCULATE THE SHIFT.
            SCALE = AMAX1(ABS(S(M)),ABS(S(M-1)),ABS(E(M-1)),ABS(S(L)),
     &              ABS(E(L)))
            SM = S(M)/SCALE
            SMM1 = S(M-1)/SCALE
            EMM1 = E(M-1)/SCALE
            SL = S(L)/SCALE
            EL = E(L)/SCALE
            B = ((SMM1+SM)*(SMM1-SM)+EMM1**2)/2.0E0
            C = (SM*EMM1)**2
            SHIFT = 0.0E0
            IF (B.NE.0.0E0 .OR. C.NE.0.0E0) THEN
               SHIFT = SQRT(B**2+C)
               IF (B .LT. 0.0E0) SHIFT = -SHIFT
               SHIFT = C/(B+SHIFT)
            END IF
            F = (SL+SM)*(SL-SM) - SHIFT
            G = SL*EL
C                                  CHASE ZEROS.
            MM1 = M - 1
            DO 130  K=L, MM1
               CALL SROTG (F, G, CS, SN)
               IF (K .NE. L) E(K-1) = F
               F = CS*S(K) + SN*E(K)
               E(K) = CS*E(K) - SN*S(K)
               G = SN*S(K+1)
               S(K+1) = CS*S(K+1)
               IF (WANTV) CALL SROT (NCA, V(1,K), 1, V(1,K+1), 1, CS,
     &             SN)
               CALL SROTG (F, G, CS, SN)
               S(K) = F
               F = CS*E(K) + SN*S(K+1)
               S(K+1) = -SN*E(K) + CS*S(K+1)
               G = SN*E(K+1)
               E(K+1) = CS*E(K+1)
               IF (WANTU .AND. K.LT.NRA) CALL SROT (NRA, U(1,K), 1,
     &             U(1,K+1), 1, CS, SN)
  130       CONTINUE
            E(M-1) = F
            ITER = ITER + 1
C                                  CONVERGENCE.
         ELSE IF (KASE .EQ. 4) THEN
C                                  MAKE THE SINGULAR VALUE POSITIVE.
            IF (S(L) .LT. 0.0E0) THEN
               S(L) = -S(L)
               IF (WANTV) CALL SSCAL (NCA, -1.0E0, V(1,L), 1)
            END IF
C                                  ORDER THE SINGULAR VALUE.
  140       IF (L .EQ. MM) GO TO 150
            IF (S(L) .GE. S(L+1)) GO TO 150
            T = S(L)
            S(L) = S(L+1)
            S(L+1) = T
            IF (WANTV .AND. L.LT.NCA) CALL SSWAP (NCA, V(1,L), 1, V(1,
     &          L+1), 1)
            IF (WANTU .AND. L.LT.NRA) CALL SSWAP (NRA, U(1,L), 1, U(1,
     &          L+1), 1)
            L = L + 1
            GO TO 140
  150       CONTINUE
            ITER = 0
            M = M - 1
         END IF
         GO TO 60
      END IF
      IF (ITER .GT. MAXIT) INFO = M
      IF (INFO .EQ. 0) THEN
C                                  ESTIMATE THE RANK OF A
         LS = MIN0(NCA+1,NRA)
         MINMN = MIN0(NCA,NRA)
         IRANK = MINMN
         DO 160  I=1, MINMN
            IF (ABS(S(I)) .LE. STOL) THEN
               IRANK = I - 1
               GO TO 170
            END IF
  160    CONTINUE
  170    CONTINUE
      ELSE
         ISTART = INFO + 1
         IEND = MIN0(NRA,NCA)
         CALL E1STI (1, ISTART)
         CALL E1STI (2, IEND)
C         CALL E1MES (4, 1, 'CONVERGENCE CAN ONLY BE OBTAINED '//
C     &               'FOR THE %(I1), ... , %(I2) SINGULAR VALUES '//
C     &               'AND THEIR CORRESPONDING SINGULAR VECTORS.')
      END IF
      CALL E1POP ('L3VRR ')
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  E1MES
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    MARCH 2, 1984
C
C  PURPOSE:    SET AN ERROR STATE FOR THE CURRENT LEVEL IN THE STACK.
C              THE MESSAGE IS PRINTED IMMEDIATELY IF THE ERROR TYPE IS
C              5, 6, OR 7 AND THE PRINT ATTRIBUTE FOR THAT TYPE IS YES.
C
C  USAGE:      CALL E1MES(IERTYP,IERCOD,MSGPKD)
C
C  ARGUMENTS:
C     IERTYP - INTEGER SPECIFYING THE ERROR TYPE.  (INPUT)
C                IERTYP=1,  INFORMATIONAL/NOTE
C                IERTYP=2,  INFORMATIONAL/ALERT
C                IERTYP=3,  INFORMATIONAL/WARNING
C                IERTYP=4,  INFORMATIONAL/FATAL
C                IERTYP=5,  TERMINAL
C                IERTYP=6,  PROTRAN/WARNING
C                IERTYP=7,  PROTRAN/FATAL
C     IERCOD - INTEGER SPECIFYING THE ERROR CODE.  (INPUT)
C     MSGPKD - A CHARACTER STRING CONTAINING THE MESSAGE.
C              (INPUT)  WITHIN THE MESSAGE, ANY OF FOLLOWING MAY APPEAR
C                %(A1),%(A2),...,%(A9) FOR CHARACTER ARRAYS
C                %(C1),%(C2),...,%(C9) FOR COMPLEX NUMBERS
C                %(D1),%(D2),...,%(D9) FOR DOUBLE PRECISION NUMBERS
C                %(I1),%(I2),...,%(I9) FOR INTEGER NUMBERS
C                %(K1),%(K2),...,%(K9) FOR KEYWORDS
C                %(L1),%(L2),...,%(L9) FOR LITERALS (STRINGS)
C                %(R1),%(R2),...,%(R9) FOR REAL NUMBERS
C                %(Z1),%(Z2),...,%(Z9) FOR DOUBLE COMPLEX NUMBERS
C              THIS PROVIDES A WAY TO INSERT CHARACTER ARRAYS, STRINGS,
C              NUMBERS, AND KEYWORDS INTO THE MESSAGE.  SEE REMARKS
C              BELOW.
C
C  REMARKS:
C     THE NUMBER OF CHARACTERS IN THE MESSAGE AFTER THE INSERTION OF
C     THE CORRESPONDING STRINGS, ETC. SHOULD NOT EXCEED 255.  IF THE
C     LIMIT IS EXCEEDED, ONLY THE FIRST 255 CHARACTERS WILL BE USED.
C     THE APPROPRIATE STRINGS, ETC. NEED TO HAVE BEEN PREVIOUSLY STORED
C     IN COMMON VIA CALLS TO E1STA, E1STD, ETC.  LINE BREAKS MAY BE
C     SPECIFIED BY INSERTING THE TWO CHARACTERS '%/' INTO THE MESSAGE
C     AT THE DESIRED LOCATIONS.
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE E1MES (IERTYP, IERCOD, MSGPKD)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    IERTYP, IERCOD
      CHARACTER  MSGPKD*(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    ERTYP2, I, IER, IPLEN, ISUB, LAST, LEN2, LOC, M, MS,
     &           NLOC, NUM, PBEG
      CHARACTER  MSGTMP(255)
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      INTEGER    IFINIT, NFORMS
      CHARACTER  BLNK, DBB(3), FIND(4), FORMS(9), INREF(25), LPAR,
     &           NCHECK(3), PERCNT, RPAR
      SAVE       BLNK, DBB, FIND, FORMS, IFINIT, INREF, LPAR, NCHECK,
     &           NFORMS, PERCNT, RPAR
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                              SPECIFICATIONS FOR COMMON /ERCOM1/
      INTEGER    CALLVL, MAXLEV, MSGLEN, ERTYPE(51), ERCODE(51),
     &           PRINTB(7), STOPTB(7), PLEN, IFERR6, IFERR7,
     &           IALLOC(51), HDRFMT(7), TRACON(7)
      COMMON     /ERCOM1/ CALLVL, MAXLEV, MSGLEN, ERTYPE, ERCODE,
     &           PRINTB, STOPTB, PLEN, IFERR6, IFERR7, IALLOC, HDRFMT,
     &           TRACON
      SAVE       /ERCOM1/
C                              SPECIFICATIONS FOR COMMON /ERCOM2/
      CHARACTER  MSGSAV(255), PLIST(300), RNAME(51)*6
      COMMON     /ERCOM2/ MSGSAV, PLIST, RNAME
      SAVE       /ERCOM2/
C                              SPECIFICATIONS FOR COMMON /ERCOM3/
      DOUBLE PRECISION ERCKSM
      COMMON     /ERCOM3/ ERCKSM
      SAVE       /ERCOM3/
C                              SPECIFICATIONS FOR COMMON /ERCOM4/
      LOGICAL    ISUSER(51)
      COMMON     /ERCOM4/ ISUSER
      SAVE       /ERCOM4/
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  LEN,MIN0
      INTRINSIC  LEN, MIN0
      INTEGER    LEN, MIN0
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   C1TCI, E1INIT, E1PRT, E1UCS, M1VE, M1VECH
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   I1DX
      INTEGER    I1DX
C
      DATA FORMS/'A', 'C', 'D', 'I', 'K', 'L', 'R', 'S', 'Z'/,
     &     NFORMS/9/
      DATA PERCNT/'%'/, LPAR/'('/, RPAR/')'/, BLNK/' '/
      DATA INREF/' ', 'I', 'N', ' ', 'R', 'E', 'F', 'E', 'R',
     &     'E', 'N', 'C', 'E', ' ', 'T', 'O', ' ', 'K', 'E',
     &     'Y', 'W', 'O', 'R', 'D', ' '/
      DATA NCHECK/'N', '1', '*'/, DBB/'.', ' ', ' '/
      DATA FIND/'*', ' ', ' ', '*'/
      DATA IFINIT/0/
C                                  INITIALIZE ERROR TABLE IF NECESSARY
      IF (IFINIT .EQ. 0) THEN
         CALL E1INIT
         IFINIT = 1
      END IF
C                                  CHECK AND SET ERROR TYPE IF NECESSARY
      IF (IERTYP .NE. -1) THEN
         ERTYPE(CALLVL) = IERTYP
      ELSE IF (IERTYP.LT.-1 .OR. IERTYP.GT.7) THEN
         MSGLEN = 51
         CALL M1VECH ('.  ERROR FROM E1MES.  ILLEGAL ERROR TYPE'//
     &                ' SPECIFIED. ', MSGLEN, MSGSAV, MSGLEN)
         CALL E1PRT
         STOP
      END IF
C
      ERTYP2 = ERTYPE(CALLVL)
C                                  SET ERROR CODE IF NECESSARY
      IF (IERCOD .GT. -1) ERCODE(CALLVL) = IERCOD
      LEN2 = LEN(MSGPKD)
C
      IF (IERTYP.EQ.0 .OR. IERCOD.EQ.0) THEN
C                                  REMOVE THE ERROR STATE
         MSGLEN = 0
      ELSE IF (LEN2.EQ.0 .OR. (LEN2.EQ.1.AND.MSGPKD(1:1).EQ.BLNK)) THEN
         IF (ERTYP2 .EQ. 6) IFERR6 = 1
         IF (ERTYP2 .EQ. 7) IFERR7 = 1
C                                  UPDATE CHECKSUM PARAMETER ERCKSM
         CALL E1UCS
C                                  PRINT MESSAGE IF NECESSARY
         IF (ERTYP2.GE.5 .AND. PRINTB(ERTYP2).EQ.1) CALL E1PRT
      ELSE
C                                  FILL UP MSGSAV WITH EXPANDED MESSAGE
         LEN2 = MIN0(LEN2,255)
         DO 10  I=1, LEN2
            MSGTMP(I) = MSGPKD(I:I)
   10    CONTINUE
         MS = 0
         M = 0
C                                  CHECK PLIST FOR KEYWORD NAME
         NLOC = I1DX(PLIST,PLEN,NCHECK,3)
         IF (NLOC.GT.0 .AND. HDRFMT(ERTYP2).EQ.3) THEN
C                                  M1VE INREF INTO MSGSAV
            CALL M1VE (INREF, 1, 25, 25, MSGSAV, 1, 25, 25, IER)
C                                  GET LENGTH OF KEYWORD NAME
            CALL C1TCI (PLIST(NLOC+3), 3, IPLEN, IER)
            PBEG = NLOC + 3 + IER
C                                  M1VE KEYWORD NAME INTO MSGSAV
            CALL M1VE (PLIST, PBEG, PBEG+IPLEN-1, PLEN, MSGSAV, 26,
     &                 IPLEN+25, 255, IER)
C                                  UPDATE POINTER
            MS = IPLEN + 25
         END IF
C                                  INSERT DOT, BLANK, BLANK
         CALL M1VE (DBB, 1, 3, 3, MSGSAV, MS+1, MS+3, 255, IER)
         MS = MS + 3
C                                  LOOK AT NEXT CHARACTER
   20    M = M + 1
         ISUB = 0
         IF (M .GT. LEN2-4) THEN
            LAST = LEN2 - M + 1
            DO 30  I=1, LAST
   30       MSGSAV(MS+I) = MSGTMP(M+I-1)
            MSGLEN = MS + LAST
            GO TO 40
         ELSE IF (MSGTMP(M).EQ.PERCNT .AND. MSGTMP(M+1).EQ.LPAR .AND.
     &           MSGTMP(M+4).EQ.RPAR) THEN
            CALL C1TCI (MSGTMP(M+3), 1, NUM, IER)
            IF (IER.EQ.0 .AND. NUM.NE.0 .AND. I1DX(FORMS,NFORMS,
     &          MSGTMP(M+2),1).NE.0) THEN
C                                  LOCATE THE ITEM IN THE PARAMETER LIST
               CALL M1VE (MSGTMP(M+2), 1, 2, 2, FIND, 2, 3, 4, IER)
               LOC = I1DX(PLIST,PLEN,FIND,4)
               IF (LOC .GT. 0) THEN
C                                  SET IPLEN = LENGTH OF STRING
                  CALL C1TCI (PLIST(LOC+4), 4, IPLEN, IER)
                  PBEG = LOC + 4 + IER
C                                  ADJUST IPLEN IF IT IS TOO BIG
                  IPLEN = MIN0(IPLEN,255-MS)
C                                  M1VE STRING FROM PLIST INTO MSGSAV
                  CALL M1VE (PLIST, PBEG, PBEG+IPLEN-1, PLEN, MSGSAV,
     &                       MS+1, MS+IPLEN, 255, IER)
                  IF (IER.GE.0 .AND. IER.LT.IPLEN) THEN
C                                  UPDATE POINTERS
                     M = M + 4
                     MS = MS + IPLEN - IER
C                                  BAIL OUT IF NO MORE ROOM
                     IF (MS .GE. 255) THEN
                        MSGLEN = 255
                        GO TO 40
                     END IF
C                                  SET FLAG TO SHOW SUBSTITION WAS MADE
                     ISUB = 1
                  END IF
               END IF
            END IF
         END IF
         IF (ISUB .EQ. 0) THEN
            MS = MS + 1
            MSGSAV(MS) = MSGTMP(M)
         END IF
         GO TO 20
   40    ERTYP2 = ERTYPE(CALLVL)
         IF (ERTYP2 .EQ. 6) IFERR6 = 1
         IF (ERTYP2 .EQ. 7) IFERR7 = 1
C                                  UPDATE CHECKSUM PARAMETER ERCKSM
         CALL E1UCS
C                                  PRINT MESSAGE IF NECESSARY
         IF (ERTYP2.GE.5 .AND. PRINTB(ERTYP2).EQ.1) CALL E1PRT
      END IF
C                                  CLEAR PARAMETER LIST
      PLEN = 1
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  E1POP
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    MARCH 13, 1984
C
C  PURPOSE:    TO POP A SUBROUTINE NAME FROM THE ERROR CONTROL STACK.
C
C  USAGE:      CALL E1POP(NAME)
C
C  ARGUMENTS:
C     NAME   - A CHARACTER STRING OF LENGTH SIX SPECIFYING THE NAME
C              OF THE SUBROUTINE.  (INPUT)
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE E1POP (NAME)
C                                  SPECIFICATIONS FOR ARGUMENTS
      CHARACTER  NAME*(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    IERTYP, IR
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                              SPECIFICATIONS FOR COMMON /ERCOM1/
      INTEGER    CALLVL, MAXLEV, MSGLEN, ERTYPE(51), ERCODE(51),
     &           PRINTB(7), STOPTB(7), PLEN, IFERR6, IFERR7,
     &           IALLOC(51), HDRFMT(7), TRACON(7)
      COMMON     /ERCOM1/ CALLVL, MAXLEV, MSGLEN, ERTYPE, ERCODE,
     &           PRINTB, STOPTB, PLEN, IFERR6, IFERR7, IALLOC, HDRFMT,
     &           TRACON
      SAVE       /ERCOM1/
C                              SPECIFICATIONS FOR COMMON /ERCOM2/
      CHARACTER  MSGSAV(255), PLIST(300), RNAME(51)*6
      COMMON     /ERCOM2/ MSGSAV, PLIST, RNAME
      SAVE       /ERCOM2/
C                              SPECIFICATIONS FOR COMMON /ERCOM3/
      DOUBLE PRECISION ERCKSM
      COMMON     /ERCOM3/ ERCKSM
      SAVE       /ERCOM3/
C                              SPECIFICATIONS FOR COMMON /ERCOM4/
      LOGICAL    ISUSER(51)
      COMMON     /ERCOM4/ ISUSER
      SAVE       /ERCOM4/
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   E1MES, E1PRT, E1PSH, E1STI, E1STL, I1KRL
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   I1KST
      INTEGER    I1KST
C
      IF (CALLVL .LE. 1) THEN
         CALL E1PSH ('E1POP ')
         CALL E1STL (1, NAME)
         CALL E1MES (5, 1, 'ERROR CONDITION IN E1POP.  CANNOT POP '//
     &               'FROM %(L1) BECAUSE STACK IS EMPTY.')
         STOP
      ELSE IF (NAME .NE. RNAME(CALLVL)) THEN
         CALL E1STL (1, NAME)
         CALL E1STL (2, RNAME(CALLVL))
         CALL E1MES (5, 2, 'ERROR CONDITION IN E1POP.  %(L1) DOES '//
     &               'NOT MATCH THE NAME %(L2) IN THE STACK.')
         STOP
      ELSE
         IERTYP = ERTYPE(CALLVL)
         IF (IERTYP .NE. 0) THEN
C                                  M1VE ERROR TYPE AND ERROR CODE TO
C                                    PREVIOUS LEVEL FOR ERROR TYPES 2-7
            IF (IERTYP.GE.2 .AND. IERTYP.LE.7) THEN
               ERTYPE(CALLVL-1) = ERTYPE(CALLVL)
               ERCODE(CALLVL-1) = ERCODE(CALLVL)
            END IF
C                                  CHECK PRINT TABLE TO DETERMINE
C                                    WHETHER TO PRINT STORED MESSAGE
            IF (IERTYP .LE. 4) THEN
               IF (ISUSER(CALLVL-1) .AND. PRINTB(IERTYP).EQ.1)
     &             CALL E1PRT
            ELSE
               IF (PRINTB(IERTYP) .EQ. 1) CALL E1PRT
            END IF
C                                  CHECK STOP TABLE AND ERROR TYPE TO
C                                    DETERMINE WHETHER TO STOP
            IF (IERTYP .LE. 4) THEN
               IF (ISUSER(CALLVL-1) .AND. STOPTB(IERTYP).EQ.1) THEN
                  STOP
               END IF
            ELSE IF (IERTYP .EQ. 5) THEN
               IF (STOPTB(IERTYP) .EQ. 1) THEN
                  STOP
               END IF
            ELSE IF (HDRFMT(IERTYP) .EQ. 1) THEN
               IF (ISUSER(CALLVL-1)) THEN
                  IF (N1RGB(0) .NE. 0) THEN
                     STOP
                  END IF
               END IF
            END IF
         END IF
C                                  SET ERROR TYPE AND CODE
         IF (CALLVL .LT. MAXLEV) THEN
            ERTYPE(CALLVL+1) = -1
            ERCODE(CALLVL+1) = -1
         END IF
C                                  SET IR = AMOUNT OF WORKSPACE
C                                  ALLOCATED AT THIS LEVEL
         IR = I1KST(1) - IALLOC(CALLVL-1)
         IF (IR .GT. 0) THEN
C                                  RELEASE WORKSPACE
            CALL I1KRL (IR)
            IALLOC(CALLVL) = 0
         ELSE IF (IR .LT. 0) THEN
            CALL E1STI (1, CALLVL)
            CALL E1STI (2, IALLOC(CALLVL-1))
            CALL E1STI (3, I1KST(1))
            CALL E1MES (5, 3, 'ERROR CONDITION IN E1POP. '//
     &                  ' THE NUMBER OF WORKSPACE ALLOCATIONS AT '//
     &                  'LEVEL %(I1) IS %(I2).  HOWEVER, THE TOTAL '//
     &                  'NUMBER OF WORKSPACE ALLOCATIONS IS %(I3).')
            STOP
         END IF
C                                  DECREASE THE STACK POINTER BY ONE
         CALLVL = CALLVL - 1
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  E1PSH
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    MARCH 2, 1984
C
C  PURPOSE:    TO PUSH A SUBROUTINE NAME ONTO THE ERROR CONTROL STACK.
C
C  USAGE:      CALL E1PSH(NAME)
C
C  ARGUMENTS:
C     NAME   - A CHARACTER STRING OF LENGTH SIX SPECIFING THE NAME OF
C              THE SUBROUTINE.  (INPUT)
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE E1PSH (NAME)
C                                  SPECIFICATIONS FOR ARGUMENTS
      CHARACTER  NAME*(*)
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      INTEGER    IFINIT
      SAVE       IFINIT
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                              SPECIFICATIONS FOR COMMON /ERCOM1/
      INTEGER    CALLVL, MAXLEV, MSGLEN, ERTYPE(51), ERCODE(51),
     &           PRINTB(7), STOPTB(7), PLEN, IFERR6, IFERR7,
     &           IALLOC(51), HDRFMT(7), TRACON(7)
      COMMON     /ERCOM1/ CALLVL, MAXLEV, MSGLEN, ERTYPE, ERCODE,
     &           PRINTB, STOPTB, PLEN, IFERR6, IFERR7, IALLOC, HDRFMT,
     &           TRACON
      SAVE       /ERCOM1/
C                              SPECIFICATIONS FOR COMMON /ERCOM2/
      CHARACTER  MSGSAV(255), PLIST(300), RNAME(51)*6
      COMMON     /ERCOM2/ MSGSAV, PLIST, RNAME
      SAVE       /ERCOM2/
C                              SPECIFICATIONS FOR COMMON /ERCOM3/
      DOUBLE PRECISION ERCKSM
      COMMON     /ERCOM3/ ERCKSM
      SAVE       /ERCOM3/
C                              SPECIFICATIONS FOR COMMON /ERCOM4/
      LOGICAL    ISUSER(51)
      COMMON     /ERCOM4/ ISUSER
      SAVE       /ERCOM4/
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   E1INIT, E1MES, E1STI
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   I1KST
      INTEGER    I1KST
C
      DATA IFINIT/0/
C                                  INITIALIZE ERROR TABLE IF NECESSARY
      IF (IFINIT .EQ. 0) THEN
         CALL E1INIT
         IFINIT = 1
      END IF
      IF (CALLVL .GE. MAXLEV) THEN
         CALL E1STI (1, MAXLEV)
         CALL E1MES (5, 1, 'ERROR CONDITION IN E1PSH.  PUSH WOULD '//
     &               'CAUSE STACK LEVEL TO EXCEED %(I1). ')
         STOP
      ELSE
C                                  STORE ALLOCATION LEVEL
         IALLOC(CALLVL) = I1KST(1)
C                                  INCREMENT THE STACK POINTER BY ONE
         CALLVL = CALLVL + 1
C                                  PUT SUBROUTINE NAME INTO STACK
         RNAME(CALLVL) = NAME
C                                  SET ERROR TYPE AND ERROR CODE
         ERTYPE(CALLVL) = 0
         ERCODE(CALLVL) = 0
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  E1STI
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    MARCH 6, 1984
C
C  PURPOSE:    TO STORE AN INTEGER FOR SUBSEQUENT USE WITHIN AN ERROR
C              MESSAGE.
C
C  USAGE:      CALL E1STI(II, IVALUE)
C
C  ARGUMENTS:
C     II     - INTEGER SPECIFYING THE SUBSTITUTION INDEX.  II MUST BE
C              BETWEEN 1 AND 9.  (INPUT)
C     IVALUE - THE INTEGER TO BE STORED.  (INPUT)
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE E1STI (II, IVALUE)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    II, IVALUE
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    IBEG, IER, ILEN
      CHARACTER  ARRAY(14)
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      INTEGER    IFINIT
      CHARACTER  BLANK(1)
      SAVE       BLANK, IFINIT
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                              SPECIFICATIONS FOR COMMON /ERCOM1/
      INTEGER    CALLVL, MAXLEV, MSGLEN, ERTYPE(51), ERCODE(51),
     &           PRINTB(7), STOPTB(7), PLEN, IFERR6, IFERR7,
     &           IALLOC(51), HDRFMT(7), TRACON(7)
      COMMON     /ERCOM1/ CALLVL, MAXLEV, MSGLEN, ERTYPE, ERCODE,
     &           PRINTB, STOPTB, PLEN, IFERR6, IFERR7, IALLOC, HDRFMT,
     &           TRACON
      SAVE       /ERCOM1/
C                              SPECIFICATIONS FOR COMMON /ERCOM2/
      CHARACTER  MSGSAV(255), PLIST(300), RNAME(51)*6
      COMMON     /ERCOM2/ MSGSAV, PLIST, RNAME
      SAVE       /ERCOM2/
C                              SPECIFICATIONS FOR COMMON /ERCOM3/
      DOUBLE PRECISION ERCKSM
      COMMON     /ERCOM3/ ERCKSM
      SAVE       /ERCOM3/
C                              SPECIFICATIONS FOR COMMON /ERCOM4/
      LOGICAL    ISUSER(51)
      COMMON     /ERCOM4/ ISUSER
      SAVE       /ERCOM4/
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   C1TIC, E1INIT, E1INPL
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   I1ERIF
      INTEGER    I1ERIF
C
      DATA BLANK/' '/, IFINIT/0/
C                                  INITIALIZE IF NECESSARY
      IF (IFINIT .EQ. 0) THEN
         CALL E1INIT
         IFINIT = 1
      END IF
      CALL C1TIC (IVALUE, ARRAY, 14, IER)
      IBEG = I1ERIF(ARRAY,14,BLANK,1)
      IF (II.GE.1 .AND. II.LE.9 .AND. IER.EQ.0) THEN
         ILEN = 15 - IBEG
         CALL E1INPL ('I', II, ILEN, ARRAY(IBEG))
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  I1KGT
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    JANUARY 17, 1984
C
C  PURPOSE:    ALLOCATE NUMERICAL WORKSPACE.
C
C  USAGE:      I1KGT(NELMTS,ITYPE)
C
C  ARGUMENTS:
C     NELMTS - NUMBER OF ELEMENTS OF DATA TYPE ITYPE TO BE
C              ALLOCATED.  (INPUT)
C     ITYPE  - DATA TYPE OF ARRAY TO BE ALLOCATED.  (INPUT)
C                 1 - LOGICAL
C                 2 - INTEGER
C                 3 - REAL
C                 4 - DOUBLE PRECISION
C                 5 - COMPLEX
C                 6 - DOUBLE COMPLEX
C     I1KGT  - INTEGER FUNCTION.  (OUTPUT)  RETURNS THE INDEX OF THE
C              FIRST ELEMENT IN THE CURRENT ALLOCATION.
C
C  REMARKS:
C  1. ON RETURN, THE ARRAY WILL OCCUPY
C     WKSP(I1KGT), WKSP(I1KGT+1), ..., WKSP(I1KGT+NELMTS-1) WHERE
C     WKSP IS AN ARRAY OF DATA TYPE ITYPE EQUIVALENCED TO RWKSP.
C
C  2. IF I1KGT IS NEGATIVE, THE ABSOLUTE VALUE OF I1KGT IS THE
C     ADDITIONAL WORKSPACE NEEDED FOR THE CURRENT ALLOCATION.
C
C  3. THE ALLOCATOR RESERVES THE FIRST SIXTEEN INTEGER LOCATIONS OF
C     THE STACK FOR ITS OWN INTERNAL BOOKKEEPING.  THESE ARE INITIALIZED
C     BY THE FUNCTION IWKIN UPON THE FIRST CALL TO THE ALLOCATION
C     PACKAGE.
C
C  4. THE USE OF THE FIRST TEN INTEGER LOCATIONS IS AS FOLLOWS:
C      WKSP( 1) - LOUT    THE NUMBER OF CURRENT ALLOCATIONS
C      WKSP( 2) - LNOW    THE CURRENT ACTIVE LENGTH OF THE STACK
C      WKSP( 3) - LUSED   THE MAXIMUM VALUE OF WKSP(2) ACHIEVED
C                         THUS FAR
C      WKSP( 4) - LBND    THE LOWER BOUND OF PERMANENT STORAGE WHICH
C                         IS ONE NUMERIC STORAGE UNIT MORE THAN THE
C                         MAXIMUM ALLOWED LENGTH OF THE STACK.
C      WKSP( 5) - LMAX    THE MAXIMUM LENGTH OF THE STORAGE ARRAY
C      WKSP( 6) - LALC    THE TOTAL NUMBER OF ALLOCATIONS HANDLED BY
C                         I1KGT
C      WKSP( 7) - LNEED   THE NUMBER OF NUMERIC STORAGE UNITS BY WHICH
C                         THE ARRAY SIZE MUST BE INCREASED FOR ALL PAST
C                         ALLOCATIONS TO SUCCEED
C      WKSP( 8) - LBOOK   THE NUMBER OF NUMERIC STORAGE UNITS USED FOR
C                         BOOKKEEPING
C      WKSP( 9) - LCHAR   THE POINTER TO THE PORTION OF THE PERMANENT
C                         STACK WHICH CONTAINS THE BOOKKEEPING AND
C                         POINTERS FOR THE CHARACTER WORKSPACE
C                         ALLOCATION.
C      WKSP(10) - LLCHAR  THE LENGTH OF THE ARRAY BEGINNING AT LCHAR
C                         SET ASIDE FOR CHARACTER WORKSPACE BOOKKEEPING
C                         AND POINTERS.
C                 NOTE -  IF CHARACTER WORKSPACE IS NOT BEING USED,
C                         LCHAR AND LLCHAR CAN BE IGNORED.
C  5. THE NEXT SIX INTEGER LOCATIONS CONTAIN VALUES DESCRIBING THE
C     AMOUNT OF STORAGE ALLOCATED BY THE ALLOCATION SYSTEM TO THE
C     VARIOUS DATA TYPES.
C      WKSP(11) - NUMERIC STORAGE UNITS ALLOCATED TO LOGICAL
C      WKSP(12) - NUMERIC STORAGE UNITS ALLOCATED TO INTEGER
C      WKSP(13) - NUMERIC STORAGE UNITS ALLOCATED TO REAL
C      WKSP(14) - NUMERIC STORAGE UNITS ALLOCATED TO DOUBLE PRECISION
C      WKSP(15) - NUMERIC STORAGE UNITS ALLOCATED TO COMPLEX
C      WKSP(16) - NUMERIC STORAGE UNITS ALLOCATED TO DOUBLE COMPLEX
C
C  COPYRIGHT:  1984 BY IMSL, INC. ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      INTEGER FUNCTION I1KGT (NELMTS, ITYPE)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    NELMTS, ITYPE
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, IDUMAL, IGAP, ILEFT, IPA, IPA7, ISA, ISA7,
     &           ISIZE(6), JTYPE, LALC, LBND, LBOOK, LMAX, LNEED,
     &           LNEED1, LNOW, LOUT, LUSED
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      LOGICAL    FIRST
      SAVE       FIRST
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                              SPECIFICATIONS FOR COMMON /ERCOM8/
      INTEGER    PROLVL, XXLINE(10), XXPLEN(10), ICALOC(10), INALOC(10)
      COMMON     /ERCOM8/ PROLVL, XXLINE, XXPLEN, ICALOC, INALOC
      SAVE       /ERCOM8/
C                              SPECIFICATIONS FOR COMMON /ERCOM9/
      CHARACTER  XXPROC(10)*31
      COMMON     /ERCOM9/ XXPROC
      SAVE       /ERCOM9/
C                                  SPECIFICATIONS FOR COMMON /WORKSP/
      REAL       RWKSP(61913)
      REAL       RDWKSP(5000)
      DOUBLE PRECISION DWKSP(2500)
      COMPLEX    CWKSP(2500)
      COMPLEX    CZWKSP(2500)
      COMPLEX    *16 ZWKSP(1250)
      INTEGER    IWKSP(5000)
      LOGICAL    LWKSP(5000)
      EQUIVALENCE (DWKSP(1), RWKSP(1))
      EQUIVALENCE (CWKSP(1), RWKSP(1)), (ZWKSP(1), RWKSP(1))
      EQUIVALENCE (IWKSP(1), RWKSP(1)), (LWKSP(1), RWKSP(1))
      EQUIVALENCE (RDWKSP(1), RWKSP(1)), (CZWKSP(1), RWKSP(1))
      COMMON     /WORKSP/ RWKSP
C                                  SPECIFICATIONS FOR EQUIVALENCE
      EQUIVALENCE (LOUT, IWKSP(1))
      EQUIVALENCE (LNOW, IWKSP(2))
      EQUIVALENCE (LUSED, IWKSP(3))
      EQUIVALENCE (LBND, IWKSP(4))
      EQUIVALENCE (LMAX, IWKSP(5))
      EQUIVALENCE (LALC, IWKSP(6))
      EQUIVALENCE (LNEED, IWKSP(7))
      EQUIVALENCE (LBOOK, IWKSP(8))
      EQUIVALENCE (ISIZE(1), IWKSP(11))
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  IABS,MAX0,MOD
      INTRINSIC  IABS, MAX0, MOD
      INTEGER    IABS, MAX0, MOD
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   E1MES, E1POP, E1POS, E1PSH, E1STI, IWKIN
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   I1KQU
      INTEGER    I1KQU
C
      DATA FIRST/.TRUE./
C
      CALL E1PSH ('I1KGT ')
C
      IF (FIRST) THEN
C                                  INITIALIZE WORKSPACE IF NEEDED
         FIRST = .FALSE.
         CALL IWKIN (0)
      END IF
C                                  NUMBER OF ELEMENTS LESS THAN 0
      IF (NELMTS .LT. 0) THEN
         CALL E1STI (1, NELMTS)
         CALL E1MES (5, 2, 'NUMBER OF ELEMENTS IS NOT POSITIVE.%/'//
     &               'NELMTS = %(I1).')
         CALL E1POP ('I1KGT ')
         GO TO 9000
      END IF
C                                  ILLEGAL DATA TYPE REQUESTED
      IF (ITYPE.EQ.0 .OR. IABS(ITYPE).GE.7) THEN
         CALL E1MES (5, 3, 'ILLEGAL DATA TYPE REQUESTED.')
         CALL E1POP ('I1KGT ')
         GO TO 9000
      END IF
C                                  BOOKKEEPING OVERWRITTEN
      IF (LNOW.LT.LBOOK .OR. LNOW.GT.LUSED .OR. LUSED.GT.LMAX .OR.
     &    LNOW.GE.LBND .OR. LOUT.GT.LALC) THEN
         CALL E1MES (5, 4, 'ONE OR MORE OF THE FIRST EIGHT '//
     &               'BOOKKEEPING LOCATIONS IN IWKSP HAVE BEEN '//
     &               'OVERWRITTEN.')
         CALL E1POP ('I1KGT ')
         GO TO 9000
      END IF
C
      CALL E1POP ('I1KGT ')
C                                  DETERMINE NUMBER OF LOCATIONS STILL
C                                  AVAILABLE FOR DATA TYPE ITYPE
C                                  NOTE: I1KQU ALLOWS FOR 2 INTEGER
C                                        POINTERS WHICH MUST BE HANDLED
C                                        ARTIFICIALLY IF ILEFT = 0.
      ILEFT = I1KQU(IABS(ITYPE))
C
      IF (ITYPE .GT. 0) THEN
C                                  RELEASABLE STORAGE
         IF (ILEFT .GE. NELMTS) THEN
            I1KGT = (LNOW*ISIZE(2)-1)/ISIZE(ITYPE) + 2
            I = ((I1KGT-1+NELMTS)*ISIZE(ITYPE)-1)/ISIZE(2) + 3
C                                  IWKSP(I-1) CONTAINS THE DATA TYPE FOR
C                                  THIS ALLOCATION. IWKSP(I) CONTAINS
C                                  LNOW FOR THE PREVIOUS ALLOCATION.
            IWKSP(I-1) = ITYPE
            IWKSP(I) = LNOW
            LOUT = LOUT + 1
            LALC = LALC + 1
            LNOW = I
            LUSED = MAX0(LUSED,LNOW)
            LNEED = 0
         ELSE
C                                  RELEASABLE STORAGE WAS REQUESTED
C                                  BUT THE STACK WOULD OVERFLOW.
C                                  THEREFORE, ALLOCATE RELEASABLE
C                                  SPACE THROUGH THE END OF THE STACK
            IF (LNEED .EQ. 0) THEN
               IDUMAL = (LNOW*ISIZE(2)-1)/ISIZE(ITYPE) + 2
               I = ((IDUMAL-1+ILEFT)*ISIZE(ITYPE)-1)/ISIZE(2) + 3
C                                  ADVANCE COUNTERS AND STORE POINTERS
C                                  IF THERE IS ROOM TO DO SO
               IF (I .LT. LBND) THEN
C                                  IWKSP(I-1) CONTAINS THE DATA TYPE FOR
C                                  THIS ALLOCATION. IWKSP(I) CONTAINS
C                                  LNOW FOR THE PREVIOUS ALLOCATION.
                  IWKSP(I-1) = ITYPE
                  IWKSP(I) = LNOW
                  LOUT = LOUT + 1
                  LALC = LALC + 1
                  LNOW = I
                  LUSED = MAX0(LUSED,LNOW)
               END IF
            END IF
C                                  CALCULATE AMOUNT NEEDED TO ACCOMODATE
C                                  THIS ALLOCATION REQUEST
            LNEED1 = (NELMTS-ILEFT)*ISIZE(ITYPE)
            IF (ILEFT .EQ. 0) THEN
               IGAP = ISIZE(ITYPE) - MOD(LNOW+LNEED,ISIZE(ITYPE))
               IF (IGAP .EQ. ISIZE(ITYPE)) IGAP = 0
               LNEED1 = LNEED1 + 2*ISIZE(2) + IGAP
            END IF
C                                  MODIFY LNEED ACCORDING TO THE SIZE
C                                  OF THE BASE BEING USED (D.P. HERE)
            LNEED = LNEED + ((LNEED1+ISIZE(3)-1)/ISIZE(3))
C                                  SINCE CURRENT ALLOCATION IS ILLEGAL,
C                                  RETURN THE NEGATIVE OF THE ADDITIONAL
C                                  AMOUNT NEEDED TO MAKE IT LEGAL
            I1KGT = -LNEED
         END IF
      ELSE
C                                  PERMANENT STORAGE
         IF (ILEFT .GE. NELMTS) THEN
            JTYPE = -ITYPE
            I1KGT = (LBND*ISIZE(2)-1)/ISIZE(JTYPE) + 1 - NELMTS
            I = ((I1KGT-1)*ISIZE(JTYPE))/ISIZE(2) - 1
C                                  IWKSP(I) CONTAINS LBND FOR PREVIOUS
C                                  PERMANENT STORAGE ALLOCATION.
C                                  IWKSP(I+1) CONTAINS THE DATA TYPE FOR
C                                  THIS ALLOCATION.
            IWKSP(I) = LBND
            IWKSP(I+1) = JTYPE
            LALC = LALC + 1
            LBND = I
            LNEED = 0
         ELSE
C                                  PERMANENT STORAGE WAS REQUESTED
C                                  BUT THE STACK WOULD OVERFLOW,
C                                  THEREFORE, ALLOCATE RELEASABLE
C                                  SPACE THROUGH THE END OF THE STACK
            IF (LNEED .EQ. 0) THEN
               JTYPE = -ITYPE
               IDUMAL = (LNOW*ISIZE(2)-1)/ISIZE(JTYPE) + 2
               I = ((IDUMAL-1+ILEFT)*ISIZE(JTYPE)-1)/ISIZE(2) + 3
C                                  ADVANCE COUNTERS AND STORE POINTERS
C                                  IF THERE IS ROOM TO DO SO
               IF (I .LT. LBND) THEN
C                                  IWKSP(I-1) CONTAINS THE DATA TYPE FOR
C                                  THIS ALLOCATION. IWKSP(I) CONTAINS
C                                  LNOW FOR THE PREVIOUS ALLOCATION.
                  IWKSP(I-1) = JTYPE
                  IWKSP(I) = LNOW
                  LOUT = LOUT + 1
                  LALC = LALC + 1
                  LNOW = I
                  LUSED = MAX0(LUSED,LNOW)
               END IF
            END IF
C                                  CALCULATE AMOUNT NEEDED TO ACCOMODATE
C                                  THIS ALLOCATION REQUEST
            LNEED1 = (NELMTS-ILEFT)*ISIZE(-ITYPE)
            IF (ILEFT .EQ. 0) THEN
               IGAP = ISIZE(-ITYPE) - MOD(LNOW+LNEED,ISIZE(-ITYPE))
               IF (IGAP .EQ. ISIZE(-ITYPE)) IGAP = 0
               LNEED1 = LNEED1 + 2*ISIZE(2) + IGAP
            END IF
C                                  MODIFY LNEED ACCORDING TO THE SIZE
C                                  OF THE BASE BEING USED (D.P. HERE)
            LNEED = LNEED + ((LNEED1+ISIZE(3)-1)/ISIZE(3))
C                                  SINCE CURRENT ALLOCATION IS ILLEGAL,
C                                  RETURN THE NEGATIVE OF THE ADDITIONAL
C                                  AMOUNT NEEDED TO MAKE IT LEGAL
            I1KGT = -LNEED
         END IF
      END IF
C                                  STACK OVERFLOW - UNRECOVERABLE ERROR
 9000 IF (LNEED .GT. 0) THEN
         CALL E1POS (-5, IPA, ISA)
         CALL E1POS (5, 0, 0)
         CALL E1POS (-7, IPA7, ISA7)
         CALL E1POS (7, 0, 0)
         CALL E1PSH ('I1KGT ')
         CALL E1STI (1, LNEED+(LMAX/ISIZE(3)))
         IF (XXLINE(PROLVL).GE.1 .AND. XXLINE(PROLVL).LE.999) THEN
            CALL E1MES (7, 1, 'INSUFFICIENT WORKSPACE FOR CURRENT '//
     &                  'ALLOCATION(S).  CORRECT BY INSERTING THE '//
     &                  'FOLLOWING PROTRAN LINE: $OPTIONS;WORKSPACE=%'//
     &                  '(I1)')
         ELSE
            CALL E1MES (5, 5, 'INSUFFICIENT WORKSPACE FOR CURRENT '//
     &                  'ALLOCATION(S). CORRECT BY CALLING IWKIN '//
     &                  'FROM MAIN PROGRAM WITH THE THREE FOLLOWING '//
     &                  'STATEMENTS:  (REGARDLESS OF PRECISION)%/'//
     &                  '      COMMON /WORKSP/  RWKSP%/      REAL '//
     &                  'RWKSP(%(I1))%/      CALL IWKIN(%(I1))')
         END IF
         CALL E1POP ('I1KGT ')
         CALL E1POS (5, IPA, ISA)
         CALL E1POS (7, IPA7, ISA7)
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  N1RCD
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    MARCH 6, 1984
C
C  PURPOSE:    RETRIEVE AN ERROR CODE.
C
C  USAGE:      N1RCD(IOPT)
C
C  ARGUMENTS:
C     IOPT   - INTEGER SPECIFYING THE LEVEL.  (INPUT)
C              IF IOPT=0 THE ERROR CODE FOR THE CURRENT LEVEL IS
C              RETURNED.  IF IOPT=1 THE ERROR CODE FOR THE MOST
C              RECENTLY CALLED ROUTINE (LAST POP) IS RETURNED.
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      INTEGER FUNCTION N1RCD (IOPT)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    IOPT
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                              SPECIFICATIONS FOR COMMON /ERCOM1/
      INTEGER    CALLVL, MAXLEV, MSGLEN, ERTYPE(51), ERCODE(51),
     &           PRINTB(7), STOPTB(7), PLEN, IFERR6, IFERR7,
     &           IALLOC(51), HDRFMT(7), TRACON(7)
      COMMON     /ERCOM1/ CALLVL, MAXLEV, MSGLEN, ERTYPE, ERCODE,
     &           PRINTB, STOPTB, PLEN, IFERR6, IFERR7, IALLOC, HDRFMT,
     &           TRACON
      SAVE       /ERCOM1/
C                              SPECIFICATIONS FOR COMMON /ERCOM2/
      CHARACTER  MSGSAV(255), PLIST(300), RNAME(51)*6
      COMMON     /ERCOM2/ MSGSAV, PLIST, RNAME
      SAVE       /ERCOM2/
C                              SPECIFICATIONS FOR COMMON /ERCOM3/
      DOUBLE PRECISION ERCKSM
      COMMON     /ERCOM3/ ERCKSM
      SAVE       /ERCOM3/
C                              SPECIFICATIONS FOR COMMON /ERCOM4/
      LOGICAL    ISUSER(51)
      COMMON     /ERCOM4/ ISUSER
      SAVE       /ERCOM4/
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   E1PRT, M1VECH
C
      IF (IOPT.NE.0 .AND. IOPT.NE.1) THEN
         ERTYPE(CALLVL) = 5
         ERCODE(CALLVL) = 1
         MSGLEN = 47
         CALL M1VECH ('.  THE ARGUMENT PASSED TO N1RCD MUST BE 0 OR '//
     &                '1. ', MSGLEN, MSGSAV, MSGLEN)
         CALL E1PRT
         STOP
      ELSE
         N1RCD = ERCODE(CALLVL+IOPT)
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  SASUM (SINGLE PRECISION VERSION)
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    AUGUST 9, 1986
C
C  PURPOSE:    SUM THE ABSOLUTE VALUES OF THE COMPONENTS OF A
C              SINGLE PRECISION VECTOR.
C
C  USAGE:      SASUM(N, SX, INCX)
C
C  ARGUMENTS:
C     N      - LENGTH OF VECTORS X.  (INPUT)
C     SX     - REAL VECTOR OF LENGTH N*INCX.  (INPUT)
C     INCX   - DISPLACEMENT BETWEEN ELEMENTS OF SX.  (INPUT)
C              X(I) IS DEFINED TO BE SX(1+(I-1)*INCX). INCX MUST BE
C              GREATER THAN 0.
C     SASUM  - SINGLE PRECISION SUM FROM I=1 TO N OF ABS(X(I)).
C              (OUTPUT)
C              X(I) REFERS TO A SPECIFIC ELEMENT OF SX.
C
C  GAMS:       D1A
C
C  CHAPTERS:   MATH/LIBRARY BASIC MATRIX/VECTOR OPERATIONS
C              STAT/LIBRARY MATHEMATICAL SUPPORT
C
C  COPYRIGHT:  1986 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      REAL FUNCTION SASUM (N, SX, INCX)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    N, INCX
      REAL       SX(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, M, MP1, NINCX
C                                  SPECIFICATIONS FOR SPECIAL CASES
C     INTRINSIC  MOD
      INTRINSIC  MOD
      INTEGER    MOD
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  ABS
      INTRINSIC  ABS
      REAL       ABS
C
      SASUM = 0.0E0
      IF (N .GT. 0) THEN
         IF (INCX .NE. 1) THEN
C                                  CODE FOR INCREMENT NOT EQUAL TO 1
            NINCX = N*INCX
            DO 10  I=1, NINCX, INCX
               SASUM = SASUM + ABS(SX(I))
   10       CONTINUE
         ELSE
C                                  CODE FOR INCREMENT EQUAL TO 1
            M = MOD(N,6)
C                                  CLEAN-UP LOOP
            DO 30  I=1, M
               SASUM = SASUM + ABS(SX(I))
   30       CONTINUE
            MP1 = M + 1
            DO 40  I=MP1, N, 6
               SASUM = SASUM + ABS(SX(I)) + ABS(SX(I+1)) +
     &                 ABS(SX(I+2)) + ABS(SX(I+3)) + ABS(SX(I+4)) +
     &                 ABS(SX(I+5))
   40       CONTINUE
         END IF
      END IF
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  SCOPY (SINGLE PRECISION VERSION)
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    AUGUST 9, 1986
C
C  PURPOSE:    COPY A VECTOR X TO A VECTOR Y, BOTH SINGLE PRECISION.
C
C  USAGE:      CALL SCOPY (N, SX, INCX, SY, INCY)
C
C  ARGUMENTS:
C     N      - LENGTH OF VECTORS X AND Y.  (INPUT)
C     SX     - REAL VECTOR OF LENGTH MAX(N*IABS(INCX),1).  (INPUT)
C     INCX   - DISPLACEMENT BETWEEN ELEMENTS OF SX.  (INPUT)
C              X(I) IS DEFINED TO BE.. SX(1+(I-1)*INCX) IF INCX .GE. 0
C              OR SX(1+(I-N)*INCX) IF INCX .LT. 0.
C     SY     - REAL VECTOR OF LENGTH MAX(N*IABS(INCY),1).  (OUTPUT)
C              SCOPY COPIES X(I) TO Y(I) FOR I=1,...,N. X(I) AND Y(I)
C              REFER TO SPECIFIC ELEMENTS OF SX AND SY, RESPECTIVELY.
C              SEE INCX AND INCY ARGUMENT DESCRIPTIONS.
C     INCY   - DISPLACEMENT BETWEEN ELEMENTS OF SY.  (INPUT)
C              Y(I) IS DEFINED TO BE.. SY(1+(I-1)*INCY) IF INCY .GE. 0
C              OR SY(1+(I-N)*INCY) IF INCY .LT. 0.
C
C  GAMS:       D1A
C
C  CHAPTERS:   MATH/LIBRARY BASIC MATRIX/VECTOR OPERATIONS
C              STAT/LIBRARY MATHEMATICAL SUPPORT
C
C  COPYRIGHT:  1986 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE SCOPY (N, SX, INCX, SY, INCY)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    N, INCX, INCY
      REAL       SX(*), SY(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, IX, IY, M, MP1
C                                  SPECIFICATIONS FOR SPECIAL CASES
C     INTRINSIC  MOD
      INTRINSIC  MOD
      INTEGER    MOD
C
      IF (N .GT. 0) THEN
         IF (INCX.NE.1 .OR. INCY.NE.1) THEN
C                                  CODE FOR UNEQUAL INCREMENTS
            IX = 1
            IY = 1
            IF (INCX .LT. 0) IX = (-N+1)*INCX + 1
            IF (INCY .LT. 0) IY = (-N+1)*INCY + 1
            DO 10  I=1, N
               SY(IY) = SX(IX)
               IX = IX + INCX
               IY = IY + INCY
   10       CONTINUE
         ELSE
C                                  CODE FOR BOTH INCREMENTS EQUAL TO 1
            M = MOD(N,7)
C                                  CLEAN-UP LOOP
            DO 30  I=1, M
               SY(I) = SX(I)
   30       CONTINUE
            MP1 = M + 1
            DO 40  I=MP1, N, 7
               SY(I) = SX(I)
               SY(I+1) = SX(I+1)
               SY(I+2) = SX(I+2)
               SY(I+3) = SX(I+3)
               SY(I+4) = SX(I+4)
               SY(I+5) = SX(I+5)
               SY(I+6) = SX(I+6)
   40       CONTINUE
         END IF
      END IF
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  SGEMV  (SINGLE PRECISION VERSION)
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    JULY 15, 1986
C
C  PURPOSE:    PERFORM THE MATRIX-VECTOR MULTIPLICATION
C              Y = ALPHA*A*X + BETA*Y OR Y = ALPHA*A'*X + BETA*Y,
C              ALL SINGLE PRECISION.
C
C  USAGE:      CALL SGEMV (TRANS, M, N, ALPHA, A, LDA, X, INCX, BETA, Y,
C                          INCY)
C
C  ARGUMENTS:
C     TRANS  - CHARACTER SPECIFING THE OPERATION TO BE PERFORMED.
C              (INPUT)
C                 TRANS               OPERATION
C              'N' OR 'N'      Y = ALPHA*A*X + BETA*Y
C              'T' OR 'T'      Y = ALPHA*A'*X + BETA*Y
C              'C' OR 'C'      Y = ALPHA*A'*X + BETA*Y
C     M      - NUMBER OF ROWS IN A.  (INPUT)
C     N      - NUMBER OF COLUMNS IN A.  (INPUT)
C     ALPHA  - SCALAR.  (INPUT)
C     A      - ARRAY OF SIZE M BY N.  (INPUT)
C     LDA    - LEADING DIMENSION OF A EXACTLY AS SPECIFIED IN THE
C              CALLING ROUTINE.  (INPUT)
C     X      - VECTOR OF LENGTH (N-1)*IABS(INCX)+1 WHEN TRANS IS
C              'N' OR 'N' AND OF LENGTH (M-1)*IABS(INCX)+1 OTHERWISE.
C              (INPUT)
C     INCX   - DISPLACEMENT BETWEEN ELEMENTS OF X.  (INPUT)
C     BETA   - SCALAR.  (INPUT)
C              WHEN BETA IS ZERO, Y IS NOT REFERENCED.
C     Y      - VECTOR OF LENGTH (N-1)*IABS(INCY)+1 WHEN TRANS IS
C              'M' OR 'M' AND OF LENGTH (M-1)*IABS(INCY)+1 OTHERWISE.
C              (INPUT/OUTPUT)
C     INCY   - DISPLACEMENT BETWEEN ELEMENTS OF Y.  (INPUT)
C
C  GAMS:       D1B
C
C  CHAPTER:    MATH/LIBRARY BASIC MATRIX/VECTOR OPERATIONS
C
C  COPYRIGHT:  1986 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE SGEMV (TRANS, M, N, ALPHA, A, LDA, X, INCX, BETA, Y,
     &                  INCY)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    M, N, LDA, INCX, INCY
      REAL       ALPHA, BETA, X(*), Y(*)
      CHARACTER  TRANS*1
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, IX, IY, KY, LENX, LENY
C                                  SPECIFICATIONS FOR SPECIAL CASES
      REAL       A(*)
      EXTERNAL   SAXPY, SDOT
      INTEGER    KX
      REAL       SDOT
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  IABS
      INTRINSIC  IABS
      INTEGER    IABS
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   SSCAL, SSET
C                                  QUICK RETURN IF POSSIBLE
      IF (M.EQ.0 .OR. N.EQ.0 .OR. (ALPHA.EQ.0.0) .AND. (BETA.EQ.1.0))
     &    GO TO 9000
C
      IF (TRANS.EQ.'N' .OR. TRANS.EQ.'N') THEN
         LENX = N
         LENY = M
      ELSE
         LENX = M
         LENY = N
      END IF
C
      IX = 1
      IY = 1
      IF (INCX .LT. 0) IX = (-LENX+1)*INCX + 1
      IF (INCY .LT. 0) IY = (-LENY+1)*INCY + 1
C
      IF (BETA .EQ. 1) THEN
      ELSE IF (INCY .EQ. 0) THEN
         IF (BETA .EQ. 0.0) THEN
            Y(1) = 0.0
         ELSE
            Y(1) = BETA**LENY*Y(1)
         END IF
      ELSE IF (BETA .EQ. 0.0) THEN
         CALL SSET (LENY, 0.0, Y, IABS(INCY))
      ELSE
         CALL SSCAL (LENY, BETA, Y, IABS(INCY))
      END IF
C
      IF (ALPHA .EQ. 0.0) GO TO 9000
C                                  NOT TRANSPOSE
      IF (TRANS.EQ.'N' .OR. TRANS.EQ.'N') THEN
         KX = IX
         DO 10  I=1, N
            CALL SAXPY (M, ALPHA*X(KX), A(LDA*(I-1)+1), 1, Y, INCY)
            KX = KX + INCX
   10    CONTINUE
      ELSE
C                                  TRANSPOSE
         KY = IY
         DO 20  I=1, N
            Y(KY) = Y(KY) + ALPHA*SDOT(M,A(LDA*(I-1)+1),1,X,INCX)
            KY = KY + INCY
   20    CONTINUE
      END IF
C
 9000 RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  SGER  (SINGLE PRECISION VERSION)
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    JULY 15, 1986
C
C  PURPOSE:    PERFORM THE RANK-ONE MATRIX UPDATE A = ALPHA*X*Y' + A,
C              ALL SINGLE PRECISION.
C
C  USAGE:      CALL SGER (M, N, ALPHA, X, INCX, Y, INCY, A, LDA)
C
C  ARGUMENTS:
C     M      - NUMBER OF ROWS IN A.  (INPUT)
C     N      - NUMBER OF COLUMNS IN A.  (INPUT)
C     ALPHA  - REAL SCALAR.  (INPUT)
C     X      - REAL VECTOR OF LENGTH (M-1)*IABS(INCX)+1.  (INPUT)
C     INCX   - DISPLACEMENT BETWEEN ELEMENTS OF X.  (INPUT)
C     Y      - REAL VECTOR OF LENGTH (N-1)*IABS(INCY)+1.  (INPUT)
C     INCY   - DISPLACEMENT BETWEEN ELEMENTS OF Y.  (INPUT)
C     A      - ARRAY OF SIZE M BY N.  (INPUT/OUTPUT)
C              ON INPUT, A CONTAINS THE MATRIX TO BE UPDATED.
C              ON OUTPUT, A CONTAINS THE UPDATED MATRIX.
C     LDA    - LEADING DIMENSION OF A EXACTLY AS SPECIFIED IN THE
C              CALLING ROUTINE.  (INPUT)
C
C  GAMS:       D1B
C
C  CHAPTER:    MATH/LIBRARY BASIC MATRIX/VECTOR OPERATIONS
C
C  COPYRIGHT:  1986 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE SGER (M, N, ALPHA, X, INCX, Y, INCY, A, LDA)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    M, N, INCX, INCY, LDA
      REAL       ALPHA, X(*), Y(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    IY, J
C                                  SPECIFICATIONS FOR SPECIAL CASES
      REAL       A(*)
      INTEGER    I1X
      EXTERNAL   SAXPY
C                                  QUICK RETURN IF POSSIBLE
      IF (M.EQ.0 .OR. N.EQ.0 .OR. ALPHA.EQ.0.0) GO TO 9000
C
      IY = 1
      IF (INCY .LT. 0) IY = (-N+1)*INCY + 1
C
      I1X = 1
      DO 10  J=1, N
         CALL SAXPY (M, ALPHA*Y(IY), X, INCX, A(I1X), 1)
         IY = IY + INCY
         I1X = I1X + LDA
   10 CONTINUE
C
 9000 RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  SNRM2 (SINGLE PRECISION VERSION)
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    AUGUST 9, 1986
C
C  PURPOSE:    COMPUTE THE EUCLIDEAN LENGTH OR L2 NORM OF A
C              SINGLE-PRECISION VECTOR.
C
C  USAGE:      SNRM2(N, SX, INCX)
C
C  ARGUMENTS:
C     N      - LENGTH OF VECTOR X.  (INPUT)
C     SX     - REAL VECTOR OF LENGTH N*INCX.  (INPUT)
C     INCX   - DISPLACEMENT BETWEEN ELEMENTS OF SX.  (INPUT)
C              X(I) IS DEFINED TO BE SX(1+(I-1)*INCX). INCX MUST BE
C              GREATER THAN ZERO.
C     SNRM2  - SQUARE ROOT OF THE SUM FROM I=1 TO N OF X(I)**2.
C              (OUTPUT)
C              X(I) REFERS TO A SPECIFIC ELEMENT OF SX. SEE INCX
C              ARGUMENT DESCRIPTION.
C
C  GAMS:       D1A3B
C
C  CHAPTERS:   MATH/LIBRARY BASIC MATRIX/VECTOR OPERATIONS
C              STAT/LIBRARY MATHEMATICAL SUPPORT
C
C  COPYRIGHT:  1986 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      REAL FUNCTION SNRM2 (N, SX, INCX)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    N, INCX
      REAL       SX(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, J, NEXT, NN
      REAL       HITEST, SUM, XMAX
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      REAL       CUTHI, CUTLO, ONE, ZERO
      SAVE       CUTHI, CUTLO, ONE, ZERO
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  ABS,SQRT
      INTRINSIC  ABS, SQRT
      REAL       ABS, SQRT
C
      DATA ZERO/0.0E0/, ONE/1.0E0/
      DATA CUTLO/4.441E-16/, CUTHI/1.304E19/
C
      IF (N .GT. 0) GO TO 10
      SNRM2 = ZERO
      GO TO 140
C
   10 ASSIGN 30 TO NEXT
      SUM = ZERO
      NN = N*INCX
C                                  BEGIN MAIN LOOP
      I = 1
   20 GO TO NEXT, (30, 40, 70, 80)
   30 IF (ABS(SX(I)) .GT. CUTLO) GO TO 110
      ASSIGN 40 TO NEXT
      XMAX = ZERO
C                                  PHASE 1. SUM IS ZERO
   40 IF (SX(I) .EQ. ZERO) GO TO 130
      IF (ABS(SX(I)) .GT. CUTLO) GO TO 110
C                                  PREPARE FOR PHASE 2.
      ASSIGN 70 TO NEXT
      GO TO 60
C                                  PREPARE FOR PHASE 4.
   50 I = J
      ASSIGN 80 TO NEXT
      SUM = (SUM/SX(I))/SX(I)
   60 XMAX = ABS(SX(I))
      GO TO 90
C                                  PHASE 2. SUM IS SMALL. SCALE TO
C                                  AVOID DESTRUCTIVE UNDERFLOW.
   70 IF (ABS(SX(I)) .GT. CUTLO) GO TO 100
C                                  COMMON CODE FOR PHASES 2 AND 4. IN
C                                  PHASE 4 SUM IS LARGE. SCALE TO
C                                  AVOID OVERFLOW.
   80 IF (ABS(SX(I)) .LE. XMAX) GO TO 90
      SUM = ONE + SUM*(XMAX/SX(I))**2
      XMAX = ABS(SX(I))
      GO TO 130
C
   90 SUM = SUM + (SX(I)/XMAX)**2
      GO TO 130
C                                  PREPARE FOR PHASE 3.
  100 SUM = (SUM*XMAX)*XMAX
C                                  FOR REAL OR D.P. SET HITEST =
C                                  CUTHI/N FOR COMPLEX SET HITEST =
C                                  CUTHI/(2*N)
  110 HITEST = CUTHI/N
C                                  PHASE 3. SUM IS MID-RANGE. NO
C                                  SCALING.
      DO 120  J=I, NN, INCX
         IF (ABS(SX(J)) .GE. HITEST) GO TO 50
  120 SUM = SUM + SX(J)*SX(J)
      SNRM2 = SQRT(SUM)
      GO TO 140
C
  130 CONTINUE
      I = I + INCX
      IF (I .LE. NN) GO TO 20
C                                  END OF MAIN LOOP. COMPUTE SQUARE
C                                  ROOT AND ADJUST FOR SCALING.
      SNRM2 = XMAX*SQRT(SUM)
  140 CONTINUE
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  SROT (SINGLE PRECISION VERSION)
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    AUGUST 9, 1986
C
C  PURPOSE:    APPLY A GIVENS PLANE ROTATION IN SINGLE PRECISION.
C
C  USAGE:      CALL SROT (N, SX, INCX, SY, INCY, C, S)
C
C  ARGUMENTS:
C     N      - LENGTH OF VECTORS X AND Y.  (INPUT)
C     SX     - REAL VECTOR OF LENGTH MAX(N*IABS(INCX),1).
C              (INPUT/OUTPUT)
C              SROT REPLACES X(I) WITH SC*X(I) + SS*Y(I) FOR I=1,...,N.
C              X(I) AND Y(I) REFER TO SPECIFIC ELEMENTS OF SX AND SY.
C     INCX   - DISPLACEMENT BETWEEN ELEMENTS OF SX.  (INPUT)
C              X(I) IS DEFINED TO BE
C                 SX(1+(I-1)*INCX) IF INCX.GE.0  OR
C                 SX(1+(I-N)*INCX) IF INCX.LT.0.
C     SY     - REAL VECTOR OF LENGTH MAX(N*IABS(INCY),1).
C              (INPUT/OUTPUT)
C              SROT REPLACES Y(I) WITH -SS*X(I) + SC*Y(I) FOR I=1,...,N.
C              X(I) AND Y(I) REFER TO SPECIFIC ELEMENTS OF SX AND SY.
C     INCY   - DISPLACEMENT BETWEEN ELEMENTS OF SY.  (INPUT)
C              Y(I) IS DEFINED TO BE
C                 SY(1+(I-1)*INCY) IF INCY.GE.0  OR
C                 SY(1+(I-N)*INCY) IF INCY.LT.0.
C     C      - REAL SCALAR CONTAINING ELEMENTS OF THE ROTATION MATRIX.
C              (INPUT)
C     S      - REAL SCALAR CONTAINING ELEMENTS OF THE ROTATION MATRIX.
C              (INPUT)
C
C  REMARK:
C                  ( SC SS )    (X(1) ... X(N))
C     SROT APPLIES (       ) TO (             )
C                  (-SS SC )    (Y(1) ... Y(N))
C
C  GAMS:       D1A8
C
C  CHAPTERS:   MATH/LIBRARY BASIC MATRIX/VECTOR OPERATIONS
C              STAT/LIBRARY MATHEMATICAL SUPPORT
C
C  COPYRIGHT:  1986 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE SROT (N, SX, INCX, SY, INCY, C, S)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    N, INCX, INCY
      REAL       C, S, SX(*), SY(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, IX, IY
      REAL       STEMP
C
      IF (N .GT. 0) THEN
         IF (INCX.NE.1 .OR. INCY.NE.1) THEN
C                                  CODE FOR UNEQUAL INCREMENTS OR EQUAL
C                                    INCREMENTS NOT EQUAL TO 1
            IX = 1
            IY = 1
            IF (INCX .LT. 0) IX = (-N+1)*INCX + 1
            IF (INCY .LT. 0) IY = (-N+1)*INCY + 1
            DO 10  I=1, N
               STEMP = C*SX(IX) + S*SY(IY)
               SY(IY) = C*SY(IY) - S*SX(IX)
               SX(IX) = STEMP
               IX = IX + INCX
               IY = IY + INCY
   10       CONTINUE
         ELSE
C                                  CODE FOR BOTH INCREMENTS EQUAL TO 1
            DO 20  I=1, N
               STEMP = C*SX(I) + S*SY(I)
               SY(I) = C*SY(I) - S*SX(I)
               SX(I) = STEMP
   20       CONTINUE
         END IF
      END IF
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  SROTG (SINGLE PRECISION VERSION)
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    AUGUST 9, 1986
C
C  PURPOSE:    CONSTRUCT A GIVENS PLANE ROTATION IN SINGLE PRECISION.
C
C  USAGE:      CALL SROTG (SA, SB, SC, SS)
C
C  ARGUMENTS:
C     SA     - FIRST ELEMENT OF VECTOR.  (INPUT/OUTPUT)
C              ON OUTPUT, R = (+/-)SQRT(SA**2 + SB**2) OVERWRITES SA.
C     SB     - SECOND ELEMENT OF VECTOR.  (INPUT/OUTPUT)
C              ON OUTPUT, Z OVERWRITES SB WHERE Z IS DEFINED TO BE
C                SS        IF ABS(SA) .GT. ABS(SB)
C                1.0/SC    IF ABS(SB) .GE. ABS(SA) AND SC .NE. 0.0
C                1.0       IF SC .EQ. 0.0.
C     SC     - REAL SCALAR CONTAINING ELEMENTS OF THE ROTATION MATRIX.
C              (OUTPUT)
C     SS     - REAL SCALAR CONTAINING ELEMENTS OF THE ROTATION MATRIX.
C              (OUTPUT)
C
C  REMARK:
C     SROTG CONSTRUCTS THE GIVENS ROTATION
C                         ( SC  SS )
C                     G = (        ) , SC**2 + SS**2 = 1
C                         (-SS  SC )
C     WHICH ZEROS THE SECOND ELEMENT OF (SA SB)(TRANSPOSE).
C
C  GAMS:       D1A8
C
C  CHAPTERS:   MATH/LIBRARY BASIC MATRIX/VECTOR OPERATIONS
C              STAT/LIBRARY MATHEMATICAL SUPPORT
C
C  COPYRIGHT:  1986 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE SROTG (SA, SB, SC, SS)
C                                  SPECIFICATIONS FOR ARGUMENTS
      REAL       SA, SB, SC, SS
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      REAL       R, U, V
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  ABS,SQRT
      INTRINSIC  ABS, SQRT
      REAL       ABS, SQRT
C
      IF (ABS(SA) .GT. ABS(SB)) THEN
C                                  HERE ABS(SA) .GT. ABS(SB)
         U = SA + SA
         V = SB/U
C                                  NOTE THAT U AND R HAVE THE SIGN OF
C                                    SA
         R = SQRT(.25+V**2)*U
C                                  NOTE THAT SC IS POSITIVE
         SC = SA/R
         SS = V*(SC+SC)
         SB = SS
         SA = R
      ELSE
C                                  HERE ABS(SA) .LE. ABS(SB)
         IF (SB .NE. 0.0) THEN
            U = SB + SB
            V = SA/U
C                                  NOTE THAT U AND R HAVE THE SIGN OF
C                                    SB (R IS IMMEDIATELY STORED IN SA)
            SA = SQRT(.25+V**2)*U
C                                  NOTE THAT SS IS POSITIVE
            SS = SB/SA
            SC = V*(SS+SS)
            IF (SC .NE. 0.0) THEN
               SB = 1.0/SC
            ELSE
               SB = 1.0
            END IF
C                                  HERE SA = SB = 0.
         ELSE
            SC = 1.0
            SS = 0.0
            SA = 0.0
            SB = 0.0
         END IF
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  SSCAL (SINGLE PRECISION VERSION)
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    AUGUST 9, 1986
C
C  PURPOSE:    MULTIPLY A VECTOR BY A SCALAR, Y = AY, BOTH SINGLE
C              PRECISION.
C
C  USAGE:      CALL SSCAL (N, SA, SX, INCX)
C
C  ARGUMENTS:
C     N      - LENGTH OF VECTOR X.  (INPUT)
C     SA     - REAL SCALAR.  (INPUT)
C     SX     - REAL VECTOR OF LENGTH N*INCX.  (INPUT/OUTPUT)
C              SSCAL REPLACES X(I) WITH SA*X(I) FOR I=1,...,N. X(I)
C              REFERS TO A SPECIFIC ELEMENT OF SX. SEE INCX ARGUMENT
C              DESCRIPTION.
C     INCX   - DISPLACEMENT BETWEEN ELEMENTS OF SX.  (INPUT)
C              X(I) IS DEFINED TO BE SX(1+(I-1)*INCX). INCX MUST BE
C              GREATER THAN ZERO.
C
C  GAMS:       D1A6
C
C  CHAPTERS:   MATH/LIBRARY BASIC MATRIX/VECTOR OPERATIONS
C              STAT/LIBRARY MATHEMATICAL SUPPORT
C
C  COPYRIGHT:  1986 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE SSCAL (N, SA, SX, INCX)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    N, INCX
      REAL       SA, SX(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, M, MP1, NS
C
      IF (N .GT. 0) THEN
         IF (INCX .NE. 1) THEN
C                                  CODE FOR INCREMENTS NOT EQUAL TO 1.
            NS = N*INCX
            DO 10  I=1, NS, INCX
               SX(I) = SA*SX(I)
   10       CONTINUE
         ELSE
C                                  CODE FOR INCREMENTS EQUAL TO 1.
C                                  CLEAN-UP LOOP SO REMAINING VECTOR
C                                  LENGTH IS A MULTIPLE OF 5.
            M = N - (N/5)*5
            DO 30  I=1, M
               SX(I) = SA*SX(I)
   30       CONTINUE
            MP1 = M + 1
            DO 40  I=MP1, N, 5
               SX(I) = SA*SX(I)
               SX(I+1) = SA*SX(I+1)
               SX(I+2) = SA*SX(I+2)
               SX(I+3) = SA*SX(I+3)
               SX(I+4) = SA*SX(I+4)
   40       CONTINUE
         END IF
      END IF
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  SSET (SINGLE PRECISION VERSION)
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    AUGUST 9, 1986
C
C  PURPOSE:    SET THE COMPONENTS OF A VECTOR TO A SCALAR, ALL
C              SINGLE PRECISION.
C
C  USAGE:      CALL SSET (N, SA, SX, INCX)
C
C  ARGUMENTS:
C     N      - LENGTH OF VECTOR X.  (INPUT)
C     SA     - REAL SCALAR.  (INPUT)
C     SX     - REAL VECTOR OF LENGTH N*INCX.  (INPUT/OUTPUT)
C              SSET REPLACES X(I) WITH SA FOR I=1,...,N. X(I) REFERS TO
C              A SPECIFIC ELEMENT OF SX. SEE INCX ARGUMENT DESCRIPTION.
C     INCX   - DISPLACEMENT BETWEEN ELEMENTS OF SX.  (INPUT)
C              X(I) IS DEFINED TO BE SX(1+(I-1)*INCX). INCX MUST BE
C              GREATER THAN ZERO.
C
C  GAMS:       D1A1
C
C  CHAPTERS:   MATH/LIBRARY BASIC MATRIX/VECTOR OPERATIONS
C              STAT/LIBRARY MATHEMATICAL SUPPORT
C
C  COPYRIGHT:  1986 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE SSET (N, SA, SX, INCX)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    N, INCX
      REAL       SA, SX(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, M, MP1, NINCX
C                                  SPECIFICATIONS FOR SPECIAL CASES
C     INTRINSIC  MOD
      INTRINSIC  MOD
      INTEGER    MOD
C
      IF (N .GT. 0) THEN
         IF (INCX .NE. 1) THEN
C                                  CODE FOR INCREMENT NOT EQUAL TO 1
            NINCX = N*INCX
            DO 10  I=1, NINCX, INCX
               SX(I) = SA
   10       CONTINUE
         ELSE
C                                  CODE FOR INCREMENT EQUAL TO 1
            M = MOD(N,8)
C                                  CLEAN-UP LOOP
            DO 30  I=1, M
               SX(I) = SA
   30       CONTINUE
            MP1 = M + 1
            DO 40  I=MP1, N, 8
               SX(I) = SA
               SX(I+1) = SA
               SX(I+2) = SA
               SX(I+3) = SA
               SX(I+4) = SA
               SX(I+5) = SA
               SX(I+6) = SA
               SX(I+7) = SA
   40       CONTINUE
         END IF
      END IF
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  SSWAP (SINGLE PRECISION VERSION)
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    AUGUST 9, 1986
C
C  PURPOSE:    INTERCHANGE VECTORS X AND Y, BOTH SINGLE PRECISION.
C
C  USAGE:      CALL SSWAP (N, SX, INCX, SY, INCY)
C
C  ARGUMENTS:
C     N      - LENGTH OF VECTORS X AND Y.  (INPUT)
C     SX     - REAL VECTOR OF LENGTH MAX(N*IABS(INCX),1).
C              (INPUT/OUTPUT)
C     INCX   - DISPLACEMENT BETWEEN ELEMENTS OF SX.  (INPUT)
C              X(I) IS DEFINED TO BE
C                 SX(1+(I-1)*INCX) IF INCX.GE.0  OR
C                 SX(1+(I-N)*INCX) IF INCX.LT.0.
C     SY     - REAL VECTOR OF LENGTH MAX(N*IABS(INCY),1).
C              (INPUT/OUTPUT)
C     INCY   - DISPLACEMENT BETWEEN ELEMENTS OF SY.  (INPUT)
C              Y(I) IS DEFINED TO BE
C                 SY(1+(I-1)*INCY) IF INCY.GE.0  OR
C                 SY(1+(I-N)*INCY) IF INCY.LT.0.
C
C  GAMS:       D1A5
C
C  CHAPTERS:   MATH/LIBRARY BASIC MATRIX/VECTOR OPERATIONS
C              STAT/LIBRARY MATHEMATICAL SUPPORT
C
C  COPYRIGHT:  1986 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE SSWAP (N, SX, INCX, SY, INCY)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    N, INCX, INCY
      REAL       SX(*), SY(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, IX, IY, M, MP1
      REAL       STEMP
C                                  SPECIFICATIONS FOR SPECIAL CASES
C     INTRINSIC  MOD
      INTRINSIC  MOD
      INTEGER    MOD
C
      IF (N .GT. 0) THEN
         IF (INCX.NE.1 .OR. INCY.NE.1) THEN
C                                  CODE FOR UNEQUAL INCREMENTS OR EQUAL
C                                    INCREMENTS NOT EQUAL TO 1
            IX = 1
            IY = 1
            IF (INCX .LT. 0) IX = (-N+1)*INCX + 1
            IF (INCY .LT. 0) IY = (-N+1)*INCY + 1
            DO 10  I=1, N
               STEMP = SX(IX)
               SX(IX) = SY(IY)
               SY(IY) = STEMP
               IX = IX + INCX
               IY = IY + INCY
   10       CONTINUE
         ELSE
C                                  CODE FOR BOTH INCREMENTS EQUAL TO 1
            M = MOD(N,3)
C                                  CLEAN-UP LOOP
            DO 30  I=1, M
               STEMP = SX(I)
               SX(I) = SY(I)
               SY(I) = STEMP
   30       CONTINUE
            MP1 = M + 1
            DO 40  I=MP1, N, 3
               STEMP = SX(I)
               SX(I) = SY(I)
               SY(I) = STEMP
               STEMP = SX(I+1)
               SX(I+1) = SY(I+1)
               SY(I+1) = STEMP
               STEMP = SX(I+2)
               SX(I+2) = SY(I+2)
               SY(I+2) = STEMP
   40       CONTINUE
         END IF
      END IF
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  C1TCI
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    AUGUST 13, 1984
C
C  PURPOSE:    CONVERT CHARACTER STRING INTO CORRESPONDING INTEGER
C              FORM.
C
C  USAGE:      CALL C1TCI (CHRSTR, SLEN, NUM, IER)
C
C  ARGUMENTS:
C   CHRSTR  - CHARACTER ARRAY THAT CONTAINS THE NUMBER DESCRIPTION.
C             (INPUT)
C   SLEN    - LENGTH OF THE CHARACTER ARRAY.  (INPUT)
C   NUM     - THE ANSWER.  (OUTPUT)
C   IER     - COMPLETION CODE.  (OUTPUT)  WHERE
C                IER =-2  INDICATES THAT THE NUMBER IS TOO LARGE TO
C                         BE CONVERTED;
C                IER =-1  INDICATES THAT SLEN <= 0;
C                IER = 0  INDICATES NORMAL COMPLETION;
C                IER > 0  INDICATES THAT THE INPUT STRING CONTAINS A
C                         NONNUMERIC CHARACTER.  IER IS THE INDEX OF
C                         THE FIRST NONNUMERIC CHARACTER IN CHRSTR.
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE C1TCI (CHRSTR, SLEN, NUM, IER)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    SLEN, NUM, IER
      CHARACTER  CHRSTR(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    COUNT, I, IMACH5, J, N, S, SIGN
      CHARACTER  ZERO
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      CHARACTER  BLANK, DIGIT*10, MINUS, PLUS
      SAVE       BLANK, DIGIT, MINUS, PLUS
C                                  SPECIFICATIONS FOR EQUIVALENCE
      EQUIVALENCE (DIGIT, ZERO)
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  INDEX
      INTRINSIC  INDEX
      INTEGER    INDEX
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   IMACH
      INTEGER    IMACH
C
      DATA DIGIT/'0123456789'/
      DATA BLANK/' '/, MINUS/'-'/, PLUS/'+'/
C
C                                  CHECK SLEN
      NUM = 0
      IF (SLEN .LE. 0) THEN
         IER = -1
         GO TO 50
      END IF
C                                  HANDLE LEADING BLANKS
      SIGN = 1
      I = 1
   10 IF (I .LE. SLEN) THEN
         IF (CHRSTR(I) .EQ. BLANK) THEN
            I = I + 1
            GO TO 10
         END IF
      ELSE
         IER = 1
         GO TO 50
      END IF
C                                  CHECK FOR SIGN, IF ANY
      S = I
      IF (CHRSTR(I) .EQ. MINUS) THEN
         SIGN = -1
         I = I + 1
      ELSE IF (CHRSTR(I) .EQ. PLUS) THEN
         I = I + 1
      END IF
   20 IF (I .LE. SLEN) THEN
         IF (CHRSTR(I) .EQ. BLANK) THEN
            I = I + 1
            GO TO 20
         END IF
      ELSE
         IER = S
         GO TO 50
      END IF
C                                  SKIP LEADING ZERO
      J = I
   30 IF (I .LE. SLEN) THEN
         IF (CHRSTR(I) .EQ. ZERO) THEN
            I = I + 1
            GO TO 30
         END IF
      ELSE
         IER = 0
         GO TO 50
      END IF
C                                  CHECK FIRST NONBLANK CHARACTER
      COUNT = 0
C                                  CHECK NUMERIC CHARACTERS
      IMACH5 = IMACH(5)
   40 N = INDEX(DIGIT,CHRSTR(I))
      IF (N .NE. 0) THEN
         COUNT = COUNT + 1
         IF (NUM .GT. ((IMACH5-N)+1)/10) THEN
            IER = -2
            GO TO 50
         ELSE
            NUM = NUM*10 - 1 + N
            I = I + 1
            IF (I .LE. SLEN) GO TO 40
         END IF
      END IF
C
      IF (COUNT .EQ. 0) THEN
         IF (I .GT. J) THEN
            IER = I
         ELSE
            IER = S
         END IF
      ELSE IF (I .GT. SLEN) THEN
         NUM = SIGN*NUM
         IER = 0
      ELSE
         NUM = SIGN*NUM
         IER = I
      END IF
C
   50 CONTINUE
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  E1INIT
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    MARCH 13, 1984
C
C  PURPOSE:    INITIALIZATION.
C
C  USAGE:      CALL E1INIT
C
C  ARGUMENTS:  NONE
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE E1INIT
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    L
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      INTEGER    ISINIT
      SAVE       ISINIT
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                              SPECIFICATIONS FOR COMMON /ERCOM1/
      INTEGER    CALLVL, MAXLEV, MSGLEN, ERTYPE(51), ERCODE(51),
     &           PRINTB(7), STOPTB(7), PLEN, IFERR6, IFERR7,
     &           IALLOC(51), HDRFMT(7), TRACON(7)
      COMMON     /ERCOM1/ CALLVL, MAXLEV, MSGLEN, ERTYPE, ERCODE,
     &           PRINTB, STOPTB, PLEN, IFERR6, IFERR7, IALLOC, HDRFMT,
     &           TRACON
      SAVE       /ERCOM1/
C                              SPECIFICATIONS FOR COMMON /ERCOM2/
      CHARACTER  MSGSAV(255), PLIST(300), RNAME(51)*6
      COMMON     /ERCOM2/ MSGSAV, PLIST, RNAME
      SAVE       /ERCOM2/
C                              SPECIFICATIONS FOR COMMON /ERCOM3/
      DOUBLE PRECISION ERCKSM
      COMMON     /ERCOM3/ ERCKSM
      SAVE       /ERCOM3/
C                              SPECIFICATIONS FOR COMMON /ERCOM4/
      LOGICAL    ISUSER(51)
      COMMON     /ERCOM4/ ISUSER
      SAVE       /ERCOM4/
C                              SPECIFICATIONS FOR COMMON /ERCOM8/
      INTEGER    PROLVL, XXLINE(10), XXPLEN(10), ICALOC(10), INALOC(10)
      COMMON     /ERCOM8/ PROLVL, XXLINE, XXPLEN, ICALOC, INALOC
      SAVE       /ERCOM8/
C                              SPECIFICATIONS FOR COMMON /ERCOM9/
      CHARACTER  XXPROC(10)*31
      COMMON     /ERCOM9/ XXPROC
      SAVE       /ERCOM9/
C
      DATA ISINIT/0/
C
      IF (ISINIT .EQ. 0) THEN
C                                  INITIALIZE
         CALLVL = 1
         ERCODE(1) = 0
         ERTYPE(1) = 0
         IALLOC(1) = 0
         ISUSER(1) = .TRUE.
         IFERR6 = 0
         IFERR7 = 0
         PLEN = 1
         MAXLEV = 50
         DO 10  L=2, 51
            ERTYPE(L) = -1
            ERCODE(L) = -1
            IALLOC(L) = 0
            ISUSER(L) = .FALSE.
   10    CONTINUE
         DO 20  L=1, 7
            HDRFMT(L) = 1
            TRACON(L) = 1
   20    CONTINUE
         PROLVL = 1
         DO 30  L=1, 10
   30    ICALOC(L) = 0
         XXLINE(1) = 0
         XXPLEN(1) = 1
         XXPROC(1) = '?'
         RNAME(1) = 'USER'
         PRINTB(1) = 0
         PRINTB(2) = 0
         DO 40  L=3, 7
   40    PRINTB(L) = 1
         STOPTB(1) = 0
         STOPTB(2) = 0
         STOPTB(3) = 0
         STOPTB(4) = 1
         STOPTB(5) = 1
         STOPTB(6) = 0
         STOPTB(7) = 1
         ERCKSM = 0.0D0
C                                  SET FLAG TO INDICATE THAT
C                                    INITIALIZATION HAS OCCURRED
         ISINIT = 1
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  E1PRT
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    MARCH 14, 1984
C
C  PURPOSE:    TO PRINT AN ERROR MESSAGE.
C
C  USAGE:      CALL E1PRT
C
C  ARGUMENTS:  NONE
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE E1PRT
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    ALL, I, IBEG, IBLOC, IBLOC2, IEND, IER, IHDR, J,
     &           LERTYP, LOC, LOCM1, LOCX, MAXLOC, MAXTMP, MLOC, MOD,
     &           NCBEG, NLOC, NOUT
      CHARACTER  MSGTMP(70), STRING(10)
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      CHARACTER  ATLINE(9), BLANK(1), DBB(3), FROM(6), MSGTYP(8,7),
     &           PERSLA(2), QMARK, UNKNOW(8)
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                              SPECIFICATIONS FOR COMMON /ERCOM1/
      INTEGER    CALLVL, MAXLEV, MSGLEN, ERTYPE(51), ERCODE(51),
     &           PRINTB(7), STOPTB(7), PLEN, IFERR6, IFERR7,
     &           IALLOC(51), HDRFMT(7), TRACON(7)
      COMMON     /ERCOM1/ CALLVL, MAXLEV, MSGLEN, ERTYPE, ERCODE,
     &           PRINTB, STOPTB, PLEN, IFERR6, IFERR7, IALLOC, HDRFMT,
     &           TRACON
      SAVE       /ERCOM1/
C                              SPECIFICATIONS FOR COMMON /ERCOM2/
      CHARACTER  MSGSAV(255), PLIST(300), RNAME(51)*6
      COMMON     /ERCOM2/ MSGSAV, PLIST, RNAME
      SAVE       /ERCOM2/
C                              SPECIFICATIONS FOR COMMON /ERCOM3/
      DOUBLE PRECISION ERCKSM
      COMMON     /ERCOM3/ ERCKSM
      SAVE       /ERCOM3/
C                              SPECIFICATIONS FOR COMMON /ERCOM4/
      LOGICAL    ISUSER(51)
      COMMON     /ERCOM4/ ISUSER
      SAVE       /ERCOM4/
C                              SPECIFICATIONS FOR COMMON /ERCOM8/
      INTEGER    PROLVL, XXLINE(10), XXPLEN(10), ICALOC(10), INALOC(10)
      COMMON     /ERCOM8/ PROLVL, XXLINE, XXPLEN, ICALOC, INALOC
      SAVE       /ERCOM8/
C                              SPECIFICATIONS FOR COMMON /ERCOM9/
      CHARACTER  XXPROC(10)*31
      COMMON     /ERCOM9/ XXPROC
      SAVE       /ERCOM9/
      SAVE       ATLINE, BLANK, DBB, FROM, MSGTYP, PERSLA, QMARK,
     &           UNKNOW
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  MIN0
      INTRINSIC  MIN0
      INTEGER    MIN0
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   C1TIC, M1VE, UMACH
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   I1DX, I1ERIF
      INTEGER    I1DX, I1ERIF
C
      DATA MSGTYP/'N', 'O', 'T', 'E', ' ', ' ', ' ', ' ', 'A',
     &     'L', 'E', 'R', 'T', ' ', ' ', ' ', 'W', 'A', 'R',
     &     'N', 'I', 'N', 'G', ' ', 'F', 'A', 'T', 'A', 'L',
     &     ' ', ' ', ' ', 'T', 'E', 'R', 'M', 'I', 'N', 'A',
     &     'L', 'W', 'A', 'R', 'N', 'I', 'N', 'G', ' ', 'F',
     &     'A', 'T', 'A', 'L', ' ', ' ', ' '/
      DATA UNKNOW/'U', 'N', 'K', 'N', 'O', 'W', 'N', ' '/
      DATA ATLINE/' ', 'A', 'T', ' ', 'L', 'I', 'N', 'E', ' '/
      DATA BLANK/' '/, FROM/' ', 'F', 'R', 'O', 'M', ' '/
      DATA DBB/'.', ' ', ' '/, PERSLA/'%', '/'/
      DATA QMARK/'?'/
C
      IF (MSGLEN .LE. 0) RETURN
      CALL UMACH (2, NOUT)
      MAXTMP = 70
      MOD = 0
      LERTYP = ERTYPE(CALLVL)
      IHDR = HDRFMT(LERTYP)
      IF (IHDR .EQ. 3) THEN
         IF (XXPROC(PROLVL)(1:1).EQ.QMARK .AND. XXLINE(PROLVL).EQ.0)
     &       THEN
            IHDR = 1
         END IF
      END IF
      IEND = 0
      IF (IHDR.EQ.1 .AND. ERTYPE(CALLVL).LE.4) THEN
         MSGTMP(1) = BLANK(1)
         IEND = 1
C                                  CONVERT ERROR CODE INTO CHAR STRING
         CALL C1TIC (ERCODE(CALLVL), STRING, 10, IER)
C                                  LOCATE START OF NON-BLANK CHARACTERS
         IBEG = I1ERIF(STRING,10,BLANK,1)
C                                  M1VE IT TO MSGTMP
         CALL M1VE (STRING, IBEG, 10, 10, MSGTMP, IEND+1,
     &              IEND+11-IBEG, MAXTMP, IER)
         IEND = IEND + 11 - IBEG
      END IF
      IF (IHDR .NE. 2) THEN
         CALL M1VE (FROM, 1, 6, 6, MSGTMP, IEND+1, IEND+6, MAXTMP, IER)
         IEND = IEND + 6
      END IF
      IF (IHDR .EQ. 3) THEN
C                                  THIS IS A PROTRAN RUN TIME ERROR MSG.
C                                  RETRIEVE THE PROCEDURE NAME
         CALL M1VE (XXPROC(PROLVL), 1, XXPLEN(PROLVL), 31, MSGTMP,
     &              IEND+1, IEND+XXPLEN(PROLVL), MAXTMP, IER)
         MLOC = IEND + XXPLEN(PROLVL) + 1
         MSGTMP(MLOC) = BLANK(1)
         IEND = IEND + I1DX(MSGTMP(IEND+1),XXPLEN(PROLVL)+1,BLANK,1) -
     &          1
         IF (XXLINE(PROLVL) .GT. 0) THEN
C                                  INSERT ATLINE
            CALL M1VE (ATLINE, 1, 9, 9, MSGTMP, IEND+1, IEND+9,
     &                 MAXTMP, IER)
            IEND = IEND + 9
C                                  CONVERT PROTRAN GLOBAL LINE NUMBER
            CALL C1TIC (XXLINE(PROLVL), STRING, 10, IER)
C                                  LOCATE START OF NON-BLANK CHARACTERS
            IBEG = I1ERIF(STRING,10,BLANK,1)
C                                  M1VE GLOBAL LINE NUMBER TO MSGTMP
            CALL M1VE (STRING, IBEG, 10, 10, MSGTMP, IEND+1,
     &                 IEND+11-IBEG, MAXTMP, IER)
            IEND = IEND + 11 - IBEG
         END IF
      ELSE
C                                  THIS IS EITHER A LIBRARY ERROR MSG
C                                  OR A PROTRAN PREPROCESSOR ERROR MSG
         IF (IHDR .EQ. 1) THEN
C                                  THIS IS A LIBRARY ERROR MESSAGE.
C                                  RETRIEVE ROUTINE NAME
            CALL M1VE (RNAME(CALLVL), 1, 6, 6, MSGTMP, IEND+1, IEND+6,
     &                 MAXTMP, IER)
            MSGTMP(IEND+7) = BLANK(1)
            IEND = IEND + I1DX(MSGTMP(IEND+1),7,BLANK,1) - 1
         END IF
C                                  ADD DOT, BLANK, BLANK IF NEEDED
         IF (I1DX(MSGSAV,3,DBB,3) .NE. 1) THEN
            CALL M1VE (DBB, 1, 3, 3, MSGTMP, IEND+1, IEND+3, MAXTMP,
     &                 IER)
            IEND = IEND + 3
            MOD = 3
         END IF
      END IF
C                                  MSGTMP AND MSGSAV NOW CONTAIN THE
C                                   ERROR MESSAGE IN FINAL FORM.
      NCBEG = 59 - IEND - MOD
      ALL = 0
      IBLOC = I1DX(MSGSAV,MSGLEN,PERSLA,2)
      IF (IBLOC.NE.0 .AND. IBLOC.LT.NCBEG) THEN
         LOCM1 = IBLOC - 1
         LOC = IBLOC + 1
      ELSE IF (MSGLEN .LE. NCBEG) THEN
         LOCM1 = MSGLEN
         ALL = 1
      ELSE
         LOC = NCBEG
C                                  CHECK FOR APPROPRIATE PLACE TO SPLIT
   10    CONTINUE
         IF (MSGSAV(LOC) .NE. BLANK(1)) THEN
            LOC = LOC - 1
            IF (LOC .GT. 1) GO TO 10
            LOC = NCBEG + 1
         END IF
         LOCM1 = LOC - 1
      END IF
C                                  NO BLANKS FOUND IN FIRST NCBEG CHARS
      IF (LERTYP.GE.1 .AND. LERTYP.LE.7) THEN
         WRITE (NOUT,99995) (MSGTYP(I,LERTYP),I=1,8),
     &                     (MSGTMP(I),I=1,IEND), (MSGSAV(I),I=1,LOCM1)
      ELSE
         WRITE (NOUT,99995) (UNKNOW(I),I=1,8), (MSGTMP(I),I=1,IEND),
     &                     (MSGSAV(I),I=1,LOCM1)
      END IF
      IF (ALL .EQ. 0) THEN
C                                  PREPARE TO WRITE CONTINUATION OF
C                                    MESSAGE
C
C                                  FIND WHERE TO BREAK MESSAGE
C                                    LOC = NUMBER OF CHARACTERS OF
C                                          MESSAGE WRITTEN SO FAR
   20    LOCX = LOC + 64
         NLOC = LOC + 1
         IBLOC2 = IBLOC
         MAXLOC = MIN0(MSGLEN-LOC,64)
         IBLOC = I1DX(MSGSAV(NLOC),MAXLOC,PERSLA,2)
         IF (MSGSAV(NLOC).EQ.BLANK(1) .AND. IBLOC2.EQ.0) NLOC = NLOC +
     &       1
         IF (IBLOC .GT. 0) THEN
C                                  PAGE BREAK FOUND AT IBLOC
            LOCX = NLOC + IBLOC - 2
            WRITE (NOUT,99996) (MSGSAV(I),I=NLOC,LOCX)
            LOC = NLOC + IBLOC
            GO TO 20
C                                  DON'T BOTHER LOOKING FOR BLANK TO
C                                    BREAK AT IF LOCX .GE. MSGLEN
         ELSE IF (LOCX .LT. MSGLEN) THEN
C                                  CHECK FOR BLANK TO BREAK THE LINE
   30       CONTINUE
            IF (MSGSAV(LOCX) .EQ. BLANK(1)) THEN
C                                  BLANK FOUND AT LOCX
               WRITE (NOUT,99996) (MSGSAV(I),I=NLOC,LOCX)
               LOC = LOCX
               GO TO 20
            END IF
            LOCX = LOCX - 1
            IF (LOCX .GT. NLOC) GO TO 30
            LOCX = LOC + 64
C                                  NO BLANKS FOUND IN NEXT 64 CHARS
            WRITE (NOUT,99996) (MSGSAV(I),I=NLOC,LOCX)
            LOC = LOCX
            GO TO 20
         ELSE
C                                  ALL THE REST WILL FIT ON 1 LINE
            LOCX = MSGLEN
            WRITE (NOUT,99996) (MSGSAV(I),I=NLOC,LOCX)
         END IF
      END IF
C                                  SET LENGTH OF MSGSAV AND PLEN
C                                    TO SHOW THAT MESSAGE HAS
C                                    ALREADY BEEN PRINTED
 9000 MSGLEN = 0
      PLEN = 1
      IF (TRACON(LERTYP).EQ.1 .AND. CALLVL.GT.2) THEN
C                                  INITIATE TRACEBACK
         WRITE (NOUT,99997)
         DO 9005  J=CALLVL, 1, -1
            IF (J .GT. 1) THEN
               IF (ISUSER(J-1)) THEN
                  WRITE (NOUT,99998) RNAME(J), ERTYPE(J), ERCODE(J)
               ELSE
                  WRITE (NOUT,99999) RNAME(J), ERTYPE(J), ERCODE(J)
               END IF
            ELSE
               WRITE (NOUT,99998) RNAME(J), ERTYPE(J), ERCODE(J)
            END IF
 9005    CONTINUE
      END IF
C
      RETURN
99995 FORMAT (/, ' *** ', 8A1, ' ERROR', 59A1)
99996 FORMAT (' *** ', 9X, 64A1)
99997 FORMAT (14X, 'HERE IS A TRACEBACK OF SUBPROGRAM CALLS',
     &       ' IN REVERSE ORDER:', /, 14X, '      ROUTINE    ERROR ',
     &       'TYPE    ERROR CODE', /, 14X, '      -------    ',
     &       '----------    ----------')
99998 FORMAT (20X, A6, 5X, I6, 8X, I6)
99999 FORMAT (20X, A6, 5X, I6, 8X, I6, 4X, '(CALLED INTERNALLY)')
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  E1UCS
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    MARCH 8, 1984
C
C  PURPOSE:    TO UPDATE THE CHECKSUM NUMBER FOR ERROR MESSAGES.
C
C  USAGE:      CALL E1UCS
C
C  ARGUMENTS:  NONE
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE E1UCS
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, IBEG, IBEG2, IEND, ILOC, IPOS, JLOC, NCODE, NLEN
      DOUBLE PRECISION DNUM
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      DOUBLE PRECISION DMAX
      CHARACTER  BLANK(1), COMMA(1), EQUAL(1), LPAR(1)
      SAVE       BLANK, COMMA, DMAX, EQUAL, LPAR
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                              SPECIFICATIONS FOR COMMON /ERCOM1/
      INTEGER    CALLVL, MAXLEV, MSGLEN, ERTYPE(51), ERCODE(51),
     &           PRINTB(7), STOPTB(7), PLEN, IFERR6, IFERR7,
     &           IALLOC(51), HDRFMT(7), TRACON(7)
      COMMON     /ERCOM1/ CALLVL, MAXLEV, MSGLEN, ERTYPE, ERCODE,
     &           PRINTB, STOPTB, PLEN, IFERR6, IFERR7, IALLOC, HDRFMT,
     &           TRACON
      SAVE       /ERCOM1/
C                              SPECIFICATIONS FOR COMMON /ERCOM2/
      CHARACTER  MSGSAV(255), PLIST(300), RNAME(51)*6
      COMMON     /ERCOM2/ MSGSAV, PLIST, RNAME
      SAVE       /ERCOM2/
C                              SPECIFICATIONS FOR COMMON /ERCOM3/
      DOUBLE PRECISION ERCKSM
      COMMON     /ERCOM3/ ERCKSM
      SAVE       /ERCOM3/
C                              SPECIFICATIONS FOR COMMON /ERCOM4/
      LOGICAL    ISUSER(51)
      COMMON     /ERCOM4/ ISUSER
      SAVE       /ERCOM4/
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  DMOD
      INTRINSIC  DMOD
      DOUBLE PRECISION DMOD
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   S1ANUM
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   ICASE, I1X
      INTEGER    ICASE, I1X
C
      DATA BLANK(1)/' '/, COMMA(1)/','/, LPAR(1)/'('/
      DATA EQUAL(1)/'='/, DMAX/1.0D+9/
C
      IF (MSGLEN .GT. 1) THEN
         IPOS = 0
         IBEG2 = 1
   10    IBEG = IBEG2
         IEND = MSGLEN
C                                  LOOK FOR BLANK, COMMA, LEFT PAREN.,
C                                  OR EQUAL SIGN
         ILOC = I1X(MSGSAV(IBEG),IEND-IBEG+1,BLANK,1)
         JLOC = I1X(MSGSAV(IBEG),IEND-IBEG+1,COMMA,1)
         IF (ILOC.EQ.0 .OR. (JLOC.GT.0.AND.JLOC.LT.ILOC)) ILOC = JLOC
         JLOC = I1X(MSGSAV(IBEG),IEND-IBEG+1,LPAR,1)
         IF (ILOC.EQ.0 .OR. (JLOC.GT.0.AND.JLOC.LT.ILOC)) ILOC = JLOC
         JLOC = I1X(MSGSAV(IBEG),IEND-IBEG+1,EQUAL,1)
         IF (ILOC.EQ.0 .OR. (JLOC.GT.0.AND.JLOC.LT.ILOC)) ILOC = JLOC
         IF (ILOC .GE. 1) THEN
            CALL S1ANUM (MSGSAV(IBEG+ILOC), IEND-IBEG-ILOC+1, NCODE,
     &                   NLEN)
            IF (NCODE.EQ.2 .OR. NCODE.EQ.3) THEN
C                                  FLOATING POINT NUMBER FOUND.
C                                  SET POINTERS TO SKIP OVER IT
               IBEG2 = IBEG + ILOC + NLEN
               IF (IBEG2 .LE. MSGLEN) THEN
                  CALL S1ANUM (MSGSAV(IBEG2), IEND-IBEG2+1, NCODE,
     &                         NLEN)
                  IF ((MSGSAV(IBEG2).EQ.'+'.OR.MSGSAV(IBEG2).EQ.
     &                '-') .AND. NCODE.EQ.1) THEN
C                                  INTEGER IMMEDIATELY FOLLOWS A REAL AS
C                                  WITH SOME CDC NOS. LIKE 1.2345678+123
C                                  SET POINTERS TO SKIP OVER IT
                     IBEG2 = IBEG2 + NLEN
                  END IF
               END IF
            ELSE
               IBEG2 = IBEG + ILOC
            END IF
            IEND = IBEG + ILOC - 1
         END IF
C                                  UPDATE CKSUM USING PART OF MESSAGE
         DO 20  I=IBEG, IEND
            IPOS = IPOS + 1
            DNUM = ICASE(MSGSAV(I))
            ERCKSM = DMOD(ERCKSM+DNUM*IPOS,DMAX)
   20    CONTINUE
C                                  GO BACK FOR MORE IF NEEDED
         IF (IEND.LT.MSGLEN .AND. IBEG2.LT.MSGLEN) GO TO 10
C                                  UPDATE CKSUM USING ERROR TYPE
         DNUM = ERTYPE(CALLVL)
         ERCKSM = DMOD(ERCKSM+DNUM*(IPOS+1),DMAX)
C                                  UPDATE CKSUM USING ERROR CODE
         DNUM = ERCODE(CALLVL)
         ERCKSM = DMOD(ERCKSM+DNUM*(IPOS+2),DMAX)
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  M1VE
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    MARCH 5, 1984
C
C  PURPOSE:    MOVE A SUBSET OF ONE CHARACTER ARRAY TO ANOTHER.
C
C  USAGE:      CALL M1VE(INSTR, INBEG, INEND, INLEN, OUTSTR, OUTBEG,
C                         OUTEND, OUTLEN, IER)
C
C  ARGUMENTS:
C     INSTR  - SOURCE CHARACTER ARRAY.  (INPUT)
C     INBEG  - FIRST ELEMENT OF INSTR TO BE MOVED.  (INPUT)
C     INEND  - LAST ELEMENT OF INSTR TO BE MOVED.  (INPUT)
C              THE SOURCE SUBSET IS INSTR(INBEG),...,INSTR(INEND).
C     INLEN  - LENGTH OF INSTR.  (INPUT)
C     OUTSTR - DESTINATION CHARACTER ARRAY.  (OUTPUT)
C     IUTBEG - FIRST ELEMENT OF OUTSTR DESTINATION.  (INPUT)
C     IUTEND - LAST ELEMENT OF OUTSTR  DESTINATION.  (INPUT)
C              THE DESTINATION SUBSET IS OUTSRT(IUTBEG),...,
C              OUTSTR(IUTEND).
C     IUTLEN - LENGTH OF OUTSTR.  (INPUT)
C     IER    - COMPLETION CODE.  (OUTPUT)
C              IER = -2  INDICATES THAT THE INPUT PARAMETERS, INBEG,
C                        INEND, INLEN, IUTBEG, IUTEND ARE NOT
C                        CONSISTENT.  ONE OF THE CONDITIONS
C                        INBEG.GT.0, INEND.GE.INBEG, INLEN.GE.INEND,
C                        IUTBEG.GT.0, OR IUTEND.GE.IUTBEG IS NOT
C                        SATISFIED.
C              IER = -1  INDICATES THAT THE LENGTH OF OUTSTR IS
C                        INSUFFICIENT TO HOLD THE SUBSET OF INSTR.
C                        THAT IS, IUTLEN IS LESS THAN IUTEND.
C              IER =  0  INDICATES NORMAL COMPLETION
C              IER >  0  INDICATES THAT THE SPECIFIED SUBSET OF OUTSTR,
C                        OUTSTR(IUTBEG),...,OUTSTR(IUTEND) IS NOT LONG
C                        ENOUGH TO HOLD THE SUBSET INSTR(INBEG),...,
C                        INSTR(INEND) OF INSTR.  IER IS SET TO THE
C                        NUMBER OF CHARACTERS THAT WERE NOT MOVED.
C
C  REMARKS:
C  1. IF THE SUBSET OF OUTSTR IS LONGER THAN THE SUBSET OF INSTR,
C     TRAILING BLANKS ARE MOVED TO OUTSTR.
C  2. IF THE SUBSET OF INSTR IS LONGER THAN THE SUBSET OF OUTSTR,
C     THE SHORTER SUBSET IS MOVED TO OUTSTR AND IER IS SET TO THE NUMBER
C     OF CHARACTERS THAT WERE NOT MOVED TO OUTSTR.
C  3. IF THE LENGTH OF OUTSTR IS INSUFFICIENT TO HOLD THE SUBSET,
C     IER IS SET TO -2 AND NOTHING IS MOVED.
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE M1VE (INSTR, INBEG, INEND, INLEN, OUTSTR, IUTBEG,
     &                 IUTEND, IUTLEN, IER)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    INBEG, INEND, INLEN, IUTBEG, IUTEND, IUTLEN, IER
      CHARACTER  INSTR(*), OUTSTR(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    IUTLAS, KI, KO
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      CHARACTER  BLANK
      SAVE       BLANK
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  MIN0
      INTRINSIC  MIN0
      INTEGER    MIN0
C
      DATA BLANK/' '/
C                                  CHECK INBEG, INEND, INLEN, IUTBEG,
C                                  AND IUTEND
C
      IF (INBEG.LE.0 .OR. INEND.LT.INBEG .OR. INLEN.LT.INEND .OR.
     &    IUTBEG.LE.0 .OR. IUTEND.LT.IUTBEG) THEN
         IER = -2
         RETURN
      ELSE IF (IUTLEN .LT. IUTEND) THEN
         IER = -1
         RETURN
      END IF
C                                  DETERMINE LAST CHARACTER TO M1VE
      IUTLAS = IUTBEG + MIN0(INEND-INBEG,IUTEND-IUTBEG)
C                                  M1VE CHARACTERS
      KI = INBEG
      DO 10  KO=IUTBEG, IUTLAS
         OUTSTR(KO) = INSTR(KI)
         KI = KI + 1
   10 CONTINUE
C                                   SET IER TO NUMBER OF CHARACTERS THAT
C                                   WHERE NOT MOVED
      IER = KI - INEND - 1
C                                   APPEND BLANKS IF NECESSARY
      DO 20  KO=IUTLAS + 1, IUTEND
         OUTSTR(KO) = BLANK
   20 CONTINUE
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  M1VECH
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    DECEMBER 31, 1984
C
C  PURPOSE:    CHARACTER SUBSTRING ASSIGNMENT.
C
C  USAGE:      CALL M1VECH (STR1, LEN1, STR2, LEN2)
C
C  ARGUMENTS:
C     STR1   - SOURCE SUBSTRING.  (INPUT)
C              THE SOURCE SUBSTRING IS STR1(1:LEN1).
C     LEN1   - LENGTH OF STR1.  (INPUT)
C     STR2   - DESTINATION SUBSTRING.  (OUTPUT)
C              THE DESTINATION SUBSTRING IS STR2(1:LEN2).
C     LEN2   - LENGTH OF STR2.  (INPUT)
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE M1VECH (STR1, LEN1, STR2, LEN2)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    LEN1, LEN2
      CHARACTER  STR1*(*), STR2*(*)
C
      STR2(1:LEN2) = STR1(1:LEN1)
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  I1DX (SINGLE PRECISION VERSION)
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    SEPTEMBER 9, 1985
C
C  PURPOSE:    DETERMINE THE ARRAY SUBSCRIPT INDICATING THE STARTING
C              ELEMENT AT WHICH A KEY CHARACTER SEQUENCE BEGINS.
C              (CASE-INSENSITIVE VERSION)
C
C  USAGE:      I1DX(CHRSTR, I1LEN, KEY, KLEN)
C
C  ARGUMENTS:
C     CHRSTR - CHARACTER ARRAY TO BE SEARCHED.  (INPUT)
C     I1LEN  - LENGTH OF CHRSTR.  (INPUT)
C     KEY    - CHARACTER ARRAY THAT CONTAINS THE KEY SEQUENCE.  (INPUT)
C     KLEN   - LENGTH OF KEY.  (INPUT)
C     I1DX   - INTEGER FUNCTION.  (OUTPUT)
C
C  REMARKS:
C  1. RETURNS ZERO WHEN THERE IS NO MATCH.
C
C  2. RETURNS ZERO IF KLEN IS LONGER THAN ISLEN.
C
C  3. RETURNS ZERO WHEN ANY OF THE CHARACTER ARRAYS HAS A NEGATIVE OR
C     ZERO LENGTH.
C
C  GAMS:       N5C
C
C  CHAPTER:    MATH/LIBRARY UTILITIES
C
C  COPYRIGHT:  1985 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      INTEGER FUNCTION I1DX (CHRSTR, I1LEN, KEY, KLEN)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    I1LEN, KLEN
      CHARACTER  CHRSTR(*), KEY(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, II, J
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   ICASE, I1CSTR
      INTEGER    ICASE, I1CSTR
C
      I1DX = 0
      IF (KLEN.LE.0 .OR. I1LEN.LE.0) GO TO 9000
      IF (KLEN .GT. I1LEN) GO TO 9000
C
      I = 1
      II = I1LEN - KLEN + 1
   10 IF (I .LE. II) THEN
         IF (ICASE(CHRSTR(I)) .EQ. ICASE(KEY(1))) THEN
            IF (KLEN .NE. 1) THEN
               J = KLEN - 1
               IF (I1CSTR(CHRSTR(I+1),J,KEY(2),J) .EQ. 0) THEN
                  I1DX = I
                  GO TO 9000
               END IF
            ELSE
               I1DX = I
               GO TO 9000
            END IF
         END IF
         I = I + 1
         GO TO 10
      END IF
C
 9000 RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  I1KRL
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    AUGUST 9, 1983
C
C  PURPOSE:    DEALLOCATE THE LAST N ALLOCATIONS MADE IN THE WORKSPACE.
C              STACK BY I1KGT
C
C  USAGE:      CALL I1KRL(N)
C
C  ARGUMENTS:
C     N      - NUMBER OF ALLOCATIONS TO BE RELEASED TOP DOWN (INPUT)
C
C  COPYRIGHT:  1983 BY IMSL, INC.  ALL RIGHTS RESERVED
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE I1KRL (N)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    N
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, IN, LALC, LBND, LBOOK, LMAX, LNEED, LNOW, LOUT,
     &           LUSED, NDX, NEXT
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      LOGICAL    FIRST
      SAVE       FIRST
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                                  SPECIFICATIONS FOR COMMON /WORKSP/
      REAL       RWKSP(61913)
      REAL       RDWKSP(5000)
      DOUBLE PRECISION DWKSP(2500)
      COMPLEX    CWKSP(2500)
      COMPLEX    CZWKSP(2500)
      COMPLEX    *16 ZWKSP(1250)
      INTEGER    IWKSP(5000)
      LOGICAL    LWKSP(5000)
      EQUIVALENCE (DWKSP(1), RWKSP(1))
      EQUIVALENCE (CWKSP(1), RWKSP(1)), (ZWKSP(1), RWKSP(1))
      EQUIVALENCE (IWKSP(1), RWKSP(1)), (LWKSP(1), RWKSP(1))
      EQUIVALENCE (RDWKSP(1), RWKSP(1)), (CZWKSP(1), RWKSP(1))
      COMMON     /WORKSP/ RWKSP
C                                  SPECIFICATIONS FOR EQUIVALENCE
      EQUIVALENCE (LOUT, IWKSP(1))
      EQUIVALENCE (LNOW, IWKSP(2))
      EQUIVALENCE (LUSED, IWKSP(3))
      EQUIVALENCE (LBND, IWKSP(4))
      EQUIVALENCE (LMAX, IWKSP(5))
      EQUIVALENCE (LALC, IWKSP(6))
      EQUIVALENCE (LNEED, IWKSP(7))
      EQUIVALENCE (LBOOK, IWKSP(8))
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   E1MES, E1STI, IWKIN
C
      DATA FIRST/.TRUE./
C
      IF (FIRST) THEN
C                                  INITIALIZE WORKSPACE IF NEEDED
         FIRST = .FALSE.
         CALL IWKIN (0)
      END IF
C                                  CALLING I1KRL(0) WILL CONFIRM
C                                  INTEGRITY OF SYSTEM AND RETURN
      IF (N .LT. 0) THEN
         CALL E1MES (5, 10, 'ERROR FROM SUBROUTINE I1KRL:  ATTEMPT'//
     &               ' TO RELEASE A NEGATIVE NUMBER OF WORKSPACE'//
     &               ' ALLOCATIONS. ')
         GO TO 9000
      END IF
C                                  BOOKKEEPING OVERWRITTEN
      IF (LNOW.LT.LBOOK .OR. LNOW.GT.LUSED .OR. LUSED.GT.LMAX .OR.
     &    LNOW.GE.LBND .OR. LOUT.GT.LALC) THEN
         CALL E1MES (5, 11, 'ERROR FROM SUBROUTINE I1KRL:  ONE OR '//
     &               'MORE OF THE FIRST EIGHT BOOKKEEPING LOCATIONS '//
     &               'IN IWKSP HAVE BEEN OVERWRITTEN.  ')
         GO TO 9000
      END IF
C                                  CHECK ALL THE POINTERS IN THE
C                                  PERMANENT STORAGE AREA.  THEY MUST
C                                  BE MONOTONE INCREASING AND LESS THAN
C                                  OR EQUAL TO LMAX, AND THE INDEX OF
C                                  THE LAST POINTER MUST BE LMAX+1.
      NDX = LBND
      IF (NDX .NE. LMAX+1) THEN
         DO 10  I=1, LALC
            NEXT = IWKSP(NDX)
            IF (NEXT .EQ. LMAX+1) GO TO 20
C
            IF (NEXT.LE.NDX .OR. NEXT.GT.LMAX) THEN
               CALL E1MES (5, 12, 'ERROR FROM SUBROUTINE I1KRL:  '//
     &                     'A POINTER IN PERMANENT STORAGE HAS BEEN '//
     &                     ' OVERWRITTEN. ')
               GO TO 9000
            END IF
            NDX = NEXT
   10    CONTINUE
         CALL E1MES (5, 13, 'ERROR FROM SUBROUTINE I1KRL:  A '//
     &               'POINTER IN PERMANENT STORAGE HAS BEEN '//
     &               'OVERWRITTEN. ')
         GO TO 9000
      END IF
   20 IF (N .GT. 0) THEN
         DO 30  IN=1, N
            IF (LNOW .LE. LBOOK) THEN
               CALL E1MES (5, 14, 'ERROR FROM SUBROUTINE I1KRL:  '//
     &                     'ATTEMPT TO RELEASE A NONEXISTANT '//
     &                     'WORKSPACE  ALLOCATION. ')
               GO TO 9000
            ELSE IF (IWKSP(LNOW).LT.LBOOK .OR. IWKSP(LNOW).GE.LNOW-1)
     &              THEN
C                                  CHECK TO MAKE SURE THE BACK POINTERS
C                                  ARE MONOTONE.
               CALL E1STI (1, LNOW)
               CALL E1MES (5, 15, 'ERROR FROM SUBROUTINE I1KRL:  '//
     &                     'THE POINTER AT IWKSP(%(I1)) HAS BEEN '//
     &                     'OVERWRITTEN.  ')
               GO TO 9000
            ELSE
               LOUT = LOUT - 1
               LNOW = IWKSP(LNOW)
            END IF
   30    CONTINUE
      END IF
C
 9000 RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  I1KST
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    AUGUST 9, 1983
C
C  PURPOSE:    RETURN CONTROL INFORMATION ABOUT THE WORKSPACE STACK.
C
C  USAGE:      I1KST(NFACT)
C
C  ARGUMENTS:
C     NFACT  - INTEGER VALUE BETWEEN 1 AND 6 INCLUSIVE RETURNS THE
C                 FOLLOWING INFORMATION: (INPUT)
C                   NFACT = 1 - LOUT: NUMBER OF CURRENT ALLOCATIONS
C                               EXCLUDING PERMANENT STORAGE. AT THE
C                               END OF A RUN, THERE SHOULD BE NO
C                               ACTIVE ALLOCATIONS.
C                   NFACT = 2 - LNOW: CURRENT ACTIVE LENGTH
C                   NFACT = 3 - LTOTAL: TOTAL STORAGE USED THUS FAR
C                   NFACT = 4 - LMAX: MAXIMUM STORAGE ALLOWED
C                   NFACT = 5 - LALC: TOTAL NUMBER OF ALLOCATIONS MADE
C                               BY I1KGT THUS FAR
C                   NFACT = 6 - LNEED: NUMBER OF NUMERIC STORAGE UNITS
C                               BY WHICH THE STACK SIZE MUST BE
C                               INCREASED FOR ALL PAST ALLOCATIONS
C                               TO SUCCEED
C     I1KST  - INTEGER FUNCTION. (OUTPUT) RETURNS A WORKSPACE STACK
C              STATISTIC ACCORDING TO VALUE OF NFACT.
C
C  COPYRIGHT:  1983 BY IMSL, INC.  ALL RIGHTS RESERVED
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      INTEGER FUNCTION I1KST (NFACT)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    NFACT
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    ISTATS(7)
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      LOGICAL    FIRST
      SAVE       FIRST
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                                  SPECIFICATIONS FOR COMMON /WORKSP/
      REAL       RWKSP(61913)
      REAL       RDWKSP(5000)
      DOUBLE PRECISION DWKSP(2500)
      COMPLEX    CWKSP(2500)
      COMPLEX    CZWKSP(2500)
      COMPLEX    *16 ZWKSP(1250)
      INTEGER    IWKSP(5000)
      LOGICAL    LWKSP(5000)
      EQUIVALENCE (DWKSP(1), RWKSP(1))
      EQUIVALENCE (CWKSP(1), RWKSP(1)), (ZWKSP(1), RWKSP(1))
      EQUIVALENCE (IWKSP(1), RWKSP(1)), (LWKSP(1), RWKSP(1))
      EQUIVALENCE (RDWKSP(1), RWKSP(1)), (CZWKSP(1), RWKSP(1))
      COMMON     /WORKSP/ RWKSP
C                                  SPECIFICATIONS FOR EQUIVALENCE
      EQUIVALENCE (ISTATS(1), IWKSP(1))
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   E1MES, IWKIN
C
      DATA FIRST/.TRUE./
C
      IF (FIRST) THEN
C                                  INITIALIZE WORKSPACE IF NEEDED
         FIRST = .FALSE.
         CALL IWKIN (0)
      END IF
C
      IF (NFACT.LE.0 .OR. NFACT.GE.7) THEN
         CALL E1MES (5, 9, 'ERROR FROM SUBROUTINE I1KST:  ARGUMENT'//
     &               ' FOR I1KST MUST BE BETWEEN 1 AND 6 INCLUSIVE.')
      ELSE IF (NFACT .EQ. 1) THEN
C                                  LOUT
         I1KST = ISTATS(1)
      ELSE IF (NFACT .EQ. 2) THEN
C                                  LNOW + PERMANENT
         I1KST = ISTATS(2) + (ISTATS(5)-ISTATS(4)+1)
      ELSE IF (NFACT .EQ. 3) THEN
C                                  LUSED + PERMANENT
         I1KST = ISTATS(3) + (ISTATS(5)-ISTATS(4)+1)
      ELSE IF (NFACT .EQ. 4) THEN
C                                  LMAX
         I1KST = ISTATS(5)
      ELSE IF (NFACT .EQ. 5) THEN
C                                  LALC
         I1KST = ISTATS(6)
      ELSE IF (NFACT .EQ. 6) THEN
C                                  LNEED
         I1KST = ISTATS(7)
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  I1KQU
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    JANUARY 17, 1984
C
C  PURPOSE:    RETURN NUMBER OF ELEMENTS OF DATA TYPE ITYPE THAT
C              REMAIN TO BE ALLOCATED IN ONE REQUEST.
C
C  USAGE:      I1KQU(ITYPE)
C
C  ARGUMENTS:
C     ITYPE  - TYPE OF STORAGE TO BE CHECKED (INPUT)
C                 1 - LOGICAL
C                 2 - INTEGER
C                 3 - REAL
C                 4 - DOUBLE PRECISION
C                 5 - COMPLEX
C                 6 - DOUBLE COMPLEX
C     I1KQU  - INTEGER FUNCTION. (OUTPUT) RETURNS NUMBER OF ELEMENTS
C              OF DATA TYPE ITYPE REMAINING IN THE STACK.
C
C  COPYRIGHT:  1983 BY IMSL, INC.  ALL RIGHTS RESERVED
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      INTEGER FUNCTION I1KQU (ITYPE)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    ITYPE
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    ISIZE(6), LALC, LBND, LBOOK, LMAX, LNEED, LNOW, LOUT,
     &           LUSED
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      LOGICAL    FIRST
      SAVE       FIRST
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                                  SPECIFICATIONS FOR COMMON /WORKSP/
      REAL       RWKSP(61913)
      REAL       RDWKSP(5000)
      DOUBLE PRECISION DWKSP(2500)
      COMPLEX    CWKSP(2500)
      COMPLEX    CZWKSP(2500)
      COMPLEX    *16 ZWKSP(1250)
      INTEGER    IWKSP(5000)
      LOGICAL    LWKSP(5000)
      EQUIVALENCE (DWKSP(1), RWKSP(1))
      EQUIVALENCE (CWKSP(1), RWKSP(1)), (ZWKSP(1), RWKSP(1))
      EQUIVALENCE (IWKSP(1), RWKSP(1)), (LWKSP(1), RWKSP(1))
      EQUIVALENCE (RDWKSP(1), RWKSP(1)), (CZWKSP(1), RWKSP(1))
      COMMON     /WORKSP/ RWKSP
C                                  SPECIFICATIONS FOR EQUIVALENCE
      EQUIVALENCE (LOUT, IWKSP(1))
      EQUIVALENCE (LNOW, IWKSP(2))
      EQUIVALENCE (LUSED, IWKSP(3))
      EQUIVALENCE (LBND, IWKSP(4))
      EQUIVALENCE (LMAX, IWKSP(5))
      EQUIVALENCE (LALC, IWKSP(6))
      EQUIVALENCE (LNEED, IWKSP(7))
      EQUIVALENCE (LBOOK, IWKSP(8))
      EQUIVALENCE (ISIZE(1), IWKSP(11))
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  MAX0
      INTRINSIC  MAX0
      INTEGER    MAX0
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   E1MES, E1POP, E1PSH, IWKIN
C
      DATA FIRST/.TRUE./
C
      CALL E1PSH ('I1KQU ')
C
      IF (FIRST) THEN
C                                  INITIALIZE WORKSPACE IF NEEDED
         FIRST = .FALSE.
         CALL IWKIN (0)
      END IF
C                                  BOOKKEEPING OVERWRITTEN
      IF (LNOW.LT.LBOOK .OR. LNOW.GT.LUSED .OR. LUSED.GT.LMAX .OR.
     &    LNOW.GE.LBND .OR. LOUT.GT.LALC) THEN
         CALL E1MES (5, 7, 'ONE OR MORE OF THE FIRST EIGHT '//
     &               'BOOKKEEPING LOCATIONS IN IWKSP HAVE BEEN '//
     &               'OVERWRITTEN.')
      ELSE IF (ITYPE.LE.0 .OR. ITYPE.GE.7) THEN
C                                  ILLEGAL DATA TYPE REQUESTED
         CALL E1MES (5, 8, 'ILLEGAL DATA TYPE REQUESTED.')
      ELSE
C                                  THIS CALCULATION ALLOWS FOR THE
C                                  TWO POINTER LOCATIONS IN THE STACK
C                                  WHICH ARE ASSIGNED TO EACH ALLOCATION
         I1KQU = MAX0(((LBND-3)*ISIZE(2))/ISIZE(ITYPE)-(LNOW*ISIZE(2)-
     &           1)/ISIZE(ITYPE)-1,0)
      END IF
C
      CALL E1POP ('I1KQU ')
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  IWKIN (SINGLE PRECISION VERSION)
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    JANUARY 17, 1984
C
C  PURPOSE:    INITIALIZE BOOKKEEPING LOCATIONS DESCRIBING THE
C              WORKSPACE STACK.
C
C  USAGE:      CALL IWKIN (NSU)
C
C  ARGUMENT:
C     NSU    - NUMBER OF NUMERIC STORAGE UNITS TO WHICH THE WORKSPACE
C              STACK IS TO BE INITIALIZED
C
C  GAMS:       N4
C
C  CHAPTERS:   MATH/LIBRARY REFERENCE MATERIAL
C              STAT/LIBRARY REFERENCE MATERIAL
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE IWKIN (NSU)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    NSU
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    ISIZE(6), LALC, LBND, LBOOK, LMAX, LNEED, LNOW, LOUT,
     &           LUSED, MELMTS, MTYPE
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      LOGICAL    FIRST
      SAVE       FIRST
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                                  SPECIFICATIONS FOR COMMON /WORKSP/
      REAL       RWKSP(61913)
      REAL       RDWKSP(5000)
      DOUBLE PRECISION DWKSP(2500)
      COMPLEX    CWKSP(2500)
      COMPLEX    CZWKSP(2500)
      COMPLEX    *16 ZWKSP(1250)
      INTEGER    IWKSP(5000)
      LOGICAL    LWKSP(5000)
      EQUIVALENCE (DWKSP(1), RWKSP(1))
      EQUIVALENCE (CWKSP(1), RWKSP(1)), (ZWKSP(1), RWKSP(1))
      EQUIVALENCE (IWKSP(1), RWKSP(1)), (LWKSP(1), RWKSP(1))
      EQUIVALENCE (RDWKSP(1), RWKSP(1)), (CZWKSP(1), RWKSP(1))
      COMMON     /WORKSP/ RWKSP
C                                  SPECIFICATIONS FOR EQUIVALENCE
      EQUIVALENCE (LOUT, IWKSP(1))
      EQUIVALENCE (LNOW, IWKSP(2))
      EQUIVALENCE (LUSED, IWKSP(3))
      EQUIVALENCE (LBND, IWKSP(4))
      EQUIVALENCE (LMAX, IWKSP(5))
      EQUIVALENCE (LALC, IWKSP(6))
      EQUIVALENCE (LNEED, IWKSP(7))
      EQUIVALENCE (LBOOK, IWKSP(8))
      EQUIVALENCE (ISIZE(1), IWKSP(11))
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  MAX0
      INTRINSIC  MAX0
      INTEGER    MAX0
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   E1MES, E1STI
C
      DATA FIRST/.TRUE./
C
      IF (.NOT.FIRST) THEN
         IF (NSU .NE. 0) THEN
            CALL E1STI (1, LMAX)
            CALL E1MES (5, 100, 'ERROR FROM SUBROUTINE IWKIN:  '//
     &                  'WORKSPACE STACK HAS PREVIOUSLY BEEN '//
     &                  'INITIALIZED TO %(I1). CORRECT BY MAKING THE '//
     &                  'CALL TO IWKIN THE FIRST EXECUTABLE '//
     &                  'STATEMENT IN THE MAIN PROGRAM.  ')
C
            STOP
C
         ELSE
            RETURN
         END IF
      END IF
C
      IF (NSU .EQ. 0) THEN
C                                  IF NSU=0 USE DEFAULT SIZE 5000
         MELMTS = 5000
      ELSE
         MELMTS = NSU
      END IF
C                                  NUMBER OF ITEMS .LT. 0
      IF (MELMTS .LE. 0) THEN
         CALL E1STI (1, MELMTS)
         CALL E1MES (5, 1, 'ERROR FROM SUBROUTINE IWKIN:  NUMBER '//
     &               'OF NUMERIC STORAGE UNITS IS NOT POSITIVE. NSU '//
     &               '= %(I1) ')
      ELSE
C
         FIRST = .FALSE.
C                                  HERE TO INITIALIZE
C
C                                  SET DATA SIZES APPROPRIATE FOR A
C                                  STANDARD CONFORMING FORTRAN SYSTEM
C                                  USING THE FORTRAN
C                                  *NUMERIC STORAGE UNIT* AS THE
C                                  MEASURE OF SIZE.
C
C                                  TYPE IS REAL
         MTYPE = 3
C                                  LOGICAL
         ISIZE(1) = 1
C                                  INTEGER
         ISIZE(2) = 1
C                                  REAL
         ISIZE(3) = 1
C                                  DOUBLE PRECISION
         ISIZE(4) = 2
C                                  COMPLEX
         ISIZE(5) = 2
C                                  DOUBLE COMPLEX
         ISIZE(6) = 4
C                                  NUMBER OF WORDS USED FOR BOOKKEEPING
         LBOOK = 16
C                                  CURRENT ACTIVE LENGTH OF THE STACK
         LNOW = LBOOK
C                                  MAXIMUM VALUE OF LNOW ACHIEVED THUS
C                                  FAR
         LUSED = LBOOK
C                                  MAXIMUM LENGTH OF THE STORAGE ARRAY
         LMAX = MAX0(MELMTS,((LBOOK+2)*ISIZE(2)+ISIZE(3)-1)/ISIZE(3))
C                                  LOWER BOUND OF THE PERMANENT STORAGE
C                                  WHICH IS ONE WORD MORE THAN THE
C                                  MAXIMUM ALLOWED LENGTH OF THE STACK
         LBND = LMAX + 1
C                                  NUMBER OF CURRENT ALLOCATIONS
         LOUT = 0
C                                  TOTAL NUMBER OF ALLOCATIONS MADE
         LALC = 0
C                                  NUMBER OF WORDS BY WHICH THE ARRAY
C                                  SIZE MUST BE INCREASED FOR ALL PAST
C                                  ALLOCATIONS TO SUCCEED
         LNEED = 0
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  I1X (SINGLE PRECISION VERSION)
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    AUGUST 30, 1985
C
C  PURPOSE:    DETERMINE THE ARRAY SUBSCRIPT INDICATING THE STARTING
C              ELEMENT AT WHICH A KEY CHARACTER SEQUENCE BEGINS.
C              (CASE-SENSITIVE VERSION)
C
C  USAGE:      I1X(CHRSTR, I1LEN, KEY, KLEN)
C
C  ARGUMENTS:
C     CHRSTR - CHARACTER ARRAY TO BE SEARCHED.  (INPUT)
C     I1LEN  - LENGTH OF CHRSTR.  (INPUT)
C     KEY    - CHARACTER ARRAY THAT CONTAINS THE KEY SEQUENCE.  (INPUT)
C     KLEN   - LENGTH OF KEY.  (INPUT)
C     I1X    - INTEGER FUNCTION.  (OUTPUT)
C
C  REMARKS:
C  1. RETURNS ZERO WHEN THERE IS NO MATCH.
C
C  2. RETURNS ZERO IF KLEN IS LONGER THAN ISLEN.
C
C  3. RETURNS ZERO WHEN ANY OF THE CHARACTER ARRAYS HAS A NEGATIVE OR
C     ZERO LENGTH.
C
C  GAMS:       N5C
C
C  CHAPTER:    MATH/LIBRARY UTILITIES
C
C  COPYRIGHT:  1985 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      INTEGER FUNCTION I1X (CHRSTR, I1LEN, KEY, KLEN)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    I1LEN, KLEN
      CHARACTER  CHRSTR(*), KEY(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, II, J
C
      I1X = 0
      IF (KLEN.LE.0 .OR. I1LEN.LE.0) GO TO 9000
      IF (KLEN .GT. I1LEN) GO TO 9000
C
      I = 1
      II = I1LEN - KLEN + 1
   10 IF (I .LE. II) THEN
         IF (CHRSTR(I) .EQ. KEY(1)) THEN
            DO 20  J=2, KLEN
               IF (CHRSTR(I+J-1) .NE. KEY(J)) GO TO 30
   20       CONTINUE
            I1X = I
            GO TO 9000
   30       CONTINUE
         END IF
         I = I + 1
         GO TO 10
      END IF
C
 9000 RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  C1TIC
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    MARCH 9, 1984
C
C  PURPOSE:    CONVERT AN INTEGER TO ITS CORRESPONDING CHARACTER FORM.
C              (RIGHT JUSTIFIED)
C
C  USAGE:      CALL C1TIC(NUM, CHRSTR, SLEN, IER)
C
C  ARGUMENTS:
C     NUM    - INTEGER NUMBER.  (INPUT)
C     CHRSTR - CHARACTER ARRAY THAT RECEIVES THE RESULT.  (OUTPUT)
C     SLEN   - LENGTH OF THE CHARACTER ARRAY.  (INPUT)
C     IER    - COMPLETION CODE.  (OUTPUT) WHERE
C                 IER < 0  INDICATES THAT SLEN <= 0,
C                 IER = 0  INDICATES NORMAL COMPLETION,
C                 IER > 0  INDICATES THAT THE CHARACTER ARRAY IS TOO
C                       SMALL TO HOLD THE COMPLETE NUMBER.  IER
C                       INDICATES HOW MANY SIGNIFICANT DIGITS ARE
C                       BEING TRUNCATED.
C
C  REMARKS:
C  1. THE CHARACTER ARRAY IS FILLED IN A RIGHT JUSTIFIED MANNER.
C  2. LEADING ZEROS ARE REPLACED BY BLANKS.
C  3. SIGN IS INSERTED ONLY FOR NEGATIVE NUMBER.
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE C1TIC (NUM, CHRSTR, SLEN, IER)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    NUM, SLEN, IER
      CHARACTER  CHRSTR(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, J, K, L
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      CHARACTER  BLANK(1), DIGIT(10), MINUS(1)
      SAVE       BLANK, DIGIT, MINUS
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  IABS
      INTRINSIC  IABS
      INTEGER    IABS
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   M1VE
C
      DATA DIGIT/'0', '1', '2', '3', '4', '5', '6', '7', '8',
     &     '9'/
      DATA BLANK/' '/, MINUS/'-'/
C                                  CHECK SLEN
      IF (SLEN .LE. 0) THEN
         IER = -1
         RETURN
      END IF
C                                  THE NUMBER IS ZERO
      IF (NUM .EQ. 0) THEN
         CALL M1VE (BLANK, 1, 1, 1, CHRSTR, 1, SLEN-1, SLEN, I)
         CHRSTR(SLEN) = DIGIT(1)
         IER = 0
         RETURN
      END IF
C                                  CONVERT NUMBER DIGIT BY DIGIT TO
C                                  CHARACTER FORM
      J = SLEN
      K = IABS(NUM)
   10 IF (K.GT.0 .AND. J.GE.1) THEN
         L = K
         K = K/10
         L = L - K*10
         CHRSTR(J) = DIGIT(L+1)
         J = J - 1
         GO TO 10
      END IF
C
   20 IF (K .EQ. 0) THEN
         IF (NUM .LT. 0) THEN
            CALL M1VE (MINUS, 1, 1, 1, CHRSTR, J, J, SLEN, I)
            IF (I .NE. 0) THEN
               IER = 1
               RETURN
            END IF
            J = J - 1
         END IF
         IER = 0
         CALL M1VE (BLANK, 1, 1, 1, CHRSTR, 1, J, SLEN, I)
         RETURN
      END IF
C                                  DETERMINE THE NUMBER OF SIGNIFICANT
C                                  DIGITS BEING TRUNCATED
      I = 0
   30 IF (K .GT. 0) THEN
         K = K/10
         I = I + 1
         GO TO 30
      END IF
C
      IF (NUM .LT. 0) I = I + 1
      IER = I
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  N1RGB
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    MARCH 2, 1984
C
C  PURPOSE:    RETURN A POSITIVE NUMBER AS A FLAG TO INDICATED THAT A
C              STOP SHOULD OCCUR DUE TO ONE OR MORE GLOBAL ERRORS.
C
C  USAGE:      N1RGB(IDUMMY)
C
C  ARGUMENTS:
C     IDUMMY - INTEGER SCALAR DUMMY ARGUMENT.
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      INTEGER FUNCTION N1RGB (IDUMMY)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    IDUMMY
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                              SPECIFICATIONS FOR COMMON /ERCOM1/
      INTEGER    CALLVL, MAXLEV, MSGLEN, ERTYPE(51), ERCODE(51),
     &           PRINTB(7), STOPTB(7), PLEN, IFERR6, IFERR7,
     &           IALLOC(51), HDRFMT(7), TRACON(7)
      COMMON     /ERCOM1/ CALLVL, MAXLEV, MSGLEN, ERTYPE, ERCODE,
     &           PRINTB, STOPTB, PLEN, IFERR6, IFERR7, IALLOC, HDRFMT,
     &           TRACON
      SAVE       /ERCOM1/
C                              SPECIFICATIONS FOR COMMON /ERCOM2/
      CHARACTER  MSGSAV(255), PLIST(300), RNAME(51)*6
      COMMON     /ERCOM2/ MSGSAV, PLIST, RNAME
      SAVE       /ERCOM2/
C                              SPECIFICATIONS FOR COMMON /ERCOM3/
      DOUBLE PRECISION ERCKSM
      COMMON     /ERCOM3/ ERCKSM
      SAVE       /ERCOM3/
C                              SPECIFICATIONS FOR COMMON /ERCOM4/
      LOGICAL    ISUSER(51)
      COMMON     /ERCOM4/ ISUSER
      SAVE       /ERCOM4/
C                                  INITIALIZE FUNCTION
      N1RGB = 0
C                                  CHECK FOR GLOBAL ERROR TYPE 6
      IF (IFERR6 .GT. 0) THEN
         N1RGB = STOPTB(6)
         IFERR6 = 0
      END IF
C                                  CHECK FOR GLOBAL ERROR TYPE 7
      IF (IFERR7 .GT. 0) THEN
         N1RGB = STOPTB(7)
         IFERR7 = 0
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  SDOT (SINGLE PRECISION VERSION)
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    AUGUST 9, 1986
C
C  PURPOSE:    COMPUTE THE SINGLE-PRECISION DOT PRODUCT X*Y.
C
C  USAGE:      SDOT(N, SX, INCX, SY, INCY)
C
C  ARGUMENTS:
C     N      - LENGTH OF VECTORS X AND Y.  (INPUT)
C     SX     - REAL VECTOR OF LENGTH MAX(N*IABS(INCX),1).  (INPUT)
C     INCX   - DISPLACEMENT BETWEEN ELEMENTS OF SX.  (INPUT)
C              X(I) IS DEFINED TO BE.. SX(1+(I-1)*INCX) IF INCX .GE. 0
C              OR SX(1+(I-N)*INCX) IF INCX .LT. 0.
C     SY     - REAL VECTOR OF LENGTH MAX(N*IABS(INCY),1).  (INPUT)
C     INCY   - DISPLACEMENT BETWEEN ELEMENTS OF SY.  (INPUT)
C              Y(I) IS DEFINED TO BE.. SY(1+(I-1)*INCY) IF INCY .GE. 0
C              OR SY(1+(I-N)*INCY) IF INCY .LT. 0.
C     SDOT   - SUM FROM I=1 TO N OF X(I)*Y(I).  (OUTPUT)
C              X(I) AND Y(I) REFER TO SPECIFIC ELEMENTS OF SX AND SY,
C              RESPECTIVELY.  SEE INCX AND INCY ARGUMENT DESCRIPTIONS.
C
C  GAMS:       D1A4
C
C  CHAPTERS:   MATH/LIBRARY BASIC MATRIX/VECTOR OPERATIONS
C              STAT/LIBRARY MATHEMATICAL SUPPORT
C
C  COPYRIGHT:  1986 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      REAL FUNCTION SDOT (N, SX, INCX, SY, INCY)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    N, INCX, INCY
      REAL       SX(*), SY(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, IX, IY, M, MP1
C                                  SPECIFICATIONS FOR SPECIAL CASES
C     INTRINSIC  MOD
      INTRINSIC  MOD
      INTEGER    MOD
C
      SDOT = 0.0E0
      IF (N .GT. 0) THEN
         IF (INCX.NE.1 .OR. INCY.NE.1) THEN
C                                  CODE FOR UNEQUAL INCREMENTS
            IX = 1
            IY = 1
            IF (INCX .LT. 0) IX = (-N+1)*INCX + 1
            IF (INCY .LT. 0) IY = (-N+1)*INCY + 1
            DO 10  I=1, N
               SDOT = SDOT + SX(IX)*SY(IY)
               IX = IX + INCX
               IY = IY + INCY
   10       CONTINUE
         ELSE
C                                  CODE FOR BOTH INCREMENTS EQUAL TO 1
            M = MOD(N,5)
C                                  CLEAN-UP LOOP SO REMAINING VECTOR
            DO 30  I=1, M
               SDOT = SDOT + SX(I)*SY(I)
   30       CONTINUE
            MP1 = M + 1
            DO 40  I=MP1, N, 5
               SDOT = SDOT + SX(I)*SY(I) + SX(I+1)*SY(I+1) +
     &                SX(I+2)*SY(I+2) + SX(I+3)*SY(I+3) +
     &                SX(I+4)*SY(I+4)
   40       CONTINUE
         END IF
      END IF
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  E1POS
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    MARCH 2, 1984
C
C  PURPOSE:    SET OR RETRIEVE PRINT AND STOP ATTRIBUTES.
C
C  USAGE:      CALL E1POS(IERTYP,IPATT,ISATT)
C
C  ARGUMENTS:
C     IERTYP - INTEGER SPECIFYING THE ERROR TYPE FOR WHICH PRINT AND
C              STOP ATTRIBUTES ARE TO BE SET OR RETRIEVED.  (INPUT)  IF
C              IERTYP IS 0 THEN THE SETTINGS APPLY TO ALL ERROR TYPES.
C              IF IERTYP IS BETWEEN 1 AND 7, THEN THE SETTINGS ONLY
C              APPLY TO THAT SPECIFIED ERROR TYPE.  IF IERTYP IS
C              NEGATIVE THEN THE CURRENT PRINT AND STOP ATTRIBUTES WILL
C              BE RETURNED IN IPATT AND ISATT.
C     IPATT  - IF IERTYP IS POSITIVE, IPATT IS AN INTEGER SPECIFYING THE
C              DESIRED PRINT ATTRIBUTE AS FOLLOWS: -1 MEANS NO CHANGE,
C              0 MEANS NO, 1 MEANS YES, AND 2 MEANS ASSIGN THE DEFAULT
C              SETTING.  (INPUT)  IF IERTYP IS NEGATIVE, IPATT IS
C              RETURNED AS 1 IF PRINT IS YES OR 0 IF PRINT IS NO FOR
C              ERROR TYPE IABS(IERTYP).  (OUTPUT)
C     ISATT  - IF IERTYP IS POSITIVE, ISATT IS AN INTEGER SPECIFYING THE
C              DESIRED STOP ATTRIBUTE AS FOLLOWS: -1 MEANS NO CHANGE,
C              0 MEANS NO, 1 MEANS YES, AND 2 MEANS ASSIGN THE DEFAULT
C              SETTING.  (INPUT)  IF IERTYP IS NEGATIVE, ISATT IS
C              RETURNED AS 1 IF PRINT IS YES OR 0 IF PRINT IS NO FOR
C              ERROR TYPE IABS(IERTYP).  (OUTPUT)
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE E1POS (IERTYP, IPATT, ISATT)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    IERTYP, IPATT, ISATT
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, IER
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      INTEGER    DEFLTP(7), DEFLTS(7), IFINIT
      SAVE       DEFLTP, DEFLTS, IFINIT
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                              SPECIFICATIONS FOR COMMON /ERCOM1/
      INTEGER    CALLVL, MAXLEV, MSGLEN, ERTYPE(51), ERCODE(51),
     &           PRINTB(7), STOPTB(7), PLEN, IFERR6, IFERR7,
     &           IALLOC(51), HDRFMT(7), TRACON(7)
      COMMON     /ERCOM1/ CALLVL, MAXLEV, MSGLEN, ERTYPE, ERCODE,
     &           PRINTB, STOPTB, PLEN, IFERR6, IFERR7, IALLOC, HDRFMT,
     &           TRACON
      SAVE       /ERCOM1/
C                              SPECIFICATIONS FOR COMMON /ERCOM2/
      CHARACTER  MSGSAV(255), PLIST(300), RNAME(51)*6
      COMMON     /ERCOM2/ MSGSAV, PLIST, RNAME
      SAVE       /ERCOM2/
C                              SPECIFICATIONS FOR COMMON /ERCOM3/
      DOUBLE PRECISION ERCKSM
      COMMON     /ERCOM3/ ERCKSM
      SAVE       /ERCOM3/
C                              SPECIFICATIONS FOR COMMON /ERCOM4/
      LOGICAL    ISUSER(51)
      COMMON     /ERCOM4/ ISUSER
      SAVE       /ERCOM4/
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  IABS
      INTRINSIC  IABS
      INTEGER    IABS
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   E1INIT, E1MES, E1STI
C
      DATA IFINIT/0/
      DATA DEFLTP/0, 0, 1, 1, 1, 1, 1/, DEFLTS/0, 0, 0, 1, 1, 0, 1/
C                                  INITIALIZE ERROR TABLE IF NECESSARY
      IF (IFINIT .EQ. 0) THEN
         CALL E1INIT
         IFINIT = 1
      END IF
      IER = 0
      IF (IERTYP .GE. 0) THEN
         IF (IPATT.LT.-1 .OR. IPATT.GT.2) THEN
            CALL E1STI (1, IPATT)
            CALL E1MES (5, 1, 'INVALID VALUE SPECIFIED FOR PRINT '//
     &                  'TABLE ATTRIBUTE.  IPATT MUST BE -1, 0, 1, '//
     &                  'OR 2.  IPATT = %(I1)')
            IER = 1
         END IF
         IF (ISATT.LT.-1 .OR. ISATT.GT.2) THEN
            CALL E1STI (1, ISATT)
            CALL E1MES (5, 1, 'INVALID VALUE SPECIFIED FOR STOP '//
     &                  'TABLE ATTRIBUTE.  ISATT MUST BE -1, 0, 1, '//
     &                  'OR 2.  ISATT = %(I1)')
            IER = 1
         END IF
      END IF
      IF (IER .EQ. 0) THEN
         IF (IERTYP .EQ. 0) THEN
            IF (IPATT.EQ.0 .OR. IPATT.EQ.1) THEN
               DO 10  I=1, 7
   10          PRINTB(I) = IPATT
            ELSE IF (IPATT .EQ. 2) THEN
C                                  ASSIGN DEFAULT SETTINGS
               DO 20  I=1, 7
   20          PRINTB(I) = DEFLTP(I)
            END IF
            IF (ISATT.EQ.0 .OR. ISATT.EQ.1) THEN
               DO 30  I=1, 7
   30          STOPTB(I) = ISATT
            ELSE IF (ISATT .EQ. 2) THEN
C                                  ASSIGN DEFAULT SETTINGS
               DO 40  I=1, 7
   40          STOPTB(I) = DEFLTS(I)
            END IF
         ELSE IF (IERTYP.GE.1 .AND. IERTYP.LE.7) THEN
            IF (IPATT.EQ.0 .OR. IPATT.EQ.1) THEN
               PRINTB(IERTYP) = IPATT
            ELSE IF (IPATT .EQ. 2) THEN
C                                  ASSIGN DEFAULT SETTING
               PRINTB(IERTYP) = DEFLTP(IERTYP)
            END IF
            IF (ISATT.EQ.0 .OR. ISATT.EQ.1) THEN
               STOPTB(IERTYP) = ISATT
            ELSE IF (ISATT .EQ. 2) THEN
C                                  ASSIGN DEFAULT SETTING
               STOPTB(IERTYP) = DEFLTS(IERTYP)
            END IF
         ELSE IF (IERTYP.LE.-1 .AND. IERTYP.GE.-7) THEN
            I = IABS(IERTYP)
            IPATT = PRINTB(I)
            ISATT = STOPTB(I)
         END IF
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  E1STL
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    NOVEMBER 8, 1985
C
C  PURPOSE:    TO STORE A STRING FOR SUBSEQUENT USE WITHIN AN ERROR
C              MESSAGE.
C
C  USAGE:      CALL E1STL(IL,STRING)
C
C  ARGUMENTS:
C     IL     - INTEGER SPECIFYING THE SUBSTITUTION INDEX.  IL MUST BE
C              BETWEEN 1 AND 9.  (INPUT)
C     STRING - A CHARACTER STRING.  (INPUT)
C
C  COPYRIGHT:  1985 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE E1STL (IL, STRING)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    IL
      CHARACTER  STRING*(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, LEN2
      CHARACTER  STRGUP(255)
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      INTEGER    IFINIT
      SAVE       IFINIT
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                              SPECIFICATIONS FOR COMMON /ERCOM1/
      INTEGER    CALLVL, MAXLEV, MSGLEN, ERTYPE(51), ERCODE(51),
     &           PRINTB(7), STOPTB(7), PLEN, IFERR6, IFERR7,
     &           IALLOC(51), HDRFMT(7), TRACON(7)
      COMMON     /ERCOM1/ CALLVL, MAXLEV, MSGLEN, ERTYPE, ERCODE,
     &           PRINTB, STOPTB, PLEN, IFERR6, IFERR7, IALLOC, HDRFMT,
     &           TRACON
      SAVE       /ERCOM1/
C                              SPECIFICATIONS FOR COMMON /ERCOM2/
      CHARACTER  MSGSAV(255), PLIST(300), RNAME(51)*6
      COMMON     /ERCOM2/ MSGSAV, PLIST, RNAME
      SAVE       /ERCOM2/
C                              SPECIFICATIONS FOR COMMON /ERCOM3/
      DOUBLE PRECISION ERCKSM
      COMMON     /ERCOM3/ ERCKSM
      SAVE       /ERCOM3/
C                              SPECIFICATIONS FOR COMMON /ERCOM4/
      LOGICAL    ISUSER(51)
      COMMON     /ERCOM4/ ISUSER
      SAVE       /ERCOM4/
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  IABS,LEN,MIN0
      INTRINSIC  IABS, LEN, MIN0
      INTEGER    IABS, LEN, MIN0
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   E1INIT, E1INPL
C
      DATA IFINIT/0/
C                                  INITIALIZE IF NECESSARY
      IF (IFINIT .EQ. 0) THEN
         CALL E1INIT
         IFINIT = 1
      END IF
      LEN2 = LEN(STRING)
      LEN2 = MIN0(LEN2,255)
      DO 10  I=1, LEN2
         STRGUP(I) = STRING(I:I)
   10 CONTINUE
      IF (IABS(IL).GE.1 .AND. IABS(IL).LE.9) THEN
         CALL E1INPL ('L', IL, LEN2, STRGUP)
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  SAXPY (SINGLE PRECISION VERSION)
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    AUGUST 9, 1986
C
C  PURPOSE:    COMPUTE THE SCALAR TIMES A VECTOR PLUS A VECTOR,
C              Y = AX + Y, ALL SINGLE PRECISION.
C
C  USAGE:      CALL SAXPY (N, SA, SX, INCX, SY, INCY)
C
C  ARGUMENTS:
C     N      - LENGTH OF VECTORS X AND Y.  (INPUT)
C     SA     - REAL SCALAR.  (INPUT)
C     SX     - REAL VECTOR OF LENGTH MAX(N*IABS(INCX),1).  (INPUT)
C     INCX   - DISPLACEMENT BETWEEN ELEMENTS OF SX.  (INPUT)
C              X(I) IS DEFINED TO BE
C                 SX(1+(I-1)*INCX) IF INCX.GE.0  OR
C                 SX(1+(I-N)*INCX) IF INCX.LT.0.
C     SY     - REAL VECTOR OF LENGTH MAX(N*IABS(INCY),1).
C              (INPUT/OUTPUT)
C              SAXPY REPLACES Y(I) WITH SA*X(I) + Y(I) FOR I=1,...,N.
C              X(I) AND Y(I) REFER TO SPECIFIC ELEMENTS OF SX AND SY.
C     INCY   - DISPLACEMENT BETWEEN ELEMENTS OF SY.  (INPUT)
C              Y(I) IS DEFINED TO BE
C                 SY(1+(I-1)*INCY) IF INCY.GE.0  OR
C                 SY(1+(I-N)*INCY) IF INCY.LT.0.
C
C  GAMS:       D1A7
C
C  CHAPTERS:   MATH/LIBRARY BASIC MATRIX/VECTOR OPERATIONS
C              STAT/LIBRARY MATHEMATICAL SUPPORT
C
C  COPYRIGHT:  1986 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE SAXPY (N, SA, SX, INCX, SY, INCY)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    N, INCX, INCY
      REAL       SA, SX(*), SY(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, IX, IY, M, MP1
C                                  SPECIFICATIONS FOR SPECIAL CASES
C     INTRINSIC  MOD
      INTRINSIC  MOD
      INTEGER    MOD
C
      IF (N .GT. 0) THEN
         IF (SA .NE. 0.0) THEN
            IF (INCX.NE.1 .OR. INCY.NE.1) THEN
C                                  CODE FOR UNEQUAL INCREMENTS OR EQUAL
C                                  INCREMENTS NOT EQUAL TO 1
               IX = 1
               IY = 1
               IF (INCX .LT. 0) IX = (-N+1)*INCX + 1
               IF (INCY .LT. 0) IY = (-N+1)*INCY + 1
               DO 10  I=1, N
                  SY(IY) = SY(IY) + SA*SX(IX)
                  IX = IX + INCX
                  IY = IY + INCY
   10          CONTINUE
            ELSE
C                                  CODE FOR BOTH INCREMENTS EQUAL TO 1
               M = MOD(N,4)
C                                  CLEAN-UP LOOP
               DO 30  I=1, M
                  SY(I) = SY(I) + SA*SX(I)
   30          CONTINUE
               MP1 = M + 1
               DO 40  I=MP1, N, 4
                  SY(I) = SY(I) + SA*SX(I)
                  SY(I+1) = SY(I+1) + SA*SX(I+1)
                  SY(I+2) = SY(I+2) + SA*SX(I+2)
                  SY(I+3) = SY(I+3) + SA*SX(I+3)
   40          CONTINUE
            END IF
         END IF
      END IF
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  I1ERIF
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    MARCH 13, 1984
C
C  PURPOSE:    RETURN THE POSITION OF THE FIRST ELEMENT OF A GIVEN
C              CHARACTER ARRAY WHICH IS NOT AN ELEMENT OF ANOTHER
C              CHARACTER ARRAY.
C
C  USAGE:      I1ERIF(STR1, LEN1, STR2, LEN2)
C
C  ARGUMENTS:
C     STR1   - CHARACTER ARRAY TO BE SEARCHED.  (INPUT)
C     LEN1   - LENGTH OF STR1.  (INPUT)
C     STR2   - CHARACTER ARRAY TO BE SEARCHED FOR.  (INPUT)
C     LEN2   - LENGTH OF STR2.  (INPUT)
C     I1ERIF - INTEGER FUNCTION.  (OUTPUT)
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      INTEGER FUNCTION I1ERIF (STR1, LEN1, STR2, LEN2)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    LEN1, LEN2
      CHARACTER  STR1(*), STR2(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   I1X
      INTEGER    I1X
C                              FIRST EXECUTABLE STATEMENT
      IF (LEN1.LE.0 .OR. LEN2.LE.0) THEN
         I1ERIF = 1
      ELSE
         DO 10  I=1, LEN1
            IF (I1X(STR2,LEN2,STR1(I),1) .EQ. 0) THEN
               I1ERIF = I
               RETURN
            END IF
   10    CONTINUE
         I1ERIF = 0
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  ICASE (SINGLE PRECISION VERSION)
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    SEPTEMBER 9, 1985
C
C  PURPOSE:    CONVERT FROM CHARACTER TO THE INTEGER ASCII VALUE WITHOUT
C              REGARD TO CASE.
C
C  USAGE:      ICASE(CH)
C
C  ARGUMENTS:
C     CH     - CHARACTER TO BE CONVERTED.  (INPUT)
C     ICASE  - INTEGER ASCII VALUE FOR CH WITHOUT REGARD TO THE CASE
C              OF CH.  (OUTPUT)
C              ICASE RETURNS THE SAME VALUE AS IMSL ROUTINE IACHAR FOR
C              ALL BUT LOWERCASE LETTERS.  FOR THESE, IT RETURNS THE
C              IACHAR VALUE FOR THE CORRESPONDING UPPERCASE LETTER.
C
C  GAMS:       N3
C
C  CHAPTER:    MATH/LIBRARY UTILITIES
C              STAT/LIBRARY UTILITIES
C
C  COPYRIGHT:  1986 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      INTEGER FUNCTION ICASE (CH)
C                                  SPECIFICATIONS FOR ARGUMENTS
      CHARACTER  CH
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   IACHAR
      INTEGER    IACHAR
C
      ICASE = IACHAR(CH)
      IF (ICASE.GE.97 .AND. ICASE.LE.122) ICASE = ICASE - 32
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  S1ANUM
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    MARCH 28, 1984
C
C  PURPOSE:    SCAN A TOKEN AND IDENTIFY IT AS FOLLOWS: INTEGER, REAL
C              NUMBER (SINGLE/DOUBLE), FORTRAN RELATIONAL OPERATOR,
C              FORTRAN LOGICAL OPERATOR, OR FORTRAN LOGICAL CONSTANT.
C
C  USAGE:      CALL S1ANUM(INSTR, SLEN, CODE, OLEN)
C
C  ARGUMENTS:
C     INSTR  - CHARACTER STRING TO BE SCANNED.  (INPUT)
C     SLEN   - LENGTH OF INSTR.  (INPUT)
C     CODE   - TOKEN CODE.  (OUTPUT)  WHERE
C                 CODE =  0  INDICATES AN UNKNOWN TOKEN,
C                 CODE =  1  INDICATES AN INTEGER NUMBER,
C                 CODE =  2  INDICATES A (SINGLE PRECISION) REAL NUMBER,
C                 CODE =  3  INDICATES A (DOUBLE PRECISION) REAL NUMBER,
C                 CODE =  4  INDICATES A LOGICAL CONSTANT (.TRUE. OR
C                               .FALSE.),
C                 CODE =  5  INDICATES THE RELATIONAL OPERATOR .EQ.,
C                 CODE =  6  INDICATES THE RELATIONAL OPERATOR .NE.,
C                 CODE =  7  INDICATES THE RELATIONAL OPERATOR .LT.,
C                 CODE =  8  INDICATES THE RELATIONAL OPERATOR .LE.,
C                 CODE =  9  INDICATES THE RELATIONAL OPERATOR .GT.,
C                 CODE = 10  INDICATES THE RELATIONAL OPERATOR .GE.,
C                 CODE = 11  INDICATES THE LOGICAL OPERATOR .AND.,
C                 CODE = 12  INDICATES THE LOGICAL OPERATOR .OR.,
C                 CODE = 13  INDICATES THE LOGICAL OPERATOR .EQV.,
C                 CODE = 14  INDICATES THE LOGICAL OPERATOR .NEQV.,
C                 CODE = 15  INDICATES THE LOGICAL OPERATOR .NOT..
C     OLEN   - LENGTH OF THE TOKEN AS COUNTED FROM THE FIRST CHARACTER
C              IN INSTR.  (OUTPUT)  OLEN RETURNS A ZERO FOR AN UNKNOWN
C              TOKEN (CODE = 0).
C
C  REMARKS:
C  1. BLANKS ARE CONSIDERED SIGNIFICANT.
C  2. LOWER AND UPPER CASE LETTERS ARE NOT SIGNIFICANT.
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE S1ANUM (INSTR, SLEN, CODE, OLEN)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    SLEN, CODE, OLEN
      CHARACTER  INSTR(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    I, IBEG, IIBEG, J
      LOGICAL    FLAG
      CHARACTER  CHRSTR(6)
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      INTEGER    TABPTR(16), TDCNST, TICNST, TOKEN(13), TRCNST, TZERR
      CHARACTER  DIGIT(10), LETTER(52), MINUS, PERIOD, PLUS, TABLE(38)
      SAVE       DIGIT, LETTER, MINUS, PERIOD, PLUS, TABLE, TABPTR,
     &           TDCNST, TICNST, TOKEN, TRCNST, TZERR
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   I1X, I1CSTR
      INTEGER    I1X, I1CSTR
C
      DATA TOKEN/5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 4, 4/
      DATA TABLE/'D', 'E', 'E', 'Q', 'N', 'E', 'L', 'T', 'L',
     &     'E', 'G', 'T', 'G', 'E', 'A', 'N', 'D', 'O', 'R',
     &     'E', 'Q', 'V', 'N', 'E', 'Q', 'V', 'N', 'O', 'T',
     &     'T', 'R', 'U', 'E', 'F', 'A', 'L', 'S', 'E'/
      DATA TABPTR/1, 2, 3, 5, 7, 9, 11, 13, 15, 18, 20, 23, 27, 30,
     &     34, 39/
      DATA DIGIT/'0', '1', '2', '3', '4', '5', '6', '7', '8',
     &     '9'/
      DATA LETTER/'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
     &     'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
     &     'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C',
     &     'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
     &     'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
     &     'X', 'Y', 'Z'/
      DATA PERIOD/'.'/, PLUS/'+'/, MINUS/'-'/
      DATA TZERR/0/, TICNST/1/
      DATA TRCNST/2/, TDCNST/3/
C
      IF (SLEN .LE. 0) THEN
         CODE = 0
         OLEN = 0
         RETURN
      END IF
C                                  STATE 0 - ASSUME ERROR TOKEN
      IBEG = 1
      CODE = TZERR
C                                  CHECK SIGN
      IF (INSTR(IBEG).EQ.MINUS .OR. INSTR(IBEG).EQ.PLUS) THEN
         FLAG = .TRUE.
         IIBEG = IBEG
         IBEG = IBEG + 1
      ELSE
         FLAG = .FALSE.
      END IF
C                                  STATE 1 - ASSUME INTEGER CONSTANT
      IF (I1X(DIGIT,10,INSTR(IBEG),1) .NE. 0) THEN
         CODE = TICNST
         IIBEG = IBEG
         IBEG = IBEG + 1
C
   10    IF (IBEG .LE. SLEN) THEN
C
            IF (I1X(DIGIT,10,INSTR(IBEG),1) .NE. 0) THEN
               IIBEG = IBEG
               IBEG = IBEG + 1
               GO TO 10
C
            END IF
C
         ELSE
            GO TO 80
C
         END IF
C
         IF (INSTR(IBEG) .NE. PERIOD) GO TO 80
      END IF
C                                  STATE 2 - ASSUME REAL CONSTANT
      IF (CODE .EQ. TICNST) THEN
         CODE = TRCNST
         IIBEG = IBEG
         IBEG = IBEG + 1
         IF (IBEG .GT. SLEN) GO TO 80
      ELSE IF (INSTR(IBEG).EQ.PERIOD .AND. SLEN.GE.2) THEN
         IF (I1X(DIGIT,10,INSTR(IBEG+1),1) .NE. 0) THEN
            CODE = TRCNST
            IIBEG = IBEG + 1
            IBEG = IBEG + 2
            IF (IBEG .GT. SLEN) GO TO 80
         END IF
      END IF
C
      IF (I1X(DIGIT,10,INSTR(IBEG),1) .NE. 0) THEN
         CODE = TRCNST
         IIBEG = IBEG
         IBEG = IBEG + 1
C
   20    IF (IBEG .LE. SLEN) THEN
C
            IF (I1X(DIGIT,10,INSTR(IBEG),1) .NE. 0) THEN
               IIBEG = IBEG
               IBEG = IBEG + 1
               GO TO 20
C
            END IF
C
         ELSE
            GO TO 80
C
         END IF
C
      END IF
C
      IF (CODE .EQ. TZERR) THEN
         IF (INSTR(IBEG) .NE. PERIOD) GO TO 80
         IBEG = IBEG + 1
         IF (IBEG .GT. SLEN) GO TO 80
      END IF
C
      IF (I1X(LETTER,52,INSTR(IBEG),1) .EQ. 0) GO TO 80
      CHRSTR(1) = INSTR(IBEG)
C
      DO 30  I=2, 6
         IBEG = IBEG + 1
         IF (IBEG .GT. SLEN) GO TO 80
         IF (I1X(LETTER,52,INSTR(IBEG),1) .EQ. 0) GO TO 40
         CHRSTR(I) = INSTR(IBEG)
   30 CONTINUE
C
      GO TO 80
C
   40 CONTINUE
C
      DO 50  J=1, 15
         IF (I1CSTR(CHRSTR,I-1,TABLE(TABPTR(J)),TABPTR(J+1)-TABPTR(J))
     &        .EQ. 0) GO TO 60
   50 CONTINUE
C
      GO TO 80
C                                  STATE 4 - LOGICAL OPERATOR
   60 IF (J .GT. 2) THEN
C
         IF (CODE .EQ. TRCNST) THEN
C
            IF (INSTR(IBEG) .EQ. PERIOD) THEN
               CODE = TICNST
               IIBEG = IIBEG - 1
            END IF
C
            GO TO 80
C
         ELSE IF (INSTR(IBEG) .NE. PERIOD) THEN
            GO TO 80
C
         ELSE IF (FLAG) THEN
            GO TO 80
C
         ELSE
            CODE = TOKEN(J-2)
            IIBEG = IBEG
            GO TO 80
C
         END IF
C
      END IF
C                                  STATE 5 - DOUBLE PRECISION CONSTANT
      IF (CODE .NE. TRCNST) GO TO 80
      IF (INSTR(IBEG).EQ.MINUS .OR. INSTR(IBEG).EQ.PLUS) IBEG = IBEG +
     &    1
      IF (IBEG .GT. SLEN) GO TO 80
C
      IF (I1X(DIGIT,10,INSTR(IBEG),1) .EQ. 0) THEN
         GO TO 80
C
      ELSE
         IIBEG = IBEG
         IBEG = IBEG + 1
C
   70    IF (IBEG .LE. SLEN) THEN
C
            IF (I1X(DIGIT,10,INSTR(IBEG),1) .NE. 0) THEN
               IIBEG = IBEG
               IBEG = IBEG + 1
               GO TO 70
C
            END IF
C
         END IF
C
      END IF
C
      IF (J .EQ. 1) CODE = TDCNST
C
   80 CONTINUE
C
      IF (CODE .EQ. TZERR) THEN
         OLEN = 0
C
      ELSE
         OLEN = IIBEG
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  IMACH (SINGLE PRECISION VERSION)
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    MARCH 26, 1984
C
C  PURPOSE:    RETRIEVE INTEGER MACHINE CONSTANTS.
C
C  USAGE:      IMACH(N)
C
C  ARGUMENTS:
C     N      - INDEX OF DESIRED CONSTANT.  (INPUT)
C     IMACH  - MACHINE CONSTANT.  (OUTPUT)
C
C  REMARK:
C     FOLLOWING IS A DESCRIPTION OF THE ASSORTED INTEGER MACHINE
C     CONSTANTS.
C
C     WORDS
C
C        IMACH( 1) = NUMBER OF BITS PER INTEGER STORAGE UNIT.
C        IMACH( 2) = NUMBER OF CHARACTERS PER INTEGER STORAGE UNIT.
C
C     INTEGERS
C
C        ASSUME INTEGERS ARE REPRESENTED IN THE S-DIGIT, BASE-A FORM
C        SIGN ( X(S-1)*A**(S-1) + ... + X(1)*A + X(0) )
C        WHERE 0 .LE. X(I) .LT. A FOR I=0,...,S-1.  THEN
C
C        IMACH( 3) = A, THE BASE.
C        IMACH( 4) = S, NUMBER OF BASE-A DIGITS.
C        IMACH( 5) = A**S - 1, LARGEST MAGNITUDE.
C
C     FLOATING-POINT NUMBERS
C
C        ASSUME FLOATING-POINT NUMBERS ARE REPRESENTED IN THE T-DIGIT,
C        BASE-B FORM SIGN (B**E)*( (X(1)/B) + ... + (X(T)/B**T) )
C        WHERE 0 .LE. X(I) .LT. B FOR I=1,...,T,
C        0 .LT. X(1), AND EMIN .LE. E .LE. EMAX.  THEN
C
C        IMACH( 6) = B, THE BASE.
C
C        SINGLE PRECISION
C
C           IMACH( 7) = T, NUMBER OF BASE-B DIGITS.
C           IMACH( 8) = EMIN, SMALLEST EXPONENT E.
C           IMACH( 9) = EMAX, LARGEST EXPONENT E.
C
C        DOUBLE PRECISION
C
C           IMACH(10) = T, NUMBER OF BASE-B DIGITS.
C           IMACH(11) = EMIN, SMALLEST EXPONENT E.
C           IMACH(12) = EMAX, LARGEST EXPONENT E.
C
C  GAMS:       R1
C
C  CHAPTERS:   MATH/LIBRARY REFERENCE MATERIAL
C              STAT/LIBRARY REFERENCE MATERIAL
C              SFUN/LIBRARY REFERENCE MATERIAL
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      INTEGER FUNCTION IMACH (N)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    N
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    NOUT
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      INTEGER    IMACHV(12)
      SAVE       IMACHV
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   UMACH
C                                  DEFINE CONSTANTS
      DATA IMACHV(1)/32/
      DATA IMACHV(2)/4/
      DATA IMACHV(3)/2/
      DATA IMACHV(4)/31/
      DATA IMACHV(5)/2147483647/
      DATA IMACHV(6)/2/
      DATA IMACHV(7)/24/
      DATA IMACHV(8)/-125/
      DATA IMACHV(9)/128/
      DATA IMACHV(10)/53/
      DATA IMACHV(11)/-1021/
      DATA IMACHV(12)/1024/
C
      IF (N.LT.1 .OR. N.GT.12) THEN
C                                  ERROR.  INVALID RANGE FOR N.
         CALL UMACH (2, NOUT)
         WRITE (NOUT,99999) N
99999    FORMAT (/, ' *** TERMINAL ERROR 5 FROM IMACH.  THE ARGUMENT',
     &          /, ' ***          MUST BE BETWEEN 1 AND 12 INCLUSIVE.'
     &          , /, ' ***          N = ', I6, '.', /)
         IMACH = 0
         STOP
C
      ELSE
         IMACH = IMACHV(N)
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  E1INPL
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    MARCH 2, 1984
C
C  PURPOSE:    TO STORE A CHARACTER STRING IN THE PARAMETER LIST PLIST
C              FOR USE BY THE ERROR MESSAGE HANDLER.
C
C  USAGE:      CALL E1INPL(FORM,NUM,SLEN,STRUP)
C
C  ARGUMENTS:
C     FORM   - A CHARACTER STRING OF LENGTH ONE TO BE INSERTED INTO
C              PLIST WHICH SPECIFIES THE FORM OF THE STRING.  (INPUT)
C              FOR EXAMPLE, 'L' FOR STRING, 'A' FOR CHARACTER ARRAY,
C              'I' FOR INTEGER, 'K' FOR KEYWORD (PROTRAN ONLY).  AN
C              ASTERISK IS INSERTED INTO PLIST PRECEDING FORM.
C     NUM    - INTEGER TO BE INSERTED AS A CHARACTER INTO PLIST
C              IMMEDIATELY FOLLOWING FORM.  (INPUT)  NUM MUST BE BETWEEN
C              1 AND 9.
C     SLEN   - THE NUMBER OF CHARACTERS IN STRUP.  (INPUT)  LEN MUST BE
C              LESS THAN OR EQUAL TO 255.  THE CHARACTER REPRESENTATION
C              OF SLEN IS INSERTED INTO PLIST AFTER NUM AND AN ASTERISK.
C     STRUP  - A CHARACTER STRING OF LENGTH LEN WHICH IS TO BE INSERTED
C              INTO PLIST.  (INPUT)  TRAILING BLANKS ARE IGNORED.
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE E1INPL (FORM, NUM, SLEN, STRUP)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    NUM, SLEN
      CHARACTER  FORM, STRUP(*)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    IER, L, LEN2, LENCK, LOC, NLEN, NNUM
      CHARACTER  STRNCH(3)
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      CHARACTER  BLANK, PRCNT(1), TEMP(4)
      SAVE       BLANK, PRCNT, TEMP
C                                  SPECIFICATIONS FOR SPECIAL CASES
C                              SPECIFICATIONS FOR COMMON /ERCOM1/
      INTEGER    CALLVL, MAXLEV, MSGLEN, ERTYPE(51), ERCODE(51),
     &           PRINTB(7), STOPTB(7), PLEN, IFERR6, IFERR7,
     &           IALLOC(51), HDRFMT(7), TRACON(7)
      COMMON     /ERCOM1/ CALLVL, MAXLEV, MSGLEN, ERTYPE, ERCODE,
     &           PRINTB, STOPTB, PLEN, IFERR6, IFERR7, IALLOC, HDRFMT,
     &           TRACON
      SAVE       /ERCOM1/
C                              SPECIFICATIONS FOR COMMON /ERCOM2/
      CHARACTER  MSGSAV(255), PLIST(300), RNAME(51)*6
      COMMON     /ERCOM2/ MSGSAV, PLIST, RNAME
      SAVE       /ERCOM2/
C                              SPECIFICATIONS FOR COMMON /ERCOM3/
      DOUBLE PRECISION ERCKSM
      COMMON     /ERCOM3/ ERCKSM
      SAVE       /ERCOM3/
C                              SPECIFICATIONS FOR COMMON /ERCOM4/
      LOGICAL    ISUSER(51)
      COMMON     /ERCOM4/ ISUSER
      SAVE       /ERCOM4/
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  IABS
      INTRINSIC  IABS
      INTEGER    IABS
C                                  SPECIFICATIONS FOR SUBROUTINES
      EXTERNAL   C1TIC, M1VE
C
      DATA TEMP/'*', ' ', ' ', '*'/, PRCNT/'%'/, BLANK/' '/
C
      NNUM = IABS(NUM)
      LENCK = PLEN + SLEN + 8
      IF (NNUM.GE.1 .AND. NNUM.LE.9 .AND. LENCK.LE.300) THEN
         TEMP(2) = FORM
         CALL C1TIC (NNUM, TEMP(3), 1, IER)
         LOC = PLEN + 1
         IF (LOC .EQ. 2) LOC = 1
         CALL M1VE (TEMP, 1, 4, 4, PLIST(LOC), 1, 4, 262, IER)
         LOC = LOC + 4
         IF (NUM .LT. 0) THEN
            LEN2 = SLEN
         ELSE
            DO 10  L=1, SLEN
               LEN2 = SLEN - L + 1
               IF (STRUP(LEN2) .NE. BLANK) GO TO 20
   10       CONTINUE
            LEN2 = 1
   20       CONTINUE
         END IF
         NLEN = 1
         IF (LEN2 .GE. 10) NLEN = 2
         IF (LEN2 .GE. 100) NLEN = 3
         CALL C1TIC (LEN2, STRNCH, NLEN, IER)
         CALL M1VE (STRNCH, 1, NLEN, 3, PLIST(LOC), 1, NLEN, 262, IER)
         LOC = LOC + NLEN
         CALL M1VE (PRCNT, 1, 1, 1, PLIST(LOC), 1, 1, 262, IER)
         LOC = LOC + 1
         CALL M1VE (STRUP, 1, LEN2, LEN2, PLIST(LOC), 1, LEN2, 262,
     &              IER)
         PLEN = LOC + LEN2 - 1
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  UMACH (SINGLE PRECISION VERSION)
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    MARCH 21, 1984
C
C  PURPOSE:    SET OR RETRIEVE INPUT OR OUTPUT DEVICE UNIT NUMBERS.
C
C  USAGE:      CALL UMACH (N, NUNIT)
C
C  ARGUMENTS:
C     N      - INDEX OF DESIRED UNIT.  (INPUT)
C              THE VALUES OF N ARE DEFINED AS FOLLOWS:
C              N = 1, CORRESPONDS TO THE STANDARD INPUT UNIT.
C              N = 2, CORRESPONDS TO THE STANDARD OUTPUT UNIT.
C     NUNIT  - I/O UNIT.  (INPUT OR OUTPUT)
C              IF THE VALUE OF N IS NEGATIVE, THE UNIT CORRESPONDING
C              TO THE INDEX IS RESET TO THE VALUE GIVEN IN NUNIT.
C              OTHERWISE, THE VALUE CORRESPONDING TO THE INDEX IS
C              RETURNED IN NUNIT.
C
C  GAMS:       R1
C
C  CHAPTERS:   MATH/LIBRARY REFERENCE MATERIAL
C              STAT/LIBRARY REFERENCE MATERIAL
C              SFUN/LIBRARY REFERENCE MATERIAL
C
C  COPYRIGHT:  1984 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      SUBROUTINE UMACH (N, NUNIT)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    N, NUNIT
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    NN, NOUT
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      INTEGER    UNIT(2)
      SAVE       UNIT
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  IABS
      INTRINSIC  IABS
      INTEGER    IABS
C
      DATA UNIT(1)/5/
      DATA UNIT(2)/6/
C
      NN = IABS(N)
      IF (NN.NE.1 .AND. NN.NE.2) THEN
C                                  ERROR.  INVALID RANGE FOR N.
         NOUT = UNIT(2)
         WRITE (NOUT,99999) NN
99999    FORMAT (/, ' *** TERMINAL ERROR 5 FROM UMACH.  THE ABSOLUTE',
     &          /, ' ***          VALUE OF THE INDEX VARIABLE MUST BE'
     &          , /, ' ***          1 OR 2.  IABS(N) = ', I6,
     &          '.', /)
         STOP
C                                  CHECK FOR RESET OR RETRIEVAL
      ELSE IF (N .LT. 0) THEN
C                                  RESET
         UNIT(NN) = NUNIT
      ELSE
C                                  RETRIEVE
         NUNIT = UNIT(N)
      END IF
C
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  IACHAR (SINGLE PRECISION VERSION)
C
C  COMPUTER:   PCDSMS/SINGLE
C
C  REVISED:    SEPTEMBER 9, 1985
C
C  PURPOSE:    RETURN THE INTEGER ASCII VALUE OF A CHARACTER ARGUMENT.
C
C  USAGE:      IACHAR(CH)
C
C  ARGUMENTS:
C     CH     - CHARACTER ARGUMENT FOR WHICH THE INTEGER ASCII VALUE
C              IS DESIRED.  (INPUT)
C     IACHAR - INTEGER ASCII VALUE FOR CH.  (OUTPUT)
C              THE CHARACTER CH IS IN THE IACHAR-TH POSITION OF THE
C              ASCII COLLATING SEQUENCE.
C
C  GAMS:       N3
C
C  CHAPTER:    MATH/LIBRARY UTILITIES
C              STAT/LIBRARY UTILITIES
C
C  COPYRIGHT:  1986 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      INTEGER FUNCTION IACHAR (CH)
C                                  SPECIFICATIONS FOR ARGUMENTS
      CHARACTER  CH
C                                  SPECIFICATIONS FOR SAVE VARIABLES
      IACHAR = ICHAR(CH)
      RETURN
      END
C-----------------------------------------------------------------------
C  IMSL NAME:  I1CSTR (SINGLE PRECISION VERSION)
C
C  COMPUTER:   DGC/SINGLE
C
C  REVISED:    SEPTEMBER 10, 1985
C
C  PURPOSE:    CASE INSENSITIVE COMPARISON OF TWO CHARACTER ARRAYS.
C
C  USAGE:      I1CSTR(STR1, LEN1, STR2, LEN2)
C
C  ARGUMENTS:
C     STR1   - FIRST CHARACTER ARRAY.  (INPUT)
C     LEN1   - LENGTH OF STR1.  (INPUT)
C     STR2   - SECOND CHARACTER ARRAY.  (INPUT)
C     LEN2   - LENGTH OF STR2.  (INPUT)
C     I1CSTR - INTEGER FUNCTION.  (OUTPUT) WHERE
C              I1CSTR = -1  IF STR1 .LT. STR2,
C              I1CSTR =  0  IF STR1 .EQ. STR2,
C              I1CSTR =  1  IF STR1 .GT. STR2.
C
C  REMARKS:
C  1. IF THE TWO ARRAYS, STR1 AND STR2,  ARE OF UNEQUAL LENGTH, THE
C     SHORTER ARRAY IS CONSIDERED AS IF IT WERE EXTENDED WITH BLANKS
C     TO THE LENGTH OF THE LONGER ARRAY.
C
C  2. IF ONE OR BOTH LENGTHS ARE ZERO OR NEGATIVE THE I1CSTR OUTPUT IS
C     BASED ON COMPARISON OF THE LENGTHS.
C
C  GAMS:       N5C
C
C  CHAPTER:    MATH/LIBRARY UTILITIES
C
C  COPYRIGHT:  1985 BY IMSL, INC.  ALL RIGHTS RESERVED.
C
C  WARRANTY:   IMSL WARRANTS ONLY THAT IMSL TESTING HAS BEEN APPLIED
C              TO THIS CODE.  NO OTHER WARRANTY, EXPRESSED OR IMPLIED,
C              IS APPLICABLE.
C
C-----------------------------------------------------------------------
C
      INTEGER FUNCTION I1CSTR (STR1, LEN1, STR2, LEN2)
C                                  SPECIFICATIONS FOR ARGUMENTS
      INTEGER    LEN1, LEN2
      CHARACTER  STR1(LEN1), STR2(LEN2)
C                                  SPECIFICATIONS FOR LOCAL VARIABLES
      INTEGER    IC1, IC2, ICB, IS, L, LENM
C                                  SPECIFICATIONS FOR INTRINSICS
C     INTRINSIC  ISIGN,MIN0
      INTRINSIC  ISIGN, MIN0
      INTEGER    ISIGN, MIN0
C                                  SPECIFICATIONS FOR FUNCTIONS
      EXTERNAL   ICASE
      INTEGER    ICASE
C
      IF (LEN1.GT.0 .AND. LEN2.GT.0) THEN
C                                  COMPARE FIRST LENM CHARACTERS
         LENM = MIN0(LEN1,LEN2)
         DO 10  L=1, LENM
            IC1 = ICASE(STR1(L))
            IC2 = ICASE(STR2(L))
            IF (IC1 .NE. IC2) THEN
               I1CSTR = ISIGN(1,IC1-IC2)
               RETURN
            END IF
   10    CONTINUE
      END IF
C                                  COMPARISON BASED ON LENGTH OR
C                                  TRAILING BLANKS
      IS = LEN1 - LEN2
      IF (IS .EQ. 0) THEN
         I1CSTR = 0
      ELSE
         IF (LEN1.LE.0 .OR. LEN2.LE.0) THEN
C                                  COMPARISON BASED ON LENGTH
            I1CSTR = ISIGN(1,IS)
         ELSE
C                                  COMPARISON BASED ON TRAILING BLANKS
C                                  TO EXTEND SHORTER ARRAY
            LENM = LENM + 1
            ICB = ICASE(' ')
            IF (IS .GT. 0) THEN
C                                  EXTEND STR2 WITH BLANKS
               DO 20  L=LENM, LEN1
                  IC1 = ICASE(STR1(L))
                  IF (IC1 .NE. ICB) THEN
                     I1CSTR = ISIGN(1,IC1-ICB)
                     RETURN
                  END IF
   20          CONTINUE
            ELSE
C                                  EXTEND STR1 WITH BLANKS
               DO 30  L=LENM, LEN2
                  IC2 = ICASE(STR2(L))
                  IF (ICB .NE. IC2) THEN
                     I1CSTR = ISIGN(1,ICB-IC2)
                     RETURN
                  END IF
   30          CONTINUE
            END IF
C
            I1CSTR = 0
         END IF
      END IF
C
      RETURN
      END
C
C
C  ********************************************************************
C    SUBROUTINE SHARPEN -- PERMUTES BLOCKS OF 5-ADJACENT LEGISLATORS
C                            TO SHARPEN THE RANK ORDERING
C  ********************************************************************
C
      SUBROUTINE SHARPEN(NNPERM,KCUTTER,LCUTTER,
     C                    XMAT,WS,LERROR)
C
      DIMENSION KCUTTER(21011),LCUTTER(21011),XMAT(1879,1879),
     C          WS(43011),
     C          KPERM(7),MPERM(7),KBIGPERM(120,5040),
     C          YSS(21555),YSJAVA(21555),
     C          MMJAVA(21555),LLV(21555),LLVB(21555),
     C          LLE(21555),LLEB(21555),L(21555),LL(21555),
     C          LERROR(1879,21011),ZS(21555),LJAVA(21555),
     C          LV(21555),KKA(21555),KA(21555),
     C          LLSAVE(21555),YSSS(21555),
     C          XJCH(25),XJEH(25),XJCL(25),XJEL(25),
     C          LTEST(2*21555),LORDER(2*21555),
     C          MERROR(1879,21011),MMSAVE(1879,21555)
      COMMON /XXCOM/ LDATA(1899,21011)
      COMMON /MINE/ NS,NRCALL,NP
  100 FORMAT(I6,20I3)
  101 FORMAT(I4,F10.4,3I4)
  102 FORMAT(' INITIAL ERROR  ',2I7)
  103 FORMAT(' BETTER SOLUTION',4I7,I5,10I4)
  104 FORMAT(I4,2F10.4)
  105 FORMAT(' SAME SOLUTION  ',4I7)
 8250 FORMAT(I4,' ROLL CALLS ',I3,2I8,3F7.3,10I4)
C
C  KPERM(.) IS THE VECTOR TO BE PERMUTED
C  MPERM(.) IS A WORK VECTOR.  AT THE FIRST CALL OF PERM(N,K,M),
C    MPERM(1)=0.  IT IS THEN USED AS A FLAG TO DETERMINE WHEN 
C    THE LAST OF THE N! PERMUTATIONS ARE COMPLETED.  IF SO,
C    THEN MPERM(1)=0 IF THE LAST PERMUTATION IS PASSED BACK
C
      NV=NRCALL
      N=NNPERM
      KBEFORE=0
      DO 9999 IIJJ=1,NP-N
C
C  INITIALIZE ERROR
C
      KINITIAL=0
      DO 20 J=1,NRCALL
      DO 20 I=1,NP
      KINITIAL=KINITIAL+LERROR(I,J)
      MERROR(I,J)=LERROR(I,J)
  20  CONTINUE
C
      MPERM(1)=0
      DO 1 I=1,N
      KPERM(I)=I
  1   CONTINUE
C
      KKK=0
 999  CONTINUE
      KKK=KKK+1
      CALL PERM(N,KPERM,MPERM)
      DO 2 I=1,N
      KBIGPERM(KKK,I)=KPERM(I)
  2   CONTINUE
C
C  CHECK CURRENT PERMUTATION OF THE LEGISLATORS
C
      DO 11 I=1,NP
      YSS(I)=XMAT(I,1)
      YSSS(I)=XMAT(I,1)
      L(I)=I
      LL(I)=I
  11  CONTINUE
C
      CALL RSORT(YSS,NP,LL)
      CALL RSORT(YSSS,NP,L)
C
C  PERMUTE THE ORDERING BY "TRICKING" LL(.) USING KPERM(.)
C
      DO 12 I=1,N
      LLSAVE(I)=LL(KPERM(I)+KBEFORE)
  12  CONTINUE
      DO 13 I=1,N
      LL(I+KBEFORE)=LLSAVE(I)
  13  CONTINUE
      DO 14 I=1,NP
C      WRITE(22,101)I,YSS(I),LL(I),L(I)
      MMSAVE(I,KKK)=LL(I)
  14  CONTINUE
      KT=0
      KTT=0
      JCH=0
      JEH=0
      JCL=0
      JEL=0
      MSUM=0
      NSUM=0
      IVOT=0
C
C
C  ROLL CALL LOOP -- ESTIMATE BEST CUTTING POINT GIVEN FIXED LEGISLATOR
C                    CONFIGURATION
C
      DO 600 IIII=1,NV
      IVOT=IVOT+1
      KSUM=0
      LSUM=0
C
C  RECODE AND COMPUTE YES AND NO MEANS 
C
      DO 16 J=1,NP
      LV(J)=LDATA(J,IVOT)
      IF(LV(J).EQ.1.OR.LV(J).EQ.2.OR.LV(J).EQ.3)KK=1
      IF(LV(J).EQ.4.OR.LV(J).EQ.5.OR.LV(J).EQ.6)KK=6
      IF(LV(J).EQ.0.OR.LV(J).EQ.7.OR.LV(J).EQ.8.OR.LV(J).EQ.9)KK=9
      KKA(J)=KK
      IF(KK.EQ.1)KSUM=KSUM+1
      IF(KK.EQ.6)LSUM=LSUM+1
  16  CONTINUE
      DO 255 J=1,NP
  255 KA(J)=KKA(LL(J))
C
C  CHECK FOR UNANIMOUS VOTES AND DISCARD THEM
C
      IF(KSUM.EQ.0.OR.LSUM.EQ.0)GO TO 600
C
C  CUMULATIVE SUM OF MAJORITY VOTE BY ROLL CALL
C
      IF(KSUM-LSUM)201,202,202
  202 MSUM=MSUM+KSUM
      GO TO 203
  201 MSUM=MSUM+LSUM
  203 CONTINUE
C
C  BEGIN JANICE LOOP FOR ROLL CALLS
C
C  GET ONE POINT RESULTS
C
      JCH=0
      JEH=0
      JCL=0
      JEL=0
      KJAVA=0
      DO 1663 JAVA=1,NP
      IF(KA(JAVA).NE.9)THEN
        KJAVA=KJAVA+1
        YSJAVA(KJAVA)=YSS(JAVA)
        MMJAVA(KJAVA)=KA(JAVA)
        LJAVA(KJAVA)=LL(JAVA)
      ENDIF
 1663 CONTINUE
      YSJAVA(KJAVA+1)=YSJAVA(KJAVA)+.01
      MMJAVA(KJAVA+1)=9
      LJAVA(KJAVA+1)=NP+1
      NV1=KJAVA+1
      IROTC=0
      CALL JAN1PT(NV1,NV,NS,IVOT,XMAT,YSJAVA,MMJAVA,WS,
     C            LLV,LLVB,LLE,LLEB,LERROR,
     C            ZS,JCH,JEH,JCL,JEL,IROTC,KCUT,LCUT,LJAVA,
     C            XJCH,XJEH,XJCL,XJEL)
      KT=KT+JCH+JCL+JEH+JEL
      KTT=KTT+JEH+JEL
      KTOTC=KT
      KCUTTER(IVOT)=KCUT
      LCUTTER(IVOT)=LCUT
C
C  END OF ROLL CALL LOOP
C
  600 CONTINUE
C
C
      XT=FLOAT(KT)
      XTT=FLOAT(KTT)
      AB=1.0-(XTT/XT)
      XSUM=FLOAT(MSUM)
      XSUM=XSUM/XT
      YSUM=AB-XSUM
      PRE1=YSUM/(1.0-XSUM)
C
C  WRITE OUT: ITERATION #; NUMBER OF VOTES; 
C             TOTAL CHOICES; TOTAL ERROR CHOICES; PERCENT CORRECT; 
C             PERCENT MAJORITY MODEL; PROPORTIONAL REDUCTION IN ERROR
C
C      WRITE(*,8250)KKK,NS,KTT,KT,1.0-AB,AB,PRE1,(KBIGPERM(KKK,J),J=1,N)
      LTEST(KKK)=KTT
      LORDER(KKK)=KKK
C
C      WRITE(22,100)KKK,(KBIGPERM(KKK,J),J=1,N)
      IF(MPERM(1).NE.0)GO TO 999
C
C  IF A BETTER SOLUTION IS FOUND RESET THE ORIGINAL ORDERING
C
      CALL RSORT(LTEST,KKK,LORDER)
      IF(LTEST(1).LE.KINITIAL)THEN
         DO 18 I=1,NP
         YSSS(I)=XMAT(I,1)
         LL(I)=L(I)
  18     CONTINUE
         DO 15 I=1,N
         LLSAVE(I)=LL(KBIGPERM(LORDER(1),I)+KBEFORE)
  15     CONTINUE
         DO 19 I=1,N
         LL(I+KBEFORE)=LLSAVE(I)
  19     CONTINUE
C         WRITE(*,103)IIJJ,KBEFORE,KINITIAL,LTEST(1),
C     C                LORDER(1),(KBIGPERM(LORDER(1),JJ),JJ=1,N)
         DO 17 I=1,NP
         XMAT(LL(I),1)=YSS(I)
C         WRITE(44,101)I,YSS(I),LL(I),L(I),MMSAVE(I,LORDER(1))
  17     CONTINUE
         DO 38 I=1,NP
C         WRITE(44,104)I,XMAT(I,1),YSSS(I)
C         YSS(I)=XMAT(I,1)
C         YSSS(I)=XMAT(I,1)
C         L(I)=I
C         LL(I)=I
  38     CONTINUE
C
C
C
C  RESET LERROR(. , .) AND KINITIAL
C
C
C         
C         CALL RSORT(YSS,NP,LL)
         KT=0
         KTT=0
         JCH=0
         JEH=0
         JCL=0
         JEL=0
         IVOT=0
C
C
C  ROLL CALL LOOP -- ESTIMATE BEST CUTTING POINT GIVEN FIXED LEGISLATOR
C                    CONFIGURATION
C
         DO 601 IIII=1,NV
         IVOT=IVOT+1
         KSUM=0
         LSUM=0
C
C  RECODE AND COMPUTE YES AND NO MEANS 
C
         DO 36 J=1,NP
         LV(J)=LDATA(J,IVOT)
         IF(LV(J).EQ.1.OR.LV(J).EQ.2.OR.LV(J).EQ.3)KK=1
         IF(LV(J).EQ.4.OR.LV(J).EQ.5.OR.LV(J).EQ.6)KK=6
         IF(LV(J).EQ.0.OR.LV(J).EQ.7.OR.LV(J).EQ.8.OR.LV(J).EQ.9)KK=9
         KKA(J)=KK
         IF(KK.EQ.1)KSUM=KSUM+1
         IF(KK.EQ.6)LSUM=LSUM+1
  36     CONTINUE
         DO 256 J=1,NP
  256    KA(J)=KKA(LL(J))
C
C  CHECK FOR UNANIMOUS VOTES AND DISCARD THEM
C
         IF(KSUM.EQ.0.OR.LSUM.EQ.0)GO TO 601
C
C  BEGIN JANICE LOOP FOR ROLL CALLS
C
C  GET ONE POINT RESULTS
C
         JCH=0
         JEH=0
         JCL=0
         JEL=0
         KJAVA=0
         DO 1664 JAVA=1,NP
         IF(KA(JAVA).NE.9)THEN
           KJAVA=KJAVA+1
           YSJAVA(KJAVA)=YSS(JAVA)
           MMJAVA(KJAVA)=KA(JAVA)
           LJAVA(KJAVA)=LL(JAVA)
         ENDIF
 1664    CONTINUE
         YSJAVA(KJAVA+1)=YSJAVA(KJAVA)+.01
         MMJAVA(KJAVA+1)=9
         LJAVA(KJAVA+1)=NP+1
         NV1=KJAVA+1
         IROTC=0
         CALL JAN1PT(NV1,NV,NS,IVOT,XMAT,YSJAVA,MMJAVA,
     C            WS,LLV,LLVB,LLE,LLEB,LERROR,
     C            ZS,JCH,JEH,JCL,JEL,IROTC,KCUT,LCUT,LJAVA,
     C            XJCH,XJEH,XJCL,XJEL)
         KT=KT+JCH+JCL+JEH+JEL
         KTT=KTT+JEH+JEL
         KTOTC=KT
         KCUTTER(IVOT)=KCUT
         LCUTTER(IVOT)=LCUT
C
C  END OF ROLL CALL LOOP
C
  601    CONTINUE
C
C
         XT=FLOAT(KT)
         XTT=FLOAT(KTT)
         AB=1.0-(XTT/XT)
         XSUM=FLOAT(MSUM)
         XSUM=XSUM/XT
         YSUM=AB-XSUM
         PRE1=YSUM/(1.0-XSUM)
C
C  WRITE OUT: ITERATION #; NUMBER OF VOTES; 
C             TOTAL CHOICES; TOTAL ERROR CHOICES; PERCENT CORRECT; 
C             PERCENT MAJORITY MODEL; PROPORTIONAL REDUCTION IN ERROR
C
C         IF(IPRINT.EQ.1)WRITE(23,8250)KKK,NS,KTT,KT,1.0-AB,AB,PRE1
         KKKCHK=0
         DO 90 IH=1,NRCALL
         DO 90 IG=1,NP
         KKKCHK=KKKCHK+LERROR(IG,IH)
  90     CONTINUE
C         IF(IPRINT.EQ.1)WRITE(23,8250)KKK,NS,KKKCHK
      ENDIF
C
C  IF A BETTER SOLUTION IS **NOT** FOUND RESET LERROR(. , .)
C
      IF(LTEST(1).GT.KINITIAL)THEN
         DO 70 J=1,NRCALL
         DO 70 I=1,NP
         LERROR(I,J)=MERROR(I,J)
  70     CONTINUE
C         WRITE(*,105)IIJJ,KBEFORE,KINITIAL
      ENDIF
C
      KBEFORE=KBEFORE+1
 9999 CONTINUE
C
      RETURN
      END
C  
C
C  ***********************************************************************
C     SUBROUTINE PERM---GENERATES THE SEQUENCE OF PERMUTATIONS OF N
C         DISTINGUISHABLE OBJECTS, ONE AT A TIME, IN LEXOGRAPHIC ORDER.
C  ***********************************************************************
C
C
      SUBROUTINE PERM(N,K,M)
      DIMENSION K(7),M(7)
C
C  AT FIRST CALL M(1)=0
C
C  K CONTAINS THE VECTOR THAT IS TO BE PERMUTED
C
      IF(M(1).NE.0)GO TO 2
      DO 1 I=2,N
  1   M(I)=1
  2   M(1)=M(1)+1
      IF(M(2).NE.1)GO TO 3
      M(2)=2
      J=K(1)
      K(1)=K(2)
      K(2)=J
      RETURN
  3   DO 4 I=2,N
      IF(M(I).NE.I)GO TO 5
  4   M(I)=1
      M(1)=0
      I=N
      GO TO 6
  5   L=M(I)
      J=K(L)
      K(L)=K(I)
      K(I)=J
      M(I)=L+1
      I=I-1
  6   L=1
  7   J=K(L)
      K(L)=K(I)
      K(I)=J
      L=L+1
      I=I-1
      IF(L.LT.I)GO TO 7
      RETURN
      END
