diff --git a/control/tests/timeresp_test.py b/control/tests/timeresp_test.py index ba76c2b91..0645401dc 100644 --- a/control/tests/timeresp_test.py +++ b/control/tests/timeresp_test.py @@ -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 diff --git a/control/timeresp.py b/control/timeresp.py index 1d252b270..ec447166a 100644 --- a/control/timeresp.py +++ b/control/timeresp.py @@ -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] @@ -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 '