From 320d01a325670e88da0d16aad8f6843577425629 Mon Sep 17 00:00:00 2001 From: marko1olo Date: Sat, 6 Jun 2026 08:33:07 +0400 Subject: [PATCH] Fix zero-input interconnect D matrix shape Preserve explicitly shaped empty state-space matrices so linearized interconnections with zero inputs and nonzero outputs keep a (noutputs, 0) D matrix. Co-authored-by: OpenAI Codex --- control/statesp.py | 8 ++++---- control/tests/interconnect_test.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/control/statesp.py b/control/statesp.py index 65529b99d..16981e62b 100644 --- a/control/statesp.py +++ b/control/statesp.py @@ -2295,7 +2295,7 @@ def _ssmatrix(data, axis=1, square=None, rows=None, cols=None, name=None): Returns ------- - arr : 2D array, with shape (0, 0) if a is empty + arr : 2D array, with shape (0, 0) for empty vectors or matrices """ # Process the name of the object, if available @@ -2310,9 +2310,9 @@ def _ssmatrix(data, axis=1, square=None, rows=None, cols=None, name=None): if (ndim > 2): raise ValueError(f"state-space matrix{name} must be 2-dimensional") - elif (ndim == 2 and shape == (1, 0)) or \ - (ndim == 1 and shape == (0, )): - # Passed an empty matrix or empty vector; change shape to (0, 0) + elif (ndim == 2 and shape == (0, 0)) or \ + (ndim == 1 and shape == (0, )): + # Passed a 0-by-0 matrix or empty vector; change shape to (0, 0) shape = (0, 0) elif ndim == 1: diff --git a/control/tests/interconnect_test.py b/control/tests/interconnect_test.py index ccce76f34..e725b29f6 100644 --- a/control/tests/interconnect_test.py +++ b/control/tests/interconnect_test.py @@ -432,6 +432,36 @@ def test_linear_interconnect(): outlist=['plant.y'], outputs='y') assert clsys.syslist[0].name == 'ctrl' + +def test_interconnect_zero_input_one_output(): + plant = ct.ss( + [[1, 1], [0, -2]], + [[0], [1]], + [[1, 0]], + [[0]], + inputs=['u'], + outputs=['y'], + name='plant', + dt=True, + ) + controller = ct.ss( + [[2.25, 1], [-5, -0.5]], + [[-1.25], [4.5]], + [[-0.5, 1.5]], + [[0]], + inputs=['y'], + outputs=['u'], + name='controller', + dt=True, + ) + + sys = ct.interconnect([plant, controller], inplist=None, outlist=['y']) + + assert sys.ninputs == 0 + assert sys.noutputs == 1 + assert sys.D.shape == (1, 0) + + @pytest.mark.parametrize( "connections, inplist, outlist, inputs, outputs", [ pytest.param(