10000 BugFix: allow empty (no input or output) StateSpace objects. · python-control/python-control@e640b17 · GitHub
[go: up one dir, main page]

Skip to content

Commit e640b17

Browse files
committed
BugFix: allow empty (no input or output) StateSpace objects.
Conflicts: control/tests/statesp_test.py
1 parent 2794260 commit e640b17

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

control/statesp.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
import numpy as np
5454
from numpy import all, angle, any, array, asarray, concatenate, cos, delete, \
55-
dot, empty, exp, eye, matrix, ones, pi, poly, poly1d, roots, shape, sin, \
55+
dot, empty, exp, eye, ones, pi, poly, poly1d, roots, shape, sin, \
5656
zeros, squeeze
5757
from numpy.random import rand, randn
5858
from numpy.linalg import solve, eigvals, matrix_rank
@@ -66,6 +66,19 @@
6666

6767
__all__ = ['StateSpace', 'ss', 'rss', 'drss', 'tf2ss', 'ssdata']
6868

69+
70+
def _matrix(a):
71+
"""_matrix(a) -> numpy.matrix
72+
a - passed to numpy.matrix
73+
Wrapper around numpy.matrix; unlike that function, _matrix([]) will be 0x0
74+
"""
75+
from numpy import matrix
76+
am = matrix(a)
77+
if (1,0) == am.shape:
78+
am.shape = (0,0)
79+
return am
80+
81+
6982
class StateSpace(LTI):
7083
"""A class for representing state-space models
7184
@@ -122,7 +135,7 @@ def __init__(self, *args):
122135
else:
123136
raise ValueError("Needs 1 or 4 arguments; received %i." % len(args))
124137

125-
A, B, C, D = [matrix(M) for M in (A, B, C, D)]
138+
A, B, C, D = [_matrix(M) for M in (A, B, C, D)]
126139

127140
# TODO: use super here?
128141
LTI.__init__(self, inputs=D.shape[1], outputs=D.shape[0], dt=dt)
@@ -327,8 +340,9 @@ def __rmul__(self, other):
327340
return _convertToStateSpace(other) * self
328341

329342
# try to treat this as a matrix
343+
# TODO: doesn't _convertToStateSpace do this anyway?
330344
try:
331-
X = matrix(other)
345+
X = _matrix(other)
332346
C = X * self.C
333347
D = X * self.D
334348
return StateSpace(self.A, self.B, C, D, self.dt)
@@ -686,11 +700,9 @@ def _convertToStateSpace(sys, **kw):
686700

687701
# If this is a matrix, try to create a constant feedthrough
688702
try:
689-
D = matrix(sys)
690-
outputs, inputs = D.shape
691-
692-
return StateSpace(0., zeros((1, inputs)), zeros((outputs, 1)), D)
693-
except Exception(e):
703+
D = _matrix(sys)
704+
return StateSpace([], [], [], D)
705+
except Exception as e:
694706
print("Failure to assume argument is matrix-like in" \
695707
" _convertToStateSpace, result %s" % e)
696708

control/tests/statesp_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def test_scalarStaticGain(self):
253253
g6 = g1.append(g2)
254254
np.testing.assert_array_equal(np.diag([2,3]),g6.D)
255255

256+
256257
def test_matrixStaticGain(self):
257258
"""Regression: can we create matrix static gains?"""
258259
d1 = np.matrix([[1,2,3],[4,5,6]])
@@ -297,6 +298,29 @@ def test_BadEmptyMatrices(self):
297298
self.assertRaises(ValueError,StateSpace, [], [], [1], [1])
298299
self.assertRaises(ValueError,StateSpace, [1], [1], [1], [])
299300

301+
302+
def test_Empty(self):
303+
"""Regression: can we create an empty StateSpace object?"""
304+
g1=StateSpace([],[],[],[])
305+
self.assertEqual(0,g1.states)
306+
self.assertEqual(0,g1.inputs)
307+
self.assertEqual(0,g1.outputs)
308+
309+
310+
def test_MatrixToStateSpace(self):
311+
"""_convertToStateSpace(matrix) gives ss([],[],[],D)"""
312+
D = np.matrix([[1,2,3],[4,5,6]])
313+
g = _convertToStateSpace(D)
314+
def empty(shape):
315+
m = np.matrix([])
316+
m.shape = shape
317+
return m
318+
np.testing.assert_array_equal(empty((0,0)), g.A)
319+
np.testing.assert_array_equal(empty((0,D.shape[1])), g.B)
320+
np.testing.assert_array_equal(empty((D.shape[0],0)), g.C)
321+
np.testing.assert_array_equal(D,g.D)
322+
323+
300324
class TestRss(unittest.TestCase):
301325
"""These are tests for the proper functionality of statesp.rss."""
302326

0 commit comments

Comments
 (0)
0