control_systems_torbox 0.2.1

Control systems toolbox
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
      SUBROUTINE NF01BR( COND, UPLO, TRANS, N, IPAR, LIPAR, R, LDR,
     $                   SDIAG, S, LDS, B, RANKS, TOL, DWORK, LDWORK,
     $                   INFO )
C
C     PURPOSE
C
C     To solve one of the systems of linear equations
C
C           R*x = b ,  or  R'*x = b ,
C
C     in the least squares sense, where R is an n-by-n block upper
C     triangular matrix, with the structure
C
C         /   R_1    0    ..   0   |   L_1   \
C         |    0    R_2   ..   0   |   L_2   |
C         |    :     :    ..   :   |    :    | ,
C         |    0     0    ..  R_l  |   L_l   |
C         \    0     0    ..   0   |  R_l+1  /
C
C     with the upper triangular submatrices R_k, k = 1:l+1, square, and
C     the first l of the same order, BSN. The diagonal elements of each
C     block R_k have nonincreasing magnitude. The matrix R is stored in
C     the compressed form, as returned by SLICOT Library routine NF01BS,
C
C              /   R_1  |   L_1   \
C              |   R_2  |   L_2   |
C       Rc =   |    :   |    :    | ,
C              |   R_l  |   L_l   |
C              \    X   |  R_l+1  /
C
C     where the submatrix X is irrelevant. If the matrix R does not have
C     full rank, then a least squares solution is obtained. If l <= 1,
C     then R is an upper triangular matrix and its full upper triangle
C     is stored.
C
C     Optionally, the transpose of the matrix R can be stored in the
C     strict lower triangles of the submatrices R_k, k = 1:l+1, and in
C     the arrays SDIAG and S, as described at the parameter UPLO below.
C
C     ARGUMENTS
C
C     Mode Parameters
C
C     COND    CHARACTER*1
C             Specifies whether the condition of submatrices R_k should
C             be estimated, as follows:
C             = 'E' :  use incremental condition estimation and store
C                      the numerical rank of R_k in the array entry
C                      RANKS(k), for k = 1:l+1;
C             = 'N' :  do not use condition estimation, but check the
C                      diagonal entries of R_k for zero values;
C             = 'U' :  use the ranks already stored in RANKS(1:l+1).
C
C     UPLO    CHARACTER*1
C             Specifies the storage scheme for the matrix R, as follows:
C             = 'U' :  the upper triangular part is stored as in Rc;
C             = 'L' :  the lower triangular part is stored, namely,
C                      - the transpose of the strict upper triangle of
C                        R_k is stored in the strict lower triangle of
C                        R_k, for k = 1:l+1;
C                      - the diagonal elements of R_k, k = 1:l+1, are
C                        stored in the array SDIAG;
C                      - the transpose of the last block column in R
C                        (without R_l+1) is stored in the array S.
C
C     TRANS   CHARACTER*1
C             Specifies the form of the system of equations, as follows:
C             = 'N':  R*x  = b  (No transpose);
C             = 'T':  R'*x = b  (Transpose);
C             = 'C':  R'*x = b  (Transpose).
C
C     Input/Output Parameters
C
C     N       (input) INTEGER
C             The order of the matrix R.  N = BN*BSN + ST >= 0.
C             (See parameter description below.)
C
C     IPAR    (input) INTEGER array, dimension (LIPAR)
C             The integer parameters describing the structure of the
C             matrix R, as follows:
C             IPAR(1) must contain ST, the number of columns of the
C                     submatrices L_k and the order of R_l+1.  ST >= 0.
C             IPAR(2) must contain BN, the number of blocks, l, in the
C                     block diagonal part of R.  BN >= 0.
C             IPAR(3) must contain BSM, the number of rows of the blocks
C                     R_k, k = 1:l.  BSM >= 0.
C             IPAR(4) must contain BSN, the number of columns of the
C                     blocks R_k, k = 1:l.  BSN >= 0.
C             BSM is not used by this routine, but assumed equal to BSN.
C
C     LIPAR   (input) INTEGER
C             The length of the array IPAR.  LIPAR >= 4.
C
C     R       (input) DOUBLE PRECISION array, dimension (LDR, NC)
C             where NC = N if BN <= 1, and NC = BSN+ST, if BN > 1.
C             If UPLO = 'U', the leading N-by-NC part of this array must
C             contain the (compressed) representation (Rc) of the upper
C             triangular matrix R. The submatrix X in Rc and the strict
C             lower triangular parts of the diagonal blocks R_k,
C             k = 1:l+1, are not referenced. If BN <= 1 or BSN = 0, then
C             the full upper triangle of R must be stored.
C             If UPLO = 'L', BN > 1 and BSN > 0, the leading
C             (N-ST)-by-BSN part of this array must contain the
C             transposes of the strict upper triangles of R_k, k = 1:l,
C             stored in the strict lower triangles of R_k, and the
C             strict lower triangle of R_l+1 must contain the transpose
C             of the strict upper triangle of R_l+1. The submatrix X
C             in Rc is not referenced. The diagonal elements of R_k,
C             and, if COND = 'E', the upper triangular parts of R_k,
C             k = 1:l+1, are modified internally, but are restored
C             on exit.
C             If UPLO = 'L' and BN <= 1 or BSN = 0, the leading N-by-N
C             strict lower triangular part of this array must contain
C             the transpose of the strict upper triangular part of R.
C             The diagonal elements and, if COND = 'E', the upper
C             triangular elements are modified internally, but are
C             restored on exit.
C
C     LDR     INTEGER
C             The leading dimension of the array R.  LDR >= MAX(1,N).
C
C     SDIAG   (input) DOUBLE PRECISION array, dimension (N)
C             If UPLO = 'L', this array must contain the diagonal
C             entries of R_k, k = 1:l+1. This array is modified
C             internally, but is restored on exit.
C             This parameter is not referenced if UPLO = 'U'.
C
C     S       (input) DOUBLE PRECISION array, dimension (LDS,N-ST)
C             If UPLO = 'L', BN > 1, and BSN > 0, the leading
C             ST-by-(N-ST) part of this array must contain the transpose
C             of the rectangular part of the last block column in R,
C             that is [ L_1' L_2' ... L_l' ] . If COND = 'E', S is
C             modified internally, but is restored on exit.
C             This parameter is not referenced if UPLO = 'U', or
C             BN <= 1, or BSN = 0.
C
C     LDS     INTEGER
C             The leading dimension of the array S.
C             LDS >= 1,         if UPLO = 'U', or BN <= 1, or BSN = 0;
C             LDS >= MAX(1,ST), if UPLO = 'L', BN > 1, and BSN > 0.
C
C     B       (input/output) DOUBLE PRECISION array, dimension (N)
C             On entry, this array must contain the right hand side
C             vector b.
C             On exit, this array contains the (least squares) solution
C             of the system R*x = b or R'*x = b.
C
C     RANKS   (input or output) INTEGER array, dimension (r), where
C             r = BN + 1,  if ST > 0, BSN > 0, and BN > 1;
C             r = BN,      if ST = 0 and BSN > 0;
C             r = 1,       if ST > 0 and ( BSN = 0 or BN <= 1 );
C             r = 0,       if ST = 0 and BSN = 0.
C             On entry, if COND = 'U' and N > 0, this array must contain
C             the numerical ranks of the submatrices R_k, k = 1:l(+1).
C             On exit, if COND = 'E' or 'N' and N > 0, this array
C             contains the numerical ranks of the submatrices R_k,
C             k = 1:l(+1), estimated according to the value of COND.
C
C     Tolerances
C
C     TOL     DOUBLE PRECISION
C             If COND = 'E', the tolerance to be used for finding the
C             ranks of the submatrices R_k. If the user sets TOL > 0,
C             then the given value of TOL is used as a lower bound for
C             the reciprocal condition number;  a (sub)matrix whose
C             estimated condition number is less than 1/TOL is
C             considered to be of full rank. If the user sets TOL <= 0,
C             then an implicitly computed, default tolerance, defined by
C             TOLDEF = N*EPS,  is used instead, where EPS is the machine
C             precision (see LAPACK Library routine DLAMCH).
C             This parameter is not relevant if COND = 'U' or 'N'.
C
C     Workspace
C
C     DWORK   DOUBLE PRECISION array, dimension (LDWORK)
C
C     LDWORK  INTEGER
C             The length of the array DWORK.
C             Denote Full = ( BN <= 1 or  BSN = 0 );
C                    Comp = ( BN >  1 and BSN > 0 ).
C             LDWORK >= 2*N,           if Full and COND = 'E';
C             LDWORK >= 2*MAX(BSN,ST), if Comp and COND = 'E';
C             LDWORK >= 0,   in the remaining cases.
C
C     Error Indicator
C
C     INFO    INTEGER
C             = 0:  successful exit;
C             < 0:  if INFO = -i, the i-th argument had an illegal
C                   value.
C
C     METHOD
C
C     Block back or forward substitution is used (depending on TRANS
C     and UPLO), exploiting the special structure and storage scheme of
C     the matrix R. If a submatrix R_k, k = 1:l+1, is singular, a local
C     basic least squares solution is computed. Therefore, the returned
C     result is not the basic least squares solution for the whole
C     problem, but a concatenation of (least squares) solutions of the
C     individual subproblems involving R_k, k = 1:l+1 (with adapted
C     right hand sides).
C
C     NUMERICAL ASPECTS
C                                    2    2
C     The algorithm requires 0(BN*BSN + ST + N*ST) operations and is
C     backward stable, if R is nonsingular.
C
C     CONTRIBUTORS
C
C     V. Sima, Research Institute for Informatics, Bucharest, Dec. 2001.
C
C     REVISIONS
C
C     V. Sima, Research Institute for Informatics, Bucharest, Mar. 2005.
C
C     KEYWORDS
C
C     Linear system of equations, matrix operations, plane rotations.
C
C     ******************************************************************
C
C     .. Parameters ..
      DOUBLE PRECISION  ZERO, ONE, SVLMAX
      PARAMETER         ( ZERO = 0.0D0, ONE = 1.0D0, SVLMAX = 0.0D0 )
C     .. Scalar Arguments ..
      CHARACTER         COND, TRANS, UPLO
      INTEGER           INFO, LDR, LDS, LDWORK, LIPAR, N
      DOUBLE PRECISION  TOL
C     .. Array Arguments ..
      INTEGER           IPAR(*), RANKS(*)
      DOUBLE PRECISION  B(*), DWORK(*), R(LDR,*), S(LDS,*), SDIAG(*)
C     .. Local Scalars ..
      DOUBLE PRECISION  TOLDEF
      INTEGER           BN, BSM, BSN, I, I1, J, K, L, NC, NTHS, RANK, ST
      CHARACTER         TRANSL, UPLOL
      LOGICAL           ECOND, FULL, LOWER, NCOND, TRANR
C     .. Local Arrays ..
      DOUBLE PRECISION  DUM(3)
C     .. External Functions ..
      DOUBLE PRECISION  DLAMCH
      LOGICAL           LSAME
      EXTERNAL          DLAMCH, LSAME
C     .. External Subroutines ..
      EXTERNAL          DCOPY, DGEMV, DSWAP, DTRSV, MB03OD, XERBLA
C     .. Intrinsic Functions ..
      INTRINSIC         DBLE, MAX, MIN
C     ..
C     .. Executable Statements ..
C
C     Check the scalar input parameters.
C
      ECOND = LSAME( COND,  'E' )
      NCOND = LSAME( COND,  'N' )
      LOWER = LSAME( UPLO,  'L' )
      TRANR = LSAME( TRANS, 'T' ) .OR. LSAME( TRANS, 'C' )
C
      INFO = 0
      IF( .NOT.( ECOND .OR. NCOND .OR. LSAME( COND,  'U' ) ) ) THEN
         INFO = -1
      ELSEIF( .NOT.( LOWER .OR. LSAME( UPLO,  'U' ) ) ) THEN
         INFO = -2
      ELSEIF( .NOT.( TRANR .OR. LSAME( TRANS, 'N' ) ) ) THEN
         INFO = -3
      ELSEIF( N.LT.0 ) THEN
         INFO = -4
      ELSEIF( LIPAR.LT.4 ) THEN
         INFO = -6
      ELSE
         ST   = IPAR(1)
         BN   = IPAR(2)
         BSM  = IPAR(3)
         BSN  = IPAR(4)
         NTHS = BN*BSN
         FULL = BN.LE.1 .OR. BSN.EQ.0
         IF ( MIN( ST, BN, BSM, BSN ).LT.0 ) THEN
            INFO = -5
         ELSEIF ( N.NE.NTHS + ST ) THEN
            INFO = -4
         ELSEIF ( LDR.LT.MAX( 1, N ) ) THEN
            INFO = -8
         ELSEIF ( LDS.LT.1 .OR. ( LOWER .AND. .NOT.FULL .AND.
     $            LDS.LT.ST ) ) THEN
            INFO = -11
         ELSE
            IF ( ECOND ) THEN
               IF ( FULL ) THEN
                  L = 2*N
               ELSE
                  L = 2*MAX( BSN, ST )
               END IF
            ELSE
               L = 0
            END IF
            IF ( LDWORK.LT.L )
     $         INFO = -16
         END IF
      END IF
C
C     Return if there are illegal arguments.
C
      IF( INFO.NE.0 ) THEN
         CALL XERBLA( 'NF01BR', -INFO )
         RETURN
      END IF
C
C     Quick return if possible.
C
      IF ( N.EQ.0 )
     $   RETURN
C
      IF ( ECOND ) THEN
         TOLDEF = TOL
         IF ( TOLDEF.LE.ZERO ) THEN
C
C           Use the default tolerance in rank determination.
C
            TOLDEF = DBLE( N )*DLAMCH( 'Epsilon' )
         END IF
      END IF
C
      NC = BSN + ST
      IF ( FULL ) THEN
C
C        Special case: l <= 1 or BSN = 0; R is just an upper triangular
C        matrix.
C
         IF ( LOWER ) THEN
C
C           Swap the diagonal elements of R and the elements of SDIAG
C           and, if COND = 'E', swap the upper and lower triangular
C           parts of R, in order to find the numerical rank.
C
            CALL DSWAP( N, R, LDR+1, SDIAG, 1 )
            IF ( ECOND ) THEN
               UPLOL  = 'U'
               TRANSL = TRANS
C
               DO 10 J = 1, N
                  CALL DSWAP( N-J+1, R(J,J), LDR, R(J,J), 1 )
   10          CONTINUE
C
            ELSE
               UPLOL = UPLO
               IF ( TRANR ) THEN
                  TRANSL = 'N'
               ELSE
                  TRANSL = 'T'
               END IF
            END IF
         ELSE
            UPLOL  = UPLO
            TRANSL = TRANS
         END IF
C
         IF ( ECOND ) THEN
C
C           Estimate the reciprocal condition number and set the rank.
C           Workspace: 2*N.
C
            CALL MB03OD( 'No QR', N, N, R, LDR, IPAR, TOLDEF, SVLMAX,
     $                   DWORK, RANK, DUM, DWORK, LDWORK, INFO )
            RANKS(1) = RANK
C
         ELSEIF ( NCOND ) THEN
C
C           Determine rank(R) by checking zero diagonal entries.
C
            RANK = N
C
            DO 20 J = 1, N
               IF ( R(J,J).EQ.ZERO .AND. RANK.EQ.N )
     $            RANK = J - 1
   20       CONTINUE
C
            RANKS(1) = RANK
C
         ELSE
C
C           Use the stored rank.
C
            RANK = RANKS(1)
         END IF
C
C        Solve R*x = b, or R'*x = b using back or forward substitution.
C
         DUM(1) = ZERO
         IF ( RANK.LT.N )
     $      CALL DCOPY( N-RANK, DUM, 0, B(RANK+1), 1 )
         CALL DTRSV( UPLOL, TRANSL, 'NonUnit', RANK, R, LDR, B, 1 )
C
         IF ( LOWER ) THEN
C
C           Swap the diagonal elements of R and the elements of SDIAG
C           and, if COND = 'E', swap back the upper and lower triangular
C           parts of R.
C
            CALL DSWAP( N, R, LDR+1, SDIAG, 1 )
            IF ( ECOND ) THEN
C
               DO 30 J = 1, N
                  CALL DSWAP( N-J+1, R(J,J), LDR, R(J,J), 1 )
   30          CONTINUE
C
            END IF
C
         END IF
         RETURN
      END IF
C
C     General case: l > 1 and BSN > 0.
C
      I = 1
      L = BN
      IF ( ECOND ) THEN
C
C        Estimate the reciprocal condition numbers and set the ranks.
C
         IF ( LOWER ) THEN
C
C           Swap the diagonal elements of R and the elements of SDIAG
C           and swap the upper and lower triangular parts of R, in order
C           to find the numerical rank. Swap S and the transpose of the
C           rectangular part of the last block column of R.
C
            DO 50 K = 1, BN
               CALL DSWAP( BSN, R(I,1), LDR+1, SDIAG(I), 1 )
C
               DO 40 J = 1, BSN
                  CALL DSWAP( BSN-J+1, R(I,J), LDR, R(I,J), 1 )
                  I = I + 1
   40          CONTINUE
C
   50       CONTINUE
C
            IF ( ST.GT.0 ) THEN
               CALL DSWAP( ST, R(I,BSN+1), LDR+1, SDIAG(I), 1 )
C
               DO 60 J = BSN + 1, NC
                  CALL DSWAP( NTHS, R(1,J), 1, S(J-BSN,1), LDS )
                  CALL DSWAP( NC-J+1, R(I,J), LDR, R(I,J), 1 )
                  I = I + 1
   60          CONTINUE
C
            END IF
C
         END IF
C
         I1 = 1
C
C        Determine rank(R_k) using incremental condition estimation.
C        Workspace 2*MAX(BSN,ST).
C
         DO 70 K = 1, BN
            CALL MB03OD( 'No QR', BSN, BSN, R(I1,1), LDR, IPAR, TOLDEF,
     $                   SVLMAX, DWORK, RANKS(K), DUM, DWORK, LDWORK,
     $                   INFO )
            I1 = I1 + BSN
   70    CONTINUE
C
         IF ( ST.GT.0 ) THEN
            L = L + 1
            CALL MB03OD( 'No QR', ST, ST, R(I1,BSN+1), LDR, IPAR,
     $                   TOLDEF, SVLMAX, DWORK, RANKS(L), DUM, DWORK,
     $                   LDWORK, INFO )
         END IF
C
      ELSEIF ( NCOND ) THEN
C
C        Determine rank(R_k) by checking zero diagonal entries.
C
         IF ( LOWER ) THEN
C
            DO 90 K = 1, BN
               RANK = BSN
C
               DO 80 J = 1, BSN
                  IF ( SDIAG(I).EQ.ZERO .AND. RANK.EQ.BSN )
     $               RANK = J - 1
                  I = I + 1
   80          CONTINUE
C
               RANKS(K) = RANK
   90       CONTINUE
C
            IF ( ST.GT.0 ) THEN
               L = L + 1
               RANK = ST
C
               DO 100 J = 1, ST
                  IF ( SDIAG(I).EQ.ZERO .AND. RANK.EQ.ST )
     $               RANK = J - 1
                  I = I + 1
  100          CONTINUE
C
               RANKS(L) = RANK
            END IF
C
         ELSE
C
            DO 120 K = 1, BN
               RANK = BSN
C
               DO 110 J = 1, BSN
                  IF ( R(I,J).EQ.ZERO .AND. RANK.EQ.BSN )
     $               RANK = J - 1
                  I = I + 1
  110          CONTINUE
C
               RANKS(K) = RANK
  120       CONTINUE
C
            IF ( ST.GT.0 ) THEN
               L = L + 1
               RANK = ST
C
               DO 130 J = BSN + 1, NC
                  IF ( R(I,J).EQ.ZERO .AND. RANK.EQ.ST )
     $               RANK = J - BSN - 1
                  I = I + 1
  130          CONTINUE
C
               RANKS(L) = RANK
            END IF
         END IF
C
      ELSE
C
C        Set the number of elements of RANKS. Then use the stored ranks.
C
         IF ( ST.GT.0 )
     $      L = L + 1
      END IF
C
C     Solve the triangular system for x. If the system is singular,
C     then obtain a basic least squares solution.
C
      DUM(1) = ZERO
      IF ( LOWER .AND. .NOT.ECOND ) THEN
C
         IF ( .NOT.TRANR ) THEN
C
C           Solve R*x = b using back substitution, with R' stored in
C           the arrays R, SDIAG and S. Swap diag(R) and SDIAG.
C
            I1 = NTHS + 1
            IF ( ST.GT.0 ) THEN
               RANK = RANKS(L)
               IF ( RANK.LT.ST )
     $            CALL DCOPY( ST-RANK, DUM, 0, B(I1+RANK), 1 )
               CALL DSWAP( ST, R(I1,BSN+1), LDR+1, SDIAG(I1), 1 )
               CALL DTRSV( 'Lower', 'Transpose', 'NonUnit', RANK,
     $                     R(I1,BSN+1), LDR, B(I1), 1 )
               CALL DSWAP( ST, R(I1,BSN+1), LDR+1, SDIAG(I1), 1 )
               CALL DGEMV( 'Transpose', ST, NTHS, -ONE, S, LDS,
     $                     B(NTHS+1), 1, ONE, B, 1 )
            END IF
C
            DO 140 K = BN, 1, -1
               I1   = I1 - BSN
               RANK = RANKS(K)
               IF ( RANK.LT.BSN )
     $            CALL DCOPY( BSN-RANK, DUM, 0, B(I1+RANK), 1 )
               CALL DSWAP( BSN, R(I1,1), LDR+1, SDIAG(I1), 1 )
               CALL DTRSV( 'Lower', 'Transpose', 'NonUnit', RANK,
     $                     R(I1,1), LDR, B(I1), 1 )
               CALL DSWAP( BSN, R(I1,1), LDR+1, SDIAG(I1), 1 )
  140       CONTINUE
C
         ELSE
C
C           Solve R'*x = b using forward substitution, with R' stored in
C           the arrays R, SDIAG and S. Swap diag(R) and SDIAG.
C
            I1 = 1
            IF ( TRANR ) THEN
               TRANSL = 'N'
            ELSE
               TRANSL = 'T'
            END IF
C
            DO 150 K = 1, BN
               RANK = RANKS(K)
               IF ( RANK.LT.BSN )
     $            CALL DCOPY( BSN-RANK, DUM, 0, B(I1+RANK), 1 )
               CALL DSWAP( BSN, R(I1,1), LDR+1, SDIAG(I1), 1 )
               CALL DTRSV( 'Lower', TRANSL, 'NonUnit', RANK, R(I1,1),
     $                     LDR, B(I1), 1 )
               CALL DSWAP( BSN, R(I1,1), LDR+1, SDIAG(I1), 1 )
               I1 = I1 + BSN
  150       CONTINUE
C
            IF ( ST.GT.0 ) THEN
               RANK = RANKS(L)
               IF ( RANK.LT.ST )
     $            CALL DCOPY( ST-RANK, DUM, 0, B(I1+RANK), 1 )
               CALL DGEMV( 'NoTranspose', ST, NTHS, -ONE, S, LDS, B, 1,
     $                     ONE, B(I1), 1 )
               CALL DSWAP( ST, R(I1,BSN+1), LDR+1, SDIAG(I1), 1 )
               CALL DTRSV( 'Lower', TRANSL, 'NonUnit', RANK,
     $                     R(I1,BSN+1), LDR, B(I1), 1 )
               CALL DSWAP( ST, R(I1,BSN+1), LDR+1, SDIAG(I1), 1 )
            END IF
C
         END IF
C
      ELSE
C
         IF ( .NOT.TRANR ) THEN
C
C           Solve R*x = b using back substitution.
C
            I1 = NTHS + 1
            IF ( ST.GT.0 ) THEN
               RANK = RANKS(L)
               IF ( RANK.LT.ST )
     $            CALL DCOPY( ST-RANK, DUM, 0, B(I1+RANK), 1 )
               CALL DTRSV( 'Upper', TRANS, 'NonUnit', RANK, R(I1,BSN+1),
     $                     LDR, B(I1), 1 )
               CALL DGEMV( TRANS, NTHS, ST, -ONE, R(1,BSN+1), LDR,
     $                     B(NTHS+1), 1, ONE, B, 1 )
            END IF
C
            DO 160 K = BN, 1, -1
               I1   = I1 - BSN
               RANK = RANKS(K)
               IF ( RANK.LT.BSN )
     $            CALL DCOPY( BSN-RANK, DUM, 0, B(I1+RANK), 1 )
               CALL DTRSV( 'Upper', TRANS, 'NonUnit', RANK, R(I1,1),
     $                     LDR, B(I1), 1 )
  160       CONTINUE
C
         ELSE
C
C           Solve R'*x = b using forward substitution.
C
            I1 = 1
C
            DO 170 K = 1, BN
               RANK = RANKS(K)
               IF ( RANK.LT.BSN )
     $            CALL DCOPY( BSN-RANK, DUM, 0, B(I1+RANK), 1 )
               CALL DTRSV( 'Upper', TRANS, 'NonUnit', RANK, R(I1,1),
     $                     LDR, B(I1), 1 )
               I1 = I1 + BSN
  170       CONTINUE
C
            IF ( ST.GT.0 ) THEN
               RANK = RANKS(L)
               IF ( RANK.LT.ST )
     $            CALL DCOPY( ST-RANK, DUM, 0, B(I1+RANK), 1 )
               CALL DGEMV( TRANS, NTHS, ST, -ONE, R(1,BSN+1), LDR, B, 1,
     $                     ONE, B(I1), 1 )
               CALL DTRSV( 'Upper', TRANS, 'NonUnit', RANK, R(I1,BSN+1),
     $                     LDR, B(I1), 1 )
            END IF
C
         END IF
      END IF
C
      IF ( ECOND .AND. LOWER ) THEN
         I = 1
C
C        If COND = 'E' and UPLO = 'L', swap the diagonal elements of R
C        and the elements of SDIAG and swap back the upper and lower
C        triangular parts of R, including the part corresponding to S.
C
         DO 190 K = 1, BN
            CALL DSWAP( BSN, R(I,1), LDR+1, SDIAG(I), 1 )
C
            DO 180 J = 1, BSN
               CALL DSWAP( BSN-J+1, R(I,J), LDR, R(I,J), 1 )
               I = I + 1
  180       CONTINUE
C
  190    CONTINUE
C
         IF ( ST.GT.0 ) THEN
            CALL DSWAP( ST, R(I,BSN+1), LDR+1, SDIAG(I), 1 )
C
            DO 200 J = BSN + 1, NC
               CALL DSWAP( NTHS, R(1,J), 1, S(J-BSN,1), LDS )
               CALL DSWAP( NC-J+1, R(I,J), LDR, R(I,J), 1 )
               I = I + 1
  200       CONTINUE
C
         END IF
C
      END IF
C
      RETURN
C
C *** Last line of NF01BR ***
      END