Skip to content

Commit bdb3417

Browse files
jgoppertcwrowley
authored andcommitted
Numpy array style access to state-space subsystems.
Signed-off-by: Clancy Rowley <cwrowley@princeton.edu>
1 parent 8d8a84d commit bdb3417

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/statesp.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
StateSpace.feedback
2929
StateSpace.returnScipySignalLti
3030
StateSpace.append
31+
StateSpace.__getitem__
3132
_convertToStateSpace
3233
_rss_generate
3334
@@ -560,6 +561,17 @@ def append(self, other):
560561
D[self.outputs:,self.inputs:] = other.D
561562
return StateSpace(A, B, C, D, self.dt)
562563

564+
def __getitem__(self, indices):
565+
"""Array style acces"""
566+
if len(indices) != 2:
567+
raise IOError('must provide indices of length 2 for state space')
568+
i = indices[0]
569+
j = indices[1]
570+
return StateSpace(self.A,
571+
self.B[:,j],
572+
self.C[i,:],
573+
self.D[i,j], self.dt)
574+
563575
# TODO: add discrete time check
564576
def _convertToStateSpace(sys, **kw):
565577
"""Convert a system to state space form (if needed).

tests/statesp_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,25 @@ def testAppendTF(self):
204204
np.testing.assert_array_almost_equal(sys3c.A[3:,:3], np.zeros( (2, 3)) )
205205

206206

207+
def testArrayAccessSS(self):
208+
209+
sys1 = StateSpace([[1., 2.], [3., 4.]],
210+
[[5., 6.], [6., 8.]],
211+
[[9., 10.], [11., 12.]],
212+
[[13., 14.], [15., 16.]], 1)
213+
214+
sys1_11 = sys1[0,1]
215+
np.testing.assert_array_almost_equal(sys1_11.A,
216+
sys1.A)
217+
np.testing.assert_array_almost_equal(sys1_11.B,
218+
sys1.B[:,1])
219+
np.testing.assert_array_almost_equal(sys1_11.C,
220+
sys1.C[0,:])
221+
np.testing.assert_array_almost_equal(sys1_11.D,
222+
sys1.D[0,1])
223+
224+
assert sys1.dt == sys1_11.dt
225+
207226
class TestRss(unittest.TestCase):
208227
"""These are tests for the proper functionality of statesp.rss."""
209228

0 commit comments

Comments
 (0)