8000 BugFix: fix Python 2.7 failure. · OpethMC/python-control@4a74cc5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a74cc5

Browse files
committed
BugFix: fix Python 2.7 failure.
On Python 2.7, the special case "all states useless" in _remove_useless_states resulted in A=[[0]] (and similarly for B and C). The special case is no longer needed, since empty A, B, C matrices can be handled. numpy.delete does the right thing w.r.t. matrix sizes (e.g., deleting all columns of a nxm matrix gives an nx0 matrix). Added test for this.
1 parent 564b1dc commit 4a74cc5

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

control/statesp.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def __init__(self, *args):
146146
if self.states != B.shape[0]:
147147
raise ValueError("A and B must have the same number of rows.")
148148
if self.states != C.shape[1]:
149-
raise ValueError("A and C C must have the same number of columns.")
149+
raise ValueError("A and C must have the same number of columns.")
150150
if self.inputs != B.shape[1]:
151151
raise ValueError("B and D must have the same number of columns.")
152152
if self.outputs != C.shape[0]:
@@ -180,17 +180,10 @@ def _remove_useless_states(self):
180180
useless.append(i)
181181

182182
# Remove the useless states.
183-
if all(useless == range(self.states)):
184-
# All the states were useless.
185-
self.A = zeros((1, 1))
186-
self.B = zeros((1, self.inputs))
187-
self.C = zeros((self.outputs, 1))
188-
else:
189-
# A more typical scenario.
190-
self.A = delete(self.A, useless, 0)
191-
self.A = delete(self.A, useless, 1)
192-
self.B = delete(self.B, useless, 0)
193-
self.C = delete(self.C, useless, 1)
183+
self.A = delete(self.A, useless, 0)
184+
self.A = delete(self.A, useless, 1)
185+
self.B = delete(self.B, useless, 0)
186+
self.C = delete(self.C, useless, 1)
194187

195188
self.states = self.A.shape[0]
196189
self.inputs = self.B.shape[1]

control/tests/statesp_test.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,14 @@ def test_scalarStaticGain(self):
254254
np.testing.assert_array_equal(np.diag([2,3]),g6.D)
255255

256256
def test_matrixStaticGain(self):
257-
"""Regression: can we create a scalar static gain?"""
257+
"""Regression: can we create matrix static gains?"""
258258
d1 = np.matrix([[1,2,3],[4,5,6]])
259259
d2 = np.matrix([[7,8],[9,10],[11,12]])
260260
g1=StateSpace([],[],[],d1)
261+
262+
# _remove_useless_states was making A = [[0]]
263+
self.assertEqual((0,0), g1.A.shape)
264+
261265
g2=StateSpace([],[],[],d2)
262266
g3=StateSpace([],[],[],d2.T)
263267

@@ -271,6 +275,19 @@ def test_matrixStaticGain(self):
271275
np.testing.assert_array_equal(block_diag(d1,d2),h4.D)
272276

273277

278+
def test_remove_useless_states(self):
279+
"""Regression: _remove_useless_states gives correct ABC sizes"""
280+
g1 = StateSpace(np.zeros((3,3)),
281+
np.zeros((3,4)),
282+
np.zeros((5,3)),
283+
np.zeros((5,4)))
284+
self.assertEqual((0,0), g1.A.shape)
285+
self.assertEqual((0,4), g1.B.shape)
286+
self.assertEqual((5,0), g1.C.shape)
287+
self.assertEqual((5,4), g1.D.shape)
288+
self.assertEqual(0, g1.states)
289+
290+
274291
def test_BadEmptyMatrices(self):
275292
"""Mismatched ABCD matrices when some are empty"""
276293
self.assertRaises(ValueError,StateSpace, [1], [], [], [1])

0 commit comments

Comments
 (0)
0