Skip to content

Commit 87a9c57

Browse files
authored
Merge pull request #762 from bnavigator/slycot-balred-reorder
fix issue with slycot balred change in state
2 parents b9cf384 + c540970 commit 87a9c57

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

control/tests/modelsimp_test.py

+40-8
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,33 @@ def testBalredTruncate(self, matarrayin):
182182
B = matarrayin([[2.], [0.], [0.], [0.]])
183183
C = matarrayin([[0.5, 0.6875, 0.7031, 0.5]])
184184
D = matarrayin([[0.]])
185+
185186
sys = StateSpace(A, B, C, D)
186187
orders = 2
187188
rsys = balred(sys, orders, method='truncate')
189+
Ar, Br, Cr, Dr = rsys.A, rsys.B, rsys.C, rsys.D
190+
191+
# Result from MATLAB
188192
Artrue = np.array([[-1.958, -1.194], [-1.194, -0.8344]])
189193
Brtrue = np.array([[0.9057], [0.4068]])
190194
Crtrue = np.array([[0.9057, 0.4068]])
191195
Drtrue = np.array([[0.]])
192-
np.testing.assert_array_almost_equal(rsys.A, Artrue, decimal=2)
193-
np.testing.assert_array_almost_equal(rsys.B, Brtrue, decimal=4)
194-
np.testing.assert_array_almost_equal(rsys.C, Crtrue, decimal=4)
195-
np.testing.assert_array_almost_equal(rsys.D, Drtrue, decimal=4)
196+
197+
# Look for possible changes in state in slycot
198+
T1 = np.array([[1, 0], [0, -1]])
199+
T2 = np.array([[-1, 0], [0, 1]])
200+
T3 = np.array([[0, 1], [1, 0]])
201+
for T in (T1, T2, T3):
202+
if np.allclose(T @ Ar @ T, Artrue, atol=1e-2, rtol=1e-2):
203+
# Apply a similarity transformation
204+
Ar, Br, Cr = T @ Ar @ T, T @ Br, Cr @ T
205+
break
206+
207+
# Make sure we got the correct answer
208+
np.testing.assert_array_almost_equal(Ar, Artrue, decimal=2)
209+
np.testing.assert_array_almost_equal(Br, Brtrue, decimal=4)
210+
np.testing.assert_array_almost_equal(Cr, Crtrue, decimal=4)
211+
np.testing.assert_array_almost_equal(Dr, Drtrue, decimal=4)
196212

197213
@slycotonly
198214
def testBalredMatchDC(self, matarrayin):
@@ -207,16 +223,32 @@ def testBalredMatchDC(self, matarrayin):
207223
B = matarrayin([[2.], [0.], [0.], [0.]])
208224
C = matarrayin([[0.5, 0.6875, 0.7031, 0.5]])
209225
D = matarrayin([[0.]])
226+
210227
sys = StateSpace(A, B, C, D)
211228
orders = 2
212229
rsys = balred(sys,orders,method='matchdc')
230+
Ar, Br, Cr, Dr = rsys.A, rsys.B, rsys.C, rsys.D
231+
232+
# Result from MATLAB
213233
Artrue = np.array(
214234
[[-4.43094773, -4.55232904],
215235
[-4.55232904, -5.36195206]])
216236
Brtrue = np.array([[1.36235673], [1.03114388]])
217237
Crtrue = np.array([[1.36235673, 1.03114388]])
218238
Drtrue = np.array([[-0.08383902]])
219-
np.testing.assert_array_almost_equal(rsys.A, Artrue, decimal=2)
220-
np.testing.assert_array_almost_equal(rsys.B, Brtrue, decimal=4)
221-
np.testing.assert_array_almost_equal(rsys.C, Crtrue, decimal=4)
222-
np.testing.assert_array_almost_equal(rsys.D, Drtrue, decimal=4)
239+
240+
# Look for possible changes in state in slycot
241+
T1 = np.array([[1, 0], [0, -1]])
242+
T2 = np.array([[-1, 0], [0, 1]])
243+
T3 = np.array([[0, 1], [1, 0]])
244+
for T in (T1, T2, T3):
245+
if np.allclose(T @ Ar @ T, Artrue, atol=1e-2, rtol=1e-2):
246+
# Apply a similarity transformation
247+
Ar, Br, Cr = T @ Ar @ T, T @ Br, Cr @ T
248+
break
249+
250+
# Make sure we got the correct answer
251+
np.testing.assert_array_almost_equal(Ar, Artrue, decimal=2)
252+
np.testing.assert_array_almost_equal(Br, Brtrue, decimal=4)
253+
np.testing.assert_array_almost_equal(Cr, Crtrue, decimal=4)
254+
np.testing.assert_array_almost_equal(Dr, Drtrue, decimal=4)

0 commit comments

Comments
 (0)