      SUBROUTINE RG(NM,N,PA,PWR,PWI,MATZ,PZ,KIV1,PFV1,IERR)
c
      INTEGER N,NM,IS1,IS2,IERR,MATZ
      REAL PA(NM,N),PWR(N),PWI(N),PZ(NM,N),PFV1(N)
      INTEGER KIV1(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 general 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 pa.
c
c        pa contains the real general 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        pwr and  pwi contain the real and imaginary parts,
c        respectively, of the eigenvalues.  complex conjugate
c        pairs of eigenvalues appear consecutively with the
c        eigenvalue having the positive imaginary part first.
c
c        pz contains the real and imaginary parts of the eigenvectors
c        if matz is not zero.  if the j-th eigenvalue is real, the
c        j-th column of pz  contains its eigenvector.  if the j-th
c        eigenvalue is complex with positive imaginary part, the
c        j-th and (j+1)-th columns of pz  contain the real and
c        imaginary parts of its eigenvector.  the conjugate of this
c        vector is the eigenvector for the conjugate eigenvalue.
c
c        ierr  is an integer output variable set equal to an error
c           completion code described in the documentation for hqr
c           and hqr2.  the normal completion code is zero.
c
c        kiv1  and  pfv1  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 CALL  BALANC(NM,N,PA,IS1,IS2,PFV1)
      CALL  ELMHES(NM,N,IS1,IS2,PA,KIV1)
      IF (MATZ .NE. 0) GO TO 20
c     .......... find eigenvalues only ..........
      CALL  HQR(NM,N,IS1,IS2,PA,PWR,PWI,IERR)
      GO TO 50
c     .......... find both eigenvalues and eigenvectors ..........
   20 CALL  ELTRAN(NM,N,IS1,IS2,PA,KIV1,PZ)
      CALL  HQR2(NM,N,IS1,IS2,PA,PWR,PWI,PZ,IERR)
      IF (IERR .NE. 0) GO TO 50
      CALL  BALBAK(NM,N,IS1,IS2,PFV1,N,PZ)
   50 RETURN
      END
