File: IB01PD.f

package info (click to toggle)
dynare 4.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 40,640 kB
  • sloc: fortran: 82,231; cpp: 72,734; ansic: 28,874; pascal: 13,241; sh: 4,300; objc: 3,281; yacc: 2,833; makefile: 1,288; lex: 1,162; python: 162; lisp: 54; xml: 8
file content (1232 lines) | stat: -rw-r--r-- 50,515 bytes parent folder | download
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
      SUBROUTINE IB01PD( METH, JOB, JOBCV, NOBR, N, M, L, NSMPL, R,
     $                   LDR, A, LDA, C, LDC, B, LDB, D, LDD, Q, LDQ,
     $                   RY, LDRY, S, LDS, O, LDO, TOL, IWORK, DWORK,
     $                   LDWORK, IWARN, INFO )
C
C     SLICOT RELEASE 5.0.
C
C     Copyright (c) 2002-2009 NICONET e.V.
C
C     This program is free software: you can redistribute it and/or
C     modify it under the terms of the GNU General Public License as
C     published by the Free Software Foundation, either version 2 of
C     the License, or (at your option) any later version.
C
C     This program is distributed in the hope that it will be useful,
C     but WITHOUT ANY WARRANTY; without even the implied warranty of
C     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
C     GNU General Public License for more details.
C
C     You should have received a copy of the GNU General Public License
C     along with this program.  If not, see
C     <http://www.gnu.org/licenses/>.
C
C     PURPOSE
C
C     To estimate the matrices A, C, B, and D of a linear time-invariant
C     (LTI) state space model, using the singular value decomposition
C     information provided by other routines. Optionally, the system and
C     noise covariance matrices, needed for the Kalman gain, are also
C     determined.
C
C     ARGUMENTS
C
C     Mode Parameters
C
C     METH    CHARACTER*1
C             Specifies the subspace identification method to be used,
C             as follows:
C             = 'M':  MOESP  algorithm with past inputs and outputs;
C             = 'N':  N4SID  algorithm.
C
C     JOB     CHARACTER*1
C             Specifies which matrices should be computed, as follows:
C             = 'A':  compute all system matrices, A, B, C, and D;
C             = 'C':  compute the matrices A and C only;
C             = 'B':  compute the matrix B only;
C             = 'D':  compute the matrices B and D only.
C
C     JOBCV   CHARACTER*1
C             Specifies whether or not the covariance matrices are to
C             be computed, as follows:
C             = 'C':  the covariance matrices should be computed;
C             = 'N':  the covariance matrices should not be computed.
C
C     Input/Output Parameters
C
C     NOBR    (input) INTEGER
C             The number of block rows,  s,  in the input and output
C             Hankel matrices processed by other routines.  NOBR > 1.
C
C     N       (input) INTEGER
C             The order of the system.  NOBR > N > 0.
C
C     M       (input) INTEGER
C             The number of system inputs.  M >= 0.
C
C     L       (input) INTEGER
C             The number of system outputs.  L > 0.
C
C     NSMPL   (input) INTEGER
C             If JOBCV = 'C', the total number of samples used for
C             calculating the covariance matrices.
C             NSMPL >= 2*(M+L)*NOBR.
C             This parameter is not meaningful if  JOBCV = 'N'.
C
C     R       (input/workspace) DOUBLE PRECISION array, dimension
C             ( LDR,2*(M+L)*NOBR )
C             On entry, the leading  2*(M+L)*NOBR-by-2*(M+L)*NOBR  part
C             of this array must contain the relevant data for the MOESP
C             or N4SID algorithms, as constructed by SLICOT Library
C             routines IB01AD or IB01ND. Let  R_ij,  i,j = 1:4,  be the
C             ij submatrix of  R  (denoted  S  in IB01AD and IB01ND),
C             partitioned by  M*NOBR,  L*NOBR,  M*NOBR,  and  L*NOBR
C             rows and columns. The submatrix  R_22  contains the matrix
C             of left singular vectors used. Also needed, for
C             METH = 'N'  or  JOBCV = 'C',  are the submatrices  R_11,
C             R_14 : R_44,  and, for  METH = 'M'  and  JOB <> 'C',  the
C             submatrices  R_31  and  R_12,  containing the processed
C             matrices  R_1c  and  R_2c,  respectively, as returned by
C             SLICOT Library routines IB01AD or IB01ND.
C             Moreover, if  METH = 'N'  and  JOB = 'A' or 'C',  the
C             block-row  R_41 : R_43  must contain the transpose of the
C             block-column  R_14 : R_34  as returned by SLICOT Library
C             routines IB01AD or IB01ND.
C             The remaining part of  R  is used as workspace.
C             On exit, part of this array is overwritten. Specifically,
C             if  METH = 'M',  R_22  and  R_31  are overwritten if
C                 JOB = 'B' or 'D',  and  R_12,  R_22,  R_14 : R_34,
C                 and possibly  R_11  are overwritten if  JOBCV = 'C';
C             if  METH = 'N',  all needed submatrices are overwritten.
C
C     LDR     INTEGER
C             The leading dimension of the array  R.
C             LDR >= 2*(M+L)*NOBR.
C
C     A       (input or output) DOUBLE PRECISION array, dimension
C             (LDA,N)
C             On entry, if  METH = 'N'  and  JOB = 'B' or 'D',  the
C             leading N-by-N part of this array must contain the system
C             state matrix.
C             If  METH = 'M'  or  (METH = 'N'  and JOB = 'A' or 'C'),
C             this array need not be set on input.
C             On exit, if  JOB = 'A' or 'C'  and  INFO = 0,  the
C             leading N-by-N part of this array contains the system
C             state matrix.
C
C     LDA     INTEGER
C             The leading dimension of the array A.
C             LDA >= N,  if  JOB = 'A' or 'C',  or  METH = 'N'  and
C                            JOB = 'B' or 'D';
C             LDA >= 1,  otherwise.
C
C     C       (input or output) DOUBLE PRECISION array, dimension
C             (LDC,N)
C             On entry, if  METH = 'N'  and  JOB = 'B' or 'D',  the
C             leading L-by-N part of this array must contain the system
C             output matrix.
C             If  METH = 'M'  or  (METH = 'N'  and JOB = 'A' or 'C'),
C             this array need not be set on input.
C             On exit, if  JOB = 'A' or 'C'  and  INFO = 0,  or
C             INFO = 3  (or  INFO >= 0,  for  METH = 'M'),  the leading
C             L-by-N part of this array contains the system output
C             matrix.
C
C     LDC     INTEGER
C             The leading dimension of the array C.
C             LDC >= L,  if  JOB = 'A' or 'C',  or  METH = 'N'  and
C                            JOB = 'B' or 'D';
C             LDC >= 1,  otherwise.
C
C     B       (output) DOUBLE PRECISION array, dimension (LDB,M)
C             If  M > 0,  JOB = 'A', 'B', or 'D'  and  INFO = 0,  the
C             leading N-by-M part of this array contains the system
C             input matrix. If  M = 0  or  JOB = 'C',  this array is
C             not referenced.
C
C     LDB     INTEGER
C             The leading dimension of the array B.
C             LDB >= N,  if M > 0 and JOB = 'A', 'B', or 'D';
C             LDB >= 1,  if M = 0 or  JOB = 'C'.
C
C     D       (output) DOUBLE PRECISION array, dimension (LDD,M)
C             If  M > 0,  JOB = 'A' or 'D'  and  INFO = 0,  the leading
C             L-by-M part of this array contains the system input-output
C             matrix. If  M = 0  or  JOB = 'C' or 'B',  this array is
C             not referenced.
C
C     LDD     INTEGER
C             The leading dimension of the array D.
C             LDD >= L,  if M > 0 and JOB = 'A' or 'D';
C             LDD >= 1,  if M = 0 or  JOB = 'C' or 'B'.
C
C     Q       (output) DOUBLE PRECISION array, dimension (LDQ,N)
C             If JOBCV = 'C', the leading N-by-N part of this array
C             contains the positive semidefinite state covariance matrix
C             to be used as state weighting matrix when computing the
C             Kalman gain.
C             This parameter is not referenced if JOBCV = 'N'.
C
C     LDQ     INTEGER
C             The leading dimension of the array Q.
C             LDQ >= N,  if JOBCV = 'C';
C             LDQ >= 1,  if JOBCV = 'N'.
C
C     RY      (output) DOUBLE PRECISION array, dimension (LDRY,L)
C             If JOBCV = 'C', the leading L-by-L part of this array
C             contains the positive (semi)definite output covariance
C             matrix to be used as output weighting matrix when
C             computing the Kalman gain.
C             This parameter is not referenced if JOBCV = 'N'.
C
C     LDRY    INTEGER
C             The leading dimension of the array RY.
C             LDRY >= L,  if JOBCV = 'C';
C             LDRY >= 1,  if JOBCV = 'N'.
C
C     S       (output) DOUBLE PRECISION array, dimension (LDS,L)
C             If JOBCV = 'C', the leading N-by-L part of this array
C             contains the state-output cross-covariance matrix to be
C             used as cross-weighting matrix when computing the Kalman
C             gain.
C             This parameter is not referenced if JOBCV = 'N'.
C
C     LDS     INTEGER
C             The leading dimension of the array S.
C             LDS >= N,  if JOBCV = 'C';
C             LDS >= 1,  if JOBCV = 'N'.
C
C     O       (output) DOUBLE PRECISION array, dimension ( LDO,N )
C             If  METH = 'M'  and  JOBCV = 'C',  or  METH = 'N',
C             the leading  L*NOBR-by-N  part of this array contains
C             the estimated extended observability matrix, i.e., the
C             first  N  columns of the relevant singular vectors.
C             If  METH = 'M'  and  JOBCV = 'N',  this array is not
C             referenced.
C
C     LDO     INTEGER
C             The leading dimension of the array  O.
C             LDO >= L*NOBR,  if  JOBCV = 'C'  or  METH = 'N';
C             LDO >= 1,       otherwise.
C
C     Tolerances
C
C     TOL     DOUBLE PRECISION
C             The tolerance to be used for estimating the rank of
C             matrices. If the user sets  TOL > 0,  then the given value
C             of  TOL  is used as a lower bound for the reciprocal
C             condition number;  an m-by-n matrix whose estimated
C             condition number is less than  1/TOL  is considered to
C             be of full rank.  If the user sets  TOL <= 0,  then an
C             implicitly computed, default tolerance, defined by
C             TOLDEF = m*n*EPS,  is used instead, where  EPS  is the
C             relative machine precision (see LAPACK Library routine
C             DLAMCH).
C
C     Workspace
C
C     IWORK   INTEGER array, dimension (LIWORK)
C             LIWORK = N,                   if METH = 'M' and M = 0
C                                        or JOB = 'C' and JOBCV = 'N';
C             LIWORK = M*NOBR+N,            if METH = 'M', JOB = 'C',
C                                           and JOBCV = 'C';
C             LIWORK = max(L*NOBR,M*NOBR),  if METH = 'M', JOB <> 'C',
C                                           and JOBCV = 'N';
C             LIWORK = max(L*NOBR,M*NOBR+N),  if METH = 'M', JOB <> 'C',
C                                             and JOBCV = 'C';
C             LIWORK = max(M*NOBR+N,M*(N+L)), if METH = 'N'.
C
C     DWORK   DOUBLE PRECISION array, dimension (LDWORK)
C             On exit, if  INFO = 0,  DWORK(1) returns the optimal value
C             of LDWORK,  and  DWORK(2),  DWORK(3),  DWORK(4),  and
C             DWORK(5)  contain the reciprocal condition numbers of the
C             triangular factors of the matrices, defined in the code,
C             GaL  (GaL = Un(1:(s-1)*L,1:n)),  R_1c  (if  METH = 'M'),
C             M  (if  JOBCV = 'C'  or  METH = 'N'),  and  Q  or  T  (see
C             SLICOT Library routines IB01PY or IB01PX),  respectively.
C             If  METH = 'N',  DWORK(3)  is set to one without any
C             calculations. Similarly, if  METH = 'M'  and  JOBCV = 'N',
C             DWORK(4)  is set to one. If  M = 0  or  JOB = 'C',
C             DWORK(3)  and  DWORK(5)  are set to one.
C             On exit, if  INFO = -30,  DWORK(1)  returns the minimum
C             value of LDWORK.
C
C     LDWORK  INTEGER
C             The length of the array DWORK.
C             LDWORK >= max( LDW1,LDW2 ), where, if METH = 'M',
C             LDW1 >= max( 2*(L*NOBR-L)*N+2*N, (L*NOBR-L)*N+N*N+7*N ),
C                     if JOB = 'C' or JOB = 'A' and M = 0;
C             LDW1 >= max( 2*(L*NOBR-L)*N+N*N+7*N,
C                          (L*NOBR-L)*N+N+6*M*NOBR, (L*NOBR-L)*N+N+
C                          max( L+M*NOBR, L*NOBR +
C                                         max( 3*L*NOBR+1, M ) ) )
C                     if M > 0 and JOB = 'A', 'B', or 'D';
C             LDW2 >= 0,                                 if JOBCV = 'N';
C             LDW2 >= max( (L*NOBR-L)*N+Aw+2*N+max(5*N,(2*M+L)*NOBR+L),
C                          4*(M*NOBR+N)+1, M*NOBR+2*N+L ),
C                                                        if JOBCV = 'C',
C             where Aw = N+N*N, if M = 0 or JOB = 'C';
C                   Aw = 0,     otherwise;
C             and, if METH = 'N',
C             LDW1 >= max( (L*NOBR-L)*N+2*N+(2*M+L)*NOBR+L,
C                          2*(L*NOBR-L)*N+N*N+8*N, N+4*(M*NOBR+N)+1,
C                          M*NOBR+3*N+L );
C             LDW2 >= 0, if M = 0 or JOB = 'C';
C             LDW2 >= M*NOBR*(N+L)*(M*(N+L)+1)+
C                     max( (N+L)**2, 4*M*(N+L)+1 ),
C                     if M > 0 and JOB = 'A', 'B', or 'D'.
C             For good performance,  LDWORK  should be larger.
C
C     Warning Indicator
C
C     IWARN   INTEGER
C             = 0:  no warning;
C             = 4:  a least squares problem to be solved has a
C                   rank-deficient coefficient matrix;
C             = 5:  the computed covariance matrices are too small.
C                   The problem seems to be a deterministic one.
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             = 2:  the singular value decomposition (SVD) algorithm did
C                   not converge;
C             = 3:  a singular upper triangular matrix was found.
C
C     METHOD
C
C     In the MOESP approach, the matrices  A  and  C  are first
C     computed from an estimated extended observability matrix [1],
C     and then, the matrices  B  and  D  are obtained by solving an
C     extended linear system in a least squares sense.
C     In the N4SID approach, besides the estimated extended
C     observability matrix, the solutions of two least squares problems
C     are used to build another least squares problem, whose solution
C     is needed to compute the system matrices  A,  C,  B,  and  D.  The
C     solutions of the two least squares problems are also optionally
C     used by both approaches to find the covariance matrices.
C
C     REFERENCES
C
C     [1] Verhaegen M., and Dewilde, P.
C         Subspace Model Identification. Part 1: The output-error state-
C         space model identification class of algorithms.
C         Int. J. Control, 56, pp. 1187-1210, 1992.
C
C     [2] Van Overschee, P., and De Moor, B.
C         N4SID: Two Subspace Algorithms for the Identification
C         of Combined Deterministic-Stochastic Systems.
C         Automatica, Vol.30, No.1, pp. 75-93, 1994.
C
C     [3] Van Overschee, P.
C         Subspace Identification : Theory - Implementation -
C         Applications.
C         Ph. D. Thesis, Department of Electrical Engineering,
C         Katholieke Universiteit Leuven, Belgium, Feb. 1995.
C
C     [4] Sima, V.
C         Subspace-based Algorithms for Multivariable System
C         Identification.
C         Studies in Informatics and Control, 5, pp. 335-344, 1996.
C
C     NUMERICAL ASPECTS
C
C     The implemented method is numerically stable.
C
C     FURTHER COMMENTS
C
C     In some applications, it is useful to compute the system matrices
C     using two calls to this routine, the first one with  JOB = 'C',
C     and the second one with  JOB = 'B' or 'D'.  This is slightly less
C     efficient than using a single call with  JOB = 'A',  because some
C     calculations are repeated. If  METH = 'N',  all the calculations
C     at the first call are performed again at the second call;
C     moreover, it is required to save the needed submatrices of  R
C     before the first call and restore them before the second call.
C     If the covariance matrices are desired,  JOBCV  should be set
C     to  'C'  at the second call. If  B  and  D  are both needed, they
C     should be computed at once.
C     It is possible to compute the matrices A and C using the MOESP
C     algorithm (METH = 'M'), and the matrices B and D using the N4SID
C     algorithm (METH = 'N'). This combination could be slightly more
C     efficient than N4SID algorithm alone and it could be more accurate
C     than MOESP algorithm. No saving/restoring is needed in such a
C     combination, provided  JOBCV  is set to  'N'  at the first call.
C     Recommended usage:  either one call with  JOB = 'A',  or
C        first  call with  METH = 'M',  JOB = 'C',  JOBCV = 'N',
C        second call with  METH = 'M',  JOB = 'D',  JOBCV = 'C',  or
C        first  call with  METH = 'M',  JOB = 'C',  JOBCV = 'N',
C        second call with  METH = 'N',  JOB = 'D',  JOBCV = 'C'.
C
C     CONTRIBUTOR
C
C     V. Sima, Research Institute for Informatics, Bucharest, Dec. 1999.
C
C     REVISIONS
C
C     March 2000, Feb. 2001, Sep. 2001, March 2005.
C
C     KEYWORDS
C
C     Identification methods; least squares solutions; multivariable
C     systems; QR decomposition; singular value decomposition.
C
C     ******************************************************************
C
C     .. Parameters ..
      DOUBLE PRECISION   ZERO, ONE, TWO, THREE
      PARAMETER          ( ZERO  = 0.0D0, ONE = 1.0D0, TWO = 2.0D0,
     $                     THREE = 3.0D0 )
C     .. Scalar Arguments ..
      DOUBLE PRECISION   TOL
      INTEGER            INFO, IWARN, L, LDA, LDB, LDC, LDD, LDO, LDQ,
     $                   LDR, LDRY, LDS, LDWORK, M, N, NOBR, NSMPL
      CHARACTER          JOB, JOBCV, METH
C     .. Array Arguments ..
      DOUBLE PRECISION   A(LDA, *), B(LDB, *), C(LDC, *), D(LDD, *),
     $                   DWORK(*),  O(LDO, *), Q(LDQ, *), R(LDR, *),
     $                   RY(LDRY, *), S(LDS, *)
      INTEGER            IWORK( * )
C     .. Local Scalars ..
      DOUBLE PRECISION   EPS, RCOND1, RCOND2, RCOND3, RCOND4, RNRM,
     $                   SVLMAX, THRESH, TOLL, TOLL1
      INTEGER            I, IAW, ID, IERR, IGAL, IHOUS, ISV, ITAU,
     $                   ITAU1, ITAU2, IU, IUN2, IWARNL, IX, JWORK,
     $                   LDUN2, LDUNN, LDW, LMMNOB, LMMNOL, LMNOBR,
     $                   LNOBR, LNOBRN, MAXWRK, MINWRK, MNOBR, MNOBRN,
     $                   N2, NCOL, NN, NPL, NR, NR2, NR3, NR4, NR4MN,
     $                   NR4PL, NROW, RANK, RANK11, RANKM
      CHARACTER          FACT, JOBP, JOBPY
      LOGICAL            FULLR, MOESP, N4SID, SHIFT, WITHAL, WITHB,
     $                   WITHC, WITHCO, WITHD
C     .. Local Array ..
      DOUBLE PRECISION   SVAL(3)
C     .. External Functions ..
      LOGICAL            LSAME
      INTEGER            ILAENV
      DOUBLE PRECISION   DLAMCH, DLANGE
      EXTERNAL           DLAMCH, DLANGE, ILAENV, LSAME
C     .. External Subroutines ..
      EXTERNAL           DCOPY, DGEMM, DGEQRF, DLACPY, DLASET, DORMQR,
     $                   DSYRK, DTRCON, DTRSM, DTRTRS, IB01PX, IB01PY,
     $                   MA02AD, MA02ED, MB02QY, MB02UD, MB03OD, XERBLA
C     .. Intrinsic Functions ..
      INTRINSIC          DBLE, MAX
C     .. Executable Statements ..
C
C     Decode the scalar input parameters.
C
      MOESP  = LSAME( METH,  'M' )
      N4SID  = LSAME( METH,  'N' )
      WITHAL = LSAME( JOB,   'A' )
      WITHC  = LSAME( JOB,   'C' ) .OR. WITHAL
      WITHD  = LSAME( JOB,   'D' ) .OR. WITHAL
      WITHB  = LSAME( JOB,   'B' ) .OR. WITHD
      WITHCO = LSAME( JOBCV, 'C' )
      MNOBR  = M*NOBR
      LNOBR  = L*NOBR
      LMNOBR = LNOBR + MNOBR
      LMMNOB = LNOBR + 2*MNOBR
      MNOBRN = MNOBR + N
      LNOBRN = LNOBR - N
      LDUN2  = LNOBR - L
      LDUNN  = LDUN2*N
      LMMNOL = LMMNOB + L
      NR     = LMNOBR + LMNOBR
      NPL    = N + L
      N2     = N + N
      NN     = N*N
      MINWRK = 1
      IWARN  = 0
      INFO   = 0
C
C     Check the scalar input parameters.
C
      IF( .NOT.( MOESP .OR. N4SID ) ) THEN
         INFO = -1
      ELSE IF( .NOT.( WITHB .OR. WITHC ) ) THEN
         INFO = -2
      ELSE IF( .NOT.( WITHCO .OR. LSAME( JOBCV, 'N' ) ) ) THEN
         INFO = -3
      ELSE IF( NOBR.LE.1 ) THEN
         INFO = -4
      ELSE IF( N.LE.0 .OR. N.GE.NOBR ) THEN
         INFO = -5
      ELSE IF( M.LT.0 ) THEN
         INFO = -6
      ELSE IF( L.LE.0 ) THEN
         INFO = -7
      ELSE IF( WITHCO .AND. NSMPL.LT.NR ) THEN
         INFO = -8
      ELSE IF( LDR.LT.NR ) THEN
         INFO = -10
      ELSE IF( LDA.LT.1 .OR. ( ( WITHC .OR. ( WITHB .AND. N4SID ) )
     $   .AND. LDA.LT.N ) ) THEN
         INFO = -12
      ELSE IF( LDC.LT.1 .OR. ( ( WITHC .OR. ( WITHB .AND. N4SID ) )
     $   .AND. LDC.LT.L ) ) THEN
         INFO = -14
      ELSE IF( LDB.LT.1  .OR. ( WITHB  .AND. LDB.LT.N .AND. M.GT.0 ) )
     $      THEN
         INFO = -16
      ELSE IF( LDD.LT.1  .OR. ( WITHD  .AND. LDD.LT.L .AND. M.GT.0 ) )
     $      THEN
         INFO = -18
      ELSE IF( LDQ.LT.1  .OR. ( WITHCO .AND. LDQ.LT.N ) )  THEN
         INFO = -20
      ELSE IF( LDRY.LT.1 .OR. ( WITHCO .AND. LDRY.LT.L ) ) THEN
         INFO = -22
      ELSE IF( LDS.LT.1  .OR. ( WITHCO .AND. LDS.LT.N ) )  THEN
         INFO = -24
      ELSE IF( LDO.LT.1  .OR. ( ( WITHCO .OR. N4SID ) .AND.
     $         LDO.LT.LNOBR ) )  THEN
         INFO = -26
      ELSE
C
C        Compute workspace.
C        (Note: Comments in the code beginning "Workspace:" describe the
C         minimal amount of workspace needed at that point in the code,
C         as well as the preferred amount for good performance.
C         NB refers to the optimal block size for the immediately
C         following subroutine, as returned by ILAENV.)
C
         IAW    = 0
         MINWRK = LDUNN + 4*N
         MAXWRK = LDUNN + N + N*ILAENV( 1, 'DGEQRF', ' ', LDUN2, N, -1,
     $                                  -1 )
         IF( MOESP ) THEN
            ID = 0
            IF( WITHC ) THEN
               MINWRK = MAX( MINWRK, 2*LDUNN + N2, LDUNN + NN + 7*N )
               MAXWRK = MAX( MAXWRK, 2*LDUNN + N + N*ILAENV( 1,
     $                               'DORMQR', 'LT', LDUN2, N, N, -1 ) )
            END IF
         ELSE
            ID = N
         END IF
C
         IF( ( M.GT.0 .AND. WITHB ) .OR. N4SID ) THEN
            MINWRK = MAX( MINWRK, 2*LDUNN + NN + ID + 7*N )
            IF ( MOESP )
     $         MINWRK = MAX( MINWRK, LDUNN + N + 6*MNOBR, LDUNN + N +
     $                       MAX( L + MNOBR, LNOBR +
     $                                       MAX( 3*LNOBR + 1, M ) ) )
         ELSE
            IF( MOESP )
     $         IAW = N + NN
         END IF
C
         IF( N4SID .OR. WITHCO ) THEN
            MINWRK = MAX( MINWRK, LDUNN + IAW + N2 + MAX( 5*N, LMMNOL ),
     $                    ID + 4*MNOBRN+1, ID + MNOBRN + NPL )
            MAXWRK = MAX( MAXWRK, LDUNN + IAW + N2 +
     $                    MAX( N*ILAENV( 1, 'DGEQRF', ' ', LNOBR, N, -1,
     $                                   -1 ), LMMNOB*
     $                           ILAENV( 1, 'DORMQR', 'LT', LNOBR,
     $                                   LMMNOB, N, -1 ), LMMNOL*
     $                           ILAENV( 1, 'DORMQR', 'LT', LDUN2,
     $                                   LMMNOL, N, -1 ) ),
     $                    ID + N + N*ILAENV( 1, 'DGEQRF', ' ', LMNOBR,
     $                                       N, -1, -1 ),
     $                    ID + N + NPL*ILAENV( 1, 'DORMQR', 'LT',
     $                                         LMNOBR, NPL, N, -1 ) )
            IF( N4SID .AND. ( M.GT.0 .AND. WITHB ) )
     $         MINWRK = MAX( MINWRK, MNOBR*NPL*( M*NPL + 1 ) +
     $                       MAX( NPL**2, 4*M*NPL + 1 ) )
         END IF
         MAXWRK = MAX( MINWRK, MAXWRK )
C
         IF ( LDWORK.LT.MINWRK ) THEN
            INFO = -30
            DWORK( 1 ) = MINWRK
         END IF
      END IF
C
C     Return if there are illegal arguments.
C
      IF( INFO.NE.0 ) THEN
         CALL XERBLA( 'IB01PD', -INFO )
         RETURN
      END IF
C
      NR2 = MNOBR  + 1
      NR3 = LMNOBR + 1
      NR4 = LMMNOB + 1
C
C     Set the precision parameters. A threshold value  EPS**(2/3)  is
C     used for deciding to use pivoting or not, where  EPS  is the
C     relative machine precision (see LAPACK Library routine DLAMCH).
C
      EPS    = DLAMCH( 'Precision' )
      THRESH = EPS**( TWO/THREE )
      SVLMAX = ZERO
      RCOND4 = ONE
C
C     Let  Un  be the matrix of left singular vectors (stored in  R_22).
C     Copy  un1 = GaL = Un(1:(s-1)*L,1:n)  in the workspace.
C
      IGAL = 1
      CALL DLACPY( 'Full', LDUN2, N, R(NR2,NR2), LDR, DWORK(IGAL),
     $             LDUN2 )
C
C     Factor un1 = Q1*[r1'  0]' (' means transposition).
C     Workspace: need   L*(NOBR-1)*N+2*N,
C                prefer L*(NOBR-1)*N+N+N*NB.
C
      ITAU1 = IGAL  + LDUNN
      JWORK = ITAU1 + N
      LDW   = JWORK
      CALL DGEQRF( LDUN2, N, DWORK(IGAL), LDUN2, DWORK(ITAU1),
     $             DWORK(JWORK), LDWORK-JWORK+1, IERR )
C
C     Compute the reciprocal of the condition number of r1.
C     Workspace: need L*(NOBR-1)*N+4*N.
C
      CALL DTRCON( '1-norm', 'Upper', 'NonUnit', N, DWORK(IGAL), LDUN2,
     $             RCOND1, DWORK(JWORK), IWORK, INFO )
C
      TOLL1 = TOL
      IF( TOLL1.LE.ZERO )
     $   TOLL1 = NN*EPS
C
      IF ( ( M.GT.0 .AND. WITHB ) .OR. N4SID ) THEN
         JOBP = 'P'
         IF ( WITHAL ) THEN
            JOBPY = 'D'
         ELSE
            JOBPY = JOB
         END IF
      ELSE
         JOBP = 'N'
      END IF
C
      IF ( MOESP ) THEN
         NCOL = 0
         IUN2 = JWORK
         IF ( WITHC ) THEN
C
C           Set  C = Un(1:L,1:n)  and then compute the system matrix A.
C
C           Set  un2 = Un(L+1:L*s,1:n)  in  DWORK(IUN2).
C           Workspace: need   2*L*(NOBR-1)*N+N.
C
            CALL DLACPY( 'Full', L, N, R(NR2,NR2), LDR, C, LDC )
            CALL DLACPY( 'Full', LDUN2, N, R(NR2+L,NR2), LDR,
     $                   DWORK(IUN2), LDUN2 )
C
C           Note that un1 has already been factored as
C           un1 = Q1*[r1'  0]'  and usually (generically, assuming
C           observability) has full column rank.
C           Update  un2 <-- Q1'*un2  in  DWORK(IUN2)  and save its
C           first  n  rows in  A.
C           Workspace: need   2*L*(NOBR-1)*N+2*N;
C                      prefer 2*L*(NOBR-1)*N+N+N*NB.
C
            JWORK = IUN2 + LDUNN
            CALL DORMQR( 'Left', 'Transpose', LDUN2, N, N, DWORK(IGAL),
     $                   LDUN2, DWORK(ITAU1), DWORK(IUN2), LDUN2,
     $                   DWORK(JWORK), LDWORK-JWORK+1, IERR )
            CALL DLACPY( 'Full', N, N, DWORK(IUN2), LDUN2, A, LDA )
            NCOL  = N
            JWORK = IUN2
         END IF
C
         IF ( RCOND1.GT.MAX( TOLL1, THRESH ) ) THEN
C
C           The triangular factor r1 is considered to be of full rank.
C           Solve for  A  (if requested),  r1*A = un2(1:n,:)  in  A.
C
            IF ( WITHC ) THEN
               CALL DTRTRS( 'Upper', 'NoTranspose', 'NonUnit', N, N,
     $                      DWORK(IGAL), LDUN2, A, LDA, IERR )
               IF ( IERR.GT.0 ) THEN
                  INFO = 3
                  RETURN
               END IF
            END IF
            RANK = N
         ELSE
C
C           Rank-deficient triangular factor r1.  Use SVD of r1,
C           r1 = U*S*V',  also for computing  A  (if requested) from
C           r1*A = un2(1:n,:).  Matrix  U  is computed in  DWORK(IU),
C           and  V' overwrites  r1.  If  B  is requested, the
C           pseudoinverse of  r1  and then of  GaL  are also computed
C           in  R(NR3,NR2).
C           Workspace: need   c*L*(NOBR-1)*N+N*N+7*N,
C                             where  c = 1  if  B and D  are not needed,
C                                    c = 2  if  B and D  are needed;
C                      prefer larger.
C
            IU    = IUN2
            ISV   = IU  + NN
            JWORK = ISV + N
            IF ( M.GT.0 .AND. WITHB ) THEN
C
C              Save the elementary reflectors used for computing r1,
C              if  B, D  are needed.
C              Workspace: need   2*L*(NOBR-1)*N+2*N+N*N.
C
               IHOUS = JWORK
               JWORK = IHOUS + LDUNN
               CALL DLACPY( 'Lower', LDUN2, N, DWORK(IGAL), LDUN2,
     $                      DWORK(IHOUS), LDUN2 )
            ELSE
               IHOUS = IGAL
            END IF
C
            CALL MB02UD( 'Not factored', 'Left', 'NoTranspose', JOBP, N,
     $                   NCOL, ONE, TOLL1, RANK, DWORK(IGAL), LDUN2,
     $                   DWORK(IU), N, DWORK(ISV), A, LDA, R(NR3,NR2),
     $                   LDR, DWORK(JWORK), LDWORK-JWORK+1, IERR )
            IF ( IERR.NE.0 ) THEN
               INFO = 2
               RETURN
            END IF
            MAXWRK = MAX( MAXWRK, INT( DWORK(JWORK) ) + JWORK - 1 )
C
            IF ( RANK.EQ.0 ) THEN
               JOBP = 'N'
            ELSE IF ( M.GT.0 .AND. WITHB ) THEN
C
C              Compute  pinv(GaL)  in  R(NR3,NR2)  if  B, D  are needed.
C              Workspace: need   2*L*(NOBR-1)*N+N*N+3*N;
C                         prefer 2*L*(NOBR-1)*N+N*N+2*N+N*NB.
C
               CALL DLASET( 'Full', N, LDUN2-N, ZERO, ZERO,
     $                      R(NR3,NR2+N), LDR )
               CALL DORMQR( 'Right', 'Transpose', N, LDUN2, N,
     $                      DWORK(IHOUS), LDUN2, DWORK(ITAU1),
     $                      R(NR3,NR2), LDR, DWORK(JWORK),
     $                      LDWORK-JWORK+1, IERR )
               MAXWRK = MAX( MAXWRK, INT( DWORK(JWORK) ) + JWORK - 1 )
               IF ( WITHCO ) THEN
C
C                 Save  pinv(GaL)  in  DWORK(IGAL).
C
                  CALL DLACPY( 'Full', N, LDUN2, R(NR3,NR2), LDR,
     $                         DWORK(IGAL), N )
               END IF
               JWORK = IUN2
            END IF
            LDW = JWORK
         END IF
C
         IF ( M.GT.0 .AND. WITHB ) THEN
C
C           Computation of  B  and  D.
C
C           Compute the reciprocal of the condition number of R_1c.
C           Workspace: need L*(NOBR-1)*N+N+3*M*NOBR.
C
            CALL DTRCON( '1-norm', 'Upper', 'NonUnit', MNOBR, R(NR3,1),
     $                   LDR, RCOND2, DWORK(JWORK), IWORK, IERR )
C
            TOLL = TOL
            IF( TOLL.LE.ZERO )
     $         TOLL = MNOBR*MNOBR*EPS
C
C           Compute the right hand side and solve for  K  (in  R_23),
C              K*R_1c' = u2'*R_2c',
C           where  u2 = Un(:,n+1:L*s),  and  K  is  (Ls-n) x ms.
C
            CALL DGEMM( 'Transpose', 'Transpose', LNOBRN, MNOBR, LNOBR,
     $                   ONE, R(NR2,NR2+N), LDR, R(1,NR2), LDR, ZERO,
     $                   R(NR2,NR3), LDR )
C
            IF ( RCOND2.GT.MAX( TOLL, THRESH ) ) THEN
C
C              The triangular factor R_1c is considered to be of full
C              rank. Solve for  K,  K*R_1c' = u2'*R_2c'.
C
               CALL DTRSM( 'Right', 'Upper', 'Transpose', 'Non-unit',
     $                     LNOBRN, MNOBR, ONE, R(NR3,1), LDR,
     $                     R(NR2,NR3), LDR )
            ELSE
C
C              Rank-deficient triangular factor  R_1c.  Use SVD of  R_1c
C              for computing  K  from  K*R_1c' = u2'*R_2c',  where
C              R_1c = U1*S1*V1'.  Matrix  U1  is computed in  R_33,
C              and  V1'  overwrites  R_1c.
C              Workspace: need   L*(NOBR-1)*N+N+6*M*NOBR;
C                         prefer larger.
C
               ISV   = LDW
               JWORK = ISV + MNOBR
               CALL MB02UD( 'Not factored', 'Right', 'Transpose',
     $                      'No pinv', LNOBRN, MNOBR, ONE, TOLL, RANK11,
     $                      R(NR3,1), LDR, R(NR3,NR3), LDR, DWORK(ISV),
     $                      R(NR2,NR3), LDR, DWORK(JWORK), 1,
     $                      DWORK(JWORK), LDWORK-JWORK+1, IERR )
               IF ( IERR.NE.0 ) THEN
                  INFO = 2
                  RETURN
               END IF
               MAXWRK = MAX( MAXWRK, INT( DWORK(JWORK) ) + JWORK - 1 )
               JWORK  = LDW
            END IF
C
C           Compute the triangular factor of the structured matrix  Q
C           and apply the transformations to the matrix  Kexpand,  where
C           Q  and  Kexpand  are defined in SLICOT Library routine
C           IB01PY.  Compute also the matrices  B,  D.
C           Workspace: need   L*(NOBR-1)*N+N+max(L+M*NOBR,L*NOBR+
C                                                max(3*L*NOBR+1,M));
C                      prefer larger.
C
            IF ( WITHCO )
     $         CALL DLACPY( 'Full', LNOBR, N, R(NR2,NR2), LDR, O, LDO )
            CALL IB01PY( METH, JOBPY, NOBR, N, M, L, RANK, R(NR2,NR2),
     $                   LDR, DWORK(IGAL), LDUN2, DWORK(ITAU1),
     $                   R(NR3,NR2), LDR, R(NR2,NR3), LDR, R(NR4,NR2),
     $                   LDR, R(NR4,NR3), LDR, B, LDB, D, LDD, TOL,
     $                   IWORK, DWORK(JWORK), LDWORK-JWORK+1, IWARN,
     $                   INFO )
            IF ( INFO.NE.0 )
     $         RETURN
            MAXWRK = MAX( MAXWRK, INT( DWORK(JWORK) ) + JWORK - 1 )
            RCOND4 = DWORK(JWORK+1)
            IF ( WITHCO )
     $         CALL DLACPY( 'Full', LNOBR, N, O, LDO, R(NR2,1), LDR )
C
         ELSE
            RCOND2 = ONE
         END IF
C
         IF ( .NOT.WITHCO ) THEN
            RCOND3 = ONE
            GO TO 30
         END IF
      ELSE
C
C        For N4SID, set  RCOND2  to one.
C
         RCOND2 = ONE
      END IF
C
C     If needed, save the first  n  columns, representing  Gam,  of the
C     matrix of left singular vectors,  Un,  in  R_21  and in  O.
C
      IF ( N4SID .OR. ( WITHC .AND. .NOT.WITHAL ) ) THEN
         IF ( M.GT.0 )
     $      CALL DLACPY( 'Full', LNOBR, N, R(NR2,NR2), LDR, R(NR2,1),
     $                   LDR )
         CALL DLACPY( 'Full', LNOBR, N, R(NR2,NR2), LDR, O, LDO )
      END IF
C
C     Computations for covariance matrices, and system matrices (N4SID).
C     Solve the least squares problems  Gam*Y = R4(1:L*s,1:(2*m+L)*s),
C                                       GaL*X = R4(L+1:L*s,:),  where
C     GaL = Gam(1:L*(s-1),:),  Gam  has full column rank, and
C     R4 = [ R_14' R_24' R_34' R_44L' ],  R_44L = R_44(1:L,:), as
C     returned by SLICOT Library routine  IB01ND.
C     First, find the  QR  factorization of  Gam,  Gam = Q*R.
C     Workspace: need   L*(NOBR-1)*N+Aw+3*N;
C                prefer L*(NOBR-1)*N+Aw+2*N+N*NB, where
C                Aw = N+N*N,  if  (M = 0  or  JOB = 'C'),  rank(r1) < N,
C                             and  METH = 'M';
C                Aw = 0,      otherwise.
C
      ITAU2 = LDW
      JWORK = ITAU2 + N
      CALL DGEQRF( LNOBR, N, R(NR2,1), LDR, DWORK(ITAU2),
     $             DWORK(JWORK), LDWORK-JWORK+1, IERR )
C
C     For METH = 'M' or when JOB = 'B' or 'D', transpose
C     [ R_14' R_24' R_34' ]'  in the last block-row of  R, obtaining  Z,
C     and for METH = 'N' and JOB = 'A' or 'C', use the matrix  Z
C     already available in the last block-row of  R,  and then apply
C     the transformations, Z <-- Q'*Z.
C     Workspace: need   L*(NOBR-1)*N+Aw+2*N+(2*M+L)*NOBR;
C                prefer L*(NOBR-1)*N+Aw+2*N+(2*M+L)*NOBR*NB.
C
      IF ( MOESP .OR. ( WITHB .AND. .NOT. WITHAL ) )
     $   CALL MA02AD( 'Full', LMMNOB, LNOBR, R(1,NR4), LDR, R(NR4,1),
     $                LDR )
      CALL DORMQR( 'Left', 'Transpose', LNOBR, LMMNOB, N, R(NR2,1), LDR,
     $             DWORK(ITAU2), R(NR4,1), LDR, DWORK(JWORK),
     $             LDWORK-JWORK+1, IERR )
C
C     Solve for  Y,  RY = Z  in  Z  and save the transpose of the
C     solution  Y  in the second block-column of  R.
C
      CALL DTRTRS( 'Upper', 'NoTranspose', 'NonUnit', N, LMMNOB,
     $             R(NR2,1), LDR, R(NR4,1), LDR, IERR )
      IF ( IERR.GT.0 ) THEN
         INFO = 3
         RETURN
      END IF
      CALL MA02AD( 'Full', N, LMMNOB, R(NR4,1), LDR, R(1,NR2), LDR )
      NR4MN = NR4 - N
      NR4PL = NR4 + L
      NROW  = LMMNOL
C
C     SHIFT is .TRUE. if some columns of  R_14 : R_44L  should be
C     shifted to the right, to avoid overwriting useful information.
C
      SHIFT = M.EQ.0 .AND. LNOBR.LT.N2
C
      IF ( RCOND1.GT.MAX( TOLL1, THRESH ) ) THEN
C
C        The triangular factor  r1  of  GaL  (GaL = Q1*r1)  is
C        considered to be of full rank.
C
C        Transpose  [ R_14' R_24' R_34' R_44L' ]'(:,L+1:L*s)  in the
C        last block-row of  R  (beginning with the  (L+1)-th  row),
C        obtaining  Z1,  and then apply the transformations,
C        Z1 <-- Q1'*Z1.
C        Workspace: need   L*(NOBR-1)*N+Aw+2*N+ (2*M+L)*NOBR + L;
C                   prefer L*(NOBR-1)*N+Aw+2*N+((2*M+L)*NOBR + L)*NB.
C
         CALL MA02AD( 'Full', LMMNOL, LDUN2, R(1,NR4PL), LDR,
     $                R(NR4PL,1), LDR )
         CALL DORMQR( 'Left', 'Transpose', LDUN2, LMMNOL, N,
     $                DWORK(IGAL), LDUN2, DWORK(ITAU1), R(NR4PL,1), LDR,
     $                DWORK(JWORK), LDWORK-JWORK+1, IERR )
C
C        Solve for  X,  r1*X = Z1  in  Z1,  and copy the transpose of  X
C        into the last part of the third block-column of  R.
C
         CALL DTRTRS( 'Upper', 'NoTranspose', 'NonUnit', N, LMMNOL,
     $                DWORK(IGAL), LDUN2, R(NR4PL,1), LDR, IERR )
         IF ( IERR.GT.0 ) THEN
            INFO = 3
            RETURN
         END IF
C
         IF ( SHIFT ) THEN
            NR4MN = NR4
C
            DO 10 I = L - 1, 0, -1
               CALL DCOPY( LMMNOL, R(1,NR4+I), 1, R(1,NR4+N+I), 1 )
   10       CONTINUE
C
         END IF
         CALL MA02AD( 'Full', N, LMMNOL, R(NR4PL,1), LDR, R(1,NR4MN),
     $                LDR )
         NROW = 0
      END IF
C
      IF ( N4SID .OR. NROW.GT.0 ) THEN
C
C        METH = 'N'  or rank-deficient triangular factor r1.
C        For  METH = 'N',  use SVD of  r1,  r1 = U*S*V', for computing
C        X'  from  X'*GaL' = Z1',  if  rank(r1) < N.  Matrix  U  is
C        computed in  DWORK(IU)  and  V'  overwrites  r1.  Then, the
C        pseudoinverse of  GaL  is determined in  R(NR4+L,NR2).
C        For METH = 'M', the pseudoinverse of  GaL  is already available
C        if  M > 0  and  B  is requested;  otherwise, the SVD of  r1  is
C        available in  DWORK(IU),  DWORK(ISV),  and  DWORK(IGAL).
C        Workspace for N4SID: need   2*L*(NOBR-1)*N+N*N+8*N;
C                             prefer larger.
C
         IF ( MOESP ) THEN
            FACT = 'F'
            IF ( M.GT.0 .AND. WITHB )
     $         CALL DLACPY( 'Full', N, LDUN2, DWORK(IGAL), N,
     $                      R(NR4PL,NR2), LDR )
         ELSE
C
C           Save the elementary reflectors used for computing r1.
C
            IHOUS = JWORK
            CALL DLACPY( 'Lower', LDUN2, N, DWORK(IGAL), LDUN2,
     $                   DWORK(IHOUS), LDUN2 )
            FACT  = 'N'
            IU    = IHOUS + LDUNN
            ISV   = IU  + NN
            JWORK = ISV + N
         END IF
C
         CALL MB02UD( FACT, 'Right', 'Transpose', JOBP, NROW, N, ONE,
     $                TOLL1, RANK, DWORK(IGAL), LDUN2, DWORK(IU), N,
     $                DWORK(ISV), R(1,NR4PL), LDR, R(NR4PL,NR2), LDR,
     $                DWORK(JWORK), LDWORK-JWORK+1, IERR )
         IF ( NROW.GT.0 ) THEN
            IF ( SHIFT ) THEN
               NR4MN = NR4
               CALL DLACPY( 'Full', LMMNOL, L, R(1,NR4), LDR,
     $                      R(1,NR4-L), LDR )
               CALL DLACPY( 'Full', LMMNOL, N, R(1,NR4PL), LDR,
     $                      R(1,NR4MN), LDR )
               CALL DLACPY( 'Full', LMMNOL, L, R(1,NR4-L), LDR,
     $                      R(1,NR4+N), LDR )
            ELSE
               CALL DLACPY( 'Full', LMMNOL, N, R(1,NR4PL), LDR,
     $                      R(1,NR4MN), LDR )
            END IF
         END IF
C
         IF ( N4SID ) THEN
            IF ( IERR.NE.0 ) THEN
               INFO = 2
               RETURN
            END IF
            MAXWRK = MAX( MAXWRK, INT( DWORK(JWORK) ) + JWORK - 1 )
C
C           Compute  pinv(GaL)  in  R(NR4+L,NR2).
C           Workspace: need   2*L*(NOBR-1)*N+3*N;
C                      prefer 2*L*(NOBR-1)*N+2*N+N*NB.
C
            JWORK = IU
            CALL DLASET( 'Full', N, LDUN2-N, ZERO, ZERO, R(NR4PL,NR2+N),
     $                   LDR )
            CALL DORMQR( 'Right', 'Transpose', N, LDUN2, N,
     $                   DWORK(IHOUS), LDUN2, DWORK(ITAU1),
     $                   R(NR4PL,NR2), LDR, DWORK(JWORK),
     $                   LDWORK-JWORK+1, IERR )
            MAXWRK = MAX( MAXWRK, INT( DWORK(JWORK) ) + JWORK - 1 )
         END IF
      END IF
C
C     For METH = 'N', find part of the solution (corresponding to A
C     and C) and, optionally, for both  METH = 'M',  or  METH = 'N',
C     find the residual of the least squares problem that gives the
C     covariances,  M*V = N,  where
C         (     R_11 )
C     M = (  Y'      ),  N = (  X'   R4'(:,1:L) ),  V = V(n+m*s, n+L),
C         (  0   0   )
C     with  M((2*m+L)*s+L, n+m*s),  N((2*m+L)*s+L, n+L),  R4'  being
C     stored in the last block-column of  R.  The last  L  rows of  M
C     are not explicitly considered. Note that, for efficiency, the
C     last  m*s  columns of  M  are in the first positions of arrray  R.
C     This permutation does not affect the residual, only the
C     solution.  (The solution is not needed for METH = 'M'.)
C     Note that R_11 corresponds to the future outputs for both
C     METH = 'M', or METH = 'N' approaches.  (For  METH = 'N',  the
C     first two block-columns have been interchanged.)
C     For  METH = 'N',  A and C are obtained as follows:
C     [ A'  C' ] = V(m*s+1:m*s+n,:).
C
C     First, find the  QR  factorization of  Y'(m*s+1:(2*m+L)*s,:)
C     and apply the transformations to the corresponding part of N.
C     Compress the workspace for N4SID by moving the scalar reflectors
C     corresponding to  Q.
C     Workspace: need   d*N+2*N;
C                prefer d*N+N+N*NB;
C     where  d = 0,  for  MOESP,  and  d = 1,  for  N4SID.
C
      IF ( MOESP ) THEN
         ITAU = 1
      ELSE
         CALL DCOPY( N, DWORK(ITAU2), 1, DWORK, 1 )
         ITAU = N + 1
      END IF
C
      JWORK = ITAU + N
      CALL DGEQRF( LMNOBR, N, R(NR2,NR2), LDR, DWORK(ITAU),
     $             DWORK(JWORK), LDWORK-JWORK+1, IERR )
C
C     Workspace: need   d*N+N+(N+L);
C                prefer d*N+N+(N+L)*NB.
C
      CALL DORMQR( 'Left', 'Transpose', LMNOBR, NPL, N, R(NR2,NR2), LDR,
     $             DWORK(ITAU), R(NR2,NR4MN), LDR, DWORK(JWORK),
     $             LDWORK-JWORK+1, IERR )
C
      CALL DLASET( 'Lower', L-1, L-1, ZERO, ZERO, R(NR4+1,NR4), LDR )
C
C     Now, matrix  M  with permuted block-columns has been
C     triangularized.
C     Compute the reciprocal of the condition number of its
C     triangular factor in  R(1:m*s+n,1:m*s+n).
C     Workspace: need d*N+3*(M*NOBR+N).
C
      JWORK = ITAU
      CALL DTRCON( '1-norm', 'Upper', 'NonUnit', MNOBRN, R, LDR, RCOND3,
     $             DWORK(JWORK), IWORK, INFO )
C
      TOLL = TOL
      IF( TOLL.LE.ZERO )
     $   TOLL = MNOBRN*MNOBRN*EPS
      IF ( RCOND3.GT.MAX( TOLL, THRESH ) ) THEN
C
C        The triangular factor is considered to be of full rank.
C        Solve for  V(m*s+1:m*s+n,:),  giving  [ A'  C' ].
C
         FULLR = .TRUE.
         RANKM = MNOBRN
         IF ( N4SID )
     $      CALL DTRSM( 'Left', 'Upper', 'NoTranspose', 'NonUnit', N,
     $                  NPL, ONE, R(NR2,NR2), LDR, R(NR2,NR4MN), LDR )
      ELSE
         FULLR = .FALSE.
C
C        Use QR factorization (with pivoting). For METH = 'N', save
C        (and then restore) information about the QR factorization of
C        Gam,  for later use. Note that  R_11  could be modified by
C        MB03OD, but the corresponding part of  N  is also modified
C        accordingly.
C        Workspace: need   d*N+4*(M*NOBR+N)+1;
C                   prefer d*N+3*(M*NOBR+N)+(M*NOBR+N+1)*NB.
C
         DO 20 I = 1, MNOBRN
            IWORK(I) = 0
   20    CONTINUE
C
         IF ( N4SID .AND. ( M.GT.0 .AND. WITHB ) )
     $      CALL DLACPY( 'Full', LNOBR, N, R(NR2,1), LDR, R(NR4,1),
     $                   LDR )
         JWORK = ITAU + MNOBRN
         CALL DLASET( 'Lower', MNOBRN-1, MNOBRN, ZERO, ZERO, R(2,1),
     $                LDR )
         CALL MB03OD( 'QR', MNOBRN, MNOBRN, R, LDR, IWORK, TOLL,
     $                SVLMAX, DWORK(ITAU), RANKM, SVAL, DWORK(JWORK),
     $                LDWORK-JWORK+1, IERR )
         MAXWRK = MAX( MAXWRK, INT( DWORK(JWORK) ) + JWORK - 1 )
C
C        Workspace: need   d*N+M*NOBR+N+N+L;
C                   prefer d*N+M*NOBR+N+(N+L)*NB.
C
         CALL DORMQR( 'Left', 'Transpose', MNOBRN, NPL, MNOBRN,
     $                R, LDR, DWORK(ITAU), R(1,NR4MN), LDR,
     $                DWORK(JWORK), LDWORK-JWORK+1, IERR )
         MAXWRK = MAX( MAXWRK, INT( DWORK(JWORK) ) + JWORK - 1 )
      END IF
C
      IF ( WITHCO ) THEN
C
C        The residual (transposed) of the least squares solution
C        (multiplied by a matrix with orthogonal columns) is stored
C        in the rows  RANKM+1:(2*m+L)*s+L  of V,  and it should be
C        squared-up for getting the covariance matrices. (Generically,
C        RANKM = m*s+n.)
C
         RNRM = ONE/DBLE( NSMPL )
         IF ( MOESP ) THEN
            CALL DSYRK( 'Upper', 'Transpose', NPL, LMMNOL-RANKM, RNRM,
     $                  R(RANKM+1,NR4MN), LDR, ZERO, R, LDR )
            CALL DLACPY( 'Upper', N, N, R, LDR, Q, LDQ )
            CALL DLACPY( 'Full',  N, L, R(1,N+1), LDR, S, LDS )
            CALL DLACPY( 'Upper', L, L, R(N+1,N+1), LDR, RY, LDRY )
         ELSE
            CALL DSYRK( 'Upper', 'Transpose', NPL, LMMNOL-RANKM, RNRM,
     $                  R(RANKM+1,NR4MN), LDR, ZERO, DWORK(JWORK), NPL )
            CALL DLACPY( 'Upper', N, N, DWORK(JWORK), NPL, Q, LDQ )
            CALL DLACPY( 'Full',  N, L, DWORK(JWORK+N*NPL), NPL, S,
     $                   LDS )
            CALL DLACPY( 'Upper', L, L, DWORK(JWORK+N*(NPL+1)), NPL, RY,
     $                   LDRY )
         END IF
         CALL MA02ED( 'Upper', N, Q, LDQ )
         CALL MA02ED( 'Upper', L, RY, LDRY )
C
C        Check the magnitude of the residual.
C
         RNRM = DLANGE( '1-norm', LMMNOL-RANKM, NPL, R(RANKM+1,NR4MN),
     $                  LDR, DWORK(JWORK) )
         IF ( RNRM.LT.THRESH )
     $      IWARN = 5
      END IF
C
      IF ( N4SID ) THEN
         IF ( .NOT.FULLR ) THEN
            IWARN = 4
C
C           Compute part of the solution of the least squares problem,
C           M*V = N,  for the rank-deficient problem.
C           Remark: this computation should not be performed before the
C           symmetric updating operation above.
C           Workspace: need   M*NOBR+3*N+L;
C                      prefer larger.
C
            CALL MB03OD( 'No QR', N, N, R(NR2,NR2), LDR, IWORK, TOLL1,
     $                   SVLMAX, DWORK(ITAU), RANKM, SVAL, DWORK(JWORK),
     $                   LDWORK-JWORK+1, IERR )
            CALL MB02QY( N, N, NPL, RANKM, R(NR2,NR2), LDR, IWORK,
     $                   R(NR2,NR4MN), LDR, DWORK(ITAU+MNOBR),
     $                   DWORK(JWORK), LDWORK-JWORK+1, INFO )
            MAXWRK = MAX( MAXWRK, INT( DWORK(JWORK) ) + JWORK - 1 )
            JWORK  = ITAU
            IF ( M.GT.0 .AND. WITHB )
     $         CALL DLACPY( 'Full', LNOBR, N, R(NR4,1), LDR, R(NR2,1),
     $                      LDR )
         END IF
C
         IF ( WITHC ) THEN
C
C           Obtain  A  and  C,  noting that block-permutations have been
C           implicitly used.
C
            CALL MA02AD( 'Full', N, N, R(NR2,NR4MN), LDR, A, LDA )
            CALL MA02AD( 'Full', N, L, R(NR2,NR4MN+N), LDR, C, LDC )
         ELSE
C
C           Use the given  A  and  C.
C
            CALL MA02AD( 'Full', N, N, A, LDA, R(NR2,NR4MN), LDR )
            CALL MA02AD( 'Full', L, N, C, LDC, R(NR2,NR4MN+N), LDR )
         END IF
C
         IF ( M.GT.0 .AND. WITHB ) THEN
C
C           Obtain  B  and  D.
C           First, compute the transpose of the matrix K as
C           N(1:m*s,:) - M(1:m*s,m*s+1:m*s+n)*[A'  C'],  in the first
C           m*s  rows of  R(1,NR4MN).
C
            CALL DGEMM ( 'NoTranspose', 'NoTranspose', MNOBR, NPL, N,
     $                   -ONE, R(1,NR2), LDR, R(NR2,NR4MN), LDR, ONE,
     $                   R(1,NR4MN), LDR )
C
C           Denote   M = pinv(GaL)  and construct
C
C                    [ [ A ]   -1   ]                      [ R ]
C           and  L = [ [   ]  R   0 ] Q',  where Gam = Q * [   ].
C                    [ [ C ]        ]                      [ 0 ]
C
C           Then, solve the least squares problem.
C
            CALL DLACPY( 'Full', N, N, A, LDA, R(NR2,NR4), LDR )
            CALL DLACPY( 'Full', L, N, C, LDC, R(NR2+N,NR4), LDR )
            CALL DTRSM(  'Right', 'Upper', 'NoTranspose', 'NonUnit',
     $                   NPL, N, ONE, R(NR2,1), LDR, R(NR2,NR4), LDR )
            CALL DLASET( 'Full', NPL, LNOBRN, ZERO, ZERO, R(NR2,NR4+N),
     $                   LDR )
C
C           Workspace: need 2*N+L; prefer N + (N+L)*NB.
C
            CALL DORMQR( 'Right', 'Transpose', NPL, LNOBR, N, R(NR2,1),
     $                   LDR, DWORK, R(NR2,NR4), LDR, DWORK(JWORK),
     $                   LDWORK-JWORK+1, IERR )
C
C           Obtain the matrix  K  by transposition, and find  B  and  D.
C           Workspace: need   NOBR*(M*(N+L))**2+M*NOBR*(N+L)+
C                             max((N+L)**2,4*M*(N+L)+1);
C                      prefer larger.
C
            CALL MA02AD( 'Full', MNOBR, NPL, R(1,NR4MN), LDR,
     $                   R(NR2,NR3), LDR )
            IX    = MNOBR*NPL**2*M + 1
            JWORK = IX + MNOBR*NPL
            CALL IB01PX( JOBPY, NOBR, N, M, L, R, LDR, O, LDO,
     $                   R(NR2,NR4), LDR, R(NR4PL,NR2), LDR, R(NR2,NR3),
     $                   LDR, DWORK, MNOBR*NPL, DWORK(IX), B, LDB, D,
     $                   LDD, TOL, IWORK, DWORK(JWORK), LDWORK-JWORK+1,
     $                   IWARNL, INFO )
            IF ( INFO.NE.0 )
     $         RETURN
            IWARN  = MAX( IWARN, IWARNL )
            MAXWRK = MAX( MAXWRK, INT( DWORK(JWORK) ) + JWORK - 1 )
            RCOND4 = DWORK(JWORK+1)
C
         END IF
      END IF
C
   30 CONTINUE
C
C     Return optimal workspace in  DWORK(1)  and reciprocal condition
C     numbers in the next locations.
C
      DWORK(1) = MAXWRK
      DWORK(2) = RCOND1
      DWORK(3) = RCOND2
      DWORK(4) = RCOND3
      DWORK(5) = RCOND4
      RETURN
C
C *** Last line of IB01PD ***
      END