      SUBROUTINE CDRVSG( NSIZES, NN, NTYPES, DOTYPE, ISEED, THRESH,
     $                   NOUNIT, A, LDA, B, LDB, D, Z, LDZ, AB, BB, AP,
     $                   BP, WORK, NWORK, RWORK, LRWORK, IWORK, LIWORK,
     $                   RESULT, INFO )
*
*  -- LAPACK test routine (version 3.0) --
*     Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
*     Courant Institute, Argonne National Lab, and Rice University
*     September 30, 1994
*
**********************************************************************
*
*     modified August 1997, a new parameter LRWORK and LIWORK are
*     added in the calling sequence.
*
*     test routine CSGT01 is also modified
*
**********************************************************************
*
*     .. Scalar Arguments ..
      INTEGER            INFO, LDA, LDB, LDZ, LIWORK, LRWORK, NOUNIT,
     $                   NSIZES, NTYPES, NWORK
      REAL               THRESH
*     ..
*     .. Array Arguments ..
      LOGICAL            DOTYPE( * )
      INTEGER            ISEED( 4 ), IWORK( * ), NN( * )
      REAL               D( * ), RESULT( * ), RWORK( * )
      COMPLEX            A( LDA, * ), AB( LDA, * ), AP( * ),
     $                   B( LDB, * ), BB( LDB, * ), BP( * ), WORK( * ),
     $                   Z( LDZ, * )
*     ..
*
*  Purpose
*  =======
*
*       CDRVSG checks the complex Hermitian generalized eigenproblem
*       drivers.
*
*               CHEGV computes all eigenvalues and, optionally,
*               eigenvectors of a complex Hermitian-definite generalized
*               eigenproblem.
*
*               CHEGVD computes all eigenvalues and, optionally,
*               eigenvectors of a complex Hermitian-definite generalized
*               eigenproblem using a divide and conquer algorithm.
*
*               CHEGVX computes selected eigenvalues and, optionally,
*               eigenvectors of a complex Hermitian-definite generalized
*               eigenproblem.
*
*               CHPGV computes all eigenvalues and, optionally,
*               eigenvectors of a complex Hermitian-definite generalized
*               eigenproblem in packed storage.
*
*               CHPGVD computes all eigenvalues and, optionally,
*               eigenvectors of a complex Hermitian-definite generalized
*               eigenproblem in packed storage using a divide and
*               conquer algorithm.
*
*               CHPGVX computes selected eigenvalues and, optionally,
*               eigenvectors of a complex Hermitian-definite generalized
*               eigenproblem in packed storage.
*
*               CHBGV computes all eigenvalues and, optionally,
*               eigenvectors of a complex Hermitian-definite banded
*               generalized eigenproblem.
*
*               CHBGVD computes all eigenvalues and, optionally,
*               eigenvectors of a complex Hermitian-definite banded
*               generalized eigenproblem using a divide and conquer
*               algorithm.
*
*               CHBGVX computes selected eigenvalues and, optionally,
*               eigenvectors of a complex Hermitian-definite banded
*               generalized eigenproblem.
*
*       When CDRVSG is called, a number of matrix "sizes" ("n's") and a
*       number of matrix "types" are specified.  For each size ("n")
*       and each type of matrix, one matrix A of the given type will be
*       generated; a random well-conditioned matrix B is also generated
*       and the pair (A,B) is used to test the drivers.
*
*       For each pair (A,B), the following tests are performed:
*
*       (1) CHEGV with ITYPE = 1 and UPLO ='U':
*
*               | A Z - B Z D | / ( |A| |Z| n ulp )
*
*       (2) as (1) but calling CHPGV
*       (3) as (1) but calling CHBGV
*       (4) as (1) but with UPLO = 'L'
*       (5) as (4) but calling CHPGV
*       (6) as (4) but calling CHBGV
*
*       (7) CHEGV with ITYPE = 2 and UPLO ='U':
*
*               | A B Z - Z D | / ( |A| |Z| n ulp )
*
*       (8) as (7) but calling CHPGV
*       (9) as (7) but with UPLO = 'L'
*       (10) as (9) but calling CHPGV
*
*       (11) CHEGV with ITYPE = 3 and UPLO ='U':
*
*               | B A Z - Z D | / ( |A| |Z| n ulp )
*
*       (12) as (11) but calling CHPGV
*       (13) as (11) but with UPLO = 'L'
*       (14) as (13) but calling CHPGV
*
*       CHEGVD, CHPGVD and CHBGVD performed the same 14 tests.
*
*       CHEGVX, CHPGVX and CHBGVX performed the above 14 tests with
*       the parameter RANGE = 'A', 'N' and 'I', respectively.
*
*       The "sizes" are specified by an array NN(1:NSIZES); the value of
*       each element NN(j) specifies one size.
*       The "types" are specified by a logical array DOTYPE( 1:NTYPES );
*       if DOTYPE(j) is .TRUE., then matrix type "j" will be generated.
*       This type is used for the matrix A which has half-bandwidth KA.
*       B is generated as a well-conditioned positive definite matrix
*       with half-bandwidth KB (<= KA).
*       Currently, the list of possible types for A is:
*
*       (1)  The zero matrix.
*       (2)  The identity matrix.
*
*       (3)  A diagonal matrix with evenly spaced entries
*            1, ..., ULP  and random signs.
*            (ULP = (first number larger than 1) - 1 )
*       (4)  A diagonal matrix with geometrically spaced entries
*            1, ..., ULP  and random signs.
*       (5)  A diagonal matrix with "clustered" entries 1, ULP, ..., ULP
*            and random signs.
*
*       (6)  Same as (4), but multiplied by SQRT( overflow threshold )
*       (7)  Same as (4), but multiplied by SQRT( underflow threshold )
*
*       (8)  A matrix of the form  U* D U, where U is unitary and
*            D has evenly spaced entries 1, ..., ULP with random signs
*            on the diagonal.
*
*       (9)  A matrix of the form  U* D U, where U is unitary and
*            D has geometrically spaced entries 1, ..., ULP with random
*            signs on the diagonal.
*
*       (10) A matrix of the form  U* D U, where U is unitary and
*            D has "clustered" entries 1, ULP,..., ULP with random
*            signs on the diagonal.
*
*       (11) Same as (8), but multiplied by SQRT( overflow threshold )
*       (12) Same as (8), but multiplied by SQRT( underflow threshold )
*
*       (13) Hermitian matrix with random entries chosen from (-1,1).
*       (14) Same as (13), but multiplied by SQRT( overflow threshold )
*       (15) Same as (13), but multiplied by SQRT( underflow threshold )
*
*       (16) Same as (8), but with KA = 1 and KB = 1
*       (17) Same as (8), but with KA = 2 and KB = 1
*       (18) Same as (8), but with KA = 2 and KB = 2
*       (19) Same as (8), but with KA = 3 and KB = 1
*       (20) Same as (8), but with KA = 3 and KB = 2
*       (21) Same as (8), but with KA = 3 and KB = 3
*
*  Arguments
*  =========
*
*  NSIZES  INTEGER
*          The number of sizes of matrices to use.  If it is zero,
*          CDRVSG does nothing.  It must be at least zero.
*          Not modified.
*
*  NN      INTEGER array, dimension (NSIZES)
*          An array containing the sizes to be used for the matrices.
*          Zero values will be skipped.  The values must be at least
*          zero.
*          Not modified.
*
*  NTYPES  INTEGER
*          The number of elements in DOTYPE.   If it is zero, CDRVSG
*          does nothing.  It must be at least zero.  If it is MAXTYP+1
*          and NSIZES is 1, then an additional type, MAXTYP+1 is
*          defined, which is to use whatever matrix is in A.  This
*          is only useful if DOTYPE(1:MAXTYP) is .FALSE. and
*          DOTYPE(MAXTYP+1) is .TRUE. .
*          Not modified.
*
*  DOTYPE  LOGICAL array, dimension (NTYPES)
*          If DOTYPE(j) is .TRUE., then for each size in NN a
*          matrix of that size and of type j will be generated.
*          If NTYPES is smaller than the maximum number of types
*          defined (PARAMETER MAXTYP), then types NTYPES+1 through
*          MAXTYP will not be generated.  If NTYPES is larger
*          than MAXTYP, DOTYPE(MAXTYP+1) through DOTYPE(NTYPES)
*          will be ignored.
*          Not modified.
*
*  ISEED   INTEGER array, dimension (4)
*          On entry ISEED specifies the seed of the random number
*          generator. The array elements should be between 0 and 4095;
*          if not they will be reduced mod 4096.  Also, ISEED(4) must
*          be odd.  The random number generator uses a linear
*          congruential sequence limited to small integers, and so
*          should produce machine independent random numbers. The
*          values of ISEED are changed on exit, and can be used in the
*          next call to CDRVSG to continue the same random number
*          sequence.
*          Modified.
*
*  THRESH  REAL
*          A test will count as "failed" if the "error", computed as
*          described above, exceeds THRESH.  Note that the error
*          is scaled to be O(1), so THRESH should be a reasonably
*          small multiple of 1, e.g., 10 or 100.  In particular,
*          it should not depend on the precision (single vs. double)
*          or the size of the matrix.  It must be at least zero.
*          Not modified.
*
*  NOUNIT  INTEGER
*          The FORTRAN unit number for printing out error messages
*          (e.g., if a routine returns IINFO not equal to 0.)
*          Not modified.
*
*  A       COMPLEX array, dimension (LDA , max(NN))
*          Used to hold the matrix whose eigenvalues are to be
*          computed.  On exit, A contains the last matrix actually
*          used.
*          Modified.
*
*  LDA     INTEGER
*          The leading dimension of A.  It must be at
*          least 1 and at least max( NN ).
*          Not modified.
*
*  B       COMPLEX array, dimension (LDB , max(NN))
*          Used to hold the Hermitian positive definite matrix for
*          the generailzed problem.
*          On exit, B contains the last matrix actually
*          used.
*          Modified.
*
*  LDB     INTEGER
*          The leading dimension of B.  It must be at
*          least 1 and at least max( NN ).
*          Not modified.
*
*  D       REAL array, dimension (max(NN))
*          The eigenvalues of A. On exit, the eigenvalues in D
*          correspond with the matrix in A.
*          Modified.
*
*  Z       COMPLEX array, dimension (LDZ, max(NN))
*          The matrix of eigenvectors.
*          Modified.
*
*  LDZ     INTEGER
*          The leading dimension of ZZ.  It must be at least 1 and
*          at least max( NN ).
*          Not modified.
*
*  AB      COMPLEX array, dimension (LDA, max(NN))
*          Workspace.
*          Modified.
*
*  BB      COMPLEX array, dimension (LDB, max(NN))
*          Workspace.
*          Modified.
*
*  AP      COMPLEX array, dimension (max(NN)**2)
*          Workspace.
*          Modified.
*
*  BP      COMPLEX array, dimension (max(NN)**2)
*          Workspace.
*          Modified.
*
*  WORK    COMPLEX array, dimension (NWORK)
*          Workspace.
*          Modified.
*
*  NWORK   INTEGER
*          The number of entries in WORK.  This must be at least
*          2*N + N**2  where  N = max( NN(j), 2 ).
*          Not modified.
*
*  RWORK   REAL array, dimension (LRWORK)
*          Workspace.
*          Modified.
*
*  LRWORK  INTEGER
*          The number of entries in RWORK.  This must be at least
*          max( 7*N, 1 + 4*N + 2*N*lg(N) + 3*N**2 ) where
*          N = max( NN(j) ) and lg( N ) = smallest integer k such
*          that 2**k >= N .
*          Not modified.
*
*  IWORK   INTEGER array, dimension (LIWORK))
*          Workspace.
*          Modified.
*
*  LIWORK  INTEGER
*          The number of entries in IWORK.  This must be at least
*          2 + 5*max( NN(j) ).
*          Not modified.
*
*  RESULT  REAL array, dimension (70)
*          The values computed by the 70 tests described above.
*          Modified.
*
*  INFO    INTEGER
*          If 0, then everything ran OK.
*           -1: NSIZES < 0
*           -2: Some NN(j) < 0
*           -3: NTYPES < 0
*           -5: THRESH < 0
*           -9: LDA < 1 or LDA < NMAX, where NMAX is max( NN(j) ).
*          -16: LDZ < 1 or LDZ < NMAX.
*          -21: NWORK too small.
*          -23: LRWORK too small.
*          -25: LIWORK too small.
*          If  CLATMR, CLATMS, CHEGV, CHPGV, CHBGV, CHEGVD, CHPGVD,
*              CHPGVD, CHEGVX, CHPGVX, CHBGVX returns an error code,
*              the absolute value of it is returned.
*          Modified.
*
*-----------------------------------------------------------------------
*
*       Some Local Variables and Parameters:
*       ---- ----- --------- --- ----------
*       ZERO, ONE       Real 0 and 1.
*       MAXTYP          The number of types defined.
*       NTEST           The number of tests that have been run
*                       on this matrix.
*       NTESTT          The total number of tests for this call.
*       NMAX            Largest value in NN.
*       NMATS           The number of matrices generated so far.
*       NERRS           The number of tests which have exceeded THRESH
*                       so far (computed by SLAFTS).
*       COND, IMODE     Values to be passed to the matrix generators.
*       ANORM           Norm of A; passed to matrix generators.
*
*       OVFL, UNFL      Overflow and underflow thresholds.
*       ULP, ULPINV     Finest relative precision and its inverse.
*       RTOVFL, RTUNFL  Square roots of the previous 2 values.
*               The following four arrays decode JTYPE:
*       KTYPE(j)        The general type (1-10) for type "j".
*       KMODE(j)        The MODE value to be passed to the matrix
*                       generator for type "j".
*       KMAGN(j)        The order of magnitude ( O(1),
*                       O(overflow^(1/2) ), O(underflow^(1/2) )
*
*  =====================================================================
*
*     .. Parameters ..
      REAL               ZERO, ONE, TEN
      PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0, TEN = 10.0E+0 )
      COMPLEX            CZERO, CONE
      PARAMETER          ( CZERO = ( 0.0E+0, 0.0E+0 ),
     $                   CONE = ( 1.0E+0, 0.0E+0 ) )
      INTEGER            MAXTYP
      PARAMETER          ( MAXTYP = 21 )
*     ..
*     .. Local Scalars ..
      LOGICAL            BADNN
      CHARACTER          UPLO
      INTEGER            I, IBTYPE, IBUPLO, IINFO, IJ, IL, IMODE, ITEMP,
     $                   ITYPE, IU, J, JCOL, JSIZE, JTYPE, KA, KA9, KB,
     $                   KB9, M, MTYPES, N, NERRS, NMATS, NMAX, NTEST,
     $                   NTESTT
      REAL               ABSTOL, ANINV, ANORM, COND, OVFL, RTOVFL,
     $                   RTUNFL, ULP, ULPINV, UNFL, VL, VU
*     ..
*     .. Local Arrays ..
      INTEGER            IDUMMA( 1 ), IOLDSD( 4 ), ISEED2( 4 ),
     $                   KMAGN( MAXTYP ), KMODE( MAXTYP ),
     $                   KTYPE( MAXTYP )
*     ..
*     .. External Functions ..
      LOGICAL            LSAME
      REAL               SLAMCH, SLARND
      EXTERNAL           LSAME, SLAMCH, SLARND
*     ..
*     .. External Subroutines ..
      EXTERNAL           LA_TEST_CHBGV, LA_TEST_CHBGVD, LA_TEST_CHBGVX,
     &                   LA_TEST_CHEGV,
     &                   LA_TEST_CHEGVD, LA_TEST_CHEGVX, LA_TEST_CHPGV,
     &                   LA_TEST_CHPGVD, LA_TEST_CHPGVX,
     &                   CLACPY, CLASET, CLATMR,
     $                   CLATMS, CSGT01, SLABAD, SLAFTS, SLASUM, XERBLA
*     ..
*     .. Intrinsic Functions ..
      INTRINSIC          ABS, MAX, MIN, REAL, SQRT
*     ..
*     .. Data statements ..
      DATA               KTYPE / 1, 2, 5*4, 5*5, 3*8, 6*9 /
      DATA               KMAGN / 2*1, 1, 1, 1, 2, 3, 1, 1, 1, 2, 3, 1,
     $                   2, 3, 6*1 /
      DATA               KMODE / 2*0, 4, 3, 1, 4, 4, 4, 3, 1, 4, 4, 0,
     $                   0, 0, 6*4 /
*     ..
*     .. Executable Statements ..
*
*     1)      Check for errors
*
      NTESTT = 0
      INFO = 0
*
      BADNN = .FALSE.
      NMAX = 0
      DO 10 J = 1, NSIZES
         NMAX = MAX( NMAX, NN( J ) )
         IF( NN( J ).LT.0 )
     $      BADNN = .TRUE.
   10 CONTINUE
*
*     Check for errors
*
      IF( NSIZES.LT.0 ) THEN
         INFO = -1
      ELSE IF( BADNN ) THEN
         INFO = -2
      ELSE IF( NTYPES.LT.0 ) THEN
         INFO = -3
      ELSE IF( LDA.LE.1 .OR. LDA.LT.NMAX ) THEN
         INFO = -9
      ELSE IF( LDZ.LE.1 .OR. LDZ.LT.NMAX ) THEN
         INFO = -16
      ELSE IF( 2*MAX( NMAX, 2 )**2.GT.NWORK ) THEN
         INFO = -21
      ELSE IF( 2*MAX( NMAX, 2 )**2.GT.LRWORK ) THEN
         INFO = -23
      ELSE IF( 2*MAX( NMAX, 2 )**2.GT.LIWORK ) THEN
         INFO = -25
      END IF
*
      IF( INFO.NE.0 ) THEN
         CALL XERBLA( 'CDRVSG', -INFO )
         RETURN
      END IF
*
*     Quick return if possible
*
      IF( NSIZES.EQ.0 .OR. NTYPES.EQ.0 )
     $   RETURN
*
*     More Important constants
*
      UNFL = SLAMCH( 'Safe minimum' )
      OVFL = SLAMCH( 'Overflow' )
      CALL SLABAD( UNFL, OVFL )
      ULP = SLAMCH( 'Epsilon' )*SLAMCH( 'Base' )
      ULPINV = ONE / ULP
      RTUNFL = SQRT( UNFL )
      RTOVFL = SQRT( OVFL )
*
      DO 20 I = 1, 4
         ISEED2( I ) = ISEED( I )
   20 CONTINUE
*
*     Loop over sizes, types
*
      NERRS = 0
      NMATS = 0
*
      DO 650 JSIZE = 1, NSIZES
         N = NN( JSIZE )
         ANINV = ONE / REAL( MAX( 1, N ) )
*
         IF( NSIZES.NE.1 ) THEN
            MTYPES = MIN( MAXTYP, NTYPES )
         ELSE
            MTYPES = MIN( MAXTYP+1, NTYPES )
         END IF
*
         KA9 = 0
         KB9 = 0
         DO 640 JTYPE = 1, MTYPES
            IF( .NOT.DOTYPE( JTYPE ) )
     $         GO TO 640
            NMATS = NMATS + 1
            NTEST = 0
*
            DO 30 J = 1, 4
               IOLDSD( J ) = ISEED( J )
   30       CONTINUE
*
*           2)      Compute "A"
*
*                   Control parameters:
*
*               KMAGN  KMODE        KTYPE
*           =1  O(1)   clustered 1  zero
*           =2  large  clustered 2  identity
*           =3  small  exponential  (none)
*           =4         arithmetic   diagonal, w/ eigenvalues
*           =5         random log   hermitian, w/ eigenvalues
*           =6         random       (none)
*           =7                      random diagonal
*           =8                      random hermitian
*           =9                      banded, w/ eigenvalues
*
            IF( MTYPES.GT.MAXTYP )
     $         GO TO 90
*
            ITYPE = KTYPE( JTYPE )
            IMODE = KMODE( JTYPE )
*
*           Compute norm
*
            GO TO ( 40, 50, 60 )KMAGN( JTYPE )
*
   40       CONTINUE
            ANORM = ONE
            GO TO 70
*
   50       CONTINUE
            ANORM = ( RTOVFL*ULP )*ANINV
            GO TO 70
*
   60       CONTINUE
            ANORM = RTUNFL*N*ULPINV
            GO TO 70
*
   70       CONTINUE
*
            IINFO = 0
            COND = ULPINV
*
*           Special Matrices -- Identity & Jordan block
*
            IF( ITYPE.EQ.1 ) THEN
*
*              Zero
*
               KA = 0
               KB = 0
               CALL CLASET( 'Full', LDA, N, CZERO, CZERO, A, LDA )
*
            ELSE IF( ITYPE.EQ.2 ) THEN
*
*              Identity
*
               KA = 0
               KB = 0
               CALL CLASET( 'Full', LDA, N, CZERO, CZERO, A, LDA )
               DO 80 JCOL = 1, N
                  A( JCOL, JCOL ) = ANORM
   80          CONTINUE
*
            ELSE IF( ITYPE.EQ.4 ) THEN
*
*              Diagonal Matrix, [Eigen]values Specified
*
               KA = 0
               KB = 0
               CALL CLATMS( N, N, 'S', ISEED, 'H', RWORK, IMODE, COND,
     $                      ANORM, 0, 0, 'N', A, LDA, WORK, IINFO )
*
            ELSE IF( ITYPE.EQ.5 ) THEN
*
*              Hermitian, eigenvalues specified
*
               KA = MAX( 0, N-1 )
               KB = KA
               CALL CLATMS( N, N, 'S', ISEED, 'H', RWORK, IMODE, COND,
     $                      ANORM, N, N, 'N', A, LDA, WORK, IINFO )
*
            ELSE IF( ITYPE.EQ.7 ) THEN
*
*              Diagonal, random eigenvalues
*
               KA = 0
               KB = 0
               CALL CLATMR( N, N, 'S', ISEED, 'H', WORK, 6, ONE, CONE,
     $                      'T', 'N', WORK( N+1 ), 1, ONE,
     $                      WORK( 2*N+1 ), 1, ONE, 'N', IDUMMA, 0, 0,
     $                      ZERO, ANORM, 'NO', A, LDA, IWORK, IINFO )
*
            ELSE IF( ITYPE.EQ.8 ) THEN
*
*              Hermitian, random eigenvalues
*
               KA = MAX( 0, N-1 )
               KB = KA
               CALL CLATMR( N, N, 'S', ISEED, 'H', WORK, 6, ONE, CONE,
     $                      'T', 'N', WORK( N+1 ), 1, ONE,
     $                      WORK( 2*N+1 ), 1, ONE, 'N', IDUMMA, N, N,
     $                      ZERO, ANORM, 'NO', A, LDA, IWORK, IINFO )
*
            ELSE IF( ITYPE.EQ.9 ) THEN
*
*              Hermitian banded, eigenvalues specified
*
*              The following values are used for the half-bandwidths:
*
*                ka = 1   kb = 1
*                ka = 2   kb = 1
*                ka = 2   kb = 2
*                ka = 3   kb = 1
*                ka = 3   kb = 2
*                ka = 3   kb = 3
*
               KB9 = KB9 + 1
               IF( KB9.GT.KA9 ) THEN
                  KA9 = KA9 + 1
                  KB9 = 1
               END IF
               KA = MAX( 0, MIN( N-1, KA9 ) )
               KB = MAX( 0, MIN( N-1, KB9 ) )
               CALL CLATMS( N, N, 'S', ISEED, 'H', RWORK, IMODE, COND,
     $                      ANORM, KA, KA, 'N', A, LDA, WORK, IINFO )
*
            ELSE
*
               IINFO = 1
            END IF
*
            IF( IINFO.NE.0 ) THEN
               WRITE( NOUNIT, FMT = 9999 )'Generator', IINFO, N, JTYPE,
     $            IOLDSD
               INFO = ABS( IINFO )
               RETURN
            END IF
*
   90       CONTINUE
*
            ABSTOL = UNFL + UNFL
            IF( N.LE.1 ) THEN
               IL = 1
               IU = N
            ELSE
               IL = 1 + ( N-1 )*SLARND( 1, ISEED2 )
               IU = 1 + ( N-1 )*SLARND( 1, ISEED2 )
               IF( IL.GT.IU ) THEN
                  ITEMP = IL
                  IL = IU
                  IU = ITEMP
               END IF
            END IF
*
*           3) Call CHEGV, CHPGV, CHBGV, CHEGVD, CHPGVD, CHBGVD,
*              CHEGVX, CHPGVX and CHBGVX, do tests.
*
*           loop over the three generalized problems
*                 IBTYPE = 1: A*x = (lambda)*B*x
*                 IBTYPE = 2: A*B*x = (lambda)*x
*                 IBTYPE = 3: B*A*x = (lambda)*x
*
            DO 630 IBTYPE = 1, 3
*
*              loop over the setting UPLO
*
               DO 620 IBUPLO = 1, 2
                  IF( IBUPLO.EQ.1 )
     $               UPLO = 'U'
                  IF( IBUPLO.EQ.2 )
     $               UPLO = 'L'
*
*                 Generate random well-conditioned positive definite
*                 matrix B, of bandwidth not greater than that of A.
*
                  CALL CLATMS( N, N, 'U', ISEED, 'P', RWORK, 5, TEN,
     $                         ONE, KB, KB, UPLO, B, LDB, WORK( N+1 ),
     $                         IINFO )
*
*                 Test CHEGV
*
                  NTEST = NTEST + 1
*
                  CALL CLACPY( ' ', N, N, A, LDA, Z, LDZ )
                  CALL CLACPY( UPLO, N, N, B, LDB, BB, LDB )
*
                  IINFO = 0
                  CALL LA_TEST_CHEGV( IBTYPE, 'V', UPLO, N, Z, LDZ, BB,
     &              LDB, D,
     $                        WORK, NWORK, RWORK, IINFO )
                  IF( IINFO.NE.0 ) THEN
                     WRITE( NOUNIT, FMT = 9999 )'CHEGV(V,' // UPLO //
     $                  ')', IINFO, N, JTYPE, IOLDSD
                     INFO = ABS( IINFO )
                     IF( IINFO.LT.0 ) THEN
                        RETURN
                     ELSE
                        RESULT( NTEST ) = ULPINV
                        GO TO 100
                     END IF
                  END IF
*
*                 Do Test
*
                  CALL CSGT01( IBTYPE, UPLO, N, N, A, LDA, B, LDB, Z,
     $                         LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
*                 Test CHEGVD
*
                  NTEST = NTEST + 1
*
                  CALL CLACPY( ' ', N, N, A, LDA, Z, LDZ )
                  CALL CLACPY( UPLO, N, N, B, LDB, BB, LDB )
*
                  IINFO = 0
                  CALL LA_TEST_CHEGVD( IBTYPE, 'V', UPLO, N, Z, LDZ,
     &                         BB, LDB, D,
     $                         WORK, NWORK, RWORK, LRWORK, IWORK,
     $                         LIWORK, IINFO )
                  IF( IINFO.NE.0 ) THEN
                     WRITE( NOUNIT, FMT = 9999 )'CHEGVD(V,' // UPLO //
     $                  ')', IINFO, N, JTYPE, IOLDSD
                     INFO = ABS( IINFO )
                     IF( IINFO.LT.0 ) THEN
                        RETURN
                     ELSE
                        RESULT( NTEST ) = ULPINV
                        GO TO 100
                     END IF
                  END IF
*
*                 Do Test
*
                  CALL CSGT01( IBTYPE, UPLO, N, N, A, LDA, B, LDB, Z,
     $                         LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
*                 Test CHEGVX
*
                  NTEST = NTEST + 1
*
                  CALL CLACPY( ' ', N, N, A, LDA, AB, LDA )
                  CALL CLACPY( UPLO, N, N, B, LDB, BB, LDB )
*
                  IINFO = 0
                  CALL LA_TEST_CHEGVX( IBTYPE, 'V', 'A', UPLO, N,
     &                         AB, LDA, BB,
     $                         LDB, VL, VU, IL, IU, ABSTOL, M, D, Z,
     $                         LDZ, WORK, NWORK, RWORK, IWORK( N+1 ),
     $                         IWORK, IINFO )
                  IF( IINFO.NE.0 ) THEN
                     WRITE( NOUNIT, FMT = 9999 )'CHEGVX(V,A' // UPLO //
     $                  ')', IINFO, N, JTYPE, IOLDSD
                     INFO = ABS( IINFO )
                     IF( IINFO.LT.0 ) THEN
                        RETURN
                     ELSE
                        RESULT( NTEST ) = ULPINV
                        GO TO 100
                     END IF
                  END IF
*
*                 Do Test
*
                  CALL CSGT01( IBTYPE, UPLO, N, N, A, LDA, B, LDB, Z,
     $                         LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
                  NTEST = NTEST + 1
*
                  CALL CLACPY( ' ', N, N, A, LDA, AB, LDA )
                  CALL CLACPY( UPLO, N, N, B, LDB, BB, LDB )
*
*                 since we do not know the exact eigenvalues of this
*                 eigenpair, we just set VL and VU as constants.
*                 It is quite possible that there are no eigenvalues
*                 in this interval.
*
                  VL = ZERO
                  VU = ANORM
                  IINFO = 0
                  CALL LA_TEST_CHEGVX( IBTYPE, 'V', 'V', UPLO, N,
     &                         AB, LDA, BB,
     $                         LDB, VL, VU, IL, IU, ABSTOL, M, D, Z,
     $                         LDZ, WORK, NWORK, RWORK, IWORK( N+1 ),
     $                         IWORK, IINFO )
                  IF( IINFO.NE.0 ) THEN
                     WRITE( NOUNIT, FMT = 9999 )'CHEGVX(V,V,' //
     $                  UPLO // ')', IINFO, N, JTYPE, IOLDSD
                     INFO = ABS( IINFO )
                     IF( IINFO.LT.0 ) THEN
                        RETURN
                     ELSE
                        RESULT( NTEST ) = ULPINV
                        GO TO 100
                     END IF
                  END IF
*
*                 Do Test
*
                  CALL CSGT01( IBTYPE, UPLO, N, M, A, LDA, B, LDB, Z,
     $                         LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
                  NTEST = NTEST + 1
*
                  CALL CLACPY( ' ', N, N, A, LDA, AB, LDA )
                  CALL CLACPY( UPLO, N, N, B, LDB, BB, LDB )
*
                  IINFO = 0
                  CALL LA_TEST_CHEGVX( IBTYPE, 'V', 'I', UPLO, N, AB,
     &                         LDA, BB,
     $                         LDB, VL, VU, IL, IU, ABSTOL, M, D, Z,
     $                         LDZ, WORK, NWORK, RWORK, IWORK( N+1 ),
     $                         IWORK, IINFO )
                  IF( IINFO.NE.0 ) THEN
                     WRITE( NOUNIT, FMT = 9999 )'CHEGVX(V,I,' //
     $                  UPLO // ')', IINFO, N, JTYPE, IOLDSD
                     INFO = ABS( IINFO )
                     IF( IINFO.LT.0 ) THEN
                        RETURN
                     ELSE
                        RESULT( NTEST ) = ULPINV
                        GO TO 100
                     END IF
                  END IF
*
*                 Do Test
*
                  CALL CSGT01( IBTYPE, UPLO, N, M, A, LDA, B, LDB, Z,
     $                         LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
  100             CONTINUE
*
*                 Test CHPGV
*
                  NTEST = NTEST + 1
*
*                 Copy the matrices into packed storage.
*
                  IF( LSAME( UPLO, 'U' ) ) THEN
                     IJ = 1
                     DO 120 J = 1, N
                        DO 110 I = 1, J
                           AP( IJ ) = A( I, J )
                           BP( IJ ) = B( I, J )
                           IJ = IJ + 1
  110                   CONTINUE
  120                CONTINUE
                  ELSE
                     IJ = 1
                     DO 140 J = 1, N
                        DO 130 I = J, N
                           AP( IJ ) = A( I, J )
                           BP( IJ ) = B( I, J )
                           IJ = IJ + 1
  130                   CONTINUE
  140                CONTINUE
                  END IF
*
                  IINFO = 0
                  CALL LA_TEST_CHPGV( IBTYPE, 'V', UPLO, N, AP, BP, D,
     &              Z, LDZ,
     $                        WORK, RWORK, IINFO )
                  IF( IINFO.NE.0 ) THEN
                     WRITE( NOUNIT, FMT = 9999 )'CHPGV(V,' // UPLO //
     $                  ')', IINFO, N, JTYPE, IOLDSD
                     INFO = ABS( IINFO )
                     IF( IINFO.LT.0 ) THEN
                        RETURN
                     ELSE
                        RESULT( NTEST ) = ULPINV
                        GO TO 310
                     END IF
                  END IF
*
*                 Do Test
*
                  CALL CSGT01( IBTYPE, UPLO, N, N, A, LDA, B, LDB, Z,
     $                         LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
*                 Test CHPGVD
*
                  NTEST = NTEST + 1
*
*                 Copy the matrices into packed storage.
*
                  IF( LSAME( UPLO, 'U' ) ) THEN
                     IJ = 1
                     DO 160 J = 1, N
                        DO 150 I = 1, J
                           AP( IJ ) = A( I, J )
                           BP( IJ ) = B( I, J )
                           IJ = IJ + 1
  150                   CONTINUE
  160                CONTINUE
                  ELSE
                     IJ = 1
                     DO 180 J = 1, N
                        DO 170 I = J, N
                           AP( IJ ) = A( I, J )
                           BP( IJ ) = B( I, J )
                           IJ = IJ + 1
  170                   CONTINUE
  180                CONTINUE
                  END IF
*
                  IINFO = 0
                  CALL LA_TEST_CHPGVD( IBTYPE, 'V', UPLO, N, AP, BP,
     &                         D, Z, LDZ,
     $                         WORK, NWORK, RWORK, LRWORK, IWORK,
     $                         LIWORK, IINFO )
                  IF( IINFO.NE.0 ) THEN
                     WRITE( NOUNIT, FMT = 9999 )'CHPGVD(V,' // UPLO //
     $                  ')', IINFO, N, JTYPE, IOLDSD
                     INFO = ABS( IINFO )
                     IF( IINFO.LT.0 ) THEN
                        RETURN
                     ELSE
                        RESULT( NTEST ) = ULPINV
                        GO TO 310
                     END IF
                  END IF
*
*                 Do Test
*
                  CALL CSGT01( IBTYPE, UPLO, N, N, A, LDA, B, LDB, Z,
     $                         LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
*                 Test CHPGVX
*
                  NTEST = NTEST + 1
*
*                 Copy the matrices into packed storage.
*
                  IF( LSAME( UPLO, 'U' ) ) THEN
                     IJ = 1
                     DO 200 J = 1, N
                        DO 190 I = 1, J
                           AP( IJ ) = A( I, J )
                           BP( IJ ) = B( I, J )
                           IJ = IJ + 1
  190                   CONTINUE
  200                CONTINUE
                  ELSE
                     IJ = 1
                     DO 220 J = 1, N
                        DO 210 I = J, N
                           AP( IJ ) = A( I, J )
                           BP( IJ ) = B( I, J )
                           IJ = IJ + 1
  210                   CONTINUE
  220                CONTINUE
                  END IF
*
                  INFO = 0
                  CALL LA_TEST_CHPGVX( IBTYPE, 'V', 'A', UPLO, N, AP,
     &                         BP, VL,
     $                         VU, IL, IU, ABSTOL, M, D, Z, LDZ, WORK,
     $                         RWORK, IWORK( N+1 ), IWORK, INFO )
                  IF( IINFO.NE.0 ) THEN
                     WRITE( NOUNIT, FMT = 9999 )'CHPGVX(V,A' // UPLO //
     $                  ')', IINFO, N, JTYPE, IOLDSD
                     INFO = ABS( IINFO )
                     IF( IINFO.LT.0 ) THEN
                        RETURN
                     ELSE
                        RESULT( NTEST ) = ULPINV
                        GO TO 310
                     END IF
                  END IF
*
*                 Do Test
*
                  CALL CSGT01( IBTYPE, UPLO, N, N, A, LDA, B, LDB, Z,
     $                         LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
                  NTEST = NTEST + 1
*
*                 Copy the matrices into packed storage.
*
                  IF( LSAME( UPLO, 'U' ) ) THEN
                     IJ = 1
                     DO 240 J = 1, N
                        DO 230 I = 1, J
                           AP( IJ ) = A( I, J )
                           BP( IJ ) = B( I, J )
                           IJ = IJ + 1
  230                   CONTINUE
  240                CONTINUE
                  ELSE
                     IJ = 1
                     DO 260 J = 1, N
                        DO 250 I = J, N
                           AP( IJ ) = A( I, J )
                           BP( IJ ) = B( I, J )
                           IJ = IJ + 1
  250                   CONTINUE
  260                CONTINUE
                  END IF
*
                  VL = ZERO
                  VU = ANORM
                  INFO = 0
                  CALL LA_TEST_CHPGVX( IBTYPE, 'V', 'V', UPLO, N, AP,
     &                         BP, VL,
     $                         VU, IL, IU, ABSTOL, M, D, Z, LDZ, WORK,
     $                         RWORK, IWORK( N+1 ), IWORK, INFO )
                  IF( IINFO.NE.0 ) THEN
                     WRITE( NOUNIT, FMT = 9999 )'CHPGVX(V,V' // UPLO //
     $                  ')', IINFO, N, JTYPE, IOLDSD
                     INFO = ABS( IINFO )
                     IF( IINFO.LT.0 ) THEN
                        RETURN
                     ELSE
                        RESULT( NTEST ) = ULPINV
                        GO TO 310
                     END IF
                  END IF
*
*                 Do Test
*
                  CALL CSGT01( IBTYPE, UPLO, N, M, A, LDA, B, LDB, Z,
     $                         LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
                  NTEST = NTEST + 1
*
*                 Copy the matrices into packed storage.
*
                  IF( LSAME( UPLO, 'U' ) ) THEN
                     IJ = 1
                     DO 280 J = 1, N
                        DO 270 I = 1, J
                           AP( IJ ) = A( I, J )
                           BP( IJ ) = B( I, J )
                           IJ = IJ + 1
  270                   CONTINUE
  280                CONTINUE
                  ELSE
                     IJ = 1
                     DO 300 J = 1, N
                        DO 290 I = J, N
                           AP( IJ ) = A( I, J )
                           BP( IJ ) = B( I, J )
                           IJ = IJ + 1
  290                   CONTINUE
  300                CONTINUE
                  END IF
*
                  INFO = 0
                  CALL LA_TEST_CHPGVX( IBTYPE, 'V', 'I', UPLO, N, AP,
     &                         BP, VL,
     $                         VU, IL, IU, ABSTOL, M, D, Z, LDZ, WORK,
     $                         RWORK, IWORK( N+1 ), IWORK, INFO )
                  IF( IINFO.NE.0 ) THEN
                     WRITE( NOUNIT, FMT = 9999 )'CHPGVX(V,I' // UPLO //
     $                  ')', IINFO, N, JTYPE, IOLDSD
                     INFO = ABS( IINFO )
                     IF( IINFO.LT.0 ) THEN
                        RETURN
                     ELSE
                        RESULT( NTEST ) = ULPINV
                        GO TO 310
                     END IF
                  END IF
*
*                 Do Test
*
                  CALL CSGT01( IBTYPE, UPLO, N, M, A, LDA, B, LDB, Z,
     $                         LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
  310             CONTINUE
*
                  IF( IBTYPE.EQ.1 ) THEN
*
*                    TEST CHBGV
*
                     NTEST = NTEST + 1
*
*                    Copy the matrices into band storage.
*
                     IF( LSAME( UPLO, 'U' ) ) THEN
                        DO 340 J = 1, N
                           DO 320 I = MAX( 1, J-KA ), J
                              AB( KA+1+I-J, J ) = A( I, J )
  320                      CONTINUE
                           DO 330 I = MAX( 1, J-KB ), J
                              BB( KB+1+I-J, J ) = B( I, J )
  330                      CONTINUE
  340                   CONTINUE
                     ELSE
                        DO 370 J = 1, N
                           DO 350 I = J, MIN( N, J+KA )
                              AB( 1+I-J, J ) = A( I, J )
  350                      CONTINUE
                           DO 360 I = J, MIN( N, J+KB )
                              BB( 1+I-J, J ) = B( I, J )
  360                      CONTINUE
  370                   CONTINUE
                     END IF
*
                     IINFO = 0
                     CALL LA_TEST_CHBGV( 'V', UPLO, N, KA, KB, AB, LDA,
     &                 BB, LDB,
     $                           D, Z, LDZ, WORK, RWORK, IINFO )
                     IF( IINFO.NE.0 ) THEN
                        WRITE( NOUNIT, FMT = 9999 )'CHBGV(V,' //
     $                     UPLO // ')', IINFO, N, JTYPE, IOLDSD
                        INFO = ABS( IINFO )
                        IF( IINFO.LT.0 ) THEN
                           RETURN
                        ELSE
                           RESULT( NTEST ) = ULPINV
                           GO TO 620
                        END IF
                     END IF
*
*                    Do Test
*
                     CALL CSGT01( IBTYPE, UPLO, N, N, A, LDA, B, LDB, Z,
     $                            LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
*                    TEST CHBGVD
*
                     NTEST = NTEST + 1
*
*                    Copy the matrices into band storage.
*
                     IF( LSAME( UPLO, 'U' ) ) THEN
                        DO 400 J = 1, N
                           DO 380 I = MAX( 1, J-KA ), J
                              AB( KA+1+I-J, J ) = A( I, J )
  380                      CONTINUE
                           DO 390 I = MAX( 1, J-KB ), J
                              BB( KB+1+I-J, J ) = B( I, J )
  390                      CONTINUE
  400                   CONTINUE
                     ELSE
                        DO 430 J = 1, N
                           DO 410 I = J, MIN( N, J+KA )
                              AB( 1+I-J, J ) = A( I, J )
  410                      CONTINUE
                           DO 420 I = J, MIN( N, J+KB )
                              BB( 1+I-J, J ) = B( I, J )
  420                      CONTINUE
  430                   CONTINUE
                     END IF
*
                     IINFO = 0
                     CALL LA_TEST_CHBGVD( 'V', UPLO, N, KA, KB, AB,
     &                            LDA, BB,
     $                            LDB, D, Z, LDZ, WORK, NWORK, RWORK,
     $                            LRWORK, IWORK, LIWORK, IINFO )
                     IF( IINFO.NE.0 ) THEN
                        WRITE( NOUNIT, FMT = 9999 )'CHBGVD(V,' //
     $                     UPLO // ')', IINFO, N, JTYPE, IOLDSD
                        INFO = ABS( IINFO )
                        IF( IINFO.LT.0 ) THEN
                           RETURN
                        ELSE
                           RESULT( NTEST ) = ULPINV
                           GO TO 620
                        END IF
                     END IF
*
*                    Do Test
*
                     CALL CSGT01( IBTYPE, UPLO, N, N, A, LDA, B, LDB, Z,
     $                            LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
*                    Test CHBGVX
*
                     NTEST = NTEST + 1
*
*                    Copy the matrices into band storage.
*
                     IF( LSAME( UPLO, 'U' ) ) THEN
                        DO 460 J = 1, N
                           DO 440 I = MAX( 1, J-KA ), J
                              AB( KA+1+I-J, J ) = A( I, J )
  440                      CONTINUE
                           DO 450 I = MAX( 1, J-KB ), J
                              BB( KB+1+I-J, J ) = B( I, J )
  450                      CONTINUE
  460                   CONTINUE
                     ELSE
                        DO 490 J = 1, N
                           DO 470 I = J, MIN( N, J+KA )
                              AB( 1+I-J, J ) = A( I, J )
  470                      CONTINUE
                           DO 480 I = J, MIN( N, J+KB )
                              BB( 1+I-J, J ) = B( I, J )
  480                      CONTINUE
  490                   CONTINUE
                     END IF
*
                     IINFO = 0
                     CALL LA_TEST_CHBGVX( 'V', 'A', UPLO, N, KA, KB,
     &                            AB, LDA,
     $                            BB, LDB, BP, MAX( 1, N ), VL, VU, IL,
     $                            IU, ABSTOL, M, D, Z, LDZ, WORK, RWORK,
     $                            IWORK( N+1 ), IWORK, IINFO )
                     IF( IINFO.NE.0 ) THEN
                        WRITE( NOUNIT, FMT = 9999 )'CHBGVX(V,A' //
     $                     UPLO // ')', IINFO, N, JTYPE, IOLDSD
                        INFO = ABS( IINFO )
                        IF( IINFO.LT.0 ) THEN
                           RETURN
                        ELSE
                           RESULT( NTEST ) = ULPINV
                           GO TO 620
                        END IF
                     END IF
*
*                    Do Test
*
                     CALL CSGT01( IBTYPE, UPLO, N, N, A, LDA, B, LDB, Z,
     $                            LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
                     NTEST = NTEST + 1
*
*                    Copy the matrices into band storage.
*
                     IF( LSAME( UPLO, 'U' ) ) THEN
                        DO 520 J = 1, N
                           DO 500 I = MAX( 1, J-KA ), J
                              AB( KA+1+I-J, J ) = A( I, J )
  500                      CONTINUE
                           DO 510 I = MAX( 1, J-KB ), J
                              BB( KB+1+I-J, J ) = B( I, J )
  510                      CONTINUE
  520                   CONTINUE
                     ELSE
                        DO 550 J = 1, N
                           DO 530 I = J, MIN( N, J+KA )
                              AB( 1+I-J, J ) = A( I, J )
  530                      CONTINUE
                           DO 540 I = J, MIN( N, J+KB )
                              BB( 1+I-J, J ) = B( I, J )
  540                      CONTINUE
  550                   CONTINUE
                     END IF
*
                     VL = ZERO
                     VU = ANORM
                     IINFO = 0
                     CALL LA_TEST_CHBGVX( 'V', 'V', UPLO, N, KA, KB,
     &                            AB, LDA,
     $                            BB, LDB, BP, MAX( 1, N ), VL, VU, IL,
     $                            IU, ABSTOL, M, D, Z, LDZ, WORK, RWORK,
     $                            IWORK( N+1 ), IWORK, IINFO )
                     IF( IINFO.NE.0 ) THEN
                        WRITE( NOUNIT, FMT = 9999 )'CHBGVX(V,V' //
     $                     UPLO // ')', IINFO, N, JTYPE, IOLDSD
                        INFO = ABS( IINFO )
                        IF( IINFO.LT.0 ) THEN
                           RETURN
                        ELSE
                           RESULT( NTEST ) = ULPINV
                           GO TO 620
                        END IF
                     END IF
*
*                    Do Test
*
                     CALL CSGT01( IBTYPE, UPLO, N, M, A, LDA, B, LDB, Z,
     $                            LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
                     NTEST = NTEST + 1
*
*                    Copy the matrices into band storage.
*
                     IF( LSAME( UPLO, 'U' ) ) THEN
                        DO 580 J = 1, N
                           DO 560 I = MAX( 1, J-KA ), J
                              AB( KA+1+I-J, J ) = A( I, J )
  560                      CONTINUE
                           DO 570 I = MAX( 1, J-KB ), J
                              BB( KB+1+I-J, J ) = B( I, J )
  570                      CONTINUE
  580                   CONTINUE
                     ELSE
                        DO 610 J = 1, N
                           DO 590 I = J, MIN( N, J+KA )
                              AB( 1+I-J, J ) = A( I, J )
  590                      CONTINUE
                           DO 600 I = J, MIN( N, J+KB )
                              BB( 1+I-J, J ) = B( I, J )
  600                      CONTINUE
  610                   CONTINUE
                     END IF
*
                     IINFO = 0
                     CALL LA_TEST_CHBGVX( 'V', 'I', UPLO, N, KA, KB,
     &                            AB, LDA,
     $                            BB, LDB, BP, MAX( 1, N ), VL, VU, IL,
     $                            IU, ABSTOL, M, D, Z, LDZ, WORK, RWORK,
     $                            IWORK( N+1 ), IWORK, IINFO )
                     IF( IINFO.NE.0 ) THEN
                        WRITE( NOUNIT, FMT = 9999 )'CHBGVX(V,I' //
     $                     UPLO // ')', IINFO, N, JTYPE, IOLDSD
                        INFO = ABS( IINFO )
                        IF( IINFO.LT.0 ) THEN
                           RETURN
                        ELSE
                           RESULT( NTEST ) = ULPINV
                           GO TO 620
                        END IF
                     END IF
*
*                    Do Test
*
                     CALL CSGT01( IBTYPE, UPLO, N, M, A, LDA, B, LDB, Z,
     $                            LDZ, D, WORK, RWORK, RESULT( NTEST ) )
*
                  END IF
*
  620          CONTINUE
  630       CONTINUE
*
*           End of Loop -- Check for RESULT(j) > THRESH
*
            NTESTT = NTESTT + NTEST
            CALL SLAFTS( 'CSG', N, N, JTYPE, NTEST, RESULT, IOLDSD,
     $                   THRESH, NOUNIT, NERRS )
  640    CONTINUE
  650 CONTINUE
*
*     Summary
*
      CALL SLASUM( 'CSG', NOUNIT, NERRS, NTESTT )
*
      RETURN
*
 9999 FORMAT( ' CDRVSG: ', A, ' returned INFO=', I6, '.', / 9X, 'N=',
     $      I6, ', JTYPE=', I6, ', ISEED=(', 3( I5, ',' ), I5, ')' )
*
*     End of CDRVSG
*
      END


program main

   implicit none

   integer :: i, j
   real :: s1
   real, dimension(10) :: a10, b10
   real, dimension(4) :: x4 = [1., 2., 3., 4.]  ! Fortran 2003
   real, dimension(5) :: x5 = (/1., 2., 3., 4., 5./) ! Old Fortran way
   real, dimension(4) :: y4, z4
   real, dimension(8) :: z8
   real, allocatable :: xx(:)
   real, dimension(:, :), allocatable :: yy

   s1 = 1.25

   y4 = [-1., 0., 1., 2.]
   z4(1:4) = [(sqrt(real(i)), i=1, 4)]
   z8(1:7:2) = [(sqrt(real(i)), i=1, 4)]
   z8(2:8:2) = [(sqrt(real(i)), i=1, 4)]

   print *, 'x4: ', x4
   print *, 'y4: ', y4
   print *, 'z4: ', z4
   print *, 'z8: ', z8

   b10(1:9:2) = [(sin(real(i)), i=1, 5)]
   b10(2:10:2) = [(cos(real(i)), i=1, 5)]

   print *, 'b10: ', b10

   a10 = b10

   print *, 'a10: ', a10

   a10(1:10) = b10(1:10)
   a10(2:3) = b10(4:5)
   a10 = s1
   a10(1:3) = b10(1:5:2) ! a10(1) = b10(1)
   !                     ! a10(2) = b10(3)
   !                     ! a10(3) = b10(5)

   print *, 'a10: ', a10
   print *, 'b10: ', b10

end program


      SUBROUTINE DINTM (NDIMI,ANSWER,WORK,NWORK,IOPT)
c Copyright (c) 1996 California Institute of Technology, Pasadena, CA.
c ALL RIGHTS RESERVED.
c Based on Government Sponsored Research NAS7-03001.
C>> 2009-11-03 DINTM  Krogh  Initialized lots of variables.
C>> 1996-03-31 DINTM  Krogh  Removed unused variable in common.
c>> 1995-11-20 DINTM  Krogh  Converted from SFTRAN to Fortran 77.
C>> 1994-11-14 DINTM  Krogh  Declared all vars.
c>> 1994-10-19 DINTM  Krogh  Changes to use M77CON
c>> 1994-07-07 DINTM  Snyder set up for CHGTYP.
C>> 1993-05-18 DINTM  Krogh -- Changed "END" to "END PROGRAM"
C>> 1994-05-02 DINTM  Snyder corrected some comments
C>> 1991-09-20 DINTM  Krogh converted '(1)' dimensioning to '(*)'.
C>> 1987-11-19 DINTM  Snyder  Initial code.
c
c--D replaces "?": ?INTA,?intc,?intec,?INTF,?INTM,?INTMA,?INTNC,?INTOP
C
C     ******************************************************************
C
C     THIS SUBROUTINE ATTEMPTS TO CALCULATE THE INTEGRAL OF A FUNCTION
C     PROVIDED BY THE USER.  THE FUNCTION IS PROVIDED BY THE USER VIA A
C     SUBROUTINE REFERENCED BY CALL DINTF(F,X,IFLAG) OR BY REVERSE
C     COMMUNICATION.  ALL ABSCISSAE ARE WITHIN THE REGION OF
C     INTEGRATION.
C
C     THE RESULT IS OBTAINED USING QUADRATURE FORMULAE DUE TO
C     T. N. L. PATTERSON, MATHEMATICS OF COMPUTATION, VOLUME 22,
C     PAGES 847-856, 1968.
C
C     *****    WARNING   ***********************************************
C
C     THE RELIABILITY AND EFFICIENCY OF THIS PROGRAM ARE STRONGLY
C     INFLUENCED BY DISCONTINUITIES IN THE FUNCTION OR IT'S
C     DERIVATIVES, INTEGRABLE SINGULARITIES IN THE REGION OF
C     INTEGRATION, AND NON-INTEGRABLE SINGULARITIES NEAR THIS REGION.
C     (INCLUDING COMPLEX VALUES).  THE EFFICIENCY AND RELIABILITY
C     OF INTEGRATING SUCH FUNCTIONS MAY BE GREATLY IMPROVED BY MANUALLY
C     SUBDIVIDING THE REGION OF INTEGRATION AT THE DISCONTINUITY,
C     SINGULARITY OR CLOSEST POINT TO A POLE, AND SUMMING THE ANSWERS.
C     A CHANGE OF VARIABLE TO ELIMINATE OR REDUCE THE STRENGTH OF THE
C     SINGULARITY WILL SIGNIFICANTLY IMPROVE PERFORMANCE.
C
C     *****    FORMAL ARGUMENTS    *************************************
C
C NDIMI   IS THE NUMBER OF DIMENSIONS OF INTEGRATION.
C ANSWER  IS THE ESTIMATE OF THE INTEGRAL.  WHEN REVERSE COMMUNICATION
C         IS SPECIFIED IT IS USED TO PASS FUNCTION VALUES FROM
C         THE USER PROGRAM TO DINTA OR DINTMA.
      DOUBLE PRECISION ANSWER
C WORK    CONTAINS THE LIMITS, WORKING SPACE AND MAY BE REFERENCED BY
C         THE OPTION VECTOR (SEE IOPT BELOW).  WHEN THE INTEGRATION
C         IS COMPLETE, WORK(1) CONTAINS THE ESTIMATED ABSOLUTE ERROR.
C         WHEN REVERSE COMMUNICATION IS SPECIFIED, WORK(1) IS USED TO
C         PASS ABSCISSAE FROM DINTA OR DINTMA TO THE USER PROGRAM.
C         WORK(1), ..., WORK(NDIMI) ARE COMPONENTS OF THE VECTOR OF
C         ABSCISSAE.  WORK(NDIMI+1), ..., WORK(2*NDIMI) ARE LOWER
C         LIMITS.  WORK(2*NDIMI+1), ..., WORK(3*NDIMI) ARE UPPER LIMITS.
C         THE ABSCISSAE AND LIMITS ARE STORED INNERMOST FIRST.  DINTF
C         IS CALLED TO REQUEST THE LIMITS OF EVERY DIMENSION, BUT
C         CONSTANT LIMITS (THE LIMITS OF THE OUTER DIMENSION ARE ALWAYS
C         CONSTANT) MAY INSTEAD BE STORED BEFORE INTEGRATION BEGINS.
C         WORK(3*NDIMI+1), ..., WORK(3*NDIMI+KWORK*(NDIMI-1)) ARE
C         WORKING STORAGE, WHERE KWORK DEPENDS ON THE MACHINE AND THE
C         PRECISION OF THE PROGRAM VERSION.  KWORK MUST SPECIFY ENOUGH
C         STORAGE FOR 4 VARIABLES WHICH ARE ALWAYS DOUBLE PRECISION,
C         139 VARIABLES WHICH ARE DOUBLE PRECISION IN THE DOUBLE
C         PRECISION VERSION, 30 VARIABLES WHICH ARE DOUBLE PRECISION
C         IN THE DOUBLE PRECISION PROGRAM IF THE LARGEST REPRESENTABLE
C         NUMBERS IN SINGLE AND DOUBLE PRECISION ARE DIFFERENT BY MORE
C         THAN A FACTOR OF 4, 29 INTEGER VARIABLES AND 11 LOGICAL
C         VARIABLES.  REMEMBER TO CONSIDER THE DATA TYPE OF WORK WHEN
C         SPECIFYING THE VALUE OF KWORK.  EXAMPLES ARE
C-- Begin mask code changes
C           IF PRECISION IS REAL, AND INTEGER AND LOGICAL VARIABLES
C           OCCUPY THE SAME SPACE AS REAL VARIABLES, KWORK=217.
C           IF PRECISION IS REAL, AND INTEGER AND LOGICAL VARIABLES
C           OCCUPY HALF AS MUCH SPACE AS REAL VARIABLES, KWORK=?
C           IF PRECISION IS PARTLY DOUBLE, AND INTEGER AND LOGICAL
C           VARIABLES OCCUPY THE SAME SPACE AS REAL VARIABLES,
C           KWORK=?
C           IF PRECISION IS PARTLY DOUBLE, AND INTEGER AND LOGICAL
C           VARIABLES OCCUPY HALF AS MUCH SPACE AS REAL VARIABLES,
C           KWORK=?
C           IF PRECISION IS DOUBLE, AND INTEGER AND LOGICAL VARIABLES
C           OCCUPY THE SAME SPACE AS REAL VARIABLES, KWORK=193.
C           IF PRECISION IS DOUBLE, AND INTEGER AND LOGICAL VARIABLES
C           OCCUPY HALF AS MUCH SPACE AS REAL VARIABLES, KWORK=?
C           FOR PORTABILITY, ASSUME THE WORST CASE.  THAT IS, IF THE
C           PRECISION IS SINGLE, KWORK=217.  IF THE PRECISION IS DOUBLE,
C           KWORK=193.
C         WORK(3*NDIMI+KWORK*(NDIMI-1)+1), ... ARE AVAILABLE FOR
C         REFERENCE THROUGH THE OPTION VECTOR.
C-- End mask code changes
      DOUBLE PRECISION WORK(*)
C NWORK   IS THE NUMBER OF ELEMENTS ALLOCATED FOR WORK.
C         NWORK MUST BE GREATER THAN 3*NDIMI+KWORK*(NDIMI-1).
C IOPT    IS A VECTOR OF INTEGERS USED TO RETURN THE STATUS AND TO
C         SPECIFY OPTIONS.
      INTEGER IOPT(*)
C         IOPT(1) IS USED TO RETURN A STATUS INDICATOR TO THE USER.
C         VALUES OF THIS INDICATOR ARE
C         -NDIM   - NORMAL TERMINATION, EITHER THE ABSOLUTE OR THE
C                   RELATIVE ERROR CRITERIA ARE SATISFIED.
C         -NDIM-1 - NORMAL TERMINATION, NEITHER THE ABSOLUTE NOR THE
C                   RELATIVE ERROR CRITERIA ARE SATISFIED, BUT THE
C                   ERROR RELATIVE TO THE OBTAINABLE PRECISION
C                   PRECISION CRITERION IS SATISFIED.
C         -NDIM-2 - NORMAL TERMINATION, BUT NONE OF THE ERROR CRITERIA
C                   ARE SATISFIED.
C         -NDIM-3 - NOT ENOUGH SPACE IN WORK, NWORK IS TOO SMALL.
C         -NDIM-4 - BAD IOPT VALUE.
C         -NDIM-5 - TOO MANY FUNCTION VALUES NEEDED.
C         -NDIM-KDIM-5 - APPARENT NON-INTEGRABLE SINGULARITY IN
C                   DIMENSION KDIM.  ANSWER CONTAINS THE APPROXIMATE
C                   ABSCISSA OF THE SINGULARITY IN THE KDIM-TH
C                   DIMENSION, WORK(KDIM+1) THROUGH WORK(NDIM)
C                   CONTAIN THE ABSCISSAE OF EXTERIOR INTEGRALS.
C         -2*NDIM-KDIM-5 - INCORRECT INNER INTEGRAL DIMENSIONALITY
C                   SPECIFIED DURING EVALUATION OF INTEGRAL AT
C                   DIMENSION KDIM, THAT IS, IABS(IFLAG(1)) = KDIM.
C         ENTRIES IN IOPT STARTING WITH IOPT(2) ARE DESCRIBED BELOW.
C         IOPT(I)   ENTRY IN IOPT(I) MEANS
C           0       NO MORE OPTIONS, BEGIN INTEGRATION.
C           1       IOPT(I+1) CONTAINS THE UNIT NUMBER FOR OUTPUT.
C           2       IOPT(I+1) IS AN NDIMI DIGIT INTEGER, WHERE
C                   EACH DIGIT IS THE DIAGNOSTIC PRINT LEVEL FOR
C                   ONE DIMENSION.  THE LOW ORDER DIGIT IS THE PRINT
C                   LEVEL FOR THE INNER DIMENSION.
C                   0 - NO PRINTING
C                   1 - MINIMUM PRINTING - ERROR MESSAGES (DEFAULT)
C                   2 - PANEL BOUNDARIES AND ANSWERS
C                   3 - ERROR ESTIMATES FOR EACH QUADRATURE FORMULA
C                   4 - DETAILED OUTPUT (DIFFERENCE LINES, ETC).
C           3       WORK(IOPT(I+1)) CONTAINS THE ABSOLUTE TOLERANCE,
C                   WORK(IOPT(I+1)+1) CONTAINS TOLERANCE RELATIVE TO
C                   THE LOCALLY OBTAINABLE PRECISION, AND
C                   WORK(IOPT(I+1)+2) CONTAINS TOLERANCE RELATIVE TO
C                   THE VALUE OF THE INTEGRAL.  THE TOLERANCE RELATIVE
C                   TO THE LOCALLY OBTAINABLE PRECISION IS SPECIFIED AS
C                   THE FRACTION OF LOCALLY OBTAINABLE DIGITS THAT ARE
C                   PERMITTED TO BE WRONG, AND IS INTERNALLY BOUNDED
C                   BETWEEN 0.0 AND 1.0.  IF ANY OF THE ERROR CONTROL
C                   CRITERIA ARE SATISFIED, THE ANSWER IS ACCEPTED.  IF
C                   THIS OPTION IS NOT USED, THE ABSOLUTE AND RELATIVE
C                   TOLERANCES ARE SET TO ZERO, AND THE TOLERANCE
C                   RELATIVE TO THE LOCALLY OBTAINABLE PRECISION IS SET
C                   TO 0.25.
C           4       WORK(IOPT(I+1)) CONTAINS ABSOLUTE ERROR COMMITTED
C                   COMPUTING F.  WORK(IOPT(I+1)) MAY BE CHANGED DURING
C                   THE INTEGRATION.
C           5       WORK(IOPT(I+1)) CONTAINS RELATIVE ERROR EXPECTED TO
C                   BE COMMITTED COMPUTING F.  CHANGES TO THIS VALUE
C                   DURING INTEGRATION WILL NOT BE DETECTED.
C           6       USE REVERSE COMMUNICATION -
C                   CALL DINTM (NDIMI,ANSWER,WORK,NWORK,IOPT)
C                 1 CALL DINTMA (ANSWER,WORK,IOPT(1))
C                   IF (IOPT(1)) 3,2,4
C                 2 COMPUTE THE INNERMOST INTEGRAND -
C                        ANSWER=F(WORK(K)), K=1,NDIMI
C                   THEN EITHER
C                        GO TO 1
C                   OR (FOR BETTER EFFICIENCY)
C                        CALL DINTA (ANSWER,WORK,IOPT(1))
C                        IF (IOPT(1)) 1,2,5
C                   VALUES OF IOPT(1) PRODUCED BY DINTA AND DINTMA
C                   HAVE DIFFERENT MEANINGS.
C                 3 IF (IOPT(1)+NDIMI.LE.0) GO TO 6
C                   THE INTEGRAL OVER THE (-IOPT(1))TH DIMENSION IS
C                   CONTAINED IN ANSWER.  IF A TRANSFORMATION OF
C                   THE INTEGRAL IS NOT NECESSARY BEFORE IT IS USED
C                   AS AN INTEGRAND FOR THE (1-IOPT(1))TH DIMENSION
C                        GO TO 1
C                   ELSE COMPUTE ANSWER=G(ANSWER,WORK(K)),
C                   K=1-IOPT(1),NDIMI AND G IS THE TRANSFORMATION.
C                   COMPUTE ALSO WORK(1)=PARTIAL DERIVATIVE OF G
C                   WITH RESPECT TO ANSWER THEN
C                        GO TO 1
C                 4 COMPUTE WORK(NDIMI+IOPT(1))=A(WORK(K)),
C                   K=IOPT(1)+1,NDIMI, WORK(2*NDIMI+IOPT(1))=B(WORK(K))
C                   K=IOPT(1)+1,NDIM, WHERE A IS THE LOWER LIMIT OF
C                   INTEGRATION FOR THE (IOPT(1))TH DIMENSION, AND B IS
C                   THE UPPER LIMIT.  IF A TRANSFORMATION AS DISCUSSED
C                   ABOVE IS TO BE APPLIED WHEN THE INTEGRATION OF THIS
C                   DIMENSION IS COMPLETE, COMPUTE WORK(1)=AN ESTIMATE
C                   OF THE UPPER BOUND OF THE PARTIAL DERIVATIVE OF G
C                   WITH RESPECT TO THE INTEGRAL.  IF G IS LINEAR IN
C                   THE INTEGRAL, THIS DERIVATIVE CAN BE COMPUTED
C                   WITHOUT KNOWING THE INTEGRAL, RATHER THAN
C                   ESTIMATED.  IF THERE IS A SINGULARITY IN THE
C                   INTEGRAND FOR THIS DIMENSION (PERHAPS INTRODUCED
C                   BY THE BOUNDARY OR THE TRANSFORMATION G), SET
C                   IOPT(1) TO THE LOCATION IN WORK OF THE ABSCISSA IN
C                   THIS DIMENSION OF THE SINGULARITY, AND WITH THE
C                   SIGN SET AS DESCRIBED FOR OPTION 11 BELOW.  THE
C                   MAGNITUDE OF IOPT(1) MUST BE GREATER THAN NDIM FOR
C                   THIS REQUEST TO BE DETECTED.  IN PARTICULAR, IF
C                   THERE IS A SINGULARITY AT ONE OF THE LIMITS, SET
C                   IOPT(1) TO IOPT(1)+NDIM OR -(IOPT(1)+2*NDIM).  THEN
C                        GO TO 1.
C                 5 IOPT(1)=-(IOPT(1)+NDIM)
C                 6 CONTINUE
C                   AT THIS POINT, IERR CONTAINS THE STATUS DESCRIBED
C                   BELOW, FOR OPTION 8.
C           7       SET MINIMUM INDEX OF QUADRATURE FORMULA TO USE
C                   BEFORE SUBDIVISION TO IOPT(I+1).
C           8       NO EFFECT.  IOPT(I+1) MAY BE USED TO PASS
C                   INFORMATION TO DINTF.  INCREMENT I BY 2.
C           9       IOPT(I+1) IS THE MAXIMUM NUMBER OF FUNCTION VALUES
C                   TO USE TO INTEGRATE THE ENTIRE PROBLEM.  IF
C                   IOPT(I+1) .LE. 0, THE NUMBER OF FUNCTION VALUES
C                   IS NOT CONTROLLED.
C          10       IOPT(I+1) IS USED TO RETURN THE NUMBER OF FUNCTION
C                   VALUES USED TO INTEGRATE THE ENTIRE PROBLEM.
C          11       WORK(IABS(IOPT(I+1))) IS THE LOCATION OF A
C                   SINGULARITY OR DISCONTINUITY IN THE OUTERMOST
C                   INTEGRAL.  IF THE LOCATION IS INSIDE THE INTERVAL,
C                   THE INTERVAL IS IMMEDIATELY SUBDIVIDED.  IF
C                   IOPT(I+1) .GT. 0, A T**2 SUBSTITUTION BASED AT
C                   WORK(...) WILL BE USED.  IF IOPT(I+1) .LT. 0,
C                   A T**4 SUBSTITUTION WILL BE USED.  IF IOPT(I+1)
C                   .EQ. 0, NO SUBSTITUTION WILL BE USED.  TO NOTIFY
C                   THE PROGRAM OF SINGULARITIES OR DISCONTINUITIES IN
C                   INTERIOR INTEGRANDS, SET IFLAG NEGATIVE WHEN
C                   ASKED TO COMPUTE LIMITS.  SEE THE DESCRIPTION
C                   OF REVERSE COMMUNICATION ABOVE, OR THE DESCRIPTION
C                   OF DINTF BELOW.
C          12       WORK(IOPT(I+1)) CONTAINS THE ABSOLUTE ERROR IN THE
C                   LOWER LIMIT OF THE CURRENT DIMENSION.  THE ERROR IN
C                   THE UPPER LIMIT IS IN WORK(IOPT(I+1)+1).
C          13       IOPT(I+1) IS THE LOCATION IN IOPT IN WHICH THE USER
C                   IS TO STORE NON-STANDARD CHANGES TO THE DIMENSION OF
C                   INNER INTEGRALS.  THE DEFAULT VALUE IS 1, ALLOWING
C                   SUCH NONSTANDARD CHANGES TO BE STORED IN THE CELL OF
C                   IFLAG (=IOPT) USUALLY USED FOR COMMUNICATION WITH
C                   DINTF, IN THE CASE WHEN IT IS NOT SIMULTANEOUSLY
C                   NECESSARY TO INDICATE A SINGULARITY.  SEE THE
C                   DESCRIPTION OF THE INTERFACE TO DINTF BELOW.
C
C     ALL OPTIONS ARE SET TO THEIR NOMINAL VALUES BEFORE THE OPTION
C     VECTOR IS PROCESSED.
C
C    *****     DINTF     ***********************************************
C
C     DINTF IS REFERENCED VIA CALL DINTF (ANSWER,WORK,IFLAG).
C     WORK AND ANSWER ARE AS DESCRIBED ABOVE.  VALUES OF IFLAG ARE
C     IFLAG .LT. 0
C                    THE INTEGRAL OVER THE (-IFLAG)TH DIMENSION IS
C                    CONTAINED IN ANSWER.  IF A TRANSFORMATION OF
C                    THE INTEGRAL IS NOT NECESSARY BEFORE IT IS USED
C                    AS AN INTEGRAND FOR THE (1-IFLAG)TH DIMENSION
C                         RETURN
C                    ELSE IF THE INTEGRAND DEPENDS ON ONLY ONE INNER
C                    INTEGRAL COMPUTE ANSWER=G(ANSWER,WORK(K)),
C                    ALSO MULTIPLY WORK(1) BY THE PARTIAL DERIVATIVE
C                    OF G WITH RESPECT TO ANSWER THEN
C                         RETURN
C                    ELSE IF THE INTEGRAND DEPENDS ON MORE INTEGRALS
C                    SAVE ANSWER AND WORK(1), AND SET IFLAG(IXKDIM) TO
C                    THE DIMENSION OF INTEGRAL NEXT TO BE COMPUTED
C                    (IXKDIM IS THE VALUE SPECIFIED BY OPTION 12, OR 1
C                    IF OPTION 12 IS NOT SELECTED), THEN
C                         RETURN
C                    ELSE (ALL INNER INTEGRALS UPON WHICH THE INNER
C                    INTEGRAND DEPENDS HAVE BEEN EVALUATED) CALCULATE
C                    ANSWER=G(ANSWER,WORK(K),SAVED INTEGRALS)
C                    K=1-IFLAG, NDIMI (G IS THE TRANSFORMATION),
C                    MULTIPLY WORK(1) BY THE PARTIAL DERIVATIVE OF G
C                    WITH RESPECT TO THE LAST INTEGRAL, AND ADD THE
C                    PRODUCT OF EACH OF THE SAVED VALUES OF WORK(1) AND
C                    PARTIAL DERIVATIVE OF G WITH RESPECT TO THE
C                    CORRESPONDING SAVED INTEGRAL ONTO WORK(1).  THAT
C                    IS, WORK(1) IS THE TOTAL ERROR IN G, CALCULATED AS
C                    THE INNER PRODUCT OF THE ERRORS IN THE INDIVIDUAL
C                    ARGUMENTS, AND THE PARTIAL DERIVATIVES OF G WITH
C                    RESPECT TO THOSE ARGUMENTS.
C     IFLAG .EQ. 0
C                    COMPUTE THE INNERMOST INTEGRAND -
C                         ANSWER=F(WORK(K)), K=1,NDIMI
C                    AND
C                         RETURN
C     IFLAG .GT. 0
C                    COMPUTE WORK(NDIMI+IFLAG)=A(WORK(K)),
C                    K=IFLAG+1,NDIMI, WORK(2*NDIMI+IFLAG)=B(WORK(K)),
C                    K=IFLAG+1,NDIM, WHERE A IS THE LOWER LIMIT OF
C                    INTEGRATION FOR THE (IFLAG)TH DIMENSION, AND B IS
C                    THE UPPER LIMIT.  IF A TRANSFORMATION AS DISCUSSED
C                    ABOVE IS TO BE APPLIED WHEN THE INTEGRATION OF THIS
C                    DIMENSION IS COMPLETE, COMPUTE WORK(1)=AN ESTIMATE
C                    OF THE UPPER BOUND OF THE PARTIAL DERIVATIVE
C                    OF G WITH RESPECT TO THE INTEGRAL.  IF G IS
C                    LINEAR IN THE INTEGRAL, THIS DERIVATIVE MAY BE
C                    COMPUTED WITHOUT KNOWING THE INTEGRAL, RATHER
C                    THAN ESTIMATED.  IF THERE IS A SINGULARITY IN
C                    THE INTEGRAND FOR THIS DIMENSION (PERHAPS
C                    INTRODUCED BY THE BOUNDARY OR THE TRANSFORMATION),
C                    SET IFLAG TO THE LOCATION IN WORK OF THE ABSCISSA
C                    IN THIS DIMENSION OF THE SINGULARITY, AND SET THE
C                    SIGN AS FOR OPTION 11 ABOVE.  NOTE THAT THE
C                    MAGNITUDE OF IFLAG MUST BE GREATER THAN NDIM FOR
C                    THIS REQUEST TO BE DETECTED.  IN PARTICULAR, IF
C                    THERE IS A SINGULARITY AT ONE OF THE LIMITS, SET
C                    THE MAGNITUDE OF IFLAG TO IFLAG+NDIM OR
C                    IFLAG+2*NDIM, WITH THE SIGN AS DESCRIBED FOR
C                    OPTION 11.  THEN
C                         RETURN.
C                    IF THE FIRST INTEGRAL TO BE COMPUTED DOES NOT HAVE
C                    DIMENSION IFLAG(1)-1, SET IFLAG(IXKDIM) TO THE
C                    DIMENSION OF INTEGRAL TO BE NEXT COMPUTED.  SEE
C                    OPTION 12 AND THE DISCUSSION OF IFLAG .LT. 0 ABOVE
C                    FOR INFORMATION ON IXKDIM.
C
C     *****    EXTERNAL REFERENCES     *********************************
C
C DINTMA  TO DO THE INTEGRATION.
C DINTOP  TO SET OPTIONS.
C
C     *****    LOCAL VARIABLES     *************************************
C
      INTEGER NDIMI, NWORK
C
C     *****    COMMON STORAGE     **************************************
C
C     COMMON /DINTNC/ CONTAINS VARIABLES NOT SEPARATELY SAVED FOR
C     EACH DIMENSION OF A MULTIPLE QUADRATURE.  COMMON /DINTC/
C     CONTAINS VARIABLES THAT MUST BE SAVED FOR EACH DIMENSION OF THE
C     QUADRATURE.  THE VARIABLES IN EACH COMMON BLOCK ARE STORED IN THE
C     ORDER - ALWAYS DOUBLE, DOUBLE IF DOUBLE PRECISION PROGRAM, DOUBLE
C     IF DOUBLE PRECISION PROGRAM AND EXPONENT RANGE OF DOUBLE AND
C     SINGLE VERY DIFFERENT, SINGLE, INTEGER, LOGICAL.  A PAD OF LOGICAL
C     VARIABLES IS INCLUDED AT THE END OF /DINTC/.  THE DIMENSION OF
C     THE PAD MAY NEED TO BE VARIED SO THAT NO VARIABLES BEYOND THE END
C     OF THE COMMON BLOCK ARE ALTERED.
C
C     DECLARATIONS OF COMMON /DINTNC/ VARIABLES.
C
      DOUBLE PRECISION AINIT, BINIT, FNCVAL, S, TP
      DOUBLE PRECISION FER, FER1, RELOBT, TPS, XJ, XJP
      INTEGER     FEA,       FEA1,      INC,       INC2,      IPRINT,
     1 ISTOP(2,2),JPRINT,    KDIM,      KK,        KMAXF,     NDIM,
     2 NFINDX,    NFMAX,     NFMAXM,    RELTOL,    REVERM,    REVERS,
     3 WHEREM
      LOGICAL NEEDH
C
C     DECLARATIONS OF COMMON /DINTC/ VARIABLES.
C
c--D Next line special: S => D, X => Q, D => D, P => D
      DOUBLE PRECISION ACUM, PACUM, RESULT(2)
C     139 $.TYPE.$ VARIABLES
      DOUBLE PRECISION
     1 AACUM,     ABSCIS,    DELMIN,    DELTA,     DIFF,      DISCX(2),
     2 END(2),    ERRINA,    ERRINB,    FAT(2),    FSAVE,
     3 FUNCT(24), F1,        F2,        LOCAL(4),  PAACUM,    PF1,
     4 PF2,       PHISUM,    PHTSUM,    PX,        SPACE(6),
     5 STEP(2),   START(2),  SUM,       T,         TA,        TASAVE,
     6 TB,        TEND,      WORRY(2),  X,         X1,
     7 X2,        XT(17),    FT(17),    PHI(34)
c Note XT, FT, and PHI above are last, because they must be in adjacent
c locations in DINTC.
C     30 $DSTYP$ VARIABLES
      DOUBLE PRECISION
     1 ABSDIF,    COUNT,     EDUE2A,    EDUE2B,    EP,        EPNOIZ,
     2 EPS,       EPSMAX,    EPSMIN,    EPSO,      EPSR,      EPSS,
     3 ERR,       ERRAT(2),  ERRC,      ERRF,      ERRI,      ERRT(2),
     4 ESOLD,     EXTRA,     PEPSMN,    RE,        RELEPS,    REP,
     5 REPROD,    RNDC,      TLEN,      XJUMP
C     29 INTEGER VARIABLES
      INTEGER     DISCF,     DISCHK,    ENDPTS,    I,         INEW,
     1 IOLD,      IP,        IXKDIM,    J,         J1,        J1OLD,
     2 J2,        J2OLD,     K,         KAIMT,     KMAX,      KMIN,
     3 L,         LENDT,     NFEVAL,    NFJUMP,    NSUB,      NSUBSV,
     4 NXKDIM,    PART,      SEARCH,    TALOC,     WHERE,     WHERE2
C     11 TO 18 LOGICALS (7 ARE PADDING).
      LOGICAL     DID1,      FAIL,      FATS(2),   FSAVED,    HAVDIF,
     1 IEND,      INIT,      ROUNDF,    XCDOBT(2), PAD(7)
C
C     THE COMMON BLOCKS.
C
      COMMON /DINTNC/
c        1       2       3     4        5       6       7        8
     W AINIT,  BINIT,  FNCVAL, S,      TP,     FER,    FER1,   RELOBT,
c       9      10       11      12      13       1       2        3
     X TPS,    XJ,     XJP,    FEA,    FEA1,   KDIM,    INC,    INC2,
c     4 (2,2)    8       9     10       11      12       13      14
     Y ISTOP,  JPRINT, IPRINT, KK,     KMAXF,  NDIM,   NFINDX, NFMAX,
c        15     16       17      18      19      20
     Z NFMAXM, RELTOL, REVERM, REVERS, WHEREM, NEEDH
      COMMON /DINTC/
     1 ACUM,   PACUM,  RESULT
      COMMON /DINTC/
c        1     2 (4)     6      7        8       9      10     11 (2)
     1 AACUM,  LOCAL,  ABSCIS, TA,     DELTA,  DELMIN, DIFF,   DISCX,
c     13 (2)     15      16    17 (2)   19     20 (24) 44
     2 END,    ERRINA, ERRINB, FAT,    FSAVE,  FUNCT,  F2,
c       45      46     47       48      49     50      51 (6)
     3 PAACUM, PF1,    PF2,    PHISUM, PHTSUM, PX,     SPACE,
c      57 (2)  59 (2)   61     62        63    64       65
     4 STEP,   START,  SUM,    T,      TASAVE, TB,     TEND,
c      66 (2)  68      69      70      71       72
     5 WORRY,  X1,     X2,     X,      F1,     COUNT,
c      73 (17) 90 (17) 107 (34)
     6 XT,     FT,     PHI
      COMMON /DINTC/
c       141     142    143     144      145     146
     1 ABSDIF, EDUE2A, EDUE2B, EP,     EPNOIZ, EPSMAX,
c       147     148     149    150 (2)  152     153
     2 EPSO,   EPSR,   EPSS,   ERRAT,  ERRC,   ERRF,
c     154 (2)   156     157     158     159    160
     3 ERRT,   ESOLD,  EXTRA,  PEPSMN, RELEPS, REP,
c       161     162     163
     4 RNDC,   TLEN,   XJUMP,
c       164    165      166    167    168       169
     5 ERRI,   ERR,    EPSMIN, EPS,    RE,     REPROD
      COMMON /DINTC/
c       170     171     172
     1 DISCF,  DISCHK, ENDPTS, INEW,   IOLD,   IP,     IXKDIM,
     2 J,      J1,     J1OLD,  J2,     J2OLD,  KMAX,   KMIN,
     3 L,      LENDT,  NFJUMP, NSUBSV, NXKDIM, TALOC,  WHERE2,
c      1       2          3      4       5         6      7       8
     4 I,      K,      KAIMT,  NSUB,   PART,   SEARCH, WHERE, NFEVAL
      COMMON /DINTC/
     1 DID1,   FAIL,   FATS,   FSAVED, HAVDIF, IEND,   INIT,   ROUNDF,
     2 XCDOBT, PAD
      SAVE /DINTNC/, /DINTC/
C
C     THE VARIABLES HERE DEFINE THE MACHINE ENVIRONMENT.  ALL ARE SET
C     IN DINTOP.  THE MEANING ATTACHED TO THESE VARIABLES CAN BE
C     FOUND BY LOOKING AT THE DEFINITIONS IN DINTOP.
      DOUBLE PRECISION
     1  EMEPS,  EEPSM8, EDELM2, EDELM3, ESQEPS, ERSQEP, ERSQE6, EMINF,
     2  ESMALL, ENZER,  EDELM1, ENINF
      COMMON /DINTEC/
     1  EMEPS,  EEPSM8, EDELM2, EDELM3, ESQEPS, ERSQEP, ERSQE6, EMINF,
     2  ESMALL, ENZER,  EDELM1, ENINF
      SAVE /DINTEC/

c For initialization of common blocks
      integer KREAL, KINT, KLOG, ITEMP
      parameter (KINT=29, KREAL=169, KLOG=11)
      logical LMOVE(KLOG)
      integer IMOVE(KINT)
      double precision RMOVE(KREAL)
      equivalence (AACUM, RMOVE)
      equivalence (DISCF, IMOVE)
      equivalence (DID1, LMOVE)
C
C
C     *****    PROCEDURES     ******************************************
C

c Initialize common blocks to avoid references to undefined variables.

      do 5 ITEMP = 1, KLOG
        LMOVE(ITEMP) = .false.
 5    continue
      do 10 ITEMP = 1, KREAL
        RMOVE(ITEMP) = 0.D0
 10   continue
      do 15 ITEMP = 1, KINT
        IMOVE(ITEMP) = 0
 15   continue

      WHEREM=0
      NFEVAL=0
      NDIM=NDIMI
      KDIM=NWORK
      X = 0.D0

C     KDIM IS TEMPORARILY USED IN DINTMA TO CHECK THE DIMENSION OF
C     WORK.
      CALL DINTOP (IOPT,WORK)
      IF (IOPT(1).EQ.0) THEN
C
C     CALL DINTMA TO DO THE INTEGRATION.
C
         IF (REVERM.EQ.0) CALL DINTMA (ANSWER,WORK,IOPT)
      ELSE
         IOPT(1)=-NDIM-4
      END IF
      RETURN
      END