Skip to content

Commit

Permalink
replace ndarray.dot in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bnavigator committed Dec 2, 2021
1 parent be74340 commit 0a713de
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 67 deletions.
22 changes: 11 additions & 11 deletions control/tests/canonical_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def test_reachable_form(self):
[-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).dot(T_true)
A = np.linalg.solve(T_true, A_true) @ T_true
B = np.linalg.solve(T_true, B_true)
C = C_true.dot(T_true)
C = C_true @ T_true
D = D_true

# Create a state space system and convert it to the reachable canonical form
Expand Down Expand Up @@ -77,9 +77,9 @@ def test_observable_form(self):
[-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).dot(T_true)
A = np.linalg.solve(T_true, A_true) @ T_true
B = np.linalg.solve(T_true, B_true)
C = C_true.dot(T_true)
C = C_true @ T_true
D = D_true

# Create a state space system and convert it to the observable canonical form
Expand Down Expand Up @@ -266,7 +266,7 @@ def test_bdschur_ref(eigvals, condmax, 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).dot(t), b)
np.testing.assert_array_almost_equal(solve(t, a) @ t, b)


@slycotonly
Expand Down Expand Up @@ -357,9 +357,9 @@ def test_modal_form(A_true, B_true, C_true, D_true):
[-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).dot(T_true)
A = np.linalg.solve(T_true, A_true) @ T_true
B = np.linalg.solve(T_true, B_true)
C = C_true.dot(T_true)
C = C_true @ T_true
D = D_true

# Create a state space system and convert it to modal canonical form
Expand All @@ -370,7 +370,7 @@ def test_modal_form(A_true, B_true, C_true, D_true):
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.dot(t_bds))
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
Expand Down Expand Up @@ -403,7 +403,7 @@ def test_modal_form_condmax(condmax, len_blksizes):
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.dot(tmodal))
np.testing.assert_array_almost_equal(zsys.C, xsys.C @ tmodal)
np.testing.assert_array_almost_equal(zsys.D, xsys.D)


Expand All @@ -421,13 +421,13 @@ def test_modal_form_sort(sys_type):
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).dot(tmodal)
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.dot(tmodal))
np.testing.assert_array_almost_equal(zsys.C, xsys.C @ tmodal)
np.testing.assert_array_almost_equal(zsys.D, xsys.D)


Expand Down
76 changes: 38 additions & 38 deletions control/tests/mateqn_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,58 +52,58 @@ def test_lyap(self):
Q = array([[1,0],[0,1]])
X = lyap(A,Q)
# print("The solution obtained is ", X)
assert_array_almost_equal(A.dot(X) + X.dot(A.T) + Q, zeros((2,2)))
assert_array_almost_equal(A @ X + X @ A.T + Q, zeros((2,2)))

A = array([[1, 2],[-3, -4]])
Q = array([[3, 1],[1, 1]])
X = lyap(A,Q)
# print("The solution obtained is ", X)
assert_array_almost_equal(A.dot(X) + X.dot(A.T) + Q, zeros((2,2)))
assert_array_almost_equal(A @ X + X @ A.T + Q, zeros((2,2)))

def test_lyap_sylvester(self):
A = 5
B = array([[4, 3], [4, 3]])
C = array([2, 1])
X = lyap(A,B,C)
# print("The solution obtained is ", X)
assert_array_almost_equal(A * X + X.dot(B) + C, zeros((1,2)))
assert_array_almost_equal(A * X + X @ B + C, zeros((1,2)))

A = array([[2,1],[1,2]])
B = array([[1,2],[0.5,0.1]])
C = array([[1,0],[0,1]])
X = lyap(A,B,C)
# print("The solution obtained is ", X)
assert_array_almost_equal(A.dot(X) + X.dot(B) + C, zeros((2,2)))
assert_array_almost_equal(A @ X + X @ B + C, zeros((2,2)))

def test_lyap_g(self):
A = array([[-1, 2],[-3, -4]])
Q = array([[3, 1],[1, 1]])
E = array([[1,2],[2,1]])
X = lyap(A,Q,None,E)
# print("The solution obtained is ", X)
assert_array_almost_equal(A.dot(X).dot(E.T) + E.dot(X).dot(A.T) + Q,
assert_array_almost_equal(A @ X @ E.T + E @ X @ A.T + Q,
zeros((2,2)))

def test_dlyap(self):
A = array([[-0.6, 0],[-0.1, -0.4]])
Q = array([[1,0],[0,1]])
X = dlyap(A,Q)
# print("The solution obtained is ", X)
assert_array_almost_equal(A.dot(X).dot(A.T) - X + Q, zeros((2,2)))
assert_array_almost_equal(A @ X @ A.T - X + Q, zeros((2,2)))

A = array([[-0.6, 0],[-0.1, -0.4]])
Q = array([[3, 1],[1, 1]])
X = dlyap(A,Q)
# print("The solution obtained is ", X)
assert_array_almost_equal(A.dot(X).dot(A.T) - X + Q, zeros((2,2)))
assert_array_almost_equal(A @ X @ A.T - X + Q, zeros((2,2)))

def test_dlyap_g(self):
A = array([[-0.6, 0],[-0.1, -0.4]])
Q = array([[3, 1],[1, 1]])
E = array([[1, 1],[2, 1]])
X = dlyap(A,Q,None,E)
# print("The solution obtained is ", X)
assert_array_almost_equal(A.dot(X).dot(A.T) - E.dot(X).dot(E.T) + Q,
assert_array_almost_equal(A @ X @ A.T - E @ X @ E.T + Q,
zeros((2,2)))

def test_dlyap_sylvester(self):
Expand All @@ -112,14 +112,14 @@ def test_dlyap_sylvester(self):
C = array([2, 1])
X = dlyap(A,B,C)
# print("The solution obtained is ", X)
assert_array_almost_equal(A * X.dot(B.T) - X + C, zeros((1,2)))
assert_array_almost_equal(A * X @ B.T - X + C, zeros((1,2)))

A = array([[2,1],[1,2]])
B = array([[1,2],[0.5,0.1]])
C = array([[1,0],[0,1]])
X = dlyap(A,B,C)
# print("The solution obtained is ", X)
assert_array_almost_equal(A.dot(X).dot(B.T) - X + C, zeros((2,2)))
assert_array_almost_equal(A @ X @ B.T - X + C, zeros((2,2)))

def test_care(self):
A = array([[-2, -1],[-1, -1]])
Expand All @@ -128,10 +128,10 @@ def test_care(self):

X,L,G = care(A,B,Q)
# print("The solution obtained is", X)
M = A.T.dot(X) + X.dot(A) - X.dot(B).dot(B.T).dot(X) + Q
M = A.T @ X + X @ A - X @ B @ B.T @ X + Q
assert_array_almost_equal(M,
zeros((2,2)))
assert_array_almost_equal(B.T.dot(X), G)
assert_array_almost_equal(B.T @ X, G)

def test_care_g(self):
A = array([[-2, -1],[-1, -1]])
Expand All @@ -143,11 +143,11 @@ def test_care_g(self):

X,L,G = care(A,B,Q,R,S,E)
# print("The solution obtained is", X)
Gref = solve(R, B.T.dot(X).dot(E) + S.T)
Gref = solve(R, B.T @ X @ E + S.T)
assert_array_almost_equal(Gref, G)
assert_array_almost_equal(
A.T.dot(X).dot(E) + E.T.dot(X).dot(A)
- (E.T.dot(X).dot(B) + S).dot(Gref) + Q,
A.T @ X @ E + E.T @ X @ A
- (E.T @ X @ B + S) @ Gref + Q,
zeros((2,2)))

def test_care_g2(self):
Expand All @@ -160,10 +160,10 @@ def test_care_g2(self):

X,L,G = care(A,B,Q,R,S,E)
# print("The solution obtained is", X)
Gref = 1/R * (B.T.dot(X).dot(E) + S.T)
Gref = 1/R * (B.T @ X @ E + S.T)
assert_array_almost_equal(
A.T.dot(X).dot(E) + E.T.dot(X).dot(A)
- (E.T.dot(X).dot(B) + S).dot(Gref) + Q ,
A.T @ X @ E + E.T @ X @ A
- (E.T @ X @ B + S) @ Gref + Q ,
zeros((2,2)))
assert_array_almost_equal(Gref , G)

Expand All @@ -175,12 +175,12 @@ def test_dare(self):

X,L,G = dare(A,B,Q,R)
# print("The solution obtained is", X)
Gref = solve(B.T.dot(X).dot(B) + R, B.T.dot(X).dot(A))
Gref = solve(B.T @ X @ B + R, B.T @ X @ A)
assert_array_almost_equal(Gref, G)
assert_array_almost_equal(
X, A.T.dot(X).dot(A) - A.T.dot(X).dot(B).dot(Gref) + Q)
X, A.T @ X @ A - A.T @ X @ B @ Gref + Q)
# check for stable closed loop
lam = eigvals(A - B.dot(G))
lam = eigvals(A - B @ G)
assert_array_less(abs(lam), 1.0)

A = array([[1, 0],[-1, 1]])
Expand All @@ -190,15 +190,15 @@ def test_dare(self):

X,L,G = dare(A,B,Q,R)
# print("The solution obtained is", X)
AtXA = A.T.dot(X).dot(A)
AtXB = A.T.dot(X).dot(B)
BtXA = B.T.dot(X).dot(A)
BtXB = B.T.dot(X).dot(B)
AtXA = A.T @ X @ A
AtXB = A.T @ X @ B
BtXA = B.T @ X @ A
BtXB = B.T @ X @ B
assert_array_almost_equal(
X, AtXA - AtXB.dot(solve(BtXB + R, BtXA)) + Q)
X, AtXA - AtXB @ solve(BtXB + R, BtXA) + Q)
assert_array_almost_equal(BtXA / (BtXB + R), G)
# check for stable closed loop
lam = eigvals(A - B.dot(G))
lam = eigvals(A - B @ G)
assert_array_less(abs(lam), 1.0)

def test_dare_g(self):
Expand All @@ -211,13 +211,13 @@ def test_dare_g(self):

X,L,G = dare(A,B,Q,R,S,E)
# print("The solution obtained is", X)
Gref = solve(B.T.dot(X).dot(B) + R, B.T.dot(X).dot(A) + S.T)
Gref = solve(B.T @ X @ B + R, B.T @ X @ A + S.T)
assert_array_almost_equal(Gref, G)
assert_array_almost_equal(
E.T.dot(X).dot(E),
A.T.dot(X).dot(A) - (A.T.dot(X).dot(B) + S).dot(Gref) + Q)
E.T @ X @ E,
A.T @ X @ A - (A.T @ X @ B + S) @ Gref + Q)
# check for stable closed loop
lam = eigvals(A - B.dot(G), E)
lam = eigvals(A - B @ G, E)
assert_array_less(abs(lam), 1.0)

def test_dare_g2(self):
Expand All @@ -230,16 +230,16 @@ def test_dare_g2(self):

X, L, G = dare(A, B, Q, R, S, E)
# print("The solution obtained is", X)
AtXA = A.T.dot(X).dot(A)
AtXB = A.T.dot(X).dot(B)
BtXA = B.T.dot(X).dot(A)
BtXB = B.T.dot(X).dot(B)
EtXE = E.T.dot(X).dot(E)
AtXA = A.T @ X @ A
AtXB = A.T @ X @ B
BtXA = B.T @ X @ A
BtXB = B.T @ X @ B
EtXE = E.T @ X @ E
assert_array_almost_equal(
EtXE, AtXA - (AtXB + S).dot(solve(BtXB + R, BtXA + S.T)) + Q)
EtXE, AtXA - (AtXB + S) @ solve(BtXB + R, BtXA + S.T) + Q)
assert_array_almost_equal((BtXA + S.T) / (BtXB + R), G)
# check for stable closed loop
lam = eigvals(A - B.dot(G), E)
lam = eigvals(A - B @ G, E)
assert_array_less(abs(lam), 1.0)

def test_raise(self):
Expand Down
16 changes: 8 additions & 8 deletions control/tests/statefbk_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def testPlace(self, matarrayin):
P = matarrayin([-0.5 + 1j, -0.5 - 1j, -5.0566, -8.6659])
K = place(A, B, P)
assert ismatarrayout(K)
P_placed = np.linalg.eigvals(A - B.dot(K))
P_placed = np.linalg.eigvals(A - B @ K)
self.checkPlaced(P, P_placed)

# Test that the dimension checks work.
Expand All @@ -228,7 +228,7 @@ def testPlace_varga_continuous(self, matarrayin):

P = [-2., -2.]
K = place_varga(A, B, P)
P_placed = np.linalg.eigvals(A - B.dot(K))
P_placed = np.linalg.eigvals(A - B @ K)
self.checkPlaced(P, P_placed)

# Test that the dimension checks work.
Expand All @@ -241,7 +241,7 @@ def testPlace_varga_continuous(self, matarrayin):
B = matarrayin([[0], [1]])
P = matarrayin([-20 + 10*1j, -20 - 10*1j])
K = place_varga(A, B, P)
P_placed = np.linalg.eigvals(A - B.dot(K))
P_placed = np.linalg.eigvals(A - B @ K)
self.checkPlaced(P, P_placed)


Expand All @@ -261,7 +261,7 @@ def testPlace_varga_continuous_partial_eigs(self, matarrayin):
alpha = -1.5
K = place_varga(A, B, P, alpha=alpha)

P_placed = np.linalg.eigvals(A - B.dot(K))
P_placed = np.linalg.eigvals(A - B @ K)
# No guarantee of the ordering, so sort them
self.checkPlaced(P_expected, P_placed)

Expand All @@ -275,7 +275,7 @@ def testPlace_varga_discrete(self, matarrayin):

P = matarrayin([0.5, 0.5])
K = place_varga(A, B, P, dtime=True)
P_placed = np.linalg.eigvals(A - B.dot(K))
P_placed = np.linalg.eigvals(A - B @ K)
# No guarantee of the ordering, so sort them
self.checkPlaced(P, P_placed)

Expand All @@ -293,12 +293,12 @@ def testPlace_varga_discrete_partial_eigs(self, matarrayin):
P_expected = np.array([0.5, 0.6])
alpha = 0.51
K = place_varga(A, B, P, dtime=True, alpha=alpha)
P_placed = np.linalg.eigvals(A - B.dot(K))
P_placed = np.linalg.eigvals(A - B @ K)
self.checkPlaced(P_expected, P_placed)


def check_LQR(self, K, S, poles, Q, R):
S_expected = asmatarrayout(np.sqrt(Q.dot(R)))
S_expected = asmatarrayout(np.sqrt(Q @ R))
K_expected = asmatarrayout(S_expected / R)
poles_expected = -np.squeeze(np.asarray(K_expected))
np.testing.assert_array_almost_equal(S, S_expected)
Expand Down Expand Up @@ -373,7 +373,7 @@ def test_lqr_call_format(self):
K, S, E = lqr(sys.A, sys.B, sys.C, R, Q)

def check_LQE(self, L, P, poles, G, QN, RN):
P_expected = asmatarrayout(np.sqrt(G.dot(QN.dot(G).dot(RN))))
P_expected = asmatarrayout(np.sqrt(G @ QN @ G @ RN))
L_expected = asmatarrayout(P_expected / RN)
poles_expected = -np.squeeze(np.asarray(L_expected))
np.testing.assert_array_almost_equal(P, P_expected)
Expand Down

0 comments on commit 0a713de

Please sign in to comment.