c    @(#)eigrg1.f	1.2
c
      subroutine eigrg1 (nm,n,a,iflag,wr,wi,z,work1,work2,ierr)
      real a(nm,n),wr(n),wi(n),work1(nm,n),work2(2*n+2),z(nm,n) 
      do 50 i = 1,n
      do 50 j = 1,n
      work1(i,j) = a(i,j)
   50 continue
      call balanc(nm,n,work1,work2(n+1),work2(n+2),work2)
      call elmhes(nm,n,work2(n+1),work2(n+2),work1,work2(n+3))
      if (iflag .ne. 0) go to 100
      call hqr(nm,n,work2(n+1),work2(n+2),work1,wr,wi,ierr)
      if (ierr .ne. 0) go to 400
      return
  100 if (iflag .ne. n) go to 200
      call eltran(nm,n,work2(n+1),work2(n+2),work1,work2(n+3),z)
      call hqr2(nm,n,work2(n+1),work2(n+2),work1,wr,wi,z,ierr)
      if (ierr .ne. 0) go to 400
      call balbak(nm,n,work2(n+1),work2(n+2),work2,n,z) 
      return
  200 do 300 i = 1,n
         do 300 j = 1,n
      z(i,j) = work1(i,j)
  300 continue
      call hqr(nm,n,work2(n+1),work2(n+2),z,wr,wi,ierr)
      if (ierr .eq. 0) return
  400 ierr = iabs(ierr) + 32
      print 1001
 1001 format(' eigrg1 failed to compute all eigenvalues')
      return
      end
