-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathsynthesis.py
More file actions
2852 lines (2515 loc) · 109 KB
/
Copy pathsynthesis.py
File metadata and controls
2852 lines (2515 loc) · 109 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 warnings import warn
import numpy as _np
from . import _wrapper
from .exceptions import raise_if_slycot_error, SlycotParameterError
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.
Parameters
----------
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 : (n, n) array_like
State dynamics matrix.
B : (n, m) array_like
Input/state matrix.
w : (np, ) complex array_like
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.
tol : float, optional
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.
Default is `0.0`.
ldwork : int, optional
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 : (n, n) ndarray
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 : (np, ) complex ndarray
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 : (m, n) ndarray
The state feedback F, which assigns nap closed-loop eigenvalues and
keeps unaltered n-nap open-loop eigenvalues.
Z : (n, n) ndarray
The orthogonal matrix Z which reduces the closed-loop system state
matrix A + B*F to upper real Schur form.
Raises
------
SlycotArithmeticError
:info = 1:
the reduction of A to a real Schur form failed;
:info = 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.
Warns
-----
SlycotResultWarning
:info = 3:
the number of eigenvalues to be assigned is less
than the number of possibly assignable eigenvalues;
`nap`={nap} eigenvalues have been properly assigned,
but some assignable eigenvalues remain unmodified.
:info = 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`={nap}
eigenvalues have been already properly assigned.
:iwarn > 0:
{iwarn} violations of the numerical stability condition
``NORM(F) <= 100*NORM(A)/NORM(B)`` occured during the
assignment of eigenvalues
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, iwarn, info = _wrapper.sb01bd(
dico, n, m, np, alpha, A, B, w.real, w.imag, tol=tol, ldwork=ldwork)
raise_if_slycot_error([iwarn, info], arg_list, sb01bd.__doc__, locals())
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.
The matrix ``G = B*R^{-1}*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.
Parameters
----------
n : int
The order of the matrices A, Q, G and X. n > 0.
A : (n, n) array_like
G : (n, n) array_like
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 : (n, n) array_like
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.
hinv : {'D', 'I'}, optional
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'}, optional
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'}, optional
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'}, optional
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, optional
The length of the cache array. The default value is max(3, 6*n),
for optimum performance it should be larger.
Returns
-------
X : (n, n) ndarray
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 : (2*n, ) complex ndarray
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 matrix
::
-1
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 : (2*n, 2*n) ndarray
This array contains the ordered real Schur form S of the Hamiltonian
or symplectic matrix H.
U : (2*n, 2*n) ndarray
This array contains the transformation matrix U which reduces the
Hamiltonian or symplectic matrix H to the ordered real Schur form S.
A_inv : (n, n) ndarray
The inverse of A if dico = 'D' or a copy of A itself otherwise.
Raises
------
SlycotArithmeticError
:info = 1:
Matrix A is (numerically) singular in discrete-
time case;
:info = 2:
The Hamiltonian or symplectic matrix H cannot be
reduced to real Schur form;
:info = 3:
The real Schur form of the Hamiltonian or
symplectic matrix H cannot be appropriately ordered;
:info = 4:
The Hamiltonian or symplectic matrix H has less
than n stable eigenvalues;
:info = 5:
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)
raise_if_slycot_error(info, arg_list, sb02md.__doc__)
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.
Parameters
----------
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 : (n, m) array_like
R : (m, m) array_like
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.
A : (n, n) array_like, optional
If jobl = 'Z', this matrix is not needed.
Q : (n, n) array_like, optional
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 : (n, m) array_like, optional
If jobl = 'Z', this matrix is not needed.
fact : {'N', 'C'}, optional
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'}, optional
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'}, optional
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, optional
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 : (n, n) ndarray
If jobl = 'Z', this is None.
B_b : (n, m) ndarray
If oufact = 1 this array contains the matrix ``B*chol(R)^{-1}``.
It is a copy of input B if oufact = 2.
Q_b : (n, n) ndarray
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 : (m, m) ndarray
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 : (n, m) ndarray
If jobl = 'Z', this is None. If oufact = 1, this array contains the matrix
``L*chol(R)^{-1}``. If oufact = 2 this contains a copy of input L.
ipiv : (m, ) int ndarray
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 : (n, n) ndarray
The upper/lower triangular part of this array contains the corresponding
triangular part of the matrix G.
Raises
------
SlycotArithmeticError
:1 <= info <= m:
The {info}-th element of the `d` factor is
exactly zero; the ``UdU' (or LdL')`` factorization has
been completed, but the block diagonal matrix d is
exactly singular;
:info = m+1:
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 SlycotParameterError('fact must be either C or N.', -3)
else:
if A is None or Q is None or L is None:
raise SlycotParameterError(
'matrices A,Q and L are required if jobl is not Z.', -7)
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 SlycotParameterError('fact must be either C or N.', -3)
raise_if_slycot_error(out[-1], arg_list, sb02mt.__doc__, locals())
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.
Parameters
----------
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 : (n, n) array_like
The state matrix of the system.
B : (n, m) array_like
The input matrix of the system.
Q : (n, n) or (p, n) array_like
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 : (m, m) or (p, m) array_like
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.
p : int, optional
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 : (n, m) array_like, optional
If L is not specified it will considered as a zero matrix of the
appropriate dimensions.
fact : {'N', 'C', 'D', 'B'}, optional
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'}, optional
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'}, optional
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, optional
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, optional
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 : (n, n) array_like
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 : (2*n, ) complex array_like
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 : (2*n+m, 2*n) array_like
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 : (2*n+m+1, 2*n) array_like
This array contains the ordered upper triangular form T of the second
matrix in the reduced matrix pencil associated to the optimal problem.
Raises
------
SlycotArithmeticError
:info = 1:
The computed extended matrix pencil is singular,
possibly due to rounding errors;
:info = 2:
The QZ (or QR) algorithm failed;
:info = 3:
Reordering of the (generalized) eigenvalues failed;
:info = 4:
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;
:info = 5:
The computed dimension of the solution does not
equal n;
:info = 6:
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)
raise_if_slycot_error(out[-1], arg_list, sb02od.__doc__)
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 sb03md57(A, U=None, C=None,
dico='C', job='X',fact='N',trana='N',ldwork=None):
"""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.
Parameters
----------
A : (n, n) array_like
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.
U : (n, n) array_like
If fact = 'F', then this array must contain the orthogonal matrix U of
the real Schur factorization of A.
C : (n, n) array_like
If job = 'X' or 'B', this array must contain the symmetric matrix C.
If job = 'S', C is not referenced.
dico : {'C', 'D'}, optional
Specifies the equation from which X is to be determined as follows:
:= 'C': Equation (1), continuous-time case;
:= 'D': Equation (2), discrete-time case.
job : {'X', 'S', 'B'}, optional
Specifies the computation to be performed, as follows:
:= 'X': Compute the solution only; (default)
:= 'S': Compute the separation only;
:= 'B': Compute both the solution and the separation.
fact : {'N', 'F'}, optional
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. (default)
trana : {'N', 'T', 'C'}, optional
Specifies the form of op(A) to be used, as follows:
:= 'N': op(A) = A (No transpose, default=
:= 'T': op(A) = A**T (Transpose);
:= 'C': op(A) = A**T (Conjugate transpose = Transpose).
ldwork : int, optional
The length of the cache array. The default value is max(2*n*n, 3*n),
for optimum performance it should be larger.
Returns
-------
Ar : (n, n) ndarray
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 content of array A is not
modified if fact = 'F'.
Ur : (n, n) ndarray
If fact = 'N', this arrray contains the orthogonal n-by-n matrix
from the real Schur factorization of A.
X : (n, n) ndarray
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 : (n, ) complex ndarray
If fact = 'N', this array contains the eigenvalues of A.
Warns
-----
SlycotResultWarning
:0 < info <=n:
The QR algorithm failed to compute all
the eigenvalues (see LAPACK Library routine DGEES);
w[{info}:{n}] contains eigenvalues which have converged,
and A contains the partially converged Shur form
:info == n+1 and dico == 'C':
The matrices `A` and `-A'` have common or very close eigenvalues
:info == n+1 and dico == 'D':
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]
n = A.shape[0]
if U is None:
U = _np.zeros((n, n))
if C is None:
C = _np.zeros((n, n))
if ldwork is None:
ldwork = max(2*n*n, 3*n) if dico == 'C' else 2*n*n + 2*n
if dico != 'C' and dico != 'D':
raise SlycotParameterError('dico must be either D or C', -1)
out = _wrapper.sb03md(dico, n, A, U, C,
job=job, fact=fact, trana=trana, ldwork=ldwork)
raise_if_slycot_error(out[-1], arg_list, sb03md.__doc__, locals())
Ar, Ur, X, scale, sep, ferr, wr, wi = out[:-1]
w = _np.zeros(n, 'complex64')
w.real = wr[0:n]
w.imag = wi[0:n]
return Ar, Ur, X, scale, sep, ferr, w
def sb03md(n, C, A, U, dico, job='X',fact='N',trana='N',ldwork=None):
""" X,scale,sep,ferr,w = sb03md(n,C,A,U,dico,[job,fact,trana,ldwork])
.. deprecated:: 0.5
This function uses a call signature of SB03MD prior to SLICOT version
5.7. Use `sb03md57` instead.
"""
warn("sb03md uses a call signature of SB03MD prior to SLICOT version 5.7."
" Use sb03md57 for the new call signature",
DeprecationWarning, stacklevel=2)
ret = sb03md57(A, U, C, dico, job, fact, trana, ldwork)
return ret[2:]
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).
Parameters
----------
n : int
The order of the matrix A and the number of columns of
the matrix op(B). n >= 0.
m : int
The number of rows in matrix op(B). m >= 0.
A : (n, n) array_like
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 then 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 the array A is not modified if fact = 'F'.
Q : (n, n) array_like
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 the array Q is not modified if fact = 'F'.
B : (m, n) array_like
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 : {'C', 'D'}
Specifies the type of Lyapunov equation to be solved as
follows:
:= 'C': Equation (1), continuous-time case;
:= 'D': Equation (2), discrete-time case.
fact : {'N', 'F'}, optional
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', 'T'}, optional
Specifies the form of op(K) to be used, as follows:
:= 'N': op(K) = K (No transpose, default);
:= 'T': op(K) = K**T (Transpose).
ldwork : int, optional
The length of the array DWORK.
If m > 0, ldwork >= max(1, 4*n);
If m = 0, ldwork >= 1.
For optimum performance ldwork should sometimes be larger.
Returns
-------
U : (n, n) ndarray
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 : (n, ) complex ndarray
The eigenvalues of A.
Raises
------
SlycotArithmeticError
:info = 4:
FACT = 'F' and the Schur factor S supplied in
the array A has two or more consecutive non-zero
elements on the first subdiagonal, so that there is
a block larger than 2-by-2 on the diagonal
:info = 5:
FACT = 'F' and the Schur factor S supplied in
the array A has a 2-by-2 diagonal block with real
eigenvalues instead of a complex conjugate pair;
:info = 6:
FACT = 'N' and the LAPACK Library routine DGEES
has failed to converge. This failure is not likely
to occur.
Warns
-----
SlycotResultWarning
:info = 1 and dico == 'C':
The Lyapunov equation is (nearly) singular.
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;
perturbed values were used to solve the equation;
:info = 1 and dico == 'D':
The Lyapunov equation is (nearly) singular.
This means that while the matrix A
(or the factor S) has computed eigenvalues inside
the unit circle, it is nevertheless only just
convergent, in the sense that small perturbations
in A can make one or more of the eigenvalues lie
outside the unit circle;
perturbed values were used to solve the equation;
:info = 2 and fact == 'N' and dico == 'C':
The matrix A is
not stable (that is, one or more of the eigenvalues
of A has a non-negative real part), or DICO = 'D',
but the matrix A is not convergent (that is, one or
more of the eigenvalues of A lies outside the unit
circle); however, A still has been factored
and the eigenvalues of A are returned in WR and WI.
:info = 2 and dico == 'D':
The matrix A is not convergent (that is, one or
more of the eigenvalues of A lies outside the unit
circle); however, A still has been factored
and the eigenvalues of A are returned in WR and WI.
:info = 3 and fact == 'F' and dico == 'C':
The Schur factor S supplied in the array A is not
stable (that is, one or more of the eigenvalues of
S has a non-negative real part);
the eigenvalues of A are still returned in w.
:info = 3 and dico == 'D':
The Schur factor S supplied in the array A is not
convergent (that is, one or more of the eigenvalues
of S lies outside the unit circle);
the eigenvalues of A are still returned in w.
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['dico','fact', 'trans', 'n', 'm', 'a', 'lda'+hidden, 'q',
'ldq'+hidden, 'b', 'ldb'+hidden, 'scale', 'wr'+hidden,
'wi'+hidden, 'dwork'+hidden, 'ldwork', 'info'+hidden]
if ldwork is None:
if m > 0:
ldwork = max(1,4*n)
elif m == 0:
ldwork = 1
if dico != 'C' and dico != 'D':
raise SlycotParameterError('dico must be either D or C', -1)
out = _wrapper.sb03od(dico,n,m,A,Q,B,fact=fact,trans=trans,ldwork=ldwork)
raise_if_slycot_error(out[-1], arg_list, sb03od.__doc__, locals())
U,scale,wr,wi = out[:-1]
w = _np.zeros(n,'complex64')
w.real = wr[0:n]
w.imag = wi[0:n]
return U,scale,w
def sb04md(n,m,A,B,C,ldwork=None):
"""X = sb04md(n,m,A,B,C[,ldwork])
To solve for X the continuous-time Sylvester equation