-
Notifications
You must be signed in to change notification settings - Fork 43
/
transform.py
1239 lines (1111 loc) · 54.1 KB
/
transform.py
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
#
# transform.py
#
# Copyright 2010-2011 Enrico Avventi <[email protected]>
#
# 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
from .exceptions import raise_if_slycot_error, SlycotParameterError
import numpy as _np
def tb01id(n,m,p,maxred,a,b,c,job='A'):
""" s_norm,A,B,C,scale = tb01id(n,m,p,maxred,A,B,C,[job])
To reduce the 1-norm of a system matrix
S = ( A B )
( C 0 )
corresponding to the triple (A,B,C), by balancing. This involves
a diagonal similarity transformation inv(D)*A*D applied
iteratively to A to make the rows and columns of
-1
diag(D,I) * S * diag(D,I)
as close in norm as possible.
The balancing can be performed optionally on the following
particular system matrices
S = A, S = ( A B ) or S = ( A )
( C )
Required arguments:
n : input int
The order of the matrix A, the number of rows of matrix B and
the number of columns of matrix C. It represents the dimension of
the state vector. n > 0.
m : input int
The number of columns of matrix B. It represents the dimension of
the input vector. m > 0.
p : input int
The number of rows of matrix C. It represents the dimension of
the output vector. p > 0.
maxred : input float
The maximum allowed reduction in the 1-norm of S (in an iteration)
if zero rows or columns are encountered.
If maxred > 0.0, maxred must be larger than one (to enable the norm
reduction).
If maxred <= 0.0, then the value 10.0 for maxred is used.
A : input rank-2 array('d') with bounds (n,n)
The leading n-by-n part of this array must contain the system state
matrix A.
B : input rank-2 array('d') with bounds (n,m)
The leading n-by-m part of this array must contain the system input
matrix B.
C : input rank-2 array('d') with bounds (p,n)
The leading p-by-n part of this array must contain the system output
matrix C.
Optional arguments:
job := 'A' input string(len=1)
Indicates which matrices are involved in balancing, as follows:
= 'A': All matrices are involved in balancing;
= 'B': B and A matrices are involved in balancing;
= 'C': C and A matrices are involved in balancing;
= 'N': B and C matrices are not involved in balancing.
Return objects:
s_norm : float
The 1-norm of the given matrix S is non-zero, the ratio between
the 1-norm of the given matrix and the 1-norm of the balanced matrix.
A : rank-2 array('d') with bounds (n,n)
The leading n-by-n part of this array contains the balanced matrix
inv(D)*A*D.
B : rank-2 array('d') with bounds (n,m)
The leading n-by-m part of this array contains the balanced matrix
inv(D)*B.
C : rank-2 array('d') with bounds (p,n)
The leading p-by-n part of this array contains the balanced matrix C*D.
scale : rank-1 array('d') with bounds (n)
The scaling factors applied to S. If D(j) is the scaling factor
applied to row and column j, then scale(j) = D(j), for j = 1,...,n.
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['job', 'N', 'M', 'P', 'maxred', 'A', 'LDA'+hidden, 'B',
'LDB'+hidden, 'C', 'LDC'+hidden, 'scale', 'INFO'+hidden]
out = _wrapper.tb01id(n,m,p,maxred,a,b,c,job=job)
raise_if_slycot_error(out[-1], arg_list)
return out[:-1]
def tb03ad(n,m,p,A,B,C,D,leri,equil='N',tol=0.0,ldwork=None):
""" A_min,b_min,C_min,nr,index,pcoeff,qcoeff,vcoeff = tb03ad_l(n,m,p,A,B,C,D,leri,[equil,tol,ldwork])
To find a relatively prime left or right polynomial matrix representation
with the same transfer matrix as that of a given state-space representation,
i.e. if leri = 'L'
inv(P(s))*Q(s) = C*inv(s*I-A)*B + D
or, if leri = 'R'
Q(s)*inv(P(s)) = C*inv(s*I-A)*B + D.
Additionally a minimal realization (A_min,B_min,C_min) of the original
system (A,B,C) is returned.
Required arguments:
n : input int
The order of the state-space representation, i.e. the order of
the original state dynamics matrix A. n > 0.
m : input int
The number of system inputs. m > 0.
p : input int
The number of system outputs. p > 0.
A : input rank-2 array('d') with bounds (n,n)
The leading n-by-n part of this array must contain the original
state dynamics matrix A.
B : input rank-2 array('d') with bounds (n,max(m,p))
The leading n-by-m part of this array must contain the original
input/state matrix B; the remainder of the leading n-by-max(m,p)
part is used as internal workspace.
C : input rank-2 array('d') with bounds (max(m,p),n)
The leading p-by-n part of this array must contain the original
state/output matrix C; the remainder of the leading max(m,p)-by-n
part is used as internal workspace.
D : input rank-2 array('d') with bounds (max(m,p),max(m,p))
The leading p-by-m part of this array must contain the original
direct transmission matrix D; the remainder of the leading
max(m,p)-by-max(m,p) part is used as internal workspace.
leri : input string(len=1)
Indicates whether the left polynomial matrix representation or
the right polynomial matrix representation is required.
Optional arguments:
equil := 'N' input string(len=1)
Specifies whether the user wishes to balance the triplet (A,B,C),
before computing a minimal state-space representation, as follows:
= 'S': Perform balancing (scaling);
= 'N': Do not perform balancing.
tol := 0.0 input float
The tolerance to be used in rank determination when transforming
(A, B). If tol <= 0 a default value is used.
ldwork := max(2*n+3*max(m,p), p*(p+2)) input int
The length of the cache array.
ldwork >= max( n + max(n, 3*m, 3*p), pm*(pm + 2))
where pm = p, if leri = 'L';
pm = m, if leri = 'R'.
For optimum performance it should be larger.
Return objects:
A_min : rank-2 array('d') with bounds (n,n)
The leading nr-by-nr part of this array contains the upper block
Hessenberg state dynamics matrix A_min of a minimal realization for
the original system.
B_min : rank-2 array('d') with bounds (n,max(m,p))
The leading nr-by-m part of this array contains the transformed
input/state matrix B_min.
C_min : rank-2 array('d') with bounds (max(m,p),n)
The leading p-by-nr part of this array contains the transformed
state/output matrix C_min.
nr : int
The order of the minimal state-space representation
(A_min,B_min,C_min).
index : rank-1 array('i') with bounds either (p) or (m)
If leri = 'L', index(i), i = 1,2,...,p, contains the maximum degree
of the polynomials in the i-th row of the denominator matrix P(s)
of the left polynomial matrix representation. These elements are
ordered so that index(1) >= index(2) >= ... >= index(p).
If leri = 'R', index(i), i = 1,2,...,m, contains the maximum degree
of the polynomials in the i-th column of the denominator matrix P(s)
of the right polynomial matrix representation. These elements are
ordered so that index(1) >= index(2) >= ... >= index(m).
pcoeff : rank-3 array('d') with bounds either (p,p,n+1) or (m,m,n+1)
If leri = 'L' then porm = p, otherwise porm = m.
The leading porm-by-porm-by-kpcoef part of this array contains
the coefficients of the denominator matrix P(s), where
kpcoef = max(index) + 1.
pcoeff(i,j,k) is the coefficient in s**(index(iorj)-k+1) of
polynomial (i,j) of P(s), where k = 1,2,...,kpcoef; if leri = 'L'
then iorj = I, otherwise iorj = J. Thus for leri = 'L',
P(s) = diag(s**index)*(pcoeff(.,.,1)+pcoeff(.,.,2)/s+...).
qcoeff : rank-3 array('d') with bounds (p,m,n + 1) or (max(m,p),max(m,p))
If leri = 'L' then porp = m, otherwise porp = p.
If leri = 'L', the leading porm-by-porp-by-kpcoef part of this array
contains the coefficients of the numerator matrix Q(s).
If leri = 'R', the leading porp-by-porm-by-kpcoef part of this array
contains the coefficients of the numerator matrix Q(s).
qcoeff(i,j,k) is defined as for pcoeff(i,j,k).
vcoeff : rank-3 array('d') with bounds (p,n,n+1) or (m,n,n+1)
The leading porm-by-nr-by-kpcoef part of this array contains
the coefficients of the intermediate matrix V(s).
vcoeff(i,j,k) is defined as for pcoeff(i,j,k).
Raises
------
SlycotArithmeticError
:info == 1:
A singular matrix was encountered during the
computation of V(s);
:info == 2:
A singular matrix was encountered during the
computation of P(s).
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['leri', 'equil', 'n', 'm', 'P', 'A', 'LDA'+hidden, 'B',
'LDB'+hidden, 'C', 'LDC'+hidden, 'D', 'LDD'+hidden, 'nr', 'index',
'pcoeff', 'LDPCO1'+hidden, 'LDPCO2'+hidden, 'qcoeff', 'LDQCO1'+hidden,
'LDQCO2'+hidden, 'vcoeff', 'LDVCO1'+hidden, 'LDVCO2'+hidden, 'tol',
'IWORK'+hidden, 'DWORK'+hidden, 'ldwork', 'INFO'+hidden]
wfun = {"L": _wrapper.tb03ad_l,
"R": _wrapper.tb03ad_r}
mp_ = {"L": p, "R": m}
mp = mp_[leri]
if leri not in wfun.keys():
raise SlycotParameterError('leri must be either L or R', -1)
if ldwork is None:
ldwork = max(2*n + 3*max(m, p), mp*(mp+2))
out = wfun[leri](n, m, p, A, B, C, D, equil=equil, tol=tol, ldwork=ldwork)
raise_if_slycot_error(out[-1], arg_list)
return out[:-1]
def tb04ad(n,m,p,A,B,C,D,tol1=0.0,tol2=0.0,ldwork=None):
""" Ar,Br,Cr,nr,denom_degs,denom_coeffs,num_coeffs = tb04ad(n,m,p,A,B,C,D,[tol1,tol2,ldwork])
Convert a state-space system to a tranfer function or matrix of transfer functions.
The transfer function is given as rows over common denominators.
Required arguments
------------------
n : integer
state dimension
m : integer
input dimension
p : integer
output dimension
A : rank-2 array, shape(n,n)
state dynamics matrix.
B : rank-2 array, shape (n,m)
input matrix
C : rank-2 array, shape (p,n)
output matri
D : rank-2 array, shape (p,m)
direct transmission matrix
Optional arguments
------------------
tol1 = 0.0: double
tolerance in determining the transfer function coefficients,
when set to 0, a default value is used
tol2 = 0.0: double
tolerance in separating out a controllable/observable subsystem
of (A,B,C), when set to 0, a default value is used
ldwork : int
The length of the cache array. The default values is
max(1,n*(n+1)+max(n*m+2*n+max(n,p),max(3*m,p)))
Returns
-------
nr : int
state dimension of the controllable subsystem
Ar : rank-2 array, shape(nr,nr)
state dynamics matrix of the controllable subsystem
Br : rank-2 array, shape (nr,m)
input matrix of the controllable subsystem
Cr : rank-2 array, shape (p,nr)
output matri of the controllable subsystem
index : rank-1 array, shape (p)
array of orders of the denominator polynomials
dcoeff : rank-2 array, shape (p,max(index)+1)
array of denominator coefficients
ucoeff : rank-3 array, shape (p,m,max(index)+1)
array of numerator coefficients
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['rowcol','n','m','p','A','lda'+hidden,'B','ldb'+hidden,'C','ldc'+hidden,'D', 'ldd'+hidden,
'nr','index','dcoeff','lddcoe'+hidden, 'ucoeff','lduco1'+hidden,'lduco2'+hidden,'tol1','tol2','iwork'+hidden,'dwork'+hidden,'ldwork','info'+hidden]
mp, pm = m, p
porm, porp = p, m
if ldwork is None:
ldwork = max(1,n*(n+1)+max(n*mp+2*n+max(n,mp),3*mp,pm))
if B.shape != (n, m):
raise SlycotParameterError("The shape of B is ({}, {}), "
"but expected ({}, {})"
"".format(*(B.shape + (n, m))),
-7)
if C.shape != (p, n):
raise SlycotParameterError("The shape of C is ({}, {}), "
"but expected ({}, {})"
"".format(*(C.shape + (p, n))),
-9)
if D.shape != (max(1, p), m):
raise SlycotParameterError("The shape of D is ({}, {}), "
"but expected ({}, {})"
"".format(*(D.shape + (max(1, p), m))),
-11)
out = _wrapper.tb04ad_r(n,m,p,A,B,C,D,tol1,tol2,ldwork)
raise_if_slycot_error(out[-1], arg_list)
A,B,C,Nr,index,dcoeff,ucoeff = out[:-1]
kdcoef = max(index)+1
return A[:Nr,:Nr],B[:Nr,:m],C[:p,:Nr],Nr,index,dcoeff[:porm,:kdcoef],ucoeff[:porm,:porp,:kdcoef]
def tb05ad(n, m, p, jomega, A, B, C, job='NG'):
"""tb05ad(n, m, p, jomega, A, B, C, job='NG')
To find the complex frequency response matrix (transfer matrix)
G(freq) of the state-space representation (A,B,C) given by
::
-1
G(freq) = C * ((freq*I - A) ) * B
where A, B and C are real N-by-N, N-by-M and P-by-N matrices
respectively and freq is a complex scalar.
Parameters
----------
n : int
The number of states, i.e. the order of the state
transition matrix A.
m : int
The number of inputs, i.e. the number of columns in the
matrix B.
p : int
The number of outputs, i.e. the number of rows in the
matrix C.
jomega : complex float
The frequency at which the frequency response matrix
(transfer matrix) is to be evaluated. For continuous time
systems, this is j*omega, where omega is the frequency to
be evaluated. For discrete time systems,
freq = exp(j*omega*Ts)
A : (n,n) ndarray
On entry, this array must contain the state transition
matrix A.
B : (n,m) ndarray
On entry, this array must contain the input/state matrix B.
C : (p,n) ndarray
On entry, of this array must contain the state/output matrix C.
job : {'AG', 'NG', 'NH'}
If job = 'AG' (i.e., 'all', 'general matrix'), the A matrix is
first balanced. The balancing transformation
is then appropriately applied to matrices B and C. The A matrix
is (again) transformed to an upper Hessenberg representation and
the B and C matrices are also transformed. In addition,
the condition number of the problem is calculated as well as the
eigenvalues of A.
If job='NG' (i.e., 'none', 'general matrix'), no balancing is done.
Neither the condition number nor the eigenvalues are calculated.
The routine still transforms A into upper Hessenberg form. The
matrices B and C are also appropriately transformed.
If job = 'NH' (i.e., 'none', 'hessenberg matrix'), the function
assumes the matrices have already been transformed into Hessenberg
form, i.e., by a previous function call tb05ad. If this not the
case, the routine will return a wrong result without warning.
Returns
-------
if job = 'AG':
--------------
At: The A matrix which has been both balanced and
transformed to upper Hessenberg form. The balancing
transforms A according to
A1 = P^-1 * A * P.
The transformation to upper Hessenberg form then yields
At = Q^T * (P^-1 * A * P ) * Q.
Note that the lower triangle of At is in general not zero.
Rather, it contains information on the orthogonal matrix Q
used to transform A1 to Hessenberg form. See docs for lappack
DGEHRD():
http://www.netlib.org/lapack/explore-3.1.1-html/dgehrd.f.html
However, it does not apparently contain information on P, the
matrix used in the balancing procedure.
Bt: The matrix B transformed according to
Bt = Q^T * P^-1 * B.
Ct: The matrix C transformed according to
Ct = C * P * Q
rcond: RCOND contains an estimate of the reciprocal of the
condition number of matrix H with respect to inversion, where
H = (j*freq * I - A)
g_jw: complex p-by-m array, which contains the frequency response
matrix G(freq).
ev: Eigenvalues of the matrix A.
hinvb : complex n-by-m array, which contains the product
-1
H B.
if job = 'NG':
--------------
At: The matrix A transformed to upper Hessenberg form according
to
At = Q^T * A * Q.
The lower triangle is not zero. It containts info on the
orthoganal transformation. See docs for linpack DGEHRD()
http://www.netlib.org/lapack/explore-3.1.1-html/dgehrd.f.html
Bt: The matrix B transformed according to
Bt = Q^T * B.
Ct: The matrix C transformed according to
Ct = C * Q
g_jw: complex array with dim p-by-m which contains the frequency
response matrix G(freq).
hinvb : complex array with dimension p-by-m.
This array contains the
-1
product H B.
if job = 'NH'
--------------
g_jw: complex p-by-m array which contains the frequency
response matrix G(freq).
hinvb : complex p-by-m array which contains the
-1
product H B.
Raises
------
SlycotArithmeticError
:info = 1:
More than {n30} (30*`n`) iterations were required to isolate the
eigenvalues of A. The computations are continued.
:info = 2:
Either `freq`={jomega} is too near to an eigenvalue of A,
or `rcond` is less than the machine precision EPS.
Example
-------
>>> A = np.array([[0.0, 1.0],
[-100.0, -20.1]])
>>> B = np.array([[0.],[100]])
>>> C = np.array([[1., 0.]])
>>> n = np.shape(A)[0]
>>> m = np.shape(B)[1]
>>> p = np.shape(C)[0]
>>> jw_s = [1j*10, 1j*15]
>>> at, bt, ct, g_1, hinvb, info = slycot.tb05ad(n, m, p, jw_s[0],
A, B, C, job='NG')
>>> g_2, hinv2,info = slycot.tb05ad(n, m, p, jw_s[1], at, bt, ct, job='NH')
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['baleig'+hidden, 'inita'+hidden, 'n', 'm', 'p', 'freq', 'a',
'lda'+hidden, 'b', 'ldb'+hidden, 'c', 'ldc'+hidden, 'rcond',
'g', 'ldg'+hidden, 'evre', 'evim', 'hinvb', 'ldhinv'+hidden,
'iwork'+hidden, 'dwork'+hidden, 'ldwork'+hidden,
'zwork'+hidden, 'lzwork'+hidden, 'info'+hidden]
# Fortran function prototype:
# TB05AD(baleig,inita,n,m,p,freq,a,lda,b,ldb,c,ldc,rcond,g,ldg,evre,evim,hinvb,ldhinv,
# iwork,dwork,ldwork,zwork,lzwork,info)
# Sanity check on matrix dimensions
if A.shape != (n, n):
raise SlycotParameterError("The shape of A is ({0:}, {1:}), "
"but expected ({2:}, {2:})"
"".format(*(A.shape + (n,))),
-7)
if B.shape != (n, m):
raise SlycotParameterError("The shape of B is ({0:}, {1:}), "
"but expected ({2:}, {3:})"
"".format(*(B.shape + (n, m))),
-9)
if C.shape != (p, n):
raise SlycotParameterError("The shape of C is ({0:}, {1:}), "
"but expected ({2:}, {3:})"
"".format(*(C.shape + (p, n))),
-11)
# ----------------------------------------------------
# Checks done, do computation.
n30 = 30*n # for INFO = 1 error docstring
if job == 'AG':
out = _wrapper.tb05ad_ag(n, m, p, jomega, A, B, C)
At, Bt, Ct, rcond, g_jw, evre, evim, hinvb, info = out
raise_if_slycot_error(info, arg_list, tb05ad.__doc__, locals())
ev = _np.zeros(n, 'complex64')
ev.real = evre
ev.imag = evim
return At, Bt, Ct, g_jw, rcond, ev, hinvb, info
elif job == 'NG':
# use tb05ad_ng, for 'NONE' , and 'General', because balancing
# (option 'A' for 'ALL') seems to have a bug.
out = _wrapper.tb05ad_ng(n, m, p, jomega, A, B, C)
At, Bt, Ct, g_jw, hinvb, info = out
raise_if_slycot_error(info, arg_list, tb05ad.__doc__, locals())
return At, Bt, Ct, g_jw, hinvb, info
elif job == 'NH':
out = _wrapper.tb05ad_nh(n, m, p, jomega, A, B, C)
g_i, hinvb, info = out
raise_if_slycot_error(info, arg_list, tb05ad.__doc__, locals())
return g_i, hinvb, info
else:
raise SlycotParameterError("Unrecognized job. Expected job = 'AG' or "
"job='NG' or job = 'NH' but received job={}"
"".format(job),
-1) # job is baleig and inita together
def td04ad(rowcol,m,p,index,dcoeff,ucoeff,tol=0.0,ldwork=None):
""" nr,A,B,C,D = td04ad(rowcol,m,p,index,dcoeff,ucoeff,[tol,ldwork])
Convert a transfer function or matrix of transfer functions to
a minimum state space realization.
Parameters
----------
rowcol : {R', 'C'}
indicates whether the transfer matrix T(s) is given
as rows ('R') or colums ('C') over common denominators.
m : int
input dimension
p : int
output dimension
index : (p,) or (m,) array_like
array of orders of the denominator polynomials. Different
shapes corresponding to rowcol=='R' and rowcol=='C'
respectively.
dcoeff : (p,max(index)+1) or (m,max(index)+1) ndarray
array of denominator coefficients. Different shapes
corresponding to rowcol=='R' and rowcol=='C' respectively.
ucoeff : (p,m,max(index)+1) or (max(p,m),max(p,m),max(index)+1) ndarray
array of numerator coefficients. Different shapes
corresponding to rowcol=='R' and rowcol=='C' respectively.
tol : float, optional
tolerance in determining the state space system,
when set to 0, a default value is used.
ldwork : int, optional
The length of the cache array. The default values is
max(1,sum(index)+max(sum(index),max(3*m,3*p)))
Returns
-------
nr : int
minimal state dimension
A : (nr,nr) ndarray
state dynamics matrix.
B : (nr,m) ndarray
input matrix
C : (p,nr) ndarray
output matrix
D : (p,m) ndarray
direct transmission matrix
Raises
------
SlycotArithmeticError
:info > 0:
i={info} is the first index of `dcoeff` for which
``abs( dcoeff(i,1) )`` is so small that the calculations
would overflow (see SLICOT Library routine TD03AY);
that is, the leading coefficient of a polynomial is
nearly zero;
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['rowcol','m','p','index','dcoeff','lddcoe'+hidden, 'ucoeff', 'lduco1'+hidden,'lduco2'+hidden,
'nr','A','lda'+hidden,'B','ldb'+hidden,'C','ldc'+hidden,'D', 'ldd'+hidden,
'tol','iwork'+hidden,'dwork'+hidden,'ldwork','info'+hidden]
if ldwork is None:
n = sum(index)
ldwork = max(1,n+max(n,max(3*m,3*p)))
kdcoef = max(index)+1
if rowcol == 'R':
if ucoeff.ndim != 3:
raise SlycotParameterError("The numerator is not a 3D array!", -7)
expectedshape = (max(1, p), max(1, m), kdcoef)
if ucoeff.shape != expectedshape:
raise SlycotParameterError("The numerator shape is ({}, {}, {}), "
"but expected ({}, {}, {})".format(
*(ucoeff.shape + expectedshape)),
-7)
expectedshape = (max(1, p), kdcoef)
if dcoeff.shape != expectedshape:
raise SlycotParameterError("The denominator shape is ({}, {}), "
"but expected ({}, {})".format(
*(dcoeff.shape + expectedshape)),
-5)
out = _wrapper.td04ad_r(m,p,index,dcoeff,ucoeff,n,tol,ldwork)
elif rowcol == 'C':
if ucoeff.ndim != 3:
raise SlycotParameterError("The numerator is not a 3D array!", -7)
expectedshape = (max(1, m, p), max(1, m, p), kdcoef)
if ucoeff.shape != expectedshape:
raise SlycotParameterError("The numerator shape is ({}, {}, {}), "
"but expected ({}, {}, {})".format(
*(ucoeff.shape + expectedshape)),
-7)
expectedshape = (max(1, m), kdcoef)
if dcoeff.shape != expectedshape:
raise SlycotParameterError("The denominator shape is ({}, {}), "
"but expected ({}, {})".format(
*(dcoeff.shape + expectedshape)),
-5)
out = _wrapper.td04ad_c(m,p,index,dcoeff,ucoeff,n,tol,ldwork)
else:
raise SlycotParameterError("Parameter rowcol had an illegal value", -1)
raise_if_slycot_error(out[-1], arg_list, td04ad.__doc__)
Nr, A, B, C, D = out[:-1]
return Nr, A[:Nr,:Nr], B[:Nr,:m], C[:p,:Nr], D[:p,:m]
def tc04ad(m,p,index,pcoeff,qcoeff,leri,ldwork=None):
""" n,rcond,a,b,c,d = tc04ad_l(m,p,index,pcoeff,qcoeff,leri,[ldwork])
To find a state-space representation (A,B,C,D) with the same
transfer matrix as that of a given left or right polynomial
matrix representation, i.e.
C*inv(sI-A)*B + D = inv(P(s))*Q(s)
or
C*inv(sI-A)*B + D = Q(s)*inv(P(s))
respectively.
Required arguments:
m : input int
The number of system inputs. m > 0.
p := len(index) input int
The number of system outputs. p > 0.
index : input rank-1 array('i') with bounds (p) or (m)
If leri = 'L', index(i), i = 1,2,...,p, must contain the maximum
degree of the polynomials in the I-th row of the denominator matrix
P(s) of the given left polynomial matrix representation.
If leri = 'R', index(i), i = 1,2,...,m, must contain the maximum
degree of the polynomials in the I-th column of the denominator
matrix P(s) of the given right polynomial matrix representation.
pcoeff : input rank-3 array('d') with bounds (p,p,*) or (m,m,*)
If leri = 'L' then porm = p, otherwise porm = m. The leading
porm-by-porm-by-kpcoef part of this array must contain
the coefficients of the denominator matrix P(s). pcoeff(i,j,k) is
the coefficient in s**(index(iorj)-K+1) of polynomial (I,J) of P(s),
where k = 1,2,...,kpcoef and kpcoef = max(index) + 1; if leri = 'L'
then iorj = i, otherwise iorj = j. Thus for leri = 'L',
P(s) = diag(s**index)*(pcoeff(.,.,1)+pcoeff(.,.,2)/s+...).
If leri = 'R', pcoeff is modified by the routine but restored on exit.
qcoeff : input rank-3 array('d') with bounds (p,m,*) or (max(m,p),max(m,p),*)
If leri = 'L' then porp = m, otherwise porp = p. The leading
porm-by-porp-by-kpcoef part of this array must contain
the coefficients of the numerator matrix Q(s).
qcoeff(i,j,k) is defined as for pcoeff(i,j,k).
If leri = 'R', qcoeff is modified by the routine but restored on exit.
leri : input string(len=1)
Indicates whether a left polynomial matrix representation or a right
polynomial matrix representation is input as follows:
= 'L': A left matrix fraction is input;
= 'R': A right matrix fraction is input.
Optional arguments:
ldwork := max(m,p)*(max(m,p)+4) input int
The length of the cache array. ldwork >= max(m,p)*(max(m,p)+4)
For optimum performance it should be larger.
Return objects:
n : int
The order of the resulting state-space representation.
That is, n = sum(index).
rcond : float
The estimated reciprocal of the condition number of the leading row
(if leri = 'L') or the leading column (if leri = 'R') coefficient
matrix of P(s).
If rcond is nearly zero, P(s) is nearly row or column non-proper.
A : rank-2 array('d') with bounds (n,n)
The leading n-by-n part of this array contains the state dynamics matrix A.
B : rank-2 array('d') with bounds (n,max(m,p))
The leading n-by-n part of this array contains the input/state matrix B;
the remainder of the leading n-by-max(m,p) part is used as internal
workspace.
C : rank-2 array('d') with bounds (max(m,p),n)
The leading p-by-n part of this array contains the state/output matrix C;
the remainder of the leading max(m,p)-by-n part is used as internal
workspace.
D : rank-2 array('d') with bounds (max(m,p),max(m,p))
The leading p-by-m part of this array contains the direct transmission
matrix D; the remainder of the leading max(m,p)-by-max(m,p) part is
used as internal workspace.
Raises
------
SlycotArithmeticError
:info == 1 and leri = 'L':
P(s) is not row proper
:info == 1 and leri = 'R':
P(s) is not column proper
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['leri', 'm', 'P', 'index',
'pcoeff', 'LDPCO1' + hidden, 'LDPCO2' + hidden,
'qcoeff', 'LDQCO1' + hidden, 'LDQCO2' + hidden,
'N', 'rcond',
'A', 'LDA' + hidden, 'B', 'LDB' + hidden,
'C', 'LDC' + hidden, 'D', 'LDD' + hidden,
'IWORK' + hidden, 'DWORK' + hidden, 'ldwork',
'INFO' + hidden]
if ldwork is None:
ldwork = max(m, p)*(max(m, p)+4)
n = sum(index)
wfun = {"L": _wrapper.tc04ad_l, "R": _wrapper.tc04ad_r}
if leri not in wfun.keys():
raise SlycotParameterError('leri must be either L or R', -1)
out = wfun[leri](m, p, index, pcoeff, qcoeff, n)
raise_if_slycot_error(out[-1], arg_list, tc04ad.__doc__, locals())
return out[:-1]
def tc01od(m,p,indlin,pcoeff,qcoeff,leri):
""" pcoeff,qcoeff = tc01od_l(m,p,indlim,pcoeff,qcoeff,leri)
To find the dual right (left) polynomial matrix representation of a given
left (right) polynomial matrix representation, where the right and left
polynomial matrix representations are of the form Q(s)*inv(P(s)) and
inv(P(s))*Q(s) respectively.
Required arguments:
m : input int
The number of system inputs. m > 0.
p : input int
The number of system outputs. p > 0.
indlim : input int
The highest value of k for which pcoeff(.,.,k) and qcoeff(.,.,k)
are to be transposed.
k = kpcoef + 1, where kpcoef is the maximum degree of the polynomials
in P(s). indlim > 0.
pcoeff : input rank-3 array('d') with bounds (p,p,indlim) or (m,m,indlim)
If leri = 'L' then porm = p, otherwise porm = m.
On entry, the leading porm-by-porm-by-indlim part of this array
must contain the coefficients of the denominator matrix P(s).
pcoeff(i,j,k) is the coefficient in s**(indlim-k) of polynomial
(i,j) of P(s), where k = 1,2,...,indlim.
qcoeff : input rank-3 array('d') with bounds (max(m,p),max(m,p),indlim)
On entry, the leading p-by-m-by-indlim part of this array must
contain the coefficients of the numerator matrix Q(s).
qcoeff(i,j,k) is the coefficient in s**(indlim-k) of polynomial
(i,j) of Q(s), where k = 1,2,...,indlim.
leri : input string(len=1)
Return objects:
pcoeff : rank-3 array('d') with bounds (p,p,indlim)
On exit, the leading porm-by-porm-by-indlim part of this array
contains the coefficients of the denominator matrix P'(s) of
the dual system.
qcoeff : rank-3 array('d') with bounds (max(m,p),max(m,p),indlim)
On exit, the leading m-by-p-by-indlim part of the array contains
the coefficients of the numerator matrix Q'(s) of the dual system.
info : int
= 0: successful exit;
< 0: if info = -i, the i-th argument had an illegal value.
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['leri', 'M', 'P', 'indlim', 'pcoeff', 'LDPCO1'+hidden,
'LDPCO2'+hidden, 'qcoeff', 'LDQCO1'+hidden, 'LDQCO2'+hidden,
'INFO'+hidden]
if leri == 'L':
out = _wrapper.tc01od_l(m,p,indlin,pcoeff,qcoeff)
raise_if_slycot_error(out[-1], arg_list)
return out[:-1]
if leri == 'R':
out = _wrapper.tc01od_r(m,p,indlin,pcoeff,qcoeff)
raise_if_slycot_error(out[-1], arg_list)
return out[:-1]
raise SlycotParameterError('leri must be either L or R', -1)
def tf01md(n,m,p,N,A,B,C,D,u,x0):
""" xf,y = tf01md(n,m,p,N,A,B,C,D,u,x0)
To compute the output sequence of a linear time-invariant
open-loop system given by its discrete-time state-space model
Required arguments:
n : input int
Order of the State-space representation.
m : input int
Number of inputs.
p : input int
Number of outputs.
N : input int
Number of output samples to be computed.
A : input rank-2 array('d') with bounds (n,n)
State dynamics matrix.
B : input rank-2 array('d') with bounds (n,m)
Input/state matrix.
C : input rank-2 array('d') with bounds (p,n)
State/output matrix.
D : input rank-2 array('d') with bounds (p,m)
Direct transmission matrix.
u : input rank-2 array('d') with bounds (m,N)
Input signal.
x0 : input rank-1 array('d') with bounds (n)
Initial state, at time 0.
Return objects:
xf : rank-1 array('d') with bounds (n)
Final state, at time N+1.
y : rank-2 array('d') with bounds (p,N)
Output signal.
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['n','m','p','ny','A','lda'+hidden,'B','ldb'+hidden,
'C','ldc'+hidden,'D','ldd'+hidden,'u','ldu'+hidden,'x0',
'y'+hidden,'ldy'+hidden,'dwork'+hidden,'info'+hidden]
out = _wrapper.tf01md(n,m,p,N,A,B,C,D,u,x0)
raise_if_slycot_error(out[-1], arg_list)
return out[:-1]
def tf01rd(n,m,p,N,A,B,C,ldwork=None):
""" H = tf01rd(n,m,p,N,A,B,C,[ldwork])
To compute N Markov parameters M_1, M_2,..., M_N from the
parameters (A,B,C) of a linear time-invariant system, where each
M_k is an p-by-m matrix and k = 1,2,...,N.
All matrices are treated as dense, and hence TF01RD is not
intended for large sparse problems.
Required arguments:
n : input int
Order of the State-space representation.
m : input int
Number of inputs.
p : input int
Number of outputs.
N : input int
Number of Markov parameters to be computed.
A : input rank-2 array('d') with bounds (n,n)
State dynamics matrix.
B : input rank-2 array('d') with bounds (n,m)
Input/state matrix.
C : input rank-2 array('d') with bounds (p,n)
State/output matrix.
Optional arguments:
ldwork := 2*na*nc input int
Return objects:
H : rank-2 array('d') with bounds (p,N*m)
H[:,(k-1)*m : k*m] contains the k-th Markov parameter,
for k = 1,2...N.
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['n','m','p','N','A','lda'+hidden,'B','ldb'+hidden,'C',
'ldc'+hidden,'H','ldh'+hidden,'dwork'+hidden,'ldwork','info'+hidden]
if ldwork is None:
out = _wrapper.tf01rd(n,m,p,N,A,B,C)
else:
out = _wrapper.tf01rd(n,m,p,N,A,B,C,ldwork=ldwork)
raise_if_slycot_error(out[-1], arg_list)
return out[0]
def tb01pd(n, m, p, A, B, C, job='M', equil='S', tol=1e-8, ldwork=None):
"""Ar, Br, Cr, nr = tb01pd(n,m,p,A,B,C,[job,equil,tol,ldwork])
To find a reduced (controllable, observable, or minimal) state-
space representation (Ar,Br,Cr) for any original state-space
representation (A,B,C). The matrix Ar is in upper block
Hessenberg form.
Required arguments:
n : input int
Order of the State-space representation.
m : input int
Number of inputs.
p : input int
Number of outputs.
A : input rank-2 array('d') with bounds (n,n)
State dynamics matrix.
B : input rank-2 array('d') with bounds (n,max(m,p))
The leading n-by-m part of this array must contain the original
input/state matrix B; the remainder of the leading n-by-max(m,p)
part is used as internal workspace.
C : input rank-2 array('d') with bounds (p,n)
The leading p-by-n part of this array must contain the original
state/output matrix C; the remainder of the leading max(1,m,p)-by-n
part is used as internal workspace.
Optional arguments:
job : input char*1
Indicates whether the user wishes to remove the
uncontrollable and/or unobservable parts as follows:
= 'M': Remove both the uncontrollable and unobservable
parts to get a minimal state-space representation;
= 'C': Remove the uncontrollable part only to get a
controllable state-space representation;
= 'O': Remove the unobservable part only to get an
observable state-space representation.
equil : input char*1
Specifies whether the user wishes to preliminarily balance
the triplet (A,B,C) as follows:
= 'S': Perform balancing (scaling);
= 'N': Do not perform balancing.
Return objects:
Ar : output rank-2 array('d') with bounds (nr,nr)
Contains the upper block Hessenberg state dynamics matrix
Ar of a minimal, controllable, or observable realization
for the original system, depending on the value of JOB,
JOB = 'M', JOB = 'C', or JOB = 'O', respectively.
Br : output rank-2 array('d') with bounds (nr,m)
Contains the transformed input/state matrix Br of a
minimal, controllable, or observable realization for the
original system, depending on the value of JOB, JOB = 'M',
JOB = 'C', or JOB = 'O', respectively. If JOB = 'C', only
the first IWORK(1) rows of B are nonzero.
Cr : output rank-2 array('d') with bounds (p,nr)
Contains the transformed state/output matrix Cr of a
minimal, C controllable, or observable realization for the
original C system, depending on the value of JOB, JOB =
'M', C JOB = 'C', or JOB = 'O', respectively. C If JOB =
'M', or JOB = 'O', only the last IWORK(1) columns C (in
the first NR columns) of C are nonzero.
nr : output int
The order of the reduced state-space representation
(Ar,Br,Cr) of a minimal, controllable, or observable
realization for the original system, depending on
JOB = 'M', JOB = 'C', or JOB = 'O'.
"""
hidden = ' (hidden by the wrapper)'
arg_list = ['job', 'equil', 'n','m','p','A','lda'+hidden,'B','ldb'+hidden,
'C','ldc'+hidden,'nr','tol','iwork'+hidden,'dwork'+hidden,
'ldwork','info'+hidden]
if ldwork is None:
ldwork = max(1, n+max(n,3*m,3*p))
elif ldwork < max(1, n+max(n,3*m,3*p)):
raise SlycotParameterError("ldwork is too small", -15)
out = _wrapper.tb01pd(n=n,m=m,p=p,a=A,b=B,c=C,
job=job,equil=equil,tol=tol,ldwork=ldwork)
raise_if_slycot_error(out[-1], arg_list)
return out[:-1]
def tg01ad(l,n,m,p,A,E,B,C,thresh=0.0,job='A'):
""" A,E,B,C,lscale,rscale = tg01ad(l,n,m,p,A,E,B,C,[thresh,job])
To balance the matrices of the system pencil
S = ( A B ) - lambda ( E 0 ) := Q - lambda Z,
( C 0 ) ( 0 0 )
corresponding to the descriptor triple (A-lambda E,B,C),
by balancing. This involves diagonal similarity transformations
(Dl*A*Dr - lambda Dl*E*Dr, Dl*B, C*Dr) applied to the system
(A-lambda E,B,C) to make the rows and columns of system pencil
matrices
diag(Dl,I) * S * diag(Dr,I)
as close in norm as possible. Balancing may reduce the 1-norms
of the matrices of the system pencil S.
The balancing can be performed optionally on the following
particular system pencils
S = A-lambda E,
S = ( A-lambda E B ), or
S = ( A-lambda E ).
( C )
Required arguments:
l : input int
The number of rows of matrices A, B, and E. l >= 0.
n : input int
The number of columns of matrices A, E, and C. n >= 0.
m : input int
The number of columns of matrix B. m >= 0.
p : input int
The number of rows of matrix C. P >= 0.
A : rank-2 array('d') with bounds (l,n)
The leading L-by-N part of this array must
contain the state dynamics matrix A.
E : rank-2 array('d') with bounds (l,n)
The leading L-by-N part of this array must
contain the descriptor matrix E.
B : rank-2 array('d') with bounds (l,m)
The leading L-by-M part of this array must