Skip to content

Commit afc4ef4

Browse files
committed
remove np.matrix dependencies from tests
1 parent f010542 commit afc4ef4

14 files changed

+357
-324
lines changed

control/tests/canonical_test.py

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@ def test_reachable_form(self):
1616
coeffs = [1.0, 2.0, 3.0, 4.0, 1.0]
1717
A_true = np.polynomial.polynomial.polycompanion(coeffs)
1818
A_true = np.fliplr(np.rot90(A_true))
19-
B_true = np.matrix("1.0 0.0 0.0 0.0").T
20-
C_true = np.matrix("1.0 1.0 1.0 1.0")
19+
B_true = np.array([[1.0, 0.0, 0.0, 0.0]]).T
20+
C_true = np.array([[1.0, 1.0, 1.0, 1.0]])
2121
D_true = 42.0
2222

2323
# Perform a coordinate transform with a random invertible matrix
24-
T_true = np.matrix([[-0.27144004, -0.39933167, 0.75634684, 0.44135471],
25-
[-0.74855725, -0.39136285, -0.18142339, -0.50356997],
26-
[-0.40688007, 0.81416369, 0.38002113, -0.16483334],
27-
[-0.44769516, 0.15654653, -0.50060858, 0.72419146]])
28-
A = np.linalg.solve(T_true, A_true)*T_true
24+
T_true = np.array(
25+
[[-0.27144004, -0.39933167, 0.75634684, 0.44135471],
26+
[-0.74855725, -0.39136285, -0.18142339, -0.50356997],
27+
[-0.40688007, 0.81416369, 0.38002113, -0.16483334],
28+
[-0.44769516, 0.15654653, -0.50060858, 0.72419146]])
29+
A = np.dot(np.linalg.solve(T_true, A_true), T_true)
2930
B = np.linalg.solve(T_true, B_true)
30-
C = C_true*T_true
31+
C = np.dot(C_true, T_true)
3132
D = D_true
3233

3334
# Create a state space system and convert it to the reachable canonical form
@@ -44,9 +45,9 @@ def test_unreachable_system(self):
4445
"""Test reachable canonical form with an unreachable system"""
4546

4647
# Create an unreachable system
47-
A = np.matrix("1.0 2.0 2.0; 4.0 5.0 5.0; 7.0 8.0 8.0")
48-
B = np.matrix("1.0 1.0 1.0").T
49-
C = np.matrix("1.0 1.0 1.0")
48+
A = np.array([[1.0, 2.0, 2.0], [4.0, 5.0, 5.0], [7.0, 8.0, 8.0]])
49+
B = np.array([[1.0, 1.0, 1.0]]).T
50+
C = np.array([[1.0, 1.0, 1.0]])
5051
D = 42.0
5152
sys = ss(A, B, C, D)
5253

@@ -57,19 +58,20 @@ def test_modal_form(self):
5758
"""Test the modal canonical form"""
5859

5960
# Create a system in the modal canonical form
60-
A_true = np.diag([4.0, 3.0, 2.0, 1.0]) # order from the largest to the smallest
61-
B_true = np.matrix("1.1 2.2 3.3 4.4").T
62-
C_true = np.matrix("1.3 1.4 1.5 1.6")
61+
A_true = np.diag([4.0, 3.0, 2.0, 1.0]) # order from largest to smallest
62+
B_true = np.array([[1.1, 2.2, 3.3, 4.4]]).T
63+
C_true = np.array([[1.3, 1.4, 1.5, 1.6]])
6364
D_true = 42.0
6465

6566
# Perform a coordinate transform with a random invertible matrix
66-
T_true = np.matrix([[-0.27144004, -0.39933167, 0.75634684, 0.44135471],
67-
[-0.74855725, -0.39136285, -0.18142339, -0.50356997],
68-
[-0.40688007, 0.81416369, 0.38002113, -0.16483334],
69-
[-0.44769516, 0.15654653, -0.50060858, 0.72419146]])
70-
A = np.linalg.solve(T_true, A_true)*T_true
67+
T_true = np.array(
68+
[[-0.27144004, -0.39933167, 0.75634684, 0.44135471],
69+
[-0.74855725, -0.39136285, -0.18142339, -0.50356997],
70+
[-0.40688007, 0.81416369, 0.38002113, -0.16483334],
71+
[-0.44769516, 0.15654653, -0.50060858, 0.72419146]])
72+
A = np.dot(np.linalg.solve(T_true, A_true), T_true)
7173
B = np.linalg.solve(T_true, B_true)
72-
C = C_true*T_true
74+
C = np.dot(C_true, T_true)
7375
D = D_true
7476

7577
# Create a state space system and convert it to the modal canonical form
@@ -90,18 +92,19 @@ def test_observable_form(self):
9092
coeffs = [1.0, 2.0, 3.0, 4.0, 1.0]
9193
A_true = np.polynomial.polynomial.polycompanion(coeffs)
9294
A_true = np.fliplr(np.flipud(A_true))
93-
B_true = np.matrix("1.0 1.0 1.0 1.0").T
94-
C_true = np.matrix("1.0 0.0 0.0 0.0")
95+
B_true = np.array([[1.0, 1.0, 1.0, 1.0]]).T
96+
C_true = np.array([[1.0, 0.0, 0.0, 0.0]])
9597
D_true = 42.0
9698

9799
# Perform a coordinate transform with a random invertible matrix
98-
T_true = np.matrix([[-0.27144004, -0.39933167, 0.75634684, 0.44135471],
99-
[-0.74855725, -0.39136285, -0.18142339, -0.50356997],
100-
[-0.40688007, 0.81416369, 0.38002113, -0.16483334],
101-
[-0.44769516, 0.15654653, -0.50060858, 0.72419146]])
102-
A = np.linalg.solve(T_true, A_true)*T_true
100+
T_true = np.array(
101+
[[-0.27144004, -0.39933167, 0.75634684, 0.44135471],
102+
[-0.74855725, -0.39136285, -0.18142339, -0.50356997],
103+
[-0.40688007, 0.81416369, 0.38002113, -0.16483334],
104+
[-0.44769516, 0.15654653, -0.50060858, 0.72419146]])
105+
A = np.dot(np.linalg.solve(T_true, A_true), T_true)
103106
B = np.linalg.solve(T_true, B_true)
104-
C = C_true*T_true
107+
C = np.dot(C_true, T_true)
105108
D = D_true
106109

107110
# Create a state space system and convert it to the observable canonical form
@@ -118,11 +121,19 @@ def test_unobservable_system(self):
118121
"""Test observable canonical form with an unobservable system"""
119122

120123
# Create an unobservable system
121-
A = np.matrix("1.0 2.0 2.0; 4.0 5.0 5.0; 7.0 8.0 8.0")
122-
B = np.matrix("1.0 1.0 1.0").T
123-
C = np.matrix("1.0 1.0 1.0")
124+
A = np.array([[1.0, 2.0, 2.0], [4.0, 5.0, 5.0], [7.0, 8.0, 8.0]])
125+
B = np.array([[1.0, 1.0, 1.0]]).T
126+
C = np.array([[1.0, 1.0, 1.0]])
124127
D = 42.0
125128
sys = ss(A, B, C, D)
126129

127130
# Check if an exception is raised
128131
np.testing.assert_raises(ValueError, canonical_form, sys, "observable")
132+
133+
134+
def suite():
135+
return unittest.TestLoader().loadTestsFromTestCase(TestCanonical)
136+
137+
138+
if __name__ == "__main__":
139+
unittest.main()

control/tests/convert_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def testTf2ssStaticMimo(self):
203203
self.assertEqual(0, gmimo.states)
204204
self.assertEqual(3, gmimo.inputs)
205205
self.assertEqual(2, gmimo.outputs)
206-
d = np.matrix([[0.5, 30, 0.0625], [-0.5, -1.25, 101.3]])
206+
d = np.array([[0.5, 30, 0.0625], [-0.5, -1.25, 101.3]])
207207
np.testing.assert_array_equal(d, gmimo.D)
208208

209209
def testSs2tfStaticSiso(self):
@@ -220,7 +220,7 @@ def testSs2tfStaticMimo(self):
220220
a = []
221221
b = []
222222
c = []
223-
d = np.matrix([[0.5, 30, 0.0625], [-0.5, -1.25, 101.3]])
223+
d = np.array([[0.5, 30, 0.0625], [-0.5, -1.25, 101.3]])
224224
gtf = control.ss2tf(control.ss(a,b,c,d))
225225

226226
# we need a 3x2x1 array to compare with gtf.num

control/tests/discrete_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,13 @@ def test_sample_system(self):
344344

345345
def test_sample_ss(self):
346346
# double integrators, two different ways
347-
sys1 = StateSpace([[0.,1.],[0.,0.]], [[0.],[1.]], [[1.,0.]], 0.)
348-
sys2 = StateSpace([[0.,0.],[1.,0.]], [[1.],[0.]], [[0.,1.]], 0.)
347+
sys1 = StateSpace([[0., 1.], [0., 0.]], [[0.], [1.]], [[1., 0.]], 0.)
348+
sys2 = StateSpace([[0., 0.], [1., 0.]], [[1.], [0.]], [[0., 1.]], 0.)
349349
I = np.eye(2)
350350
for sys in (sys1, sys2):
351351
for h in (0.1, 0.5, 1, 2):
352352
Ad = I + h * sys.A
353-
Bd = h * sys.B + 0.5 * h**2 * (sys.A * sys.B)
353+
Bd = h * sys.B + 0.5 * h**2 * np.dot(sys.A, sys.B)
354354
sysd = sample_system(sys, h, method='zoh')
355355
np.testing.assert_array_almost_equal(sysd.A, Ad)
356356
np.testing.assert_array_almost_equal(sysd.B, Bd)

control/tests/frd_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def testFeedback2(self):
166166
def testAuto(self):
167167
omega = np.logspace(-1, 2, 10)
168168
f1 = _convertToFRD(1, omega)
169-
f2 = _convertToFRD(np.matrix([[1, 0], [0.1, -1]]), omega)
169+
f2 = _convertToFRD(np.array([[1, 0], [0.1, -1]]), omega)
170170
f2 = _convertToFRD([[1, 0], [0.1, -1]], omega)
171171
f1, f2 # reference to avoid pyflakes error
172172

@@ -213,11 +213,11 @@ def testMIMOfb(self):
213213

214214
@unittest.skipIf(not slycot_check(), "slycot not installed")
215215
def testMIMOfb2(self):
216-
sys = StateSpace(np.matrix('-2.0 0 0; 0 -1 1; 0 0 -3'),
217-
np.matrix('1.0 0; 0 0; 0 1'),
216+
sys = StateSpace(np.array([[-2.0, 0, 0], [0, -1, 1], [0, 0, -3]]),
217+
np.array([[1.0, 0], [0, 0], [0, 1]]),
218218
np.eye(3), np.zeros((3, 2)))
219219
omega = np.logspace(-1, 2, 10)
220-
K = np.matrix('1 0.3 0; 0.1 0 0')
220+
K = np.array([[1, 0.3, 0], [0.1, 0, 0]])
221221
f1 = FRD(sys, omega).feedback(K)
222222
f2 = FRD(sys.feedback(K), omega)
223223
np.testing.assert_array_almost_equal(
@@ -249,7 +249,7 @@ def testMIMOSmooth(self):
249249
[[1.0, 0.0], [0.0, 1.0]],
250250
[[1.0, 0.0], [0.0, 1.0], [1.0, 1.0]],
251251
[[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]])
252-
sys2 = np.matrix([[1, 0, 0], [0, 1, 0]]) * sys
252+
sys2 = np.array([[1, 0, 0], [0, 1, 0]]) * sys
253253
omega = np.logspace(-1, 2, 10)
254254
f1 = FRD(sys, omega, smooth=True)
255255
f2 = FRD(sys2, omega, smooth=True)
@@ -268,15 +268,15 @@ def testAgainstOctave(self):
268268
# sys = ss([-2 0 0; 0 -1 1; 0 0 -3],
269269
# [1 0; 0 0; 0 1], eye(3), zeros(3,2))
270270
# bfr = frd(bsys, [1])
271-
sys = StateSpace(np.matrix('-2.0 0 0; 0 -1 1; 0 0 -3'),
272-
np.matrix('1.0 0; 0 0; 0 1'),
271+
sys = StateSpace(np.array([[-2.0, 0, 0], [0, -1, 1], [0, 0, -3]]),
272+
np.array([[1.0, 0], [0, 0], [0, 1]]),
273273
np.eye(3), np.zeros((3, 2)))
274274
omega = np.logspace(-1, 2, 10)
275275
f1 = FRD(sys, omega)
276276
np.testing.assert_array_almost_equal(
277277
(f1.freqresp([1.0])[0] *
278278
np.exp(1j*f1.freqresp([1.0])[1])).reshape(3, 2),
279-
np.matrix('0.4-0.2j 0; 0 0.1-0.2j; 0 0.3-0.1j'))
279+
np.array([[0.4-0.2j, 0], [0, 0.1-0.2j], [0, 0.3-0.1j]]))
280280

281281

282282
def suite():

control/tests/freqresp_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
class TestFreqresp(unittest.TestCase):
2020
def setUp(self):
21-
self.A = np.matrix('1,1;0,1')
22-
self.C = np.matrix('1,0')
21+
self.A = np.array([[1, 1], [0, 1]])
22+
self.C = np.array([[1, 0]])
2323
self.omega = np.linspace(10e-2,10e2,1000)
2424

2525
def test_siso(self):
26-
B = np.matrix('0;1')
26+
B = np.array([[0], [1]])
2727
D = 0
2828
sys = StateSpace(self.A,B,self.C,D)
2929

@@ -83,18 +83,18 @@ def test_superimpose(self):
8383

8484
def test_doubleint(self):
8585
# 30 May 2016, RMM: added to replicate typecast bug in freqresp.py
86-
A = np.matrix('0, 1; 0, 0');
87-
B = np.matrix('0; 1');
88-
C = np.matrix('1, 0');
86+
A = np.array([[0, 1], [0, 0]]);
87+
B = np.array([[0], [1]]);
88+
C = np.array([[1, 0]]);
8989
D = 0;
9090
sys = ss(A, B, C, D);
9191
bode(sys);
9292

9393
@unittest.skipIf(not slycot_check(), "slycot not installed")
9494
def test_mimo(self):
9595
# MIMO
96-
B = np.matrix('1,0;0,1')
97-
D = np.matrix('0,0')
96+
B = np.array([[1, 0], [0, 1]])
97+
D = np.array([[0, 0]])
9898
sysMIMO = ss(self.A,B,self.C,D)
9999

100100
frqMIMO = sysMIMO.freqresp(self.omega)

control/tests/input_element_int_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ def test_tf_input_with_int_element_works(self):
4141
self.assertAlmostEqual(1.0, ctl.dcgain(sys))
4242

4343
def test_ss_input_with_int_element(self):
44-
ident = np.matrix(np.identity(2), dtype=int)
45-
a = np.matrix([[0, 1],
46-
[-1, -2]], dtype=int) * ident
47-
b = np.matrix([[0],
48-
[1]], dtype=int)
49-
c = np.matrix([[0, 1]], dtype=int)
44+
ident = np.array(np.identity(2), dtype=int)
45+
a = np.array([[0, 1],
46+
[-1, -2]], dtype=int) * ident
47+
b = np.array([[0],
48+
[1]], dtype=int)
49+
c = np.array([[0, 1]], dtype=int)
5050
d = 0
5151

5252
sys = ctl.ss(a, b, c, d)

0 commit comments

Comments
 (0)