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
      SUBROUTINE SB02RU( DICO, HINV, TRANA, UPLO, N, A, LDA, G, LDG, Q,
     $                   LDQ, S, LDS, IWORK, DWORK, LDWORK, INFO )
C
C     PURPOSE
C
C     To construct the 2n-by-2n Hamiltonian or symplectic matrix S
C     associated to the linear-quadratic optimization problem, used to
C     solve the continuous- or discrete-time algebraic Riccati equation,
C     respectively.
C
C     For a continuous-time problem, S is defined by
C
C             ( op(A)   -G    )
C         S = (               ),                                     (1)
C             (  -Q   -op(A)' )
C
C     and for a discrete-time problem by
C
C                     -1              -1
C             (  op(A)           op(A)  *G       )
C         S = (        -1                   -1   ),                  (2)
C             ( Q*op(A)     op(A)' + Q*op(A)  *G )
C
C     or
C                              -T             -T
C             ( op(A) + G*op(A)  *Q   -G*op(A)   )
C         S = (           -T                 -T  ),                  (3)
C             (     -op(A)  *Q          op(A)    )
C
C     where op(A) = A or A' (A**T), A, G, and Q are n-by-n matrices,
C     with G and Q symmetric. Matrix A must be nonsingular in the
C     discrete-time case.
C
C     ARGUMENTS
C
C     Mode Parameters
C
C     DICO    CHARACTER*1
C             Specifies the type of the system as follows:
C             = 'C':  Continuous-time system;
C             = 'D':  Discrete-time system.
C
C     HINV    CHARACTER*1
C             If DICO = 'D', specifies which of the matrices (2) or (3)
C             is constructed, as follows:
C             = 'D':  The matrix S in (2) is constructed;
C             = 'I':  The (inverse) matrix S in (3) is constructed.
C             HINV is not referenced if DICO = 'C'.
C
C     TRANA   CHARACTER*1
C             Specifies the form of op(A) to be used, as follows:
C             = 'N':  op(A) = A    (No transpose);
C             = 'T':  op(A) = A**T (Transpose);
C             = 'C':  op(A) = A**T (Conjugate transpose = Transpose).
C
C     UPLO    CHARACTER*1
C             Specifies which triangle of the matrices G and Q is
C             stored, as follows:
C             = 'U':  Upper triangle is stored;
C             = 'L':  Lower triangle is stored.
C
C     Input/Output Parameters
C
C     N       (input) INTEGER
C             The order of the matrices A, G, and Q.  N >= 0.
C
C     A       (input) DOUBLE PRECISION array, dimension (LDA,N)
C             The leading N-by-N part of this array must contain the
C             matrix A.
C
C     LDA     INTEGER
C             The leading dimension of the array A.  LDA >= MAX(1,N).
C
C     G       (input/output) DOUBLE PRECISION array, dimension (LDG,N)
C             On entry, the leading N-by-N upper triangular part (if
C             UPLO = 'U') or lower triangular part (if UPLO = 'L') of
C             this array must contain the upper triangular part or lower
C             triangular part, respectively, of the symmetric matrix G.
C             On exit, if DICO = 'D', the leading N-by-N part of this
C             array contains the symmetric matrix G fully stored.
C             If DICO = 'C', this array is not modified on exit, and the
C             strictly lower triangular part (if UPLO = 'U') or strictly
C             upper triangular part (if UPLO = 'L') is not referenced.
C
C     LDG     INTEGER
C             The leading dimension of the array G.  LDG >= MAX(1,N).
C
C     Q       (input/output) DOUBLE PRECISION array, dimension (LDQ,N)
C             On entry, the leading N-by-N upper triangular part (if
C             UPLO = 'U') or lower triangular part (if UPLO = 'L') of
C             this array must contain the upper triangular part or lower
C             triangular part, respectively, of the symmetric matrix Q.
C             On exit, if DICO = 'D', the leading N-by-N part of this
C             array contains the symmetric matrix Q fully stored.
C             If DICO = 'C', this array is not modified on exit, and the
C             strictly lower triangular part (if UPLO = 'U') or strictly
C             upper triangular part (if UPLO = 'L') is not referenced.
C
C     LDQ     INTEGER
C             The leading dimension of the array Q.  LDQ >= MAX(1,N).
C
C     S       (output) DOUBLE PRECISION array, dimension (LDS,2*N)
C             If INFO = 0, the leading 2N-by-2N part of this array
C             contains the Hamiltonian or symplectic matrix of the
C             problem.
C
C     LDS     INTEGER
C             The leading dimension of the array S.  LDS >= MAX(1,2*N).
C
C     Workspace
C
C     IWORK   INTEGER array, dimension (LIWORK), where
C             LIWORK >= 0,   if DICO = 'C';
C             LIWORK >= 2*N, if DICO = 'D'.
C
C     DWORK   DOUBLE PRECISION array, dimension (LDWORK)
C             On exit, if DICO = 'D', DWORK(1) returns the reciprocal
C             condition number  RCOND  of the given matrix  A,  and
C             DWORK(2) returns the reciprocal pivot growth factor
C             norm(A)/norm(U) (see SLICOT Library routine MB02PD).
C             If DWORK(2) is much less than 1, then the computed  S
C             and  RCOND  could be unreliable. If 0 < INFO <= N, then
C             DWORK(2) contains the reciprocal pivot growth factor for
C             the leading INFO columns of  A.
C
C     LDWORK  INTEGER
C             The length of the array DWORK.
C             LDWORK >= 0,          if DICO = 'C';
C             LDWORK >= MAX(2,6*N), if DICO = 'D'.
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             = i:  if the leading i-by-i (1 <= i <= N) upper triangular
C                   submatrix of A is singular in discrete-time case;
C             = N+1:  if matrix A is numerically singular in discrete-
C                   time case.
C
C     METHOD
C
C     For a continuous-time problem, the 2n-by-2n Hamiltonian matrix (1)
C     is constructed.
C     For a discrete-time problem, the 2n-by-2n symplectic matrix (2) or
C     (3) - the inverse of the matrix in (2) - is constructed.
C
C     NUMERICAL ASPECTS
C
C     The discrete-time case needs the inverse of the matrix A, hence
C     the routine should not be used when A is ill-conditioned.
C                               3
C     The algorithm requires 0(n ) floating point operations in the
C     discrete-time case.
C
C     FURTHER COMMENTS
C
C     This routine is a functionally extended and with improved accuracy
C     version of the SLICOT Library routine SB02MU. Transposed problems
C     can be dealt with as well. The LU factorization of  op(A)  (with
C     no equilibration) and iterative refinement are used for solving
C     the various linear algebraic systems involved.
C
C     CONTRIBUTOR
C
C     V. Sima, Research Institute for Informatics, Bucharest, Apr. 1999.
C
C     REVISIONS
C
C     -
C
C     KEYWORDS
C
C     Algebraic Riccati equation, closed loop system, continuous-time
C     system, discrete-time system, optimal regulator, Schur form.
C
C     ******************************************************************
C
C     .. Parameters ..
      DOUBLE PRECISION  ZERO, ONE
      PARAMETER         ( ZERO = 0.0D0, ONE = 1.0D0 )
C     .. Scalar Arguments ..
      CHARACTER         DICO, HINV, TRANA, UPLO
      INTEGER           INFO, LDA, LDG, LDQ, LDS, LDWORK, N
C     .. Array Arguments ..
      INTEGER           IWORK(*)
      DOUBLE PRECISION  A(LDA,*), DWORK(*), G(LDG,*), Q(LDQ,*),
     $                  S(LDS,*)
C     .. Local Scalars ..
      CHARACTER         EQUED, TRANAT
      LOGICAL           DISCR, LHINV, LUPLO, NOTRNA
      INTEGER           I, J, N2, NJ, NP1
      DOUBLE PRECISION  PIVOTG, RCOND, RCONDA, TEMP
C     .. External Functions ..
      LOGICAL           LSAME
      EXTERNAL          LSAME
C     .. External Subroutines ..
      EXTERNAL          DCOPY, DGEMM, DLACPY, DLASET, DSWAP, MA02AD,
     $                  MA02ED, MB02PD, XERBLA
C     .. Intrinsic Functions ..
      INTRINSIC         MAX
C     .. Executable Statements ..
C
      N2 = N + N
      INFO = 0
      DISCR  = LSAME( DICO,  'D' )
      LUPLO  = LSAME( UPLO,  'U' )
      NOTRNA = LSAME( TRANA, 'N' )
      IF( DISCR )
     $   LHINV = LSAME( HINV, 'D' )
C
C     Test the input scalar arguments.
C
      IF( .NOT.DISCR .AND. .NOT.LSAME( DICO, 'C' ) ) THEN
         INFO = -1
      ELSE IF( DISCR ) THEN
         IF( .NOT.LHINV .AND. .NOT.LSAME( HINV, 'I' ) )
     $      INFO = -2
      ELSE IF( INFO.EQ.0 ) THEN
         IF( .NOT.NOTRNA .AND. .NOT.LSAME( TRANA, 'T' )
     $                   .AND. .NOT.LSAME( TRANA, 'C' ) ) THEN
            INFO = -3
         ELSE IF( .NOT.LUPLO .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
            INFO = -4
         ELSE IF( N.LT.0 ) THEN
            INFO = -5
         ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
            INFO = -7
         ELSE IF( LDG.LT.MAX( 1, N ) ) THEN
            INFO = -9
         ELSE IF( LDQ.LT.MAX( 1, N ) ) THEN
            INFO = -11
         ELSE IF( LDS.LT.MAX( 1, N2 ) ) THEN
            INFO = -13
         ELSE IF( ( LDWORK.LT.0 ) .OR.
     $            ( DISCR .AND. LDWORK.LT.MAX( 2, 6*N ) ) ) THEN
            INFO = -16
         END IF
      END IF
C
      IF ( INFO.NE.0 ) THEN
C
C        Error return.
C
         CALL XERBLA( 'SB02RU', -INFO )
         RETURN
      END IF
C
C     Quick return if possible.
C
      IF ( N.EQ.0 ) THEN
         IF ( DISCR ) THEN
            DWORK(1) = ONE
            DWORK(2) = ONE
         END IF
         RETURN
      END IF
C
C     The code tries to exploit data locality as much as possible,
C     assuming that LDS is greater than LDA, LDQ, and/or LDG.
C
      IF ( .NOT.DISCR ) THEN
C
C        Continuous-time case: Construct Hamiltonian matrix column-wise.
C
C        Copy op(A) in S(1:N,1:N), and construct full Q
C        in S(N+1:2*N,1:N) and change the sign.
C
         DO 100 J = 1, N
            IF ( NOTRNA ) THEN
               CALL DCOPY( N, A(1,J), 1, S(1,J), 1 )
            ELSE
               CALL DCOPY( N, A(J,1), LDA, S(1,J), 1 )
            END IF
C
            IF ( LUPLO ) THEN
C
               DO 20 I = 1, J
                  S(N+I,J) = -Q(I,J)
   20          CONTINUE
C
               DO 40 I = J + 1, N
                  S(N+I,J) = -Q(J,I)
   40          CONTINUE
C
            ELSE
C
               DO 60 I = 1, J - 1
                  S(N+I,J) = -Q(J,I)
   60          CONTINUE
C
               DO 80 I = J, N
                  S(N+I,J) = -Q(I,J)
   80          CONTINUE
C
            END IF
  100    CONTINUE
C
C        Construct full G in S(1:N,N+1:2*N) and change the sign, and
C        construct -op(A)' in S(N+1:2*N,N+1:2*N).
C
         DO 240 J = 1, N
            NJ = N + J
            IF ( LUPLO ) THEN
C
               DO 120 I = 1, J
                  S(I,NJ) = -G(I,J)
  120          CONTINUE
C
               DO 140 I = J + 1, N
                  S(I,NJ) = -G(J,I)
  140          CONTINUE
C
            ELSE
C
               DO 160 I = 1, J - 1
                  S(I,NJ) = -G(J,I)
  160          CONTINUE
C
               DO 180 I = J, N
                  S(I,NJ) = -G(I,J)
  180          CONTINUE
C
            END IF
C
            IF ( NOTRNA ) THEN
C
               DO 200 I = 1, N
                  S(N+I,NJ) = -A(J,I)
  200          CONTINUE
C
            ELSE
C
               DO 220 I = 1, N
                  S(N+I,NJ) = -A(I,J)
  220          CONTINUE
C
            END IF
  240    CONTINUE
C
      ELSE
C
C        Discrete-time case: Construct the symplectic matrix (2) or (3).
C
C        Fill in the remaining triangles of the symmetric matrices Q
C        and G.
C
         CALL MA02ED( UPLO, N, Q, LDQ )
         CALL MA02ED( UPLO, N, G, LDG )
C
C        Prepare the construction of S in (2) or (3).
C
         NP1 = N + 1
         IF ( NOTRNA ) THEN
            TRANAT = 'T'
         ELSE
            TRANAT = 'N'
         END IF
C
C        Solve  op(A)'*X = Q  in  S(N+1:2*N,1:N),  using the LU
C        factorization of  op(A),  obtained in  S(1:N,1:N),  and
C        iterative refinement. No equilibration of  A  is used.
C        Workspace:  6*N.
C
         CALL MB02PD( 'No equilibration', TRANAT, N, N, A, LDA, S,
     $                LDS, IWORK, EQUED, DWORK, DWORK, Q, LDQ,
     $                S(NP1,1), LDS, RCOND, DWORK, DWORK(NP1),
     $                IWORK(NP1), DWORK(N2+1), INFO )
C
C        Return if the matrix is exactly singular or singular to
C        working precision.
C
         IF( INFO.GT.0 ) THEN
            DWORK(1) = RCOND
            DWORK(2) = DWORK(N2+1)
            RETURN
         END IF
C
         RCONDA = RCOND
         PIVOTG = DWORK(N2+1)
C
         IF ( LHINV ) THEN
C
C           Complete the construction of S in (2).
C
C           Transpose  X  in-situ.
C
            DO 260 J = 1, N - 1
               CALL DSWAP( N-J, S(NP1+J,J), 1, S(N+J,J+1), LDS )
  260       CONTINUE
C
C           Solve  op(A)*X = I_n  in  S(N+1:2*N,N+1:2*N),  using the LU
C           factorization of  op(A),  computed in  S(1:N,1:N),  and
C           iterative refinement.
C
            CALL DLASET( 'Full', N, N, ZERO, ONE, S(1,NP1), LDS )
            CALL MB02PD( 'Factored', TRANA, N, N, A, LDA, S, LDS, IWORK,
     $                   EQUED, DWORK, DWORK, S(1,NP1), LDS, S(NP1,NP1),
     $                   LDS, RCOND, DWORK, DWORK(NP1), IWORK(NP1),
     $                   DWORK(N2+1), INFO )
C
C           Solve  op(A)*X = G  in  S(1:N,N+1:2*N),  using the LU
C           factorization of  op(A),  computed in  S(1:N,1:N),  and
C           iterative refinement.
C
            CALL MB02PD( 'Factored', TRANA, N, N, A, LDA, S, LDS, IWORK,
     $                   EQUED, DWORK, DWORK, G, LDG, S(1,NP1), LDS,
     $                   RCOND, DWORK, DWORK(NP1), IWORK(NP1),
     $                   DWORK(N2+1), INFO )
C
C                      -1
C           Copy  op(A)    from  S(N+1:2*N,N+1:2*N)  in  S(1:N,1:N).
C
            CALL DLACPY( 'Full', N, N, S(NP1,NP1), LDS, S, LDS )
C
C                                    -1
C           Compute  op(A)' + Q*op(A)  *G  in  S(N+1:2*N,N+1:2*N).
C
            IF ( NOTRNA ) THEN
               CALL MA02AD( 'Full', N, N, A, LDA, S(NP1,NP1), LDS )
            ELSE
               CALL DLACPY( 'Full', N, N, A, LDA, S(NP1,NP1), LDS )
            END IF
            CALL DGEMM( 'No transpose', 'No transpose', N, N, N, ONE,
     $                  Q, LDQ, S(1,NP1), LDS, ONE, S(NP1,NP1), LDS )
C
         ELSE
C
C           Complete the construction of S in (3).
C
C           Change the sign of  X.
C
            DO 300 J = 1, N
C
               DO 280 I = NP1, N2
                  S(I,J) = -S(I,J)
  280          CONTINUE
C
  300       CONTINUE
C
C           Solve  op(A)'*X = I_n  in  S(N+1:2*N,N+1:2*N),  using the LU
C           factorization of  op(A),  computed in  S(1:N,1:N),  and
C           iterative refinement.
C
            CALL DLASET( 'Full', N, N, ZERO, ONE, S(1,NP1), LDS )
            CALL MB02PD( 'Factored', TRANAT, N, N, A, LDA, S, LDS,
     $                   IWORK, EQUED, DWORK, DWORK, S(1,NP1), LDS,
     $                   S(NP1,NP1), LDS, RCOND, DWORK, DWORK(NP1),
     $                   IWORK(NP1), DWORK(N2+1), INFO )
C
C           Solve  op(A)*X' = -G  in  S(1:N,N+1:2*N),  using the LU
C           factorization of  op(A),  obtained in  S(1:N,1:N),  and
C           iterative refinement.
C
            CALL MB02PD( 'Factored', TRANA, N, N, A, LDA, S, LDS, IWORK,
     $                   EQUED, DWORK, DWORK, G, LDG, S(1,NP1), LDS,
     $                   RCOND, DWORK, DWORK(NP1), IWORK(NP1),
     $                   DWORK(N2+1), INFO )
C
C           Change the sign of  X  and transpose it in-situ.
C
            DO 340 J = NP1, N2
C
               DO 320 I = 1, N
                  TEMP   = -S(I,J)
                  S(I,J) = -S(J-N,I+N)
                  S(J-N,I+N) = TEMP
  320          CONTINUE
C
  340       CONTINUE
C                                   -T
C           Compute  op(A) + G*op(A)  *Q  in  S(1:N,1:N).
C
            IF ( NOTRNA ) THEN
               CALL DLACPY( 'Full', N, N, A, LDA, S, LDS )
            ELSE
               CALL MA02AD( 'Full', N, N, A, LDA, S, LDS )
            END IF
            CALL DGEMM( 'No transpose', 'No transpose', N, N, N, -ONE,
     $                  G, LDG, S(NP1,1), LDS, ONE, S, LDS )
C
         END IF
         DWORK(1) = RCONDA
         DWORK(2) = PIVOTG
      END IF
      RETURN
C
C *** Last line of SB02RU ***
      END