@@ -182,17 +182,33 @@ def testBalredTruncate(self, matarrayin):
182
182
B = matarrayin ([[2. ], [0. ], [0. ], [0. ]])
183
183
C = matarrayin ([[0.5 , 0.6875 , 0.7031 , 0.5 ]])
184
184
D = matarrayin ([[0. ]])
185
+
185
186
sys = StateSpace (A , B , C , D )
186
187
orders = 2
187
188
rsys = balred (sys , orders , method = 'truncate' )
189
+ Ar , Br , Cr , Dr = rsys .A , rsys .B , rsys .C , rsys .D
190
+
191
+ # Result from MATLAB
188
192
Artrue = np .array ([[- 1.958 , - 1.194 ], [- 1.194 , - 0.8344 ]])
189
193
Brtrue = np .array ([[0.9057 ], [0.4068 ]])
190
194
Crtrue = np .array ([[0.9057 , 0.4068 ]])
191
195
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 )
196
212
197
213
@slycotonly
198
214
def testBalredMatchDC (self , matarrayin ):
@@ -207,16 +223,32 @@ def testBalredMatchDC(self, matarrayin):
207
223
B = matarrayin ([[2. ], [0. ], [0. ], [0. ]])
208
224
C = matarrayin ([[0.5 , 0.6875 , 0.7031 , 0.5 ]])
209
225
D = matarrayin ([[0. ]])
226
+
210
227
sys = StateSpace (A , B , C , D )
211
228
orders = 2
212
229
rsys = balred (sys ,orders ,method = 'matchdc' )
230
+ Ar , Br , Cr , Dr = rsys .A , rsys .B , rsys .C , rsys .D
231
+
232
+ # Result from MATLAB
213
233
Artrue = np .array (
214
234
[[- 4.43094773 , - 4.55232904 ],
215
235
[- 4.55232904 , - 5.36195206 ]])
216
236
Brtrue = np .array ([[1.36235673 ], [1.03114388 ]])
217
237
Crtrue = np .array ([[1.36235673 , 1.03114388 ]])
218
238
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