c    @(#)hqr2.f	1.1
c
      subroutine hqr2(nm,n,low,igh,h,wr,wi,z,ierr)
      integer i,j,k,l,m,n,en,ii,jj,ll,mm,na,nm,nn,
     1        igh,its,low,mp2,enm2,ierr
      real h(nm,n),wr(n),wi(n),z(nm,n)
      real p,q,r,s,t,w,x,y,ra,sa,vi,vr,zz,norm,machep
      logical notlas
      complex z3
      machep = 2.**(-47)
      ierr = 0
      norm = 0.0
      k = 1
      do 50 i = 1, n
         do 40 j = k, n
   40    norm = norm + abs(h(i,j))
         k = i
         if (i .ge. low .and. i .le. igh) go to 50
         wr(i) = h(i,i)
         wi(i) = 0.0
   50 continue
      en = igh
      t = 0.0
   60 if (en .lt. low) go to 340
      its = 0
      na = en - 1
      enm2 = na - 1
   70 do 80 ll = low, en
         l = en + low - ll
         if (l .eq. low) go to 100
         s = abs(h(l-1,l-1)) + abs(h(l,l))
         if (s .eq. 0.0) s = norm
         if (abs(h(l,l-1)) .le. machep * s) go to 100
   80 continue
  100 x = h(en,en)
      if (l .eq. en) go to 270
      y = h(na,na)
      w = h(en,na) * h(na,en)
      if (l .eq. na) go to 280
      if (its .eq. 30) go to 1000 
      if (its .ne. 10 .and. its .ne. 20) go to 130
      t = t + x
      do 120 i = low, en
  120 h(i,i) = h(i,i) - x
      s = abs(h(en,na)) + abs(h(na,enm2))
      x = 0.75 * s
      y = x
      w = -0.4375 * s * s
  130 its = its + 1
      do 140 mm = l, enm2
         m = enm2 + l - mm
         zz = h(m,m)
         r = x - zz
         s = y - zz
         p = (r * s - w) / h(m+1,m) + h(m,m+1)
         q = h(m+1,m+1) - zz - r - s
         r = h(m+2,m+1)
         s = abs(p) + abs(q) + abs(r)
         p = p / s
         q = q / s
         r = r / s
         if (m .eq. l) go to 150
         if (abs(h(m,m-1)) * (abs(q) + abs(r)) .le. machep * abs(p)
     1    * (abs(h(m-1,m-1)) + abs(zz) + abs(h(m+1,m+1)))) go to 150
  140 continue
  150 mp2 = m + 2
      do 160 i = mp2, en
         h(i,i-2) = 0.0
         if (i .eq. mp2) go to 160
         h(i,i-3) = 0.0
  160 continue
      do 260 k = m, na
         notlas = k .ne. na
         if (k .eq. m) go to 170
         p = h(k,k-1)
         q = h(k+1,k-1)
         r = 0.0
         if (notlas) r = h(k+2,k-1)
         x = abs(p) + abs(q) + abs(r)
         if (x .eq. 0.0) go to 260
         p = p / x
         q = q / x
         r = r / x
  170    s = sign(sqrt(p*p+q*q+r*r),p)
         if (k .eq. m) go to 180
         h(k,k-1) = -s * x
         go to 190
  180    if (l .ne. m) h(k,k-1) = -h(k,k-1)
  190    p = p + s
         x = p / s
         y = q / s
         zz = r / s
         q = q / p
         r = r / p
         do 210 j = k, n
           p = h(k,j) + q * h(k+1,j)
           if (.not. notlas) go to 200
           p = p + r * h(k+2,j)
           h(k+2,j) = h(k+2,j) - p * zz
  200      h(k+1,j) = h(k+1,j) - p * y
           h(k,j) = h(k,j) - p * x
  210    continue
         j = min0(en,k+3)
         do 230 i = 1, j
           p = x * h(i,k) + y * h(i,k+1)
           if (.not. notlas) go to 220
           p = p + zz * h(i,k+2)
           h(i,k+2) = h(i,k+2) - p * r
  220      h(i,k+1) = h(i,k+1) - p * q
           h(i,k) = h(i,k) - p
  230    continue
         do 250 i = low, igh
           p = x * z(i,k) + y * z(i,k+1)
           if (.not. notlas) go to 240
           p = p + zz * z(i,k+2)
           z(i,k+2) = z(i,k+2) - p * r
  240      z(i,k+1) = z(i,k+1) - p * q
           z(i,k) = z(i,k) - p
  250    continue
  260 continue
      go to 70
  270 h(en,en) = x + t
      wr(en) = h(en,en)
      wi(en) = 0.0
      en = na
      go to 60
  280 p = (y - x) / 2.0
      q = p * p + w
      zz = sqrt(abs(q))
      h(en,en) = x + t
      x = h(en,en)
      h(na,na) = y + t
      if (q .lt. 0.0) go to 320
      zz = p + sign(zz,p)
      wr(na) = x + zz
      wr(en) = wr(na)
      if (zz .ne. 0.0) wr(en) = x - w / zz
      wi(na) = 0.0
      wi(en) = 0.0
      x = h(en,na)
      r = sqrt(x*x+zz*zz)
      p = x / r
      q = zz / r
      do 290 j = na, n
         zz = h(na,j)
         h(na,j) = q * zz + p * h(en,j)
         h(en,j) = q * h(en,j) - p * zz
  290 continue
      do 300 i = 1, en
         zz = h(i,na)
         h(i,na) = q * zz + p * h(i,en)
         h(i,en) = q * h(i,en) - p * zz
  300 continue
      do 310 i = low, igh
         zz = z(i,na)
         z(i,na) = q * zz + p * z(i,en)
         z(i,en) = q * z(i,en) - p * zz
  310 continue
      go to 330
  320 wr(na) = x + p
      wr(en) = x + p
      wi(na) = zz
      wi(en) = -zz
  330 en = enm2
      go to 60
  340 if (norm .eq. 0.0) go to 1001
      do 800 nn = 1, n
         en = n + 1 - nn
         p = wr(en)
         q = wi(en)
         na = en - 1
         if (q) 710, 600, 800
  600    m = en
         h(en,en) = 1.0
         if (na .eq. 0) go to 800
         do 700 ii = 1, na
            i = en - ii
            w = h(i,i) - p
            r = h(i,en)
            if (m .gt. na) go to 620
            do 610 j = m, na
  610       r = r + h(i,j) * h(j,en)
  620       if (wi(i) .ge. 0.0) go to 630
            zz = w
            s = r
            go to 700
  630       m = i
            if (wi(i) .ne. 0.0) go to 640
            t = w
            if (w .eq. 0.0) t = machep * norm
            h(i,en) = -r / t
            go to 700
  640       x = h(i,i+1)
            y = h(i+1,i)
            q = (wr(i) - p) * (wr(i) - p) + wi(i) * wi(i)
            t = (x * s - zz * r) / q
            h(i,en) = t
            if (abs(x) .le. abs(zz)) go to 650
            h(i+1,en) = (-r - w * t) / x
            go to 700
  650       h(i+1,en) = (-s - y * t) / zz
  700    continue
         go to 800
  710    m = na
         if (abs(h(en,na)) .le. abs(h(na,en))) go to 720
         h(na,na) = q / h(en,na)
         h(na,en) = -(h(en,en) - p) / h(en,na)
         go to 730
  720    z3 = cmplx(0.0,-h(na,en)) / cmplx(h(na,na)-p,q)
         h(na,na) = real(z3)
         h(na,en) = aimag(z3)
  730    h(en,na) = 0.0
         h(en,en) = 1.0
         enm2 = na - 1
         if (enm2 .eq. 0) go to 800
         do 790 ii = 1, enm2
            i = na - ii
            w = h(i,i) - p
            ra = 0.0
            sa = h(i,en)
            do 760 j = m, na
               ra = ra + h(i,j) * h(j,na)
               sa = sa + h(i,j) * h(j,en)
  760       continue
            if (wi(i) .ge. 0.0) go to 770
            zz = w
            r = ra
            s = sa
            go to 790
  770       m = i
            if (wi(i) .ne. 0.0) go to 780
            z3 = cmplx(-ra,-sa) / cmplx(w,q)
            h(i,na) = real(z3)
            h(i,en) = aimag(z3)
            go to 790
  780       x = h(i,i+1)
            y = h(i+1,i)
            vr = (wr(i) - p) * (wr(i) - p) + wi(i) * wi(i) - q * q
            vi = (wr(i) - p) * 2.0 * q
            if (vr .eq. 0.0 .and. vi .eq. 0.0) vr = machep * norm
     1       * (abs(w) + abs(q) + abs(x) + abs(y) + abs(zz))
            z3 = cmplx(x*r-zz*ra+q*sa,x*s-zz*sa-q*ra) / cmplx(vr,vi)
            h(i,na) = real(z3)
            h(i,en) = aimag(z3)
            if (abs(x) .le. abs(zz) + abs(q)) go to 785
            h(i+1,na) = (-ra - w * h(i,na) + q * h(i,en)) / x
            h(i+1,en) = (-sa - w * h(i,en) - q * h(i,na)) / x
            go to 790
  785       z3 = cmplx(-r-y*h(i,na),-s-y*h(i,en)) / cmplx(zz,q)
            h(i+1,na) = real(z3)
            h(i+1,en) = aimag(z3)
  790    continue
  800 continue
      do 840 i = 1, n
         if (i .ge. low .and. i .le. igh) go to 840
         do 820 j = i, n
  820    z(i,j) = h(i,j)
  840 continue
      do 880 jj = low, n
         j = n + low - jj
         m = min0(j,igh)
         do 880 i = low, igh
            zz = 0.0
            do 860 k = low, m
  860       zz = zz + z(i,k) * h(k,j)
            z(i,j) = zz
  880 continue
      go to 1001
 1000 ierr = en
 1001 return
      end
