Repository navigation
Expand file tree
/
Copy pathsynthesis.py
More file actions
2809 lines (2493 loc) · 120 KB
/
Copy pathsynthesis.py
File metadata and controls
2809 lines (2493 loc) · 120 KB
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
#
# synthesis.py
#
# Copyright 2010-2011 Enrico Avventi <avventi@Lonewolf>
# Copyright 2011 Jerker Nordh <jerker.nordh@control.lth.se>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
from . import _wrapper
import numpy as _np
import warnings
def sb01bd(n,m,np,alpha,A,B,w,dico,tol=0.0,ldwork=None):
""" A_z,w,nfp,nap,nup,F,Z = sb01bd(n,m,np,alpha,A,B,w,dico,[tol,ldwork])
To determine the state feedback matrix F for a given system (A,B) such that
the closed-loop state matrix A+B*F has specified eigenvalues.
Required arguments
------------------
n : int
The dimension of the state vector, n >= 0.
m : int
The dimension of input vector, m >= 0.
np : int
The number of given eigenvalues. At most n eigenvalues can be
assigned. 0 <= np <= n.
alpha : float
Specifies the maximum admissible value, either for real parts,
if dico = 'C', or for moduli, if dico = 'D', of the eigenvalues of
A which will not be modified by the eigenvalue assignment algorithm.
alpha >= 0 if dico = 'D'.
A : rank-2 array('d'), shape (n,n)
State dynamics matrix.
B : rank-2 array('d'), shape (n,m)
Input/state matrix.
w : rank-1 array('c'), shape (np,)
Array of the desired eigenvalues of the closed-loop system state-matrix
A+B*F. The eigenvalues can be unordered, except that complex conjugate
pairs must appear consecutively.
dico : {'C', 'D'}
Specifies the type of the original system as follows:
= 'C': continuous-time system;
= 'D': discrete-time system.
Optional arguments
------------------
tol : float
The absolute tolerance level below which the elements of A or B are
considered zero (used for controllability tests).
If tol <= 0 the default value is used.
ldwork : int
The length of the cache array. The default value is
max(1,5*m,5*n,2*n+4*m), for optimum performance it should be larger.
Returns
-------
A_z : rank-2 array('d'), shape (n,n)
This array contains the matrix Z'*(A+B*F)*Z in a real Schur form.
The diagonal block A[:nfp,:nfp] corresponds to the fixed (unmodified)
eigenvalues having real parts less than alpha, if dico = 'C', or moduli
less than alpha if dico = 'D'.
The diagonal block A[n-nup:,n-nup:] corresponds to the uncontrollable
eigenvalues detected by the eigenvalue assignment algorithm.
The elements under the first subdiagonal are set to zero.
w : rank-1 array('c'), shape (np,)
The first part w[:nap] contain the assigned eigenvalues.
The rest w[np-nap:] contain the unassigned eigenvalues.
nfp : int
The number of eigenvalues of A having real parts less than alpha,
if dico = 'C', or moduli less than alpha, if dico = 'D'. These
eigenvalues are not modified by the eigenvalue assignment algorithm.
nap : int
The number of assigned eigenvalues.
nup : int
The number of uncontrollable eigenvalues detected by the eigenvalue
assignment algorithm.
F : rank-2 array('d'), shape (m,n)
The state feedback F, which assigns nap closed-loop eigenvalues and
keeps unaltered n-nap open-loop eigenvalues.
Z : rank-2 array('d'), shape (n,n)
The orthogonal matrix Z which reduces the closed-loop system state
matrix A + B*F to upper real Schur form.
Raises
------
ValueError : e
e.info contains information about the exact type of exception
< 0: if info = -i, the i-th argument had an illegal value;
= 1: the reduction of A to a real Schur form failed;
= 2: a failure was detected during the ordering of the
real Schur form of A, or in the iterative process
for reordering the eigenvalues of Z'*(A + B*F)*Z
along the diagonal.
= 3: the number of eigenvalues to be assigned is less
than the number of possibly assignable eigenvalues;
nap eigenvalues have been properly assigned,
but some assignable eigenvalues remain unmodified.
= 4: an attempt is made to place a complex conjugate
pair on the location of a real eigenvalue. This
situation can only appear when n-nfp is odd,
np > n-nfp-nup is even, and for the last real
eigenvalue to be modified there exists no available
real eigenvalue to be assigned. However, nap
eigenvalues have been already properly assigned.
Example
-------
>>> import numpy as np
>>> import slycot
>>> A = np.array([[0, 1, 0],[0, 0, 1],[-2, 1, 3]])
>>> B = np.array([[0], [0], [1]])
>>> np.linalg.eig(A)[0] # open loop eigenvalues
array([ 3.11490754, 0.74589831, -0.86080585])
>>> w = np.array([0.5,0.4,0.2])
>>> out = slycot.sb01bd(3,1,3,1,A,B,w,'D')
>>> A_fb = A + np.dot(B,out[5])
>>> np.linalg.eig(A_fb)[0] # closed loop eigenvalues
array([ 0.2 , 0.40000001, 0.5 ])
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['dico', 'n', 'm', 'np', 'alpha', 'A', 'LDA'+hidden, 'B',
'LDB'+hidden, 'wr'+hidden, 'wi'+hidden, 'nfp', 'nap', 'nup', 'F',
'LDF'+hidden, 'Z', 'LDZ'+hidden, 'tol', 'DWORK'+hidden, 'ldwork',
'IWARN'+hidden, 'INFO'+hidden]
if ldwork is None:
ldwork = max(1,5*m,5*n,2*n+4*m)
A_z,wr,wi,nfp,nap,nup,F,Z,warn,info = _wrapper.sb01bd(dico,n,m,np,alpha,A,B,w.real,w.imag,tol=tol,ldwork=ldwork)
if info < 0:
error_text = "The following argument had an illegal value: "+arg_list[-info-1]
e = ValueError(error_text)
e.info = info
raise e
if info == 1:
e = ValueError('the reduction of A to a real Schur form failed')
e.info = info
raise e
if info == 2:
e = ValueError('a failure was detected during the ordering of eigenvalues')
e.info = info
raise e
if info == 3:
e = ValueError('the number of eigenvalues to be assigned is less than the number of possibly assignable eigenvalues')
e.info = info
raise e
if info == 4:
e = ValueError('an attempt was made to place a complex conjugate pair on the location of a real eigenvalue')
e.info = info
raise e
if warn != 0:
warnings.warn('%i violations of the numerical stability condition occured during the assignment of eigenvalues' % warn)
# put togheter wr and wi into a complex array of eigenvalues
w = _np.zeros(np,'complex64')
w.real = wr[0:np]
w.imag = wi[0:np]
return A_z,w,nfp,nap,nup,F,Z
def sb02md(n,A,G,Q,dico,hinv='D',uplo='U',scal='N',sort='S',ldwork=None):
""" X,rcond,w,S,U,A_inv = sb02md(dico,n,A,G,Q,[hinv,uplo,scal,sort,ldwork])
To solve for X either the continuous-time algebraic Riccati
equation
-1
Q + A'*X + X*A - X*B*R B'*X = 0 (1)
or the discrete-time algebraic Riccati equation
-1
X = A'*X*A - A'*X*B*(R + B'*X*B) B'*X*A + Q (2)
where A, B, Q and R are n-by-n, n-by-m, n-by-n and m-by-m matrices
respectively, with Q symmetric and R symmetric nonsingular; X is
an n-by-n symmetric matrix.
-1
The matrix G = B*R B' must be provided on input, instead of B and
R, that is, for instance, the continuous-time equation
Q + A'*X + X*A - X*G*X = 0 (3)
is solved, where G is an n-by-n symmetric matrix. Slycot Library
routine sb02mt should be used to compute G, given B and R. sb02mt
also enables to solve Riccati equations corresponding to optimal
problems with coupling terms.
The routine also returns the computed values of the closed-loop
spectrum of the optimal system, i.e., the stable eigenvalues
lambda(1),...,lambda(n) of the corresponding Hamiltonian or
symplectic matrix associated to the optimal problem.
Required arguments
------------------
n : int
The order of the matrices A, Q, G and X. n > 0.
A : rank-2 array('d'), shape (n,n)
G : rank-2 array('d'), shape (n,n)
The upper triangular part (if uplo = 'U') or lower triangular
part (if uplo = 'L') of this array must contain the upper
triangular part or lower triangular part, respectively, of the
symmetric matrix G.
Q : rank-2 array('d'), shape (n,n)
The upper triangular part (if uplo = 'U') or lower triangular
part (if uplo = 'L') of this array must contain the upper
triangular part or lower triangular part, respectively,
of the symmetric matrix Q.
dico : {'C', 'D'}
Specifies the type of Riccati equation to be solved as follows:
= 'C': Equation (3), continuous-time case;
= 'D': Equation (2), discrete-time case.
Optional arguments
------------------
hinv : {'D', 'I'}
If dico = 'D', specifies which symplectic matrix is to be
constructed, as follows:
= 'D': The Hamiltonian or sympletic matrix H is constructed;
= 'I': The inverse of the matrix H is constructed.
The default value is 'D'. hinv is not used if DICO = 'C'.
uplo : {'U', 'L'}
Specifies which triangle of the matrices G and Q is stored,
as follows:
= 'U': Upper triangle is stored;
= 'L': Lower triangle is stored.
The default value is 'U'.
scal : {'N', 'G'}
Specifies whether or not a scaling strategy should be used,
as follows:
= 'G': General scaling should be used;
= 'N': No scaling should be used.
The default value is 'N'.
sort : {'S', 'U'}
Specifies which eigenvalues should be obtained in the top of
the Schur form, as follows:
= 'S': Stable eigenvalues come first;
= 'U': Unstable eigenvalues come first.
The default value is 'S'.
ldwork : int
The length of the cache array. The default value is max(3, 6*n),
for optimum performance it should be larger.
Returns
-------
X : rank-2 array('d'), shape (n,n)
The solution matrix of the problem.
rcond : float
An estimate of the reciprocal of the condition number (in
the 1-norm) of the n-th order system of algebraic
equations from which the solution matrix X is obtained.
w : rank-1 array('c'), shape (2 * n)
This array contain the eigenvalues of the 2n-by-2n matrix S, ordered
as specified by sort (except for the case hinv = 'D', when the order
is opposite to that specified by sort). The leading n elements of
this array contain the closed-loop spectrum of the system
-1
matrix A - B*R *B'*X, if dico = 'C', or of the matrix
-1
A - B*(R + B'*X*B) B'*X*A, if dico = 'D'.
S : rank-2 array('d'), shape (2 * n,2 * n)
This array contains the ordered real Schur form S of the Hamiltonian
or symplectic matrix H.
U : rank-2 array('d'), shape (2 * n,2 * n)
This array contains the transformation matrix U which reduces the
Hamiltonian or symplectic matrix H to the ordered real Schur form S.
A_inv : rank-2 array('d'), shape (n,n)
The inverse of A if dico = 'D' or a copy of A itself otherwise.
Raises
------
ValueError : e
e.info contains information about the exact type of exception
< 0: if info = -i, the i-th argument had an illegal value;
= 1: if matrix A is (numerically) singular in discrete-
time case;
= 2: if the Hamiltonian or symplectic matrix H cannot be
reduced to real Schur form;
= 3: if the real Schur form of the Hamiltonian or
symplectic matrix H cannot be appropriately ordered;
= 4: if the Hamiltonian or symplectic matrix H has less
than n stable eigenvalues;
= 5: if the n-th order system of linear algebraic
equations, from which the solution matrix X would
be obtained, is singular to working precision.
Example
-------
>>> import numpy as np
>>> import slycot
>>> A = np.array([[0, 1],[0, 0]])
>>> Q = np.array([[1, 0],[0, 2]])
>>> G = np.array([[0, 0],[0, 1]])
>>> out = slycot.sb02md(2,A,G,Q,'C')
>>> out[0] # X
array([[ 2., 1.],
[ 1., 2.]])
>>> out[1] # rcond
0.3090169943749475
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['dico', 'hinv', 'uplo', 'scal', 'sort', 'n', 'A', 'LDA'+hidden,
'G', 'LDG'+hidden, 'Q', 'LDQ'+hidden, 'rcond', 'wr'+hidden, 'wi'+hidden, 'S',
'LDS'+hidden, 'U', 'LDU'+hidden, 'IWORK'+hidden, 'DWORK'+hidden, 'ldwork',
'BWORK'+hidden, 'INFO'+hidden]
if ldwork is None:
ldwork = max(3,6*n)
A_inv,X,rcond,wr,wi,S,U,info = _wrapper.sb02md(dico,n,A,G,Q,hinv=hinv,uplo=uplo,scal=scal,sort=sort,ldwork=ldwork)
if info < 0:
error_text = "The following argument had an illegal value: "+arg_list[-info-1]
e = ValueError(error_text)
e.info = info
raise e
if info == 1:
e = ValueError('matrix A is (numerically) singular in discrete-time case')
e.info = info
raise e
if info == 2:
e = ValueError('the Hamiltonian or symplectic matrix H cannot be reduced to real Schur form')
e.info = info
raise e
if info == 3:
e = ValueError('the real Schur form of the Hamiltonian or symplectic matrix H cannot be appropriately ordered')
e.info = info
raise e
if info == 4:
e = ValueError('the Hamiltonian or symplectic matrix H has less than n stable eigenvalues')
e.info = info
raise e
if info == 5:
e = ValueError('if the N-th order system of linear algebraic equations is singular to working precision')
e.info = info
raise e
w = _np.zeros(2*n,'complex64')
w.real = wr[0:2*n]
w.imag = wi[0:2*n]
return X,rcond,w,S,U,A_inv
def sb02mt(n,m,B,R,A=None,Q=None,L=None,fact='N',jobl='Z',uplo='U',ldwork=None):
""" A_b,B_b,Q_b,R_b,L_b,ipiv,oufact,G = sb02mt(n,m,B,R,[A,Q,L,fact,jobl,uplo,ldwork])
To compute the following matrices
-1
G = B*R *B',
-1
A_b = A - B*R *L',
-1
Q_b = Q - L*R *L',
where A, B, Q, R, L, and G are n-by-n, n-by-m, n-by-n, m-by-m, n-by-m,
and n-by-n matrices, respectively, with Q, R and G symmetric matrices.
When R is well-conditioned with respect to inversion, standard algorithms
for solving linear-quadratic optimization problems will then also solve
optimization problems with coupling weighting matrix L.
Required arguments
------------------
n : int
The order of the matrices A, Q, and G, and the number of rows of
the matrices B and L. n >= 0.
m : int
The order of the matrix R, and the number of columns of the matrices
B and L. m >= 0.
B : rank-2 array('d'), shape (n,m)
R : rank-2 array('d'), shape (m,m)
If fact = 'N', the upper/lower triangular part of this array must
contain the upper/lower triangular part, of the symmetric input
weighting matrix R.
If fact = 'C', the upper/lower triangular part of this array must
contain the Cholesky factor of the positive definite input weighting
matrix R.
Optional arguments
------------------
A : rank-2 array('d'), shape (n,n)
If jobl = 'Z', this matrix is not needed.
Q : rank-2 array('d'), shape (n,n)
If jobl = 'Z', this matrix is not needed. Otherwise the upper/lower
triangular part of this array (depending on uplo) must contain the
corresponding part of matrix Q.
L : rank-2 array('d'), shape (n,m)
If jobl = 'Z', this matrix is not needed.
fact : {'N', 'C'}
Specifies how the matrix R is given (factored or not), as follows:
= 'N': Array R contains the matrix R,
= 'C': Array R contains the Cholesky factor of R.
The default value is 'N'.
jobl : {'Z', 'N'}
When equal to 'Z', L is considered as a zero matrix and A,Q and L are
not needed. A,Q and L are required otherwise.The default value is 'Z'.
uplo : {'U', 'L'}
Specifies which triangle of the matrices R and Q (if jobl = 'N')
is stored, as follows:
= 'U': Upper triangle is stored;
= 'L': Lower triangle is stored.
The default value is 'U'.
ldwork : int
The length of the cache array. Whenever fact = 'N' the default value
is max(2,3*m,n*m), for optimum performance it should be larger.
No cache is needed if fact = 'C', defaults at 1.
Returns
-------
A_b : rank-2 array('d'), shape (n,n)
If jobl = 'Z', this is None.
B_b : rank-2 array('d'), shape (n,m)
-1
If oufact = 1 this array contains the matrix B*chol(R) . It is a copy
of input B if oufact = 2.
Q_b : rank-2 array('d'), shape (n,n)
If jobl = 'Z', this is None. Otherwise the upper/lower triangular part
of this array contain the corresponding triangular part of matrix Q_b
(depending on uplo).
R_b : rank-2 array('d'), shape (m,m)
If oufact = 1, the upper/lower triangular part of this array contains
the Cholesky factor of the given input weighting matrix.
If oufact = 2, the upper/lower triangular part of this array contains
the factors of the UdU' or LdL' factorization, respectively, of the given
input weighting matrix.
If fact = 'C' it is a copy of input R.
L_b : rank-2 array('d'), shape (n,m)
If jobl = 'Z', this is None. If oufact = 1, this array contains the matrix
-1
L*chol(R) .If oufact = 2 this contains a copy of input L.
ipiv : rank-1 array('i'), shape (m,)
If oufact = 2, this array contains details of the interchanges
performed and the block structure of the d factor in the UdU' or
LdL' factorization of matrix R, as produced by LAPACK routine DSYTRF.
Otherwise it is None.
oufact : int
Information about the factorization finally used.
oufact = 1: Cholesky factorization of R has been used;
oufact = 2: UdU' (if uplo = 'U') or LdL' (if uplo = 'L')
factorization of R has been used.
G : rank-2 array('d'), shape (n,n)
The upper/lower triangular part of this array contains the corresponding
triangular part of the matrix G.
Raises
------
ValueError : e
e.info contains information about the exact type of exception
< 0: if info = -i, the i-th argument had an illegal value;
= i: if the i-th element (1 <= i <= m) of the d factor is
exactly zero; the UdU' (or LdL') factorization has
been completed, but the block diagonal matrix d is
exactly singular;
= m+1: if the matrix R is numerically singular.
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['JOBG'+hidden, 'jobl', 'fact', 'uplo', 'n', 'm', 'A',
'LDA'+hidden, 'B', 'LDB'+hidden, 'Q', 'LDQ'+hidden, 'R', 'LDR'+hidden, 'L',
'LDL'+hidden, 'ipiv', 'oufact', 'G', 'LDG'+hidden, 'IWORK'+hidden,
'DWORK'+hidden, 'ldwork', 'INFO'+hidden]
out = None
if fact == 'N' and ldwork is None:
ldwork = max(2,3*m,n*m)
if jobl == 'Z':
if fact == 'C':
out = _wrapper.sb02mt_c(n,m,B,R,uplo=uplo)
if fact == 'N':
out = _wrapper.sb02mt_n(n,m,B,R,uplo=uplo,ldwork=ldwork)
if out is None:
raise ValueError('fact must be either C or N.')
else:
if A is None or Q is None or L is None:
raise ValueError('matrices A,Q and L are required if jobl is not Z.')
if fact == 'C':
out = _wrapper.sb02mt_cl(n,m,A,B,Q,R,L,uplo=uplo)
if fact == 'N':
out = _wrapper.sb02mt_nl(n,m,A,B,Q,R,L,uplo=uplo,ldwork=ldwork)
if out is None:
raise ValueError('fact must be either C or N.')
if out[-1] < 0:
error_text = "The following argument had an illegal value: "+arg_list[-out[-1]-1]
e = ValueError(error_text)
e.info = out[-1]
raise e
if out[-1] > 0 and out[-1] <= m:
e = ValueError('the %i-th elemend of d in the UdU (LdL) factorization is zero.'%out[-1])
e.info = out[-1]
raise e
if out[-1] == m+1:
e = ValueError('matrix R is numerically singular.')
e.info = out[-1]
raise e
return out[:-1]
def sb02od(n,m,A,B,Q,R,dico,p=None,L=None,fact='N',uplo='U',sort='S',tol=0.0,ldwork=None):
""" X,rcond,w,S,T = sb02od(n,m,A,B,Q,R,dico,[p,L,fact,uplo,sort,tol,ldwork])
To solve for X either the continuous-time algebraic Riccati
equation
-1
Q + A'X + XA - (L+XB)R (L+XB)' = 0 (1)
or the discrete-time algebraic Riccati equation
-1
X = A'XA - (L+A'XB)(R + B'XB) (L+A'XB)' + Q (2)
where A, B, Q, R, and L are n-by-n, n-by-m, n-by-n, m-by-m and
n-by-m matrices, respectively, such that Q = C'C, R = D'D and
L = C'D; X is an n-by-n symmetric matrix.
The routine also returns the computed values of the closed-loop
spectrum of the system, i.e., the stable eigenvalues w(1),
..., w(n) of the corresponding Hamiltonian or symplectic
pencil, in the continuous-time case or discrete-time case,
respectively.
Optionally, Q and/or R may be given in a factored form, Q = C'C,
R = D'D, and L may be treated as a zero matrix.
The routine uses the method of deflating subspaces, based on
reordering the eigenvalues in a generalized Schur matrix pair.
Required arguments
------------------
n : int
The actual state dimension, i.e. the order of the matrices A, Q,
and X, and the number of rows of the matrices B and L. n > 0.
m : int
The number of system inputs, the order of the matrix R, and the
number of columns of the matrix B. m > 0.
A : rank-2 array('d'), shape (n,n)
The state matrix of the system.
B : rank-2 array('d'), shape (n,m)
The input matrix of the system.
Q : rank-2 array('d'), shape (n,n) or (p,n)
If fact = 'N' or 'D', the shape must be (n,n) and the upper/lower
triangular part (depending on uplo) of this array must contain
the corresponding triangular part of the symmetric state weighting
matrix Q.
If fact = 'C' or 'B', the shape must be (p,n) and of this array must
contain the output matrix C of the system.
R : rank-2 array('d'), shape (m,m) or (p,m)
If fact = 'N' or 'C', the shape must be (m,m) and the upper/lower
triangular part (depending on uplo) of this array must contain the
corresponding triangular part of the symmetric input weighting matrix R.
If FACT = 'D' or 'B', the shape must be (p,m) and this array must
contain the direct transmission matrix D of the system.
dico : {'C', 'D'}
Specifies the type of Riccati equation to be solved as follows:
= 'C': Equation (1), continuous-time case;
= 'D': Equation (2), discrete-time case.
Optional arguments
------------------
p : int
The number of system outputs. If fact = 'C' or 'D' or 'B',
p is the number of rows of the matrices C and/or D. p > 0.
If fact = 'N' it is not needed.
L : rank-2 array('d'), shape (n,m)
If L is not specified it will considered as a zero matrix of the
appropriate dimensions.
fact : {'N', 'C', 'D', 'B'}
Specifies whether or not the matrices Q and/or R are factored,
as follows:
= 'N': Not factored, Q and R are given;
= 'C': C is given, and Q = C'C;
= 'D': D is given, and R = D'D;
= 'B': Both factors C and D are given, Q = C'C, R = D'D.
The default value is 'N'.
uplo : {'U', 'L'}
If fact = 'N', specifies which triangle of Q and R is stored,
as follows:
= 'U': Upper triangle is stored;
= 'L': Lower triangle is stored.
The default value is 'U'.
sort : {'S', 'U'}
Specifies which eigenvalues should be obtained in the top of
the generalized Schur form, as follows:
= 'S': Stable eigenvalues come first;
= 'U': Unstable eigenvalues come first.
The default value is 'S'.
tol : float
The tolerance to be used in rank determination of the original
matrix pencil, specifically of the triangular factor obtained during
the reduction process. If tol <= 0 a default value is used.
ldwork : int
The length of the cache array. The default value is
max(7*(2*n+1)+16,16*n,2*n+m,3*m), for optimum performance it should
be larger.
Returns
-------
X : rank-2 array('d'), shape (n,n)
The solution matrix of the problem.
rcond : float
An estimate of the reciprocal of the condition number (in
the 1-norm) of the n-th order system of algebraic equations
from which the solution matrix X is obtained.
w : rank-1 array('c'), shape (2 * n)
The generalized eigenvalues of the 2n-by-2n matrix pair, ordered as
specified by sort. For instance, if sort = 'S', the leading n
elements of these arrays contain the closed-loop spectrum of the
system matrix A - BF, where F is the optimal feedback matrix computed
based on the solution matrix X.
S : rank-2 array('d'), shape (2*n+m,2 * n)
This array contains the ordered real Schur form S of the first matrix
in the reduced matrix pencil associated to the optimal problem, or of
the corresponding Hamiltonian matrix
T : rank-2 array('d'), shape (2*n+m+1,2 * n)
This array contains the ordered upper triangular form T of the second
matrix in the reduced matrix pencil associated to the optimal problem.
Raises
------
ValueError : e
e.info contains information about the exact type of exception
< 0: if info = -i, the i-th argument had an illegal value;
= 1: if the computed extended matrix pencil is singular,
possibly due to rounding errors;
= 2: if the QZ (or QR) algorithm failed;
= 3: if reordering of the (generalized) eigenvalues failed;
= 4: if after reordering, roundoff changed values of
some complex eigenvalues so that leading eigenvalues
in the (generalized) Schur form no longer satisfy
the stability condition; this could also be caused
due to scaling;
= 5: if the computed dimension of the solution does not
equal n;
= 6: if a singular matrix was encountered during the
computation of the solution matrix X.
Example
-------
>>> import numpy as np
>>> import slycot
>>> A = np.array([[0, 1],[0, 0]])
>>> B = np.array([[0],[1]])
>>> C = np.array([[1, 0],[0, 1],[0, 0]])
>>> Q = np.dot(C.T,C)
>>> R = np.ones((1,1))
>>> out = slycot.sb02od(2,1,A,B,Q,R,'C')
>>> out[0] # X
array([[ 1.73205081, 1. ],
[ 1. , 1.73205081]])
>>> out[1] # rcond
0.4713846564431681
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['dico', 'jobb'+hidden, 'fact', 'uplo', 'jobl', 'sort', 'n',
'm', 'p', 'A', 'LDA'+hidden, 'B', 'LDB'+hidden, 'Q', 'LDQ'+hidden,
'R', 'LDR'+hidden, 'L', 'LDL'+hidden, 'rcond', 'X', 'LDX'+hidden,
'alfar'+hidden, 'alfai'+hidden, 'beta'+hidden, 'S', 'LDS'+hidden, 'T',
'LDT'+hidden, 'U', 'LDU'+hidden, 'tol', 'IWORK'+hidden, 'DWORK'+hidden,
'ldwork', 'BWORK'+hidden, 'INFO'+hidden]
if ldwork is None:
ldwork = max([7*(2*n+1)+16,16*n,2*n+m,3*m])
jobl = 'N'
if L is None:
L = _np.zeros((n,m))
jobl = 'Z'
out = None
if fact == 'N':
out = _wrapper.sb02od_n(dico,n,m,A,B,Q,R,L,uplo=uplo,jobl=jobl,sort=sort,tol=tol,ldwork=ldwork)
if fact == 'C':
if p is None:
p = _np.shape(Q)[0]
out = _wrapper.sb02od_c(dico,n,m,p,A,B,Q,R,L,uplo=uplo,jobl=jobl,sort=sort,tol=tol,ldwork=ldwork)
if fact == 'D':
if p is None:
p = _np.shape(R)[0]
out = _wrapper.sb02od_d(dico,n,m,p,A,B,Q,R,L,uplo=uplo,jobl=jobl,sort=sort,tol=tol,ldwork=ldwork)
if fact == 'B':
if p is None:
p = _np.shape(Q)[0]
out = _wrapper.sb02od_b(dico,n,m,p,A,B,Q,R,L,uplo=uplo,jobl=jobl,sort=sort,tol=tol,ldwork=ldwork)
if out[-1] < 0:
error_text = "The following argument had an illegal value: "+arg_list[-out[-1]-1]
e = ValueError(error_text)
e.info = out[-1]
raise e
if out[-1] == 1:
e = ValueError('the computed extended matrix pencil is singular, possibly due to rounding errors')
e.info = out[-1]
raise e
if out[-1] == 2:
e = ValueError('the QZ (or QR) algorithm failed')
e.info = out[-1]
raise e
if out[-1] == 3:
e = ValueError('reordering of the (generalized) eigenvalues failed')
e.info = out[-1]
raise e
if out[-1] == 4:
e = ValueError('stability condition failed due to roudoff errors')
e.info = out[-1]
raise e
if out[-1] == 5:
e = ValueError('the computed dimension of the solution does not equal N')
e.info = out[-1]
raise e
if out[-1] == 6:
e = ValueError('a singular matrix was encountered during the computation')
e.info = out[-1]
raise e
rcond,X,alphar,alphai,beta,S,T = out[:-1]
w = _np.zeros(2*n,'complex64')
w.real = alphar[0:2*n]/beta[0:2*n]
w.imag = alphai[0:2*n]/beta[0:2*n]
return X,rcond,w,S,T
def sb03md(n,C,A,U,dico,job='X',fact='N',trana='N',ldwork=None):
""" X,scale,sep,ferr,w = sb03md(dico,n,C,A,U,[job,fact,trana,ldwork])
To solve for X either the real continuous-time Lyapunov equation
op(A)'*X + X*op(A) = scale*C (1)
or the real discrete-time Lyapunov equation
op(A)'*X*op(A) - X = scale*C (2)
and/or estimate an associated condition number, called separation,
where op(A) = A or A' (A**T) and C is symmetric (C = C').
(A' denotes the transpose of the matrix A.) A is n-by-n, the right
hand side C and the solution X are n-by-n, and scale is an output
scale factor, set less than or equal to 1 to avoid overflow in X.
Required arguments
------------------
n : input int
The order of the matrices A, X, and C. n > 0.
C : input rank-2 array('d'), shape (n,n)
If job = 'X' or 'B', the leading n-by-n part of this array must
contain the symmetric matrix C. If job = 'S', C is not referenced.
A : input rank-2 array('d'), shape (n,n)
On entry, the leading n-by-n part of this array must contain the
matrix A. If fact = 'F', then A contains an upper quasi-triangular
matrix in Schur canonical form; the elements below the upper
Hessenberg part of the array A are not referenced.
On exit, the leading n-by-n upper Hessenberg part of this array
contains the upper quasi-triangular matrix in Schur canonical form
from the Schur factorization of A. The contents of array A is not
modified if fact = 'F'.
U : input rank-2 array('d'), shape (n,n)
If fact = 'F', then U is an input argument and on entry the leading
n-by-n part of this array must contain the orthogonal matrix U of
the real Schur factorization of A.
If fact = 'N', then U is an output argument and on exit, it contains
the orthogonal n-by-n matrix from the real Schur factorization of A.
dico : input string(len=1)
Specifies the equation from which X is to be determined as follows:
= 'C': Equation (1), continuous-time case;
= 'D': Equation (2), discrete-time case.
Optional arguments
------------------
job := 'X' input string(len=1)
Specifies the computation to be performed, as follows:
= 'X': Compute the solution only;
= 'S': Compute the separation only;
= 'B': Compute both the solution and the separation.
fact := 'N' input string(len=1)
Specifies whether or not the real Schur factorization of the matrix
A is supplied on entry, as follows:
= 'F': On entry, A and U contain the factors from the real Schur
factorization of the matrix A;
= 'N': The Schur factorization of A will be computed and the factors
will be stored in A and U.
trana := 'N' input string(len=1)
Specifies the form of op(A) to be used, as follows:
= 'N': op(A) = A (No transpose);
= 'T': op(A) = A**T (Transpose);
= 'C': op(A) = A**T (Conjugate transpose = Transpose).
ldwork := None input int
The length of the cache array. The default value is max(2*n*n,3*n),
for optimum performance it should be larger.
Return objects
--------------
X : rank-2 array('d'), shape (n,n)
If job = 'X' or 'B', the leading n-by-n part contains the symmetric
solution matrix.
scale : float
The scale factor, scale, set less than or equal to 1 to prevent
the solution from overflowing.
sep : float
If job = 'S' or 'B', sep contains the estimated separation of
the matrices op(A) and -op(A)', if dico = 'C' or of op(A) and op(A)',
if dico = 'D'.
ferr : float
If job = 'B', ferr contains an estimated forward error bound for
the solution X. If X_true is the true solution, ferr bounds the
relative error in the computed solution, measured in the Frobenius
norm: norm(X - X_true)/norm(X_true).
w : rank-1 array('c'), shape (n)
If fact = 'N', this array contain the eigenvalues of A.
Raises
------
ValueError : e
e.info contains information about the exact type of exception
< 0: if info = -i, the i-th argument had an illegal value;
> 0: if info = i, the QR algorithm failed to compute all
the eigenvalues (see LAPACK Library routine DGEES);
elements i+1:n of w contain eigenvalues which have converged,
and A contains the partially converged Schur form;
= N+1: if dico = 'C', and the matrices A and -A' have
common or very close eigenvalues, or
if dico = 'D', and matrix A has almost reciprocal
eigenvalues (that is, lambda(i) = 1/lambda(j) for
some i and j, where lambda(i) and lambda(j) are
eigenvalues of A and i <> j); perturbed values were
used to solve the equation (but the matrix A is
unchanged).
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['dico', 'job', 'fact', 'trana', 'n', 'A', 'LDA'+hidden, 'U',
'LDU'+hidden, 'C', 'LDC'+hidden, 'scale', 'sep', 'ferr', 'wr'+hidden,
'wi'+hidden, 'IWORK'+hidden, 'DWORK'+hidden, 'ldwork', 'INFO'+hidden]
if ldwork is None:
ldwork = max(2*n*n,3*n)
if dico != 'C' and dico != 'D':
raise ValueError('dico must be either D or C')
out = _wrapper.sb03md(dico,n,C,A,U,job=job,fact=fact,trana=trana,ldwork=ldwork)
if out[-1] < 0:
error_text = "The following argument had an illegal value: "+arg_list[-out[-1]-1]
e = ValueError(error_text)
e.info = out[-1]
raise e
if out[-1] == n+1:
if dico == 'D':
error_text = 'The matrix A has eigenvalues that are almost reciprocal.'
else:
error_text = 'The matrix A and -A have common or very close eigenvalues.'
e = ValueError(error_text)
e.info = out[-1]
raise e
else:
if out[-1] > 0:
error_text = """The QR algorithm failed to compute all the eigenvalues
(see LAPACK Library routine DGEES); elements %i:%i of w contains
eigenvalues which have converged, A contains the partially
converged Shur form'""" %(out[-1],n) # not sure about the indenting here
e = ValueError(error_text)
e.info = out[-1]
raise e
X,scale,sep,ferr,wr,wi = out[:-1]
w = _np.zeros(n,'complex64')
w.real = wr[0:n]
w.imag = wi[0:n]
return X,scale,sep,ferr,w
def sb03od(n,m,A,Q,B,dico,fact='N',trans='N',ldwork=None):
""" U,scale,w = sb03od(dico,n,m,A,Q,B,[fact,trans,ldwork])
To solve for X = op(U)'*op(U) either the stable non-negative
definite continuous-time Lyapunov equation
2
op(A)'*X + X*op(A) = -scale *op(B)'*op(B) (1)
or the convergent non-negative definite discrete-time Lyapunov
equation
2
op(A)'*X*op(A) - X = -scale *op(B)'*op(B) (2)
where op(K) = K or K' (i.e., the transpose of the matrix K), A is
an N-by-N matrix, op(B) is an M-by-N matrix, U is an upper
triangular matrix containing the Cholesky factor of the solution
matrix X, X = op(U)'*op(U), and scale is an output scale factor,
set less than or equal to 1 to avoid overflow in X. If matrix B
has full rank then the solution matrix X will be positive-definite
and hence the Cholesky factor U will be nonsingular, but if B is
rank deficient then X may be only positive semi-definite and U
will be singular.
In the case of equation (1) the matrix A must be stable (that
is, all the eigenvalues of A must have negative real parts),
and for equation (2) the matrix A must be convergent (that is,
all the eigenvalues of A must lie inside the unit circle).
Required arguments
------------------
n : input int
The order of the matrix A and the number of columns in
matrix op(B). n >= 0.
m : input int
The number of rows in matrix op(B). m >= 0.
A : input rank-2 array('d'), shape (n,n)
On entry, the leading n-by-n part of this array must
contain the matrix A. If fact = 'F', then A contains
an upper quasi-triangular matrix S in Schur canonical
form; the elements below the upper Hessenberg part of the
array A are not referenced.
On exit, the leading n-by-n upper Hessenberg part of this
array contains the upper quasi-triangular matrix S in
Schur canonical form from the Shur factorization of A.
The contents of array A is not modified if fact = 'F'.
Q : input rank-2 array('d'), shape (n,n)
On entry, if fact = 'F', then the leading n-by-n part of
this array must contain the orthogonal matrix Q of the
Schur factorization of A.
Otherwise, Q need not be set on entry.
On exit, the leading n-by-n part of this array contains
the orthogonal matrix Q of the Schur factorization of A.
The contents of array Q is not modified if fact = 'F'.
B : input rank-2 array('d'), shape (m,n)
On entry, if trans = 'N', the leading m-by-n part of this
array must contain the coefficient matrix B of the
equation.
On entry, if trans = 'T', the leading N-by-m part of this
array must contain the coefficient matrix B of the
equation.
On exit, the leading n-by-n part of this array contains
the upper triangular Cholesky factor U of the solution
matrix X of the problem, X = op(U)'*op(U).
If m = 0 and n > 0, then U is set to zero.
dico : input string(len=1)
Specifies the type of Lyapunov equation to be solved as
follows:
= 'C': Equation (1), continuous-time case;
= 'D': Equation (2), discrete-time case.
Optional arguments
------------------
fact := 'N' input string(len=1)
Specifies whether or not the real Schur factorization
of the matrix A is supplied on entry, as follows:
= 'F': On entry, A and Q contain the factors from the
real Schur factorization of the matrix A;
= 'N': The Schur factorization of A will be computed
and the factors will be stored in A and Q.
trans := 'N' input string(len=1)
Specifies the form of op(K) to be used, as follows:
= 'N': op(K) = K (No transpose);
= 'T': op(K) = K**T (Transpose).
ldwork := None input int
The length of the array DWORK.
If m > 0, ldwork >= max(1,4*n + min(m,n));
If m = 0, ldwork >= 1.
For optimum performance ldwork should sometimes be larger.
Return objects
______________
U : rank-2 array('d'), shape (n,n)
The leading n-by-n part of this array contains
the upper triangular Cholesky factor U of the solution
matrix X of the problem, X = op(U)'*op(U).
scale : float
The scale factor, scale, set less than or equal to 1 to
prevent the solution overflowing.
w : rank-1 array('c'), shape (n)
If fact = 'N', this array contains the eigenvalues of A.
Raises
______
ValueError : e
e.info contains information about the exact type of exception
< 0: if info = -i, the i-th argument had an illegal value;
= 1: if the Lyapunov equation is (nearly) singular
(warning indicator);
if DICO = 'C' this means that while the matrix A
(or the factor S) has computed eigenvalues with
negative real parts, it is only just stable in the
sense that small perturbations in A can make one or
more of the eigenvalues have a non-negative real
part;