*DECK ALST2S
      SUBROUTINE ALST2S(MDIM,M,N,A,UD,B,X)
*
*-----------------------------------------------------------------------
*
* TO SOLVE THE LEAST SQUARES PROBLEM A*X=B WHEN THE MATRIX A HAS
* ALREADY BEEN DECOMPOSED BY ALST2F.
*
*  INPUT PARAMETERS:
*    MDIM - THE DIMENSIONED COLUMN LENGTH OF A.
*    M    - THE NUMBER OF ROWS OF A
*    N    - THE NUMBER OF COLUMNS OF A. N.LE.M IS ASSUMED.
*    A    - THE DECOMPOSED MATRIX.
*    UD   - THE DIAGONAL OF U.
*    B    - THE RIGHT-HAND SIDE.
*
*  OUTPUT PARAMETERS:
*    B - B HAS BEEN CLOBBERED.
*        SQRT(SUM(I=N+1,M)(B(I)**2)) IS THE L2 NORM OF THE RESIDUAL
*        IN THE SOLUTION OF THE EQUATIONS.
*    X - THE SOLUTION VECTORS. X=B IS OK.
*
*  SCRATCH SPACE ALLOCATED - NONE.
*
*  ERROR STATES -
*
*    1 - MDIM.LT.M.
*    2 - N.LT.1.
*    3 - N.GT.M.
*    4 - UD(J)=0 OR A(J,J)=0.
*
*-----------------------------------------------------------------------
*
      DOUBLE PRECISION A(MDIM,N),UD(N),B(M),X(N)
      DOUBLE PRECISION S
*
* ... CHECK THE INPUT.
*
      IF(MDIM.LT.M) CALL XABORT('ALST2S: MDIM.LT.M')
      IF(N.LT.1) CALL XABORT('ALST2S: N.LT.1')
      IF(N.GT.M) CALL XABORT('ALST2S: N.GT.M')
*
* ... APPLY Q-TRANSPOSE TO B.
*
      DO 20 J=1,N
      IF((UD(J).EQ.0.0D0).OR.(A(J,J).EQ.0.0D0)) THEN
         CALL XABORT('ALST2S: UD(J)=0 OR A(J,J)=0')
      ENDIF
      S=0.D0
      DO 10 I=J,M
 10   S=S+A(I,J)*B(I)
      S=S/(UD(J)*A(J,J))
      DO 20 I=J,M
 20   B(I)=B(I)+S*A(I,J)
*
* ... BACK-SOLVE THE TRIANGULAR SYSTEM U*X=(Q-TRANSPOSE)*B.
*
      X(N)=B(N)/UD(N)
      IF(N.EQ.1) GO TO 50
      DO 40 II=2,N
      I=N+1-II
      S=B(I)
      DO 30 J=I+1,N
 30   S=S-A(I,J)*X(J)
 40   X(I)=S/UD(I)
*
 50   RETURN
      END
