Skip to content

change default value of statesp.remove_useless_states to False #509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions control/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,7 @@ def use_legacy_defaults(version):
linearized_system_name_prefix='',
linearized_system_name_suffix='_linearized')

# turned off _remove_useless_states
set_defaults('statesp', remove_useless_states=True)

return (major, minor, patch)
20 changes: 13 additions & 7 deletions control/statesp.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
# Define module default parameter values
_statesp_defaults = {
'statesp.use_numpy_matrix': False, # False is default in 0.9.0 and above
'statesp.remove_useless_states': True,
'statesp.remove_useless_states': False,
'statesp.latex_num_format': '.3g',
'statesp.latex_repr_type': 'partitioned',
}
Expand Down Expand Up @@ -217,8 +217,7 @@ class StateSpace(LTI):
__array_priority__ = 11 # override ndarray and matrix types

def __init__(self, *args, **kwargs):
"""
StateSpace(A, B, C, D[, dt])
"""StateSpace(A, B, C, D[, dt])

Construct a state space object.

Expand All @@ -228,6 +227,13 @@ def __init__(self, *args, **kwargs):
True for unspecified sampling time). To call the copy constructor,
call StateSpace(sys), where sys is a StateSpace object.

The `remove_useless_states` keyword can be used to scan the A, B, and
C matrices for rows or columns of zeros. If the zeros are such that a
particular state has no effect on the input-output dynamics, then that
state is removed from the A, B, and C matrices. If not specified, the
value is read from `config.defaults['statesp.remove_useless_states']
(default = False).

"""
# first get A, B, C, D matrices
if len(args) == 4:
Expand All @@ -251,8 +257,8 @@ def __init__(self, *args, **kwargs):
"Expected 1, 4, or 5 arguments; received %i." % len(args))

# Process keyword arguments
remove_useless = kwargs.get(
'remove_useless',
remove_useless_states = kwargs.get(
'remove_useless_states',
config.defaults['statesp.remove_useless_states'])

# Convert all matrices to standard form
Expand Down Expand Up @@ -321,7 +327,7 @@ def __init__(self, *args, **kwargs):
raise ValueError("C and D must have the same number of rows.")

# Check for states that don't do anything, and remove them.
if remove_useless:
if remove_useless_states:
self._remove_useless_states()

def _remove_useless_states(self):
Expand Down Expand Up @@ -1274,7 +1280,7 @@ def _convert_to_statespace(sys, **kw):
# Generate a simple state space system of the desired dimension
# The following Doesn't work due to inconsistencies in ltisys:
# return StateSpace([[]], [[]], [[]], eye(outputs, inputs))
return StateSpace(0., zeros((1, inputs)), zeros((outputs, 1)),
return StateSpace([], zeros((0, inputs)), zeros((outputs, 0)),
sys * ones((outputs, inputs)))

# If this is a matrix, try to create a constant feedthrough
Expand Down
11 changes: 4 additions & 7 deletions control/tests/statesp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,7 @@ def test_is_static_gain(self):
D0 = 0
D1 = np.ones((2,1))
assert StateSpace(A0, B0, C1, D1).is_static_gain()
# TODO: fix this once remove_useless_states is false by default
# should be False when remove_useless is false
# print(StateSpace(A1, B0, C1, D1).is_static_gain())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert not StateSpace(A1, B0, C1, D1).is_static_gain() ?

assert not StateSpace(A1, B0, C1, D1).is_static_gain()
assert not StateSpace(A0, B1, C1, D1).is_static_gain()
assert not StateSpace(A1, B1, C1, D1).is_static_gain()
assert StateSpace(A0, B0, C0, D0).is_static_gain()
Expand Down Expand Up @@ -586,10 +584,9 @@ def test_matrix_static_gain(self):

def test_remove_useless_states(self):
"""Regression: _remove_useless_states gives correct ABC sizes."""
g1 = StateSpace(np.zeros((3, 3)),
np.zeros((3, 4)),
np.zeros((5, 3)),
np.zeros((5, 4)))
g1 = StateSpace(np.zeros((3, 3)), np.zeros((3, 4)),
np.zeros((5, 3)), np.zeros((5, 4)),
remove_useless_states=True)
assert (0, 0) == g1.A.shape
assert (0, 4) == g1.B.shape
assert (5, 0) == g1.C.shape
Expand Down