      SUBROUTINE ORSOL(M, N, QR, MS, S, IP, X)
C***********************************************************************
C      IDENTIFICATION
C        ORSOL - LEAST SQUARES SOLUTION OF A LINEAR SYSTEM GIVEN AN
C                ORTHOGONAL-TRIANGULAR FACTORIZATION OF THE COEFFICIENT
C                MATRIX PRODUCED BY SUBROUTINE ORTHO
C        FORTRAN SUBROUTINE SUBPROGRAM
C        AEROSPACE RESEARCH LABORATORIES
C        WRIGHT-PATTERSON AFB, OHIO  45433
C      PURPOSE
C        ORSOL COMPUTES THE LEAST SQUARES SOLUTION OF THE LINEAR SYSTEM
C        QRX = PAX = B WHERE Q, R, AND P ARE DETERMINED FROM A BY ORTHO.
C      CONTROL
C
C        DIMENSION QR(MS,N), S(N), IP(N), X(M)
C        .
C        .
C        .
C        CALL ORSOL(M, N, QR, MS, S, IP, X)
C
C        WHERE
C        M  IS AN INTEGER INPUT VARIABLE, THE NUMBER OF ROWS OF A.
C        N  IS AN INTEGER INPUT VARIABLE, THE NUMBER OF COLUMNS OF A
C             (1 .LT. N .LE. M).
C        QR IS A REAL INPUT ARRAY, THE ORTHOGONAL AND TRIANGULAR FACTORS
C           OF A PRODUCED BY ORTHO.
C        MS IS AN INTEGER INPUT VARIABLE, THE LEADING DIMENSION OF
C           QR IN THE CALLING PROGRAM.
C        S  IS A REAL INPUT ARRAY, THE RELEVANT PARTS OF Q PRODUCED BY
C           ORTHO.
C        IP IS AN INTEGER INPUT ARRAY, THE PERMUTATION INFORMATION
C           PRODUCED BY ORTHO.
C        X  AS A REAL INPUT ARRAY IS THE RIGHT-HAND SIDE B OF AX = B.
C        X  AS A REAL OUTPUT ARRAY IS X(I), I = 1, ..., N, THE LEAST
C           SQUARES SOLUTION, AND X(J), J = N+1, ..., M, THE VECTOR
C           WHOSE LENGTH IS THE MINIMUM OF ALL RESIDUAL B - AX.
C      METHOD
C        THE FACTORED SYSTEM QRX = PAX = PB ARE SOLVED IN THE SEQUENCE
C        OF QY = PB AND RX = Y. FULL RANK FOR THE MATRIX A IS ASSUMED
C        WHICH CAN BE CHECKED BY INTERROGATING THE LOGICAL OUTPUT
C        VARIABLE PRODUCED BY ORTHO.
C      REFERENCES
C        (1) PETER BUSINGER AND G.H. GOLUB, LINEAR LEAST SQUARES SOLU-
C            TIONS BY HOUSEHOLDER TRANSFORMATIONS, NUMER. MATH. 7(1965),
C            269-276.
C        (2) N.K. TSAO AND P.J. NIKOLAI, PROCEDURES USING ORTHOGONAL
C            TRANSFORMATIONS FOR LINEAR LEAST SQUARES PROBLEMS, ARL
C            TECHNICAL REPORT ARL TR 74-0124(1974).
C***********************************************************************
      DIMENSION QR(MS,N), S(N), IP(N), X(M)
      NN = N
      IF(N .EQ. M) NN = N - 1
      DO 8 J = 1, NN
        JP = J + 1
        IJ = IP(J)
        Y = 1. - S(J)
        SAV = X(J)
        X(J) = X(IJ)
        X(IJ) = SAV
C                                       PREMULTIPLY X WITH THE J-TH
C                                       ORTHOGONAL MATRIX.
        SAV = X(J)
        DO 4 K = JP, M
    4     SAV = SAV + QR(K,J)*X(K)
        SS = X(J)
        X(J) = SAV/S(J)
        SS = (SS - X(J))/Y
        DO 5 K = JP, M
    5     X(K) = X(K) - QR(K,J)*SS
    8 CONTINUE
C                                       BACK SUBSTITUTE TO FIND THE
C                                       LEAST SQUARES SOLUTION.
      X(N) = X(N)/QR(N,N)
      NM = N - 1
      DO 13 I = 1, NM
        NI = N - I
        NN = N
        DO 12 J = 1, I
          X(NI) = X(NI) - QR(NI,NN)*X(NN)
   12     NN = NN - 1
        X(NI) = X(NI)/QR(NI,NI)
   13 CONTINUE
      RETURN
      END
