c
c     Second Edition
c     Numerical Analysis:
c     The Mathematics of Scientific Computing
c     D.R. Kincaid & E.W. Cheney
c     Brooks/Cole Publ., 1996
c     ISBN 0-534-33892-5
c     COPYRIGHT (c) 1996
c
c     Section 4.3 
c
c     Example of Gaussian elimination with scaled row pivoting 
c
c
c     file: gauss.f
c
      parameter (n = 3)
      dimension a(n,n),s(n),b(n),x(n)
      integer p(n), pk
      real max
      data (a(1,j),j=1,n) /2.0,3.0,-6.0/
      data (a(2,j),j=1,n) /1.0,-6.0,8.0/
      data (a(3,j),j=1,n) /3.0,-2.0,1.0/
      data (b(i),i=1,n) /-1.0,3.0,2.0/
c
      print *
      print *,' Gaussian elimination with scaled row pivoting'
      print *,' Section 4.3, Kincaid-Cheney'
      print *
c
      do 3 i=1,n
         p(i) = i
         smax = 0.0 
         do 2 j=1,n
            smax = max(smax,abs(a(i,j)))
 2       continue
         s(i) = smax
 3    continue
c
      do 7 k=1,n-1
         rmax = 0.0
         do 4 i=k,n
            r = abs(a(p(i),k))/s(p(i))
            if (r .gt. rmax) then
               j = i
               rmax = r
            endif
 4       continue
c
         pk = p(j)
         p(j) = p(k)
         p(k) = pk
c
         do 6 i=k+1,n      
            z = a(p(i),k)/a(p(k),k)       
            a(p(i),k) = z
            do 5 j=k+1,n    
               a(p(i),j) = a(p(i),j) - z*a(p(k),j)      
 5          continue
 6       continue  
 7    continue    
c
      do 9 k=1,n-1
         do 8 i=k+1,n
            b(p(i)) = b(p(i)) - a(p(i),k)*b(p(k))
 8       continue
 9    continue
      do 11 i=n,1,-1
         sum = b(p(i))
         do 10 j=i+1,n
            sum = sum - a(p(i),j)*x(j)
 10      continue
         x(i) = sum/a(p(i),i)
 11   continue
c
      do 12 i=1,n
         print 13,i,x(i)
 12   continue
c
 13   format (3x,'x(',i2,') =',e13.6)
      stop
      end 
