      SUBROUTINE BNDSOL (MODE,G,MDG,NB,IP,IR,X,N,RNORM)                 BSL00100
C     C.L.LAWSON AND R.J.HANSON, JET PROPULSION LABORATORY, 1973 JUN 12 BSL00200
C     TO APPEAR IN 'SOLVING LEAST SQUARES PROBLEMS', PRENTICE-HALL, 1974BSL00300
C          SEQUENTIAL SOLUTION OF A BANDED LEAST SQUARES PROBLEM..      BSL00400
C          SOLUTION PHASE.   FOR THE ACCUMULATION PHASE USE BNDACC.     BSL00500
C                                                                       BSL00600
C     MODE = 1     SOLVE R*X=Y   WHERE R AND Y ARE IN THE G( ) ARRAY    BSL00700
C                  AND X WILL BE STORED IN THE X( ) ARRAY.              BSL00800
C            2     SOLVE (R**T)*X=Y   WHERE R IS IN G( ),               BSL00900
C                  Y IS INITIALLY IN X( ), AND X REPLACES Y IN X( ),    BSL01000
C            3     SOLVE R*X=Y   WHERE R IS IN G( ).                    BSL01100
C                  Y IS INITIALLY IN X( ), AND X REPLACES Y IN X( ).    BSL01200
C                                                                       BSL01300
C     THE SECOND SUBSCRIPT OF G( ) MUST BE DIMENSIONED AT LEAST         BSL01400
C     NB+1 IN THE CALLING PROGRAM.                                      BSL01500
      DIMENSION G(MDG,1),X(N)                                           BSL01600
      ZERO=0.                                                           BSL01700
C                                                                       BSL01800
      RNORM=ZERO                                                        BSL01900
      GO TO (10,90,50), MODE                                            BSL02000
C                                   ********************* MODE = 1      BSL02100
C                                   ALGC STEP 26                        BSL02200
   10      DO 20 J=1,N                                                  BSL02300
   20      X(J)=G(J,NB+1)                                               BSL02400
      RSQ=ZERO                                                          BSL02500
      NP1=N+1                                                           BSL02600
      IRM1=IR-1                                                         BSL02700
      IF (NP1.GT.IRM1) GO TO 40                                         BSL02800
           DO 30 J=NP1,IRM1                                             BSL02900
   30      RSQ=RSQ+G(J,NB+1)**2                                         BSL03000
      RNORM=SQRT(RSQ)                                                   BSL03100
   40 CONTINUE                                                          BSL03200
C                                   ********************* MODE = 3      BSL03300
C                                   ALG. STEP 27                        BSL03400
   50      DO 80 II=1,N                                                 BSL03500
           I=N+1-II                                                     BSL03600
C                                   ALG. STEP 28                        BSL03700
           S=ZERO                                                       BSL03800
           L=MAX0(0,I-IP)                                               BSL03900
C                                   ALG. STEP 29                        BSL04000
           IF (I.EQ.N) GO TO 70                                         BSL04100
C                                   ALG. STEP 30                        BSL04200
           IE=MIN0(N+1-I,NB)                                            BSL04300
                DO 60 J=2,IE                                            BSL04400
                JG=J+L                                                  BSL04500
                IX=I-1+J                                                BSL04600
   60           S=S+G(I,JG)*X(IX)                                       BSL04700
C                                   ALG. STEP 31                        BSL04800
   70      IF (G(I,L+1)) 80,130,80                                      BSL04900
   80      X(I)=(X(I)-S)/G(I,L+1)                                       BSL05000
C                                   ALG. STEP 32                        BSL05100
      RETURN                                                            BSL05200
C                                   ********************* MODE = 2      BSL05300
   90      DO 120 J=1,N                                                 BSL05400
           S=ZERO                                                       BSL05500
           IF (J.EQ.1) GO TO 110                                        BSL05600
           I1=MAX0(1,J-NB+1)                                            BSL05700
           I2=J-1                                                       BSL05800
                DO 100 I=I1,I2                                          BSL05900
                L=J-I+1+MAX0(0,I-IP)                                    BSL06000
  100           S=S+X(I)*G(I,L)                                         BSL06100
  110      L=MAX0(0,J-IP)                                               BSL06200
           IF (G(J,L+1)) 120,130,120                                    BSL06300
  120      X(J)=(X(J)-S)/G(J,L+1)                                       BSL06400
      RETURN                                                            BSL06500
C                                                                       BSL06600
  130 WRITE (6,140) MODE,I,J,L                                          BSL06700
      STOP                                                              BSL06800
  140 FORMAT (30H0ZERO DIAGONAL TERM IN BNDSOL.,12H MODE,I,J,L=,4I6)    BSL06900
      END                                                               BSL07000
