Skip to content
Closed
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
21 changes: 21 additions & 0 deletions control/tests/timeresp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,27 @@ def test_response_transpose(
assert x.shape == (T.size, sys.nstates)


@pytest.mark.parametrize("ninputs", [1, 2])
def test_forced_response_transpose_discrete(ninputs):
# Transposed inputs have time along the first axis, including when the
# time vector is computed from them (gh-931)
sys = ct.rss(3, 2, ninputs, dt=0.1)
U = np.random.default_rng(0).standard_normal((ninputs, 10))

resp = ct.forced_response(sys, U=U, squeeze=False)
resp_t = ct.forced_response(
sys, U=U.T, transpose=True, squeeze=False)
np.testing.assert_equal(resp_t.time, resp.time)
np.testing.assert_allclose(np.transpose(resp_t.outputs), resp.outputs)

# Scalar input with a given time vector
T = np.arange(10) * 0.1
resp = ct.forced_response(sys, T=T, U=1.0, squeeze=False)
resp_t = ct.forced_response(
sys, T=T, U=1.0, transpose=True, squeeze=False)
np.testing.assert_allclose(np.transpose(resp_t.outputs), resp.outputs)


@pytest.mark.pandas
def test_to_pandas():
# Create a SISO time response
Expand Down
6 changes: 5 additions & 1 deletion control/timeresp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,8 @@ def forced_response(
raise ValueError('Parameters `T` and `U` can\'t both be '
'zero for discrete-time simulation')
# Set T to equally spaced samples with same length as U
if U.ndim == 1:
# (time is along the first axis of a transposed U)
if U.ndim == 1 or transpose:
n_steps = U.shape[0]
else:
n_steps = U.shape[1]
Expand All @@ -1121,6 +1122,9 @@ def forced_response(
else:
if U.ndim == 0:
U = np.full((n_inputs, T.shape[0]), U)
if transpose:
# U gets transposed back when its shape is checked
U = U.T
else:
if T is None:
raise ValueError('Parameter `T` is mandatory for continuous '
Expand Down