Skip to content

Commit de85969

Browse files
committed
replace np.dot with @ matmul operator where applicable in tests
1 parent 6b96c7f commit de85969

File tree

6 files changed

+29
-30
lines changed

6 files changed

+29
-30
lines changed

control/tests/canonical_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,8 @@ def test_modal_form(A_true, B_true, C_true, D_true):
384384
# Make sure Hankel coefficients are OK
385385
for i in range(A.shape[0]):
386386
np.testing.assert_almost_equal(
387-
np.dot(np.dot(C_true, np.linalg.matrix_power(A_true, i)),
388-
B_true),
389-
np.dot(np.dot(C, np.linalg.matrix_power(A, i)), B))
387+
C_true @ np.linalg.matrix_power(A_true, i) @ B_true,
388+
C @ np.linalg.matrix_power(A, i) @ B)
390389

391390

392391
@slycotonly

control/tests/discrete_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def test_sample_ss(self, tsys):
416416
for sys in (sys1, sys2):
417417
for h in (0.1, 0.5, 1, 2):
418418
Ad = I + h * sys.A
419-
Bd = h * sys.B + 0.5 * h**2 * np.dot(sys.A, sys.B)
419+
Bd = h * sys.B + 0.5 * h**2 * sys.A @ sys.B
420420
sysd = sample_system(sys, h, method='zoh')
421421
np.testing.assert_array_almost_equal(sysd.A, Ad)
422422
np.testing.assert_array_almost_equal(sysd.B, Bd)

control/tests/iosys_test.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_linear_iosys(self, tsys):
5656
for x, u in (([0, 0], 0), ([1, 0], 0), ([0, 1], 0), ([0, 0], 1)):
5757
np.testing.assert_array_almost_equal(
5858
np.reshape(iosys._rhs(0, x, u), (-1, 1)),
59-
np.dot(linsys.A, np.reshape(x, (-1, 1))) + np.dot(linsys.B, u))
59+
linsys.A @ np.reshape(x, (-1, 1)) + linsys.B * u)
6060

6161
# Make sure that simulations also line up
6262
T, U, X0 = tsys.T, tsys.U, tsys.X0
@@ -152,11 +152,13 @@ def test_nonlinear_iosys(self, tsys):
152152

153153
# Create a nonlinear system with the same dynamics
154154
nlupd = lambda t, x, u, params: \
155-
np.reshape(np.dot(linsys.A, np.reshape(x, (-1, 1)))
156-
+ np.dot(linsys.B, u), (-1,))
155+
np.reshape(linsys.A @ np.reshape(x, (-1, 1))
156+
+ linsys.B @ np.reshape(u, (-1, 1)),
157+
(-1,))
157158
nlout = lambda t, x, u, params: \
158-
np.reshape(np.dot(linsys.C, np.reshape(x, (-1, 1)))
159-
+ np.dot(linsys.D, u), (-1,))
159+
np.reshape(linsys.C @ np.reshape(x, (-1, 1))
160+
+ linsys.D @ np.reshape(u, (-1, 1)),
161+
(-1,))
160162
nlsys = ios.NonlinearIOSystem(nlupd, nlout, inputs=1, outputs=1)
161163

162164
# Make sure that simulations also line up
@@ -905,12 +907,12 @@ def test_params(self, tsys):
905907
def test_named_signals(self, tsys):
906908
sys1 = ios.NonlinearIOSystem(
907909
updfcn = lambda t, x, u, params: np.array(
908-
np.dot(tsys.mimo_linsys1.A, np.reshape(x, (-1, 1))) \
909-
+ np.dot(tsys.mimo_linsys1.B, np.reshape(u, (-1, 1)))
910+
tsys.mimo_linsys1.A @ np.reshape(x, (-1, 1)) \
911+
+ tsys.mimo_linsys1.B @ np.reshape(u, (-1, 1))
910912
).reshape(-1,),
911913
outfcn = lambda t, x, u, params: np.array(
912-
np.dot(tsys.mimo_linsys1.C, np.reshape(x, (-1, 1))) \
913-
+ np.dot(tsys.mimo_linsys1.D, np.reshape(u, (-1, 1)))
914+
tsys.mimo_linsys1.C @ np.reshape(x, (-1, 1)) \
915+
+ tsys.mimo_linsys1.D @ np.reshape(u, (-1, 1))
914916
).reshape(-1,),
915917
inputs = ['u[0]', 'u[1]'],
916918
outputs = ['y[0]', 'y[1]'],
@@ -1138,8 +1140,8 @@ def test_named_signals_linearize_inconsistent(self, tsys):
11381140
def updfcn(t, x, u, params):
11391141
"""2 inputs, 2 states"""
11401142
return np.array(
1141-
np.dot(tsys.mimo_linsys1.A, np.reshape(x, (-1, 1)))
1142-
+ np.dot(tsys.mimo_linsys1.B, np.reshape(u, (-1, 1)))
1143+
tsys.mimo_linsys1.A @ np.reshape(x, (-1, 1))
1144+
+ tsys.mimo_linsys1.B @ np.reshape(u, (-1, 1))
11431145
).reshape(-1,)
11441146

11451147
def outfcn(t, x, u, params):
@@ -1413,11 +1415,13 @@ def test_linear_interconnection():
14131415
outputs = ('y[0]', 'y[1]'), name = 'sys2')
14141416
nl_sys2 = ios.NonlinearIOSystem(
14151417
lambda t, x, u, params: np.array(
1416-
np.dot(ss_sys2.A, np.reshape(x, (-1, 1))) \
1417-
+ np.dot(ss_sys2.B, np.reshape(u, (-1, 1)))).reshape((-1,)),
1418+
ss_sys2.A @ np.reshape(x, (-1, 1)) \
1419+
+ ss_sys2.B @ np.reshape(u, (-1, 1))
1420+
).reshape((-1,)),
14181421
lambda t, x, u, params: np.array(
1419-
np.dot(ss_sys2.C, np.reshape(x, (-1, 1))) \
1420-
+ np.dot(ss_sys2.D, np.reshape(u, (-1, 1)))).reshape((-1,)),
1422+
ss_sys2.C @ np.reshape(x, (-1, 1)) \
1423+
+ ss_sys2.D @ np.reshape(u, (-1, 1))
1424+
).reshape((-1,)),
14211425
states = 2,
14221426
inputs = ('u[0]', 'u[1]'),
14231427
outputs = ('y[0]', 'y[1]'),

control/tests/modelsimp_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ def testMarkovResults(self, k, m, n):
9595
Hd = c2d(Hc, Ts, 'zoh')
9696

9797
# Compute the Markov parameters from state space
98-
Mtrue = np.hstack([Hd.D] + [np.dot(
99-
Hd.C, np.dot(np.linalg.matrix_power(Hd.A, i),
100-
Hd.B)) for i in range(m-1)])
98+
Mtrue = np.hstack([Hd.D] + [
99+
Hd.C @ np.linalg.matrix_power(Hd.A, i) @ Hd.B
100+
for i in range(m-1)])
101101

102102
# Generate input/output data
103103
T = np.array(range(n)) * Ts

control/tests/statesp_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,12 +592,12 @@ def test_matrix_static_gain(self):
592592
g3 = StateSpace([], [], [], d2.T)
593593

594594
h1 = g1 * g2
595-
np.testing.assert_allclose(np.dot(d1, d2), h1.D)
595+
np.testing.assert_allclose(d1 @ d2, h1.D)
596596
h2 = g1 + g3
597597
np.testing.assert_allclose(d1 + d2.T, h2.D)
598598
h3 = g1.feedback(g2)
599599
np.testing.assert_array_almost_equal(
600-
solve(np.eye(2) + np.dot(d1, d2), d1), h3.D)
600+
solve(np.eye(2) + d1 @ d2, d1), h3.D)
601601
h4 = g1.append(g2)
602602
np.testing.assert_allclose(block_diag(d1, d2), h4.D)
603603

control/tests/timeresp_test.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,9 +1010,7 @@ def test_squeeze(self, fcn, nstate, nout, ninp, squeeze, shape1, shape2):
10101010

10111011
# Generate the time and input vectors
10121012
tvec = np.linspace(0, 1, 8)
1013-
uvec = np.dot(
1014-
np.ones((sys.ninputs, 1)),
1015-
np.reshape(np.sin(tvec), (1, 8)))
1013+
uvec = np.ones((sys.ninputs, 1)) @ np.reshape(np.sin(tvec), (1, 8))
10161014

10171015
#
10181016
# Pass squeeze argument and make sure the shape is correct
@@ -1144,9 +1142,7 @@ def test_squeeze_0_8_4(self, nstate, nout, ninp, squeeze, shape):
11441142
# Generate system, time, and input vectors
11451143
sys = ct.rss(nstate, nout, ninp, strictly_proper=True)
11461144
tvec = np.linspace(0, 1, 8)
1147-
uvec = np.dot(
1148-
np.ones((sys.ninputs, 1)),
1149-
np.reshape(np.sin(tvec), (1, 8)))
1145+
uvec =np.ones((sys.ninputs, 1)) @ np.reshape(np.sin(tvec), (1, 8))
11501146

11511147
_, yvec = ct.initial_response(sys, tvec, 1, squeeze=squeeze)
11521148
assert yvec.shape == shape

0 commit comments

Comments
 (0)