Skip to content

Commit b96a039

Browse files
committed
add slice processing
1 parent 310798e commit b96a039

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

control/modelsimp.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ def model_reduction(
146146
other checking is done, so users to be careful not to render a system
147147
unobservable or unreachable.
148148
149+
States, inputs, and outputs can be specified using integer offers.
150+
Slices can also be specified, but must use the Python ``slice()`` function.
151+
149152
"""
150153
if not isinstance(sys, StateSpace):
151154
raise TypeError("system must be a a StateSpace system")
@@ -158,8 +161,16 @@ def model_reduction(
158161

159162
# Utility function to process keep/elim keywords
160163
def _process_elim_or_keep(elim, keep, labels, allow_both=False):
161-
elim = [] if elim is None else np.atleast_1d(elim)
162-
keep = [] if keep is None else np.atleast_1d(keep)
164+
def _expand_key(key):
165+
if key is None:
166+
return []
167+
elif isinstance(key, slice):
168+
return range(len(labels))[key]
169+
else:
170+
return np.atleast_1d(key)
171+
172+
elim = _expand_key(elim)
173+
keep = _expand_key(keep)
163174

164175
if len(elim) > 0 and len(keep) > 0:
165176
if not allow_both:

control/tests/modelsimp_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ def testBalredMatchDC(self):
480480
({'elim_inputs': [1, 2], 'keep_states': [1, 3]}, 2, 3, 1),
481481
({'elim_outputs': [1, 2], 'keep_inputs': [0, 1],}, 5, 1, 2),
482482
({'keep_states': [2, 0], 'keep_outputs': [0, 1]}, 2, 2, 3),
483+
({'keep_states': slice(0, 4, 2), 'keep_outputs': slice(None, 2)}, 2, 2, 3),
483484
({'elim_inputs': [0, 1, 2]}, 5, 3, 0), # no inputs
484485
({'elim_outputs': [0, 1, 2]}, 5, 0, 3), # no outputs
485486
({'elim_states': [0, 1, 2, 3, 4]}, 0, 3, 3), # no states

0 commit comments

Comments
 (0)