-
Notifications
You must be signed in to change notification settings - Fork 458
Expand file tree
/
Copy pathcanonical_test.py
More file actions
443 lines (365 loc) · 17 KB
/
Copy pathcanonical_test.py
File metadata and controls
443 lines (365 loc) · 17 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
"""canonical_test.py"""
import numpy as np
import pytest
import scipy.linalg
from control import ss, tf, tf2ss
from control.canonical import canonical_form, reachable_form, \
observable_form, modal_form, similarity_transform, bdschur
from control.exception import ControlNotImplemented
class TestCanonical:
"""Tests for the canonical forms class"""
def test_reachable_form(self):
"""Test the reachable canonical form"""
# Create a system in the reachable canonical form
coeffs = [1.0, 2.0, 3.0, 4.0, 1.0]
A_true = np.polynomial.polynomial.polycompanion(coeffs)
A_true = np.fliplr(np.rot90(A_true))
B_true = np.array([[1.0, 0.0, 0.0, 0.0]]).T
C_true = np.array([[1.0, 1.0, 1.0, 1.0]])
D_true = 42.0
# Perform a coordinate transform with a random invertible matrix
T_true = np.array([[-0.27144004, -0.39933167, 0.75634684, 0.44135471],
[-0.74855725, -0.39136285, -0.18142339, -0.50356997],
[-0.40688007, 0.81416369, 0.38002113, -0.16483334],
[-0.44769516, 0.15654653, -0.50060858, 0.72419146]])
A = np.linalg.solve(T_true, A_true) @ T_true
B = np.linalg.solve(T_true, B_true)
C = C_true @ T_true
D = D_true
# Create a state space system and convert it to the reachable canonical form
sys_check, T_check = canonical_form(ss(A, B, C, D), "reachable")
# Check against the true values
np.testing.assert_array_almost_equal(sys_check.A, A_true)
np.testing.assert_array_almost_equal(sys_check.B, B_true)
np.testing.assert_array_almost_equal(sys_check.C, C_true)
np.testing.assert_array_almost_equal(sys_check.D, D_true)
np.testing.assert_array_almost_equal(T_check, T_true)
# Reachable form only supports SISO
sys = tf([[ [1], [1] ]], [[ [1, 2, 1], [1, 2, 1] ]])
np.testing.assert_raises(ControlNotImplemented, reachable_form, sys)
def test_unreachable_system(self):
"""Test reachable canonical form with an unreachable system"""
# Create an unreachable system
A = np.array([[1., 2., 2.],
[4., 5., 5.],
[7., 8., 8.]])
B = np.array([[1.], [1.],[1.]])
C = np.array([[1., 1.,1.]])
D = np.array([[42.0]])
sys = ss(A, B, C, D)
# Check if an exception is raised
np.testing.assert_raises(ValueError, canonical_form, sys, "reachable")
def test_observable_form(self):
"""Test the observable canonical form"""
# Create a system in the observable canonical form
coeffs = [1.0, 2.0, 3.0, 4.0, 1.0]
A_true = np.polynomial.polynomial.polycompanion(coeffs)
A_true = np.fliplr(np.flipud(A_true))
B_true = np.array([[1.0, 1.0, 1.0, 1.0]]).T
C_true = np.array([[1.0, 0.0, 0.0, 0.0]])
D_true = 42.0
# Perform a coordinate transform with a random invertible matrix
T_true = np.array([[-0.27144004, -0.39933167, 0.75634684, 0.44135471],
[-0.74855725, -0.39136285, -0.18142339, -0.50356997],
[-0.40688007, 0.81416369, 0.38002113, -0.16483334],
[-0.44769516, 0.15654653, -0.50060858, 0.72419146]])
A = np.linalg.solve(T_true, A_true) @ T_true
B = np.linalg.solve(T_true, B_true)
C = C_true @ T_true
D = D_true
# Create a state space system and convert it to the observable canonical form
sys_check, T_check = canonical_form(ss(A, B, C, D), "observable")
# Check against the true values
np.testing.assert_array_almost_equal(sys_check.A, A_true)
np.testing.assert_array_almost_equal(sys_check.B, B_true)
np.testing.assert_array_almost_equal(sys_check.C, C_true)
np.testing.assert_array_almost_equal(sys_check.D, D_true)
np.testing.assert_array_almost_equal(T_check, T_true)
def test_observable_form_MIMO(self):
"""Test error as Observable form only supports SISO"""
sys = tf([[[1], [1] ]], [[[1, 2, 1], [1, 2, 1]]])
with pytest.raises(ControlNotImplemented):
observable_form(sys)
def test_unobservable_system(self):
"""Test observable canonical form with an unobservable system"""
# Create an unobservable system
A = np.array([[1., 2., 2.],
[4., 5., 5.],
[7., 8., 8.]])
B = np.array([[1.], [1.], [1.]])
C = np.array([[1., 1., 1.]])
D = 42.0
sys = ss(A, B, C, D)
# Check if an exception is raised
with pytest.raises(ValueError):
canonical_form(sys, "observable")
def test_arguments(self):
# Additional unit tests added on 25 May 2019 to increase coverage
# Unknown canonical forms should generate exception
sys = tf([1], [1, 2, 1])
with pytest.raises(ControlNotImplemented):
canonical_form(sys, 'unknown')
def test_similarity(self):
"""Test similarty transform"""
# Single input, single output systems
siso_ini = tf2ss(tf([1, 1], [1, 1, 1]))
for form in 'reachable', 'observable':
# Convert the system to one of the canonical forms
siso_can, T_can = canonical_form(siso_ini, form)
# Use a similarity transformation to transform it back
siso_sim = similarity_transform(siso_can, np.linalg.inv(T_can))
# Make sure everything goes back to the original form
np.testing.assert_array_almost_equal(siso_sim.A, siso_ini.A)
np.testing.assert_array_almost_equal(siso_sim.B, siso_ini.B)
np.testing.assert_array_almost_equal(siso_sim.C, siso_ini.C)
np.testing.assert_array_almost_equal(siso_sim.D, siso_ini.D)
# Multi-input, multi-output systems
mimo_ini = ss(
[[-1, 1, 0, 0], [0, -2, 1, 0], [0, 0, -3, 1], [0, 0, 0, -4]],
[[1, 0], [0, 0], [0, 1], [1, 1]],
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]],
np.zeros((3, 2)))
# Simple transformation: row/col flips + scaling
mimo_txf = np.array(
[[0, 1, 0, 0], [2, 0, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
# Transform the system and transform it back
mimo_sim = similarity_transform(mimo_ini, mimo_txf)
mimo_new = similarity_transform(mimo_sim, np.linalg.inv(mimo_txf))
np.testing.assert_array_almost_equal(mimo_new.A, mimo_ini.A)
np.testing.assert_array_almost_equal(mimo_new.B, mimo_ini.B)
np.testing.assert_array_almost_equal(mimo_new.C, mimo_ini.C)
np.testing.assert_array_almost_equal(mimo_new.D, mimo_ini.D)
# Make sure rescaling by identify does nothing
mimo_new = similarity_transform(mimo_ini, np.eye(4))
np.testing.assert_array_almost_equal(mimo_new.A, mimo_ini.A)
np.testing.assert_array_almost_equal(mimo_new.B, mimo_ini.B)
np.testing.assert_array_almost_equal(mimo_new.C, mimo_ini.C)
np.testing.assert_array_almost_equal(mimo_new.D, mimo_ini.D)
# Time rescaling
mimo_tim = similarity_transform(mimo_ini, np.eye(4), timescale=0.3)
mimo_new = similarity_transform(mimo_tim, np.eye(4), timescale=1/0.3)
np.testing.assert_array_almost_equal(mimo_new.A, mimo_ini.A)
np.testing.assert_array_almost_equal(mimo_new.B, mimo_ini.B)
np.testing.assert_array_almost_equal(mimo_new.C, mimo_ini.C)
np.testing.assert_array_almost_equal(mimo_new.D, mimo_ini.D)
# Time + transformation, in one step
mimo_sim = similarity_transform(mimo_ini, mimo_txf, timescale=0.3)
mimo_new = similarity_transform(mimo_sim, np.linalg.inv(mimo_txf),
timescale=1/0.3)
np.testing.assert_array_almost_equal(mimo_new.A, mimo_ini.A)
np.testing.assert_array_almost_equal(mimo_new.B, mimo_ini.B)
np.testing.assert_array_almost_equal(mimo_new.C, mimo_ini.C)
np.testing.assert_array_almost_equal(mimo_new.D, mimo_ini.D)
# Time + transformation, in two steps
mimo_sim = similarity_transform(mimo_ini, mimo_txf, timescale=0.3)
mimo_tim = similarity_transform(mimo_sim, np.eye(4), timescale=1/0.3)
mimo_new = similarity_transform(mimo_tim, np.linalg.inv(mimo_txf))
np.testing.assert_array_almost_equal(mimo_new.A, mimo_ini.A)
np.testing.assert_array_almost_equal(mimo_new.B, mimo_ini.B)
np.testing.assert_array_almost_equal(mimo_new.C, mimo_ini.C)
np.testing.assert_array_almost_equal(mimo_new.D, mimo_ini.D)
def extract_bdiag(a, blksizes):
"""
Extract block diagonals
Parameters
----------
a - matrix to get blocks from
blksizes - sequence of block diagonal sizes
Returns
-------
Block diagonals
Notes
-----
Conceptually, inverse of scipy.linalg.block_diag
"""
idx0s = np.hstack([0, np.cumsum(blksizes[:-1], dtype=int)])
return tuple(a[idx0:idx0+blksize,idx0:idx0+blksize]
for idx0, blksize in zip(idx0s, blksizes))
def companion_from_eig(eigvals):
"""
Find companion matrix for given eigenvalue sequence.
"""
from numpy.polynomial.polynomial import polyfromroots, polycompanion
return polycompanion(polyfromroots(eigvals)).real
def block_diag_from_eig(eigvals):
"""
Find block-diagonal matrix for given eigenvalue sequence
Returns ideal, non-defective, schur block-diagonal form.
"""
blocks = []
i = 0
while i < len(eigvals):
e = eigvals[i]
if e.imag == 0:
blocks.append(e.real)
i += 1
else:
assert e == eigvals[i+1].conjugate()
blocks.append([[e.real, e.imag],
[-e.imag, e.real]])
i += 2
return scipy.linalg.block_diag(*blocks)
@pytest.mark.slycot
@pytest.mark.parametrize(
"eigvals, condmax, blksizes",
[
([-1,-2,-3,-4,-5], None, [1,1,1,1,1]),
([-1,-2,-3,-4,-5], 1.01, [5]),
([-1,-1,-2,-2,-2], None, [2,3]),
([-1+1j,-1-1j,-2+2j,-2-2j,-2], None, [2,2,1]),
])
def test_bdschur_ref(eigvals, condmax, blksizes):
# "reference" check
# uses companion form to introduce numerical complications
from numpy.linalg import solve
a = companion_from_eig(eigvals)
b, t, test_blksizes = bdschur(a, condmax=condmax)
np.testing.assert_array_equal(np.sort(test_blksizes), np.sort(blksizes))
bdiag_b = scipy.linalg.block_diag(*extract_bdiag(b, test_blksizes))
np.testing.assert_array_almost_equal(bdiag_b, b)
np.testing.assert_array_almost_equal(solve(t, a) @ t, b)
@pytest.mark.slycot
@pytest.mark.parametrize(
"eigvals, sorted_blk_eigvals, sort",
[
([-2,-1,0,1,2], [2,1,0,-1,-2], 'continuous'),
([-2,-2+2j,-2-2j,-2-3j,-2+3j], [-2+3j,-2+2j,-2], 'continuous'),
(np.exp([-0.2,-0.1,0,0.1,0.2]), np.exp([0.2,0.1,0,-0.1,-0.2]), 'discrete'),
(np.exp([-0.2+0.2j,-0.2-0.2j, -0.01, -0.03-0.3j,-0.03+0.3j,]),
np.exp([-0.01, -0.03+0.3j, -0.2+0.2j]),
'discrete'),
])
def test_bdschur_sort(eigvals, sorted_blk_eigvals, sort):
# use block diagonal form to prevent numerical complications
# for discrete case, exp and log introduce round-off, can't test as compeletely
a = block_diag_from_eig(eigvals)
b, t, blksizes = bdschur(a, sort=sort)
assert len(blksizes) == len(sorted_blk_eigvals)
np.testing.assert_allclose(a, t @ b @ t.T)
np.testing.assert_allclose(t.T, np.linalg.inv(t))
blocks = extract_bdiag(b, blksizes)
for block, blk_eigval in zip(blocks, sorted_blk_eigvals):
test_eigvals = np.linalg.eigvals(block)
np.testing.assert_allclose(test_eigvals.real,
blk_eigval.real)
np.testing.assert_allclose(abs(test_eigvals.imag),
blk_eigval.imag)
@pytest.mark.slycot
def test_bdschur_defective():
# the eigenvalues of this simple defective matrix cannot be separated
# a previous version of the bdschur would fail on this
a = companion_from_eig([-1, -1])
amodal, tmodal, blksizes = bdschur(a, condmax=1e200)
def test_bdschur_empty():
# empty matrix in gives empty matrix out
a = np.empty(shape=(0,0))
b, t, blksizes = bdschur(a)
np.testing.assert_array_equal(b, a)
np.testing.assert_array_equal(t, a)
np.testing.assert_array_equal(blksizes, np.array([]))
def test_bdschur_condmax_lt_1():
# require condmax >= 1.0
with pytest.raises(ValueError):
bdschur(1, condmax=np.nextafter(1, 0))
@pytest.mark.slycot
def test_bdschur_invalid_sort():
# sort must be in ('continuous', 'discrete')
with pytest.raises(ValueError):
bdschur(1, sort='no-such-sort')
@pytest.mark.slycot
@pytest.mark.parametrize(
"A_true, B_true, C_true, D_true",
[(np.diag([4.0, 3.0, 2.0, 1.0]), # order from largest to smallest
np.array([[1.1, 2.2, 3.3, 4.4]]).T,
np.array([[1.3, 1.4, 1.5, 1.6]]),
np.array([[42.0]])),
(np.array([[-1, 1, 0, 0],
[-1, -1, 0, 0],
[ 0, 0, -2, 1],
[ 0, 0, 0, -3]]),
np.array([[0, 1, 0, 0],
[0, 0, 0, 1]]).T,
np.array([[1, 0, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 1]]),
np.array([[0, 1],
[1, 0],
[0, 0]])),
],
ids=["sys1", "sys2"])
def test_modal_form(A_true, B_true, C_true, D_true):
# Check modal_canonical corresponds to bdschur
# Perform a coordinate transform with a random invertible matrix
T_true = np.array([[-0.27144004, -0.39933167, 0.75634684, 0.44135471],
[-0.74855725, -0.39136285, -0.18142339, -0.50356997],
[-0.40688007, 0.81416369, 0.38002113, -0.16483334],
[-0.44769516, 0.15654653, -0.50060858, 0.72419146]])
A = np.linalg.solve(T_true, A_true) @ T_true
B = np.linalg.solve(T_true, B_true)
C = C_true @ T_true
D = D_true
# Create a state space system and convert it to modal canonical form
sys_check, T_check = modal_form(ss(A, B, C, D))
a_bds, t_bds, _ = bdschur(A)
np.testing.assert_array_almost_equal(sys_check.A, a_bds)
np.testing.assert_array_almost_equal(T_check, t_bds)
np.testing.assert_array_almost_equal(sys_check.B, np.linalg.solve(t_bds, B))
np.testing.assert_array_almost_equal(sys_check.C, C @ t_bds)
np.testing.assert_array_almost_equal(sys_check.D, D)
# canonical_form(...,'modal') is the same as modal_form with default parameters
cf_sys, T_cf = canonical_form(ss(A, B, C, D), 'modal')
np.testing.assert_array_almost_equal(cf_sys.A, sys_check.A)
np.testing.assert_array_almost_equal(cf_sys.B, sys_check.B)
np.testing.assert_array_almost_equal(cf_sys.C, sys_check.C)
np.testing.assert_array_almost_equal(cf_sys.D, sys_check.D)
np.testing.assert_array_almost_equal(T_check, T_cf)
# Make sure Hankel coefficients are OK
for i in range(A.shape[0]):
np.testing.assert_almost_equal(
C_true @ np.linalg.matrix_power(A_true, i) @ B_true,
C @ np.linalg.matrix_power(A, i) @ B)
@pytest.mark.slycot
@pytest.mark.parametrize(
"condmax, len_blksizes",
[(1.1, 1),
(None, 5)])
def test_modal_form_condmax(condmax, len_blksizes):
# condmax passed through as expected
a = companion_from_eig([-1, -2, -3, -4, -5])
amodal, tmodal, blksizes = bdschur(a, condmax=condmax)
assert len(blksizes) == len_blksizes
xsys = ss(a, [[1],[0],[0],[0],[0]], [0,0,0,0,1], 0)
zsys, t = modal_form(xsys, condmax=condmax)
np.testing.assert_array_almost_equal(zsys.A, amodal)
np.testing.assert_array_almost_equal(t, tmodal)
np.testing.assert_array_almost_equal(zsys.B, np.linalg.solve(tmodal, xsys.B))
np.testing.assert_array_almost_equal(zsys.C, xsys.C @ tmodal)
np.testing.assert_array_almost_equal(zsys.D, xsys.D)
@pytest.mark.slycot
@pytest.mark.parametrize(
"sys_type",
['continuous',
'discrete'])
def test_modal_form_sort(sys_type):
a = companion_from_eig([0.1+0.9j,0.1-0.9j, 0.2+0.8j, 0.2-0.8j])
amodal, tmodal, blksizes = bdschur(a, sort=sys_type)
dt = 0 if sys_type == 'continuous' else True
xsys = ss(a, [[1],[0],[0],[0],], [0,0,0,1], 0, dt)
zsys, t = modal_form(xsys, sort=True)
my_amodal = np.linalg.solve(tmodal, a) @ tmodal
np.testing.assert_array_almost_equal(amodal, my_amodal)
np.testing.assert_array_almost_equal(t, tmodal)
np.testing.assert_array_almost_equal(zsys.A, amodal)
np.testing.assert_array_almost_equal(zsys.B, np.linalg.solve(tmodal, xsys.B))
np.testing.assert_array_almost_equal(zsys.C, xsys.C @ tmodal)
np.testing.assert_array_almost_equal(zsys.D, xsys.D)
def test_modal_form_empty():
# empty system should be returned as-is
# t empty matrix
insys = ss([], [], [], 123)
outsys, t = modal_form(insys)
np.testing.assert_array_equal(outsys.A, insys.A)
np.testing.assert_array_equal(outsys.B, insys.B)
np.testing.assert_array_equal(outsys.C, insys.C)
np.testing.assert_array_equal(outsys.D, insys.D)
assert t.shape == (0,0)