-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial_matrix.py
1115 lines (960 loc) · 33.7 KB
/
polynomial_matrix.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
from __future__ import print_function
import numpy as np
from numpy.random import normal, randint, choice, seed, uniform
from numpy import exp, pi, log
from numpy.fft import ifft
from numpy import poly1d, polydiv
from numpy.linalg import eig, solve, inv
"""Various construction for polynomial matrix
"""
class PolynomialMatrix(np.ndarray):
"""Input matrix of dimension 2 of type np.poly1d
We could realize a polynomial matrix in two ways
One is a matrix of poly1d which is the main method we use here.
Another is as a 3d array with the third dimension is the polynomial
dimension. We provide a few method to navigate between the two ways
as it may be convenient to use the secondway sometime
"""
def __new__(cls, *args, **kwargs):
kwargs['dtype'] = poly1d
return super(PolynomialMatrix, cls).__new__(
cls, *args, **kwargs)
def __init__(self, shape):
"""Input matrix of dimension 2 of type np.poly1d
"""
if len(shape) != 2:
raise(ValueError("matrix is not of dimension 2"))
self.order = 0
def calc_order(self):
self.order = np.max([a.order for a in self.reshape(-1)])
"""
def scalar_div(self, b):
q = zeros(self.shape)
r = zeros(self.shape)
for i in range(self.shape[0]):
for j in self.shape[1]:
qx, rx = polydiv(self[i, j], b)
q[i, j] = qx
r[i, j] = rx
return q, r
"""
def pprint(self):
for i in range(0, self.shape[0]):
for j in range(0, self.shape[1]):
print_poly(self[i, j])
if j < self.shape[1] - 1:
print(", ", end='')
print("")
@classmethod
def coef_array_to_PolynomialMatrix(cls, array, tolerance=None):
m = PolynomialMatrix(array.shape[:2])
if len(array.shape) == 2:
for i in range(m.shape[0]):
for j in range(m.shape[1]):
if tolerance is None or\
np.abs(array[i, j]) > tolerance:
m[i, j] = poly1d(
array[i, j])
else:
m[i, j] = poly1d(0)
m.calc_order()
return m
if tolerance is None:
for i in range(m.shape[0]):
for j in range(m.shape[1]):
m[i, j] = poly1d(
array[i, j, :])
m.calc_order()
return m
for i in range(m.shape[0]):
for j in range(m.shape[1]):
m[i, j] = poly1d(
cut_off_polynomial(
array[i, j, :], tolerance))
m.calc_order()
return m
def PolynomialMatrix_to_3darray(self):
"""Create a 3d array of coefficients of the matrix
The third dimension is order
"""
ret = np.zeros(
(self.shape[0], self.shape[1], self.order+1),
dtype=float)
for i in range(self.shape[0]):
for j in range(self.shape[1]):
ret[i, j, -self[i, j].coeffs.shape[0]:] =\
self[i, j].coeffs
return ret
def eval(self, x, dtype=float):
"""x is a vector
"""
shape = self.shape
ret = np.zeros(
(shape[0], shape[1], x.shape[0]),
dtype=dtype)
for i in range(shape[0]):
for j in range(shape[1]):
ret[i, j, :] = np.polyval(self[i, j], x)
return ret
def determinant(self, real=True):
"""Evaluate the determinant of a polynomial matrix
the return is a scalar polynomial.
We return the full polynomial of degree
k * p. In case of low McMillan degree the higher degree
terms could be zeros. The caller should use cut off to
reduce terms.
The algorithm is to evaluate the determinant
at kp+1 points (using fft) then use lagrange interpolation
"""
if self.shape[0] != self.shape[1]:
raise(
ValueError("not a square matrix sizes are {}{}".format(
self.shape[:2])))
k = self.shape[0]
p = self.order
d = k * p
x = np.vectorize(lambda i: exp(-2j*pi*i/(d+1)))(
np.arange(d+1))
# evals = np.full((k, k, d+1), np.nan)
evals = self.eval(x, dtype=np.complex)
det_evals = [np.linalg.det(evals[:, :, j])
for j in range(evals.shape[2])]
det_pol = ifft(det_evals)
if real:
det_pol = det_pol.real
return poly1d(np.flip(det_pol, 0))
def cut_off(self, tolerance):
for i in range(self.shape[0]):
for j in range(self.shape[1]):
cc = self[i, j].coeffs
cut = None
top_cut = False
for k in range(cc.shape[0]):
if np.abs(cc[k]) < tolerance:
cut = k
cc[k] = 0
if cut == 0 and cc.shape[0] > 1:
self[i, j] = poly1d(cc[1:])
elif cut is not None:
self[i, j] = poly1d(cc)
def smith_mcmillan_form(self, tolerance=None):
"""return P, A, Q
such that self = P * A * Q
P Q are unimodular and A is diagonal
if numerator is None it returns the smith normal form
"""
n_row, n_col = self.shape[:2]
A = self.copy()
n = min(n_row, n_col)
cnt = 0
det_factor = 1.
P = PolynomialMatrix.coef_array_to_PolynomialMatrix(
np.eye(n_row)[:, :, None])
Q = PolynomialMatrix.coef_array_to_PolynomialMatrix(
np.eye(n_col)[:, :, None])
while cnt < n:
cleared = False
while not cleared:
position = _find_smallest_degree(
A, cnt, n_row, n_col)
cleared = _check_row_col_cleared(A, cnt, n_row, n_col)
if position == (cnt, cnt) and cleared:
# entry = A[cnt, cnt]
# if (entry.order == 1) and (entry.coef[-1] == 0):
# cnt = n
if A[position].coef[0] != 0 and A[position].coef[0] != 1:
if False:
print("Divide row %d with %f" % (
cnt, A[position].coef[0]))
det_factor *= A[position].coef[0]
P[:, cnt] *= A[position].coef[0]
A[position] /= A[position].coef[0]
else:
if cnt != position[0]:
A.swap_rows(cnt, position[0], P)
det_factor *= -1
"""
aux = A[cnt, :].copy()
A[cnt, :] = A[position[0], :].copy()
A[position[0], :] = aux
aux = P[:, cnt].copy()
P[:, cnt] = P[:, position[0]].copy()
P[:, position[0]] = aux
"""
if cnt != position[1]:
A.swap_cols(cnt, position[1], Q)
det_factor *= -1
"""
aux = A[:, cnt].copy()
A[:, cnt] = A[:, position[1]].copy()
A[:, position[1]] = aux
aux = Q[cnt, :].copy()
Q[cnt, :] = Q[position[1], :].copy()
Q[position[1], :] = aux
"""
for i in range(cnt + 1, n_row):
q, r = polydiv(
A[i, cnt], A[cnt, cnt])
if A[cnt, cnt].order == 0:
r = np.poly1d(0)
A.subtract_rows(i, cnt, q, r, P)
if tolerance is not None:
A.cut_off(tolerance)
"""
for j in range(n_col):
if (j == cnt):
A[i, j] = r
else:
A[i, j] -= q * A[cnt, j]
for j in range(n_row):
P[j, cnt] += q * P[j, i]
"""
if not is_zero_polynomial(q):
print(
"Subtract row %d, to row %d (" % (
i+1, cnt+1), end='')
print_poly(q)
print(")")
for i in range(cnt + 1, n_col):
q, r = polydiv(
A[cnt, i],
A[cnt, cnt])
if A[cnt, cnt].order == 0:
r = np.poly1d(0)
A.subtract_cols(i, cnt, q, r, Q)
if tolerance is not None:
A.cut_off(tolerance)
"""
for j in range(n_row):
if j == cnt:
A[j, i] = r
else:
A[j, i] -= q * A[j, cnt]
for j in range(n_col):
Q[cnt, j] += q * Q[i, j]
"""
if not is_zero_polynomial(q):
print(
"subtract column %d, to column %d (" % (
i+1, cnt+1), end='')
print_poly(q)
print(")")
cnt += 1
P.calc_order()
Q.calc_order()
A.calc_order()
return P, A, Q, det_factor
def swap_rows(self, i, j, P=None):
sw = self[i, :].copy()
self[i, :] = self[j, :]
self[j, :] = sw
if P is not None:
sw = P[:, i].copy()
P[:, i] = P[:, j].copy()
P[:, j] = sw
def swap_cols(self, i, j, Q=None):
sw = self[:, i].copy()
self[:, i] = self[:, j]
self[:, j] = sw
if Q is not None:
sw = Q[i, :].copy()
Q[i, :] = Q[j, :].copy()
Q[j, :] = sw
def subtract_rows(self, r1, r2, q, r=None, P=None):
""" subtracting row r1 to q times row r2
the inverse operation is operated on P
sp that P * self is unchanged
Major use case is when q, r is from the division
self[r1, r2] = q* self[r2, r2] +r
in that case we force the result of the operation to be
r to avoid residual term. So r should be None
except for this case
"""
self[r1, :] -= scalar_mult(q, self[r2, :])
if r is not None:
self[r1, r2] = r
if P is not None:
P[:, r2] += scalar_mult(q, P[:, r1])
def subtract_cols(self, r1, r2, q, r=None, Q=None):
""" subtracting row r1 to q times row r2
the inverse operation is operated on P
sp that P * self is unchanged
Major use case is when q, r is from the division
self[r1, r2] = q* self[r2, r2] +r
in that case we force the result of the operation to be
r to avoid residual term. So r should be None
except for this case
"""
self[:, r1] -= scalar_mult(q, self[:, r2])
if r is not None:
self[r2, r1] = r
if Q is not None:
Q[r2, :] += scalar_mult(q, Q[r1, :])
def pprint(mat3d):
for l in range(mat3d.shape[2]):
print(mat3d[:, :, l])
print('______')
def mat_mult(A, B):
ret = np.dot(A, B)
ret.calc_order()
return ret
def eval_array(array3d, x):
"""x is a vector
Evaluate array3d as a matrix polynomial
"""
ret = np.zeros(
(array3d.shape[0], array3d.shape[1], x.shape[0]),
dtype=float)
ret = ret + array3d[:, :, 0][:, :, None]
for i in range(1, array3d.shape[2]):
ret = ret * x[None, None, :] + array3d[:, :, i][:, :, None]
return ret
def pprint_array(array3d):
for i in range(array3d.shape[2]):
print(array3d[:, :, i])
print("______")
def cut_off_array(array3d, cut_off_level):
d = array3d.shape[2]
for i in range(d):
ls = np.where(np.abs(array3d[:, :, i]))[0].shape[0]
if ls > 0:
return array3d[:, :, i:].copy()
return array3d.copy()
def cut_off_polynomial(pol, cut_off_level):
big_array = np.where(np.abs(pol.coeffs) > cut_off_level)[0]
if big_array.shape[0] == 0:
return poly1d([0])
return poly1d(pol.coeffs[big_array[0]:])
def determinant_array(array3d, real=True):
"""Evaluate the determinant of a polynomial matrix
the return is a scalar polynomial.
We return the full polynomial of degree
k * p. In case of low McMillan degree the higher degree
terms could be zeros. The caller should use cut off to
reduce terms.
The algorithm is to evaluate the determinant
at kp+1 points (using fft) then use lagrange interpolation
"""
if array3d.shape[0] != array3d.shape[1]:
raise(
ValueError("not a square matrix sizes are {}{}".format(
array3d.shape[:2])))
k = array3d.shape[0]
p = array3d.shape[2]
d = k * p
x = np.vectorize(lambda i: exp(-2j*pi*i/(d+1)))(
np.arange(d))
evals = np.full((k, k, d+1), np.nan)
evals = eval_array(array3d, x)
det_pol = fft(evals)
if real:
det_pol = det_pol.real
return det_pol
def print_poly(poly):
strpoly = ""
# counter = poly.order
coefs = poly.coeffs
first = True
for i in range(0, poly.order + 1):
if coefs[i] != 0:
if not first:
strpoly += " "
if coefs[i] > 0:
strpoly += "+"
if not (abs(coefs[i]) == 1 and i < poly.order):
strpoly += str(coefs[i])
elif coefs[i] == -1:
strpoly += "-"
if i < poly.order:
strpoly += "x"
if i < poly.order - 1:
strpoly += str(poly.order - i)
first = False
if strpoly == "":
strpoly = "0"
print(strpoly, end='')
def _find_smallest_degree(A, cnt, n_row, n_col):
position = (cnt, cnt)
for i in range(cnt, n_row):
for j in range(cnt, n_col):
entry = A[i, j]
if not is_zero_polynomial(entry):
if is_zero_polynomial(A[position]):
position = (i, j)
elif entry.order < A[position].order:
position = (i, j)
return position
def _check_row_col_cleared(A, cnt, n_row, n_col):
return _check_vector_cleared(A[:, cnt], cnt) and\
_check_vector_cleared(A[cnt, :], cnt)
def _check_vector_cleared(poly_vec, cnt):
cleared = True
for i in range(poly_vec.shape[0]):
if (i != cnt) and (not is_zero_polynomial(poly_vec[i])):
return False
return cleared
def is_zero_polynomial(poly):
return (poly.order == 0) and (poly.coeffs[-1] == 0)
def normalize_diagonal(P, A, Q):
"""Assuming A is diagonal. We put A in normalized
Smith form by applying appropriate operations that
keep PAQ unchanged and P, Q invertible"""
n = min(A.shape)
for i in range(n-1):
for j in range(i+1, n):
_normalize_one_pair_diagonal_entries(P, A, Q, i, j)
def scalar_mult(pol, vec):
out = np.empty_like(vec)
for j in range(vec.shape[0]):
out[j] = vec[j] * pol
return out
def scalar_div(M, b):
q = zeros(M.shape)
r = zeros(M.shape)
for i in range(M.shape[0]):
for j in range(M.shape[1]):
qx, rx = polydiv(M[i, j], b)
q[i, j] = qx
r[i, j] = rx
return q, r
def _normalize_one_pair_diagonal_entries(P, A, Q, i, j):
"""Normalize one pair of diagonal entries
We note
if a = ug, b = vg with s and t
relatively prime:
su + tv = 1
then
[ug 0]
[0 vg]
=
[u -t] [g 0] [su tv]
[v s] [0 uvg] [-1 1]
"""
g, s, t, u, v = polynomial_gcd(A[i, i], A[j, j])
new_p_i_col = scalar_mult(u, P[:, i]) + scalar_mult(v, P[:, j])
new_p_j_col = scalar_mult(s, P[:, j]) - scalar_mult(t, P[:, i])
su = s * u
tv = 1 - su
new_q_i_col = scalar_mult(su, Q[i, :]) + scalar_mult(tv, Q[j, :])
new_q_j_col = Q[j, :] - Q[i, :]
P[:, i] = new_p_i_col
P[:, j] = new_p_j_col
Q[i, :] = new_q_i_col
Q[j, :] = new_q_j_col
A[i, i] = g
A[i, j] = poly1d([0])
A[j, i] = poly1d([0])
A[j, j] = u * A[j, j]
def polynomial_gcd(a, b):
"""Function to find gcd of two poly1d polynomials.
Return gcd, s, t, u, v
with a s + bt = gcd (Bezout s theorem)
a = u gcd
b = v gcd
Hence
s u + t v = 1
These are used in diagimalize procedure
"""
s = poly1d(0)
old_s = poly1d(1)
t = poly1d(1)
old_t = poly1d(0)
r = b
old_r = a
while not is_zero_polynomial(r):
quotient, remainder = polydiv(old_r, r)
(old_r, r) = (r, remainder)
(old_s, s) = (s, old_s - quotient * s)
(old_t, t) = (t, old_t - quotient * t)
u, _ = polydiv(a, old_r)
v, _ = polydiv(b, old_r)
return old_r, old_s, old_t, u, v
def diag(poly_array):
n = len(poly_array)
ret = PolynomialMatrix.coef_array_to_PolynomialMatrix(
np.zeros((n, n)))
for i in range(n):
ret[i, i] = poly_array[i]
ret.calc_order()
return ret
def eye(n):
return PolynomialMatrix.coef_array_to_PolynomialMatrix(
np.eye(n))
def zeros(shape):
return PolynomialMatrix.coef_array_to_PolynomialMatrix(
np.zeros(shape))
def ones(shape):
return PolynomialMatrix.coef_array_to_PolynomialMatrix(
np.ones(shape))
def gen_random(row, col, diag_items, n_ops=5):
"""Generate a random matrix of size row col
root mults add up to
"""
# N_OPS = 6
n = min(row, col)
if n != len(diag_items):
raise(ValueError(
"diagonal size is not consistent with row %d and column %d" % (
row, col)))
A = zeros((row, col))
np.fill_diagonal(A, diag_items)
# for each side (right or left)
# pick one of three operations:
# subtract, swap, multiply
choices = ['SUB', 'SWAP', 'MULT']
action_list = choice(choices, n_ops)
# doing rows
for shp in [0, 1]:
for a in action_list:
if a == 'SUB':
terms = choice([1, 2, 3], 1)
p = poly1d(randint(0, 10, size=terms+1))
r1, r2 = tuple(choice(
np.arange(A.shape[shp]), 2, replace=False))
if shp == 0:
A.subtract_rows(r1, r2, p)
else:
A.subtract_cols(r1, r2, p)
elif a == 'SWAP':
r1, r2 = tuple(choice(
np.arange(A.shape[shp]), 2, replace=False))
if shp == 0:
A.swap_rows(r1, r2)
else:
A.swap_cols(r1, r2)
else:
r1 = tuple(choice(
np.arange(A.shape[shp]), 1, replace=False))
f = randint(1, 5) / 5.
if shp == 0:
A[r1, :] = scalar_mult(f, A[r1, :])
else:
A[:, r1] = scalar_mult(f, A[:, r1])
A.calc_order()
return A
def gen_a_random_matrix(k):
V0 = normal(size=k*k).reshape(k, k)
a_det = np.linalg.det(V0)
if a_det < 0:
sw = V0[:, 0].copy()
V0[:, 0] = V0[:, 1]
V0[:, 1] = sw
a_det = - a_det
return V0 / exp(log(a_det) / k)
def gen_unimodular(k, r):
"""Generate a unimodular matrix of size k and
and jordan size r of order 1
s + J. This gives rise to 1 + JL which is unimodular
"""
b = np.zeros((k, k, 2), dtype=float)
b[:, :, 0] = np.eye(k)
np.fill_diagonal(b[:r, 1:r+1, 1], 1)
V0 = gen_a_random_matrix(k)
V0a = inv(V0)
# b[:, :, 0] = np.dot(np.dot(V0, b[:, :, 0]) V0a))
b[:, :, 1] = np.dot(np.dot(V0, b[:, :, 1]), V0a)
return PolynomialMatrix.coef_array_to_PolynomialMatrix(b)
def gen_simple_rr(k, mult):
"""of form sI - H
H has eigenvalue of multiplicity mult
"""
dg = np.zeros((k), dtype=float)
c = 0
for m in mult:
dg[c:c+m] = uniform(-1, 1, 1)
c = c + m
V0 = gen_a_random_matrix(k)
V0a = inv(V0)
b = np.zeros((k, k, 2), dtype=float)
b[:, :, 1] = np.dot(np.dot(V0, np.diag(dg)), V0a)
b[:, :, 0] = np.eye(k)
return PolynomialMatrix.coef_array_to_PolynomialMatrix(b)
def _test_one_stable():
# degree invertible with nilpotenpart degree 1 rank 2
# rank 3, degree 1
# rank 3, degree 1
# total k = 12
seed(0)
k = 12
rj0 = 2
rj1 = 3
rj2 = 1
U0 = gen_unimodular(k, rj0)
U1 = gen_unimodular(k, rj1)
rr0 = [2, 1]
rr1 = [3]
R0 = gen_simple_rr(k, rr0)
R1 = gen_simple_rr(k, rr1)
U2 = gen_unimodular(k, rj2)
ret = np.dot(np.dot(np.dot(
np.dot(U0, R0), U1), R1), U2)
ret.calc_order()
return ret
def _test_one_stable_1():
T = _test_one_stable()
# T is stable polynomial of order 5 with highest order 1
# the rational function rT(s) = s^{-5} T(s) is proper
# prT(s) = rT(s) - 1 is strictly proper
# the polynomial matrix Ti(L) = rT(1/L) has Ti(0) = I_k
arr_T = T.PolynomialMatrix_to_3darray()
poly_Phi = PolynomialMatrix.coef_array_to_PolynomialMatrix(
arr_T[:, :, 1:])
P, A, Q, det_factor = poly_Phi.smith_mcmillan_form(1e-8)
def split_E_Psi(A, root, mult):
""" A is diagonal and normalized
under the form diag(a_1, ..., a_k)
with $a_i | a_{i+1}$.
We consider (s-root)^{-mult} A. We reduc
the fractions $a_i / (s-root)^{mult}$
to irreducibel form
$e_i / (s-root)^l$
We collect terms the non zeros $e_i$ to form $E$
Psi is returned as a list
[(l1, mult_1), ...(l_f, mult_f)]
l_1, ... are powers of (s - root)
whitch are mult - zero_order after simplifcation
mult_i denotes the number of terms with
denominator (s - root)^{l_i}
l_1 * mult_1 + ... l_f * mult_f = McMillan degree
mult_1 + ... + mult_f = E.shape[0]
"""
Psi = []
E = []
k = min(A.shape[0], A.shape[1])
b = poly1d([1, -root])
current_zero_order = 0
current_multiple = 0
for i in range(k):
if is_zero_polynomial(A[i, i]):
Psi.append((
mult-current_zero_order, current_multiple))
return E, Psi
else:
ratio, zero_order = find_zero_order(A[i, i], b)
E.append(ratio)
if zero_order == current_zero_order:
current_multiple = current_multiple + 1
else:
if current_multiple > 0:
Psi.append((mult-current_zero_order,
current_multiple))
current_zero_order = zero_order
current_multiple = 1
if current_multiple > 0:
Psi.append((
mult-current_zero_order,
current_multiple))
return E, Psi
def find_zero_order(p, b):
ratio = None
zero_order = 0
r = 0
current_p = p
Found = False
old_ratio = p
while not Found:
ratio, r = polydiv(current_p, b)
current_p = ratio
if r.coeffs[0] != 0:
Found = True
else:
zero_order = zero_order + 1
old_ratio = ratio
return old_ratio, zero_order
def poly_taylor_expansion(p, root):
ret = []
current_p = p
b = poly1d([1, -root])
while current_p.order != 0:
q, r = polydiv(current_p, b)
ret.append(r)
current_p = q
ret.append(current_p)
return ret.reverse()
def Taylor_expansion(M, root, mult):
"""Expanding the polynomial matrix M
to taylor series around root. Cut off after
mult
"""
ret = []
current_M = M
b = poly1d([1, -root])
M.calc_order()
m_order = M.order
order = m_order
while order >= max(0, m_order - mult + 1):
q, r = scalar_div(current_M, b)
ret.append(r.PolynomialMatrix_to_3darray()[:, :, 0])
current_M = q
order -= 1
# ret.append(current_M)
# ret.reverse()
return ret
def calc_McMillan_degree(Psi):
return sum([Psi[i][0] * Psi[i][1]
for i in range(len(Psi))])
def minimal_realizeation(P, A, Q, T, root=0, mult=None):
""" Deriving the Kalman's minimal state
realization for the rational
matrix P*A*Q / (s-root)^{mult}.
P, A, Q come from a Smith normal form decomposition.
P and Q are therefore unimodular
A is of degree p-1.
We also assume s^p - PAQ to be stable.
mult >= p.
If mult is None we set it to p.
Steps:
*** Assuming A is normalized
*** Reduce by factors of (s-roots)
*** Apply the formula by Kalman
Result is H, F, G and that we can verify numerically that
PAQ / (s-root)^{mult} = H(sI-F)^{-1}G
"""
if mult is None:
mult = T.order + 1
k = P.shape[0]
E, Psi = split_E_Psi(A, root, mult)
w = len(E) # width
mm_degree = calc_McMillan_degree(Psi)
f = len(Psi)
PE = mat_mult(P[:, :w], diag(E))
QE = Q[:w, :]
H = np.zeros((k, mm_degree), dtype=float)
G = np.zeros((mm_degree, k), dtype=float)
F = np.zeros((mm_degree, mm_degree), dtype=float)
pol_start = 0 # start for the PE / QE blocks (polynomial)
rel_start = 0 # start for the H and G blocks (realization)
for rho in range(f):
l_rho, m_rho = Psi[rho]
"""
The block corresponding to rho
is [start, start + l_rho * m_rho)
"""
rel_end = rel_start + l_rho * m_rho
tH = Taylor_expansion(
PE[:, pol_start:pol_start+m_rho],
root, l_rho)
for lx in range(min(len(tH), l_rho)):
H[:, rel_start+m_rho*lx:rel_start+m_rho*(lx+1)] = tH[lx]
tG = Taylor_expansion(
QE[pol_start:pol_start+m_rho, :],
root, l_rho)
n_G = len(tG)
for lx in range(min(l_rho, n_G)):
G[rel_end-m_rho*(lx+1):rel_end-m_rho*lx, :] = tG[lx]
eye_m = np.eye(m_rho)
for lx in range(l_rho-1):
F[rel_start+lx*m_rho:rel_start+(lx+1)*m_rho,
rel_start+(lx+1)*m_rho:rel_start+(lx+2)*m_rho] = eye_m
np.fill_diagonal(F[rel_start:rel_end], root)
rel_start = rel_end
pol_start += m_rho
return H, G, F, Psi
def state_to_Phi(H, F, G, Psi):
""" Combining state form to the regular Phi form
return Phi, and root of the corresponding
system. Returning s^pH(sI-F)^{-1}G
"""
k = H.shape[0]
m = G.shape[1]
mm = H.shape[1]
p = Psi[0][0]
Phi_arr = np.zeros((k, m, p))
F_i = np.eye(mm)
for i in range(p):
Phi_arr[:, :, p-i-1] = np.dot(H, np.dot(F_i, G))
F_i = np.dot(F_i, F)
return PolynomialMatrix.coef_array_to_PolynomialMatrix(Phi_arr)
def calc_full_transfer_function(Phi, p):
"""Calc s^p - Phi
"""
s_p = poly1d([1] + p * [0])
T = diag(Phi.shape[0] * [s_p]) - Phi
T.calc_order()
return T
def check_stable(Phi, p):
"""Check that s^p - Phi is stable
If Psi is not None cut off the determinant
at mcmillant
"""
T = calc_full_transfer_function(Phi, p)
dd = T.determinant()
"""
if Psi is not None:
mm_degree = calc_McMillan_degree(Psi)
dd = poly1d[dd.coeffs()[:mm_degree+1]]
"""
roots = np.roots(dd)
is_stable = np.where((np.absolute(roots) < 1))[0].shape[0] == dd.order
return is_stable, roots, dd
def Gilbert_realization(P, A, Q, denominator):
""" Deriving the Gilbert realization for the polynomial matrix m.
P, A, Q come from a smith normal form decomposition.
P and Q are therefore unimodular
A is of degree p-1.
We also assume s^p - PAQ to be stable.
Otherwise it will have the form of
[(root_1, mult_1), ..., (root_l, mult_l)]
the d enominator will have form (s-root_1)^{mult_1) ... (s-root_l)^{mult_l)
we assume sum mult_i >= p (strictly properness)
"""
pass
def gen_unimodular_pol(k, d):
max_b = 5
min_b = -5
V0 = np.random.randint(min_b, max_b, size=(k*k-k)*(d+1)).reshape(
(k*k-k), d+1)
V = [poly1d(V0[i, :]) for i in range(k*k-k)]
L = eye(k)
U = eye(k)
for i in range(k):
for j in range(i):
L[i, j] = V[i*(i-1) // 2 + j]
U[j, i] = V[len(V) // 2 + i*(i-1) // 2 + j]
return mat_mult(L, U), L, U
def inverse_triangular(M, trig_type):
"""Inverting a triangular polynomial matrix.
Assuming the diagonal entries are all scalar and invertible
"""
if M.shape[0] != M.shape[1]:
raise(ValueError('not a square matrix %d %d ' % M.shape))
k = M.shape[0]
inv_diag = np.zeros(k, dtype=float)
if trig_type.lower() == 'l':
ret = zeros(M.shape)
for i in range(M.shape[0]):
if M[i, i].order > 0 or M[i, i].coeffs[0] == 0:
raise(ValueError(
("Matrix is not invertible."
"Diagonal element %d is zero or non scalar" % i)))
inv_diag[i] = 1 / M[i, i].coeffs[0]
ret[i, i] = poly1d(inv_diag[i])
for j in range(i):
prd = sum(M[i, i-j-1:i] *
ret[i-j-1:i, i-j-1])
ret[i, i-j-1] = prd / (-inv_diag[i])
return ret
elif trig_type.lower() == 'u':
return inverse_triangular(M.T, 'l').T
else:
raise(ValueError('trig_type %s must be l or u' % trig_type))
def test_minimal_realization():
# Total rank 8
# 2 wi th s^{-3}: (terms are (s -r1), (s-r1) (s-r2)
# 1 with s^{-2}: s(s-r1) (s-r2) (s-r3)
# 3 with s^{-1} : s^2(s-r1) (s-r2) (s-r3)
# 2 terms with zero- > max degree 5
# Start with A. P generated by L, U, Q is just L,
# U+some deviation. Small enough so not a problem.
# Total have degree 8 (?)
# Then run the reduction over
seed(0)
roots = randint(-15, 15, size=3) / 16.
k = 8
p = [poly1d([1, r]) for r in roots]
p_s = poly1d([1, 0])
p_s_2 = poly1d([1, 0, 0])
if True:
A = diag(
[p[0],
p[0] * p[1],