      SUBROUTINE SGETF2( M, N, A, LDA, IPIV, INFO )
      IMPLICIT NONE
*
*  -- LAPACK routine --
*     Argonne National Lab, Courant Institute, and N.A.G. Ltd.
*     April 1, 1989
*
*     -- NPAC, Syracuse University --
*     Manish Parashar
*     April 1992
*
*     .. Scalar Arguments ..
      INTEGER            INFO, LDA, M, N
*     ..
*     .. Array Arguments ..
      INTEGER            IPIV( * )
      REAL               A( LDA, * )
*     ..
*
*  Purpose
*  =======
*
*     SGETF2 computes the LU factorization of a general m-by-n
*     matrix A, using partial pivoting with row interchanges.
*     This is the Level 2 BLAS version of the algorithm.
*
*  Arguments
*  =========
*
*  M      - INTEGER
*           On entry, M specifies the number of rows of the matrix
*           A . M must be at least  zero.
*           Unchanged on exit.
*
*  N      - INTEGER
*           On entry, N specifies the number of columns of the matrix
*           A . N must be at least zero.
*           Unchanged on exit.
*
*  A      - REAL array of dimension( LDA, N )
*           On entry, A specifies the array which contains the matrix
*           being factored.
*           On exit, the array A is overwritten by the
*           LU factorization. The factorization can be written as
*           A = L*U where L is a product of permutation and unit lower
*           triangular matrices and U is an upper triangular matrix.
*
*  LDA    - INTEGER
*           On entry, LDA specifies the first dimension of A as declared
*           in the calling (sub) program.
*           LDA must be at least max( 1, M ).
*           Unchanged on exit.
*
*  IPIV   - INTEGER array of dimension( M )
*           On exit, the array IPIV contains the pivot indices.
*
*  INFO   - INTEGER
*           On exit, a value of 0 indicates a normal return;
*           a value nonzero, say K, indicates that U(K,K) = 0.0 exactly.
*           This is not an error condition for this subroutine, but it
*           does indicate that SGETRS or SGETRI will divide by zero
*           if called. Use routine SGECON for a reliable indication of
*           singularity.
*           A negative value, say -K, indicates the Kth argument has an
*           illegal value.
*
*     .. Parameters ..
      REAL               ONE, ZERO
      PARAMETER          ( ONE = 1.0E+0, ZERO = 0.0E+0 )
*     ..
*     .. Local Scalars ..
      INTEGER            I, IP, J, JP
      REAL               T
*     ..
*     .. External Functions ..
      INTEGER            ISAMAX
      EXTERNAL           ISAMAX
*     ..
*     .. External Subroutines ..
      EXTERNAL           SGEMV, SSCAL, SSWAP, STRSV, XERBLA
*     ..
*     .. Intrinsic Functions ..
      INTRINSIC          MAX
*     ..
*     .. Executable Statements ..
*
*     Gaussian elimination with partial pivoting
*
*     Test the input parameters.
*
      INFO = 0
      IF( M.LT.0 ) THEN
         INFO = -1
      ELSE IF( N.LT.0 ) THEN
         INFO = -2
      ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
         INFO = -4
      END IF
      IF( INFO.NE.0 ) THEN
         CALL XERBLA( 'SGETF2', -INFO )
         RETURN
      END IF
*
*     Quick return if possible.
*
      IF( N.EQ.0 )
     $   RETURN
*
      DO 20 J = 1, N
*
*        Apply previous interchanges to J-th column.
*
         DO 10 I = 1, J - 1
            IP = IPIV( I )
            IF( IP.NE.I ) THEN
               T = A( I, J )
               A( I, J ) = A( IP, J )
               A( IP, J ) = T
            END IF
   10    CONTINUE
*
*        Compute elements 1:J-1 of J-th column.
*
         CALL STRSV( 'Lower', 'No transpose', 'Unit', J-1, A, LDA,
     $               A( 1, J ), 1 )
*
*        Update elements J:N of J-th column.
*
         CALL SGEMV( 'No transpose', M-J+1, J-1, -ONE, A( J, 1 ), LDA,
     $               A( 1, J ), 1, ONE, A( J, J ), 1 )
*
*        Find pivot and test for singularity.
*
         JP = J - 1 + ISAMAX( M-J+1, A( J, J ), 1 )
         IPIV( J ) = JP
         IF( A( JP, J ).NE.ZERO ) THEN
*
*           Apply interchange to columns 1:J.
*
            IF( JP.NE.J )
     $         CALL SSWAP( J, A( J, 1 ), LDA, A( JP, 1 ), LDA )
*
*           Compute elements J+1:M of J-th column.
*
            IF( J.LT.M )
     $         CALL SSCAL( M-J, ONE / A( J, J ), A( J+1, J ), 1 )
         ELSE
*
*           If A( JP, J ) is zero, set INFO to indicate that a zero
*           pivot has been found.  INFO returns the index of the
*           last zero pivot to the calling routine if there is more
*           than one.
*
            INFO = J
         END IF
   20 CONTINUE
      RETURN
*
*     End of SGETF2
*
      END
