Skip to content

Add type error checks, unit tests, documentation for real-valued systems #1142

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
Mar 25, 2025
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
1 change: 1 addition & 0 deletions control/tests/statesp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def test_constructor(self, sys322ABCD, dt, argfun):
((np.ones((3, 3)), np.ones((3, 2)),
np.ones((2, 3)), np.ones((2, 3))), ValueError,
r"Incompatible dimensions of D matrix; expected \(2, 2\)"),
(([1j], 2, 3, 0), TypeError, "real number, not 'complex'"),
])
def test_constructor_invalid(self, args, exc, errmsg):
"""Test invalid input to StateSpace() constructor"""
Expand Down
4 changes: 4 additions & 0 deletions control/tests/xferfcn_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def test_constructor_bad_input_type(self):
[[4, 5], [6, 7]]],
[[[6, 7], [4, 5]],
[[2, 3]]])

with pytest.raises(TypeError, match="unsupported data type"):
ct.tf([1j], [1, 2, 3])

# good input
TransferFunction([[[0, 1], [2, 3]],
[[4, 5], [6, 7]]],
Expand Down
6 changes: 5 additions & 1 deletion control/xferfcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -1954,6 +1954,7 @@ def _clean_part(data, name="<unknown>"):

"""
valid_types = (int, float, complex, np.number)
unsupported_types = (complex, np.complexfloating)
valid_collection = (list, tuple, ndarray)

if isinstance(data, np.ndarray) and data.ndim == 2 and \
Expand Down Expand Up @@ -1998,8 +1999,11 @@ def _clean_part(data, name="<unknown>"):
for i in range(out.shape[0]):
for j in range(out.shape[1]):
for k in range(len(out[i, j])):
if isinstance(out[i, j][k], (int, np.int32, np.int64)):
if isinstance(out[i, j][k], (int, np.integer)):
out[i, j][k] = float(out[i, j][k])
elif isinstance(out[i, j][k], unsupported_types):
raise TypeError(
f"unsupported data type: {type(out[i, j][k])}")
return out


Expand Down
5 changes: 3 additions & 2 deletions doc/linear.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ of linear time-invariant (LTI) systems:
y &= C x + D u

where :math:`u` is the input, :math:`y` is the output, and :math:`x`
is the state.
is the state. All vectors and matrices must be real-valued.

To create a state space system, use the :func:`ss` function:

Expand Down Expand Up @@ -94,7 +94,8 @@ transfer functions
{b_0 s^n + b_1 s^{n-1} + \cdots + b_n},

where :math:`n` is greater than or equal to :math:`m` for a proper
transfer function. Improper transfer functions are also allowed.
transfer function. Improper transfer functions are also allowed. All
coefficients must be real-valued.

To create a transfer function, use the :func:`tf` function::

Expand Down
6 changes: 3 additions & 3 deletions examples/cruise.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"system: a = (0.010124405669387215-0j) , b = (1.3203061238159202+0j)\n",
"pzcancel: kp = 0.5 , ki = (0.005062202834693608+0j) , 1/(kp b) = (1.5148002148317266+0j)\n",
"system: a = 0.010124405669387215 , b = 1.3203061238159202\n",
"pzcancel: kp = 0.5 , ki = 0.005062202834693608 , 1/(kp b) = 1.5148002148317266\n",
"sfb_int: K = 0.5 , ki = 0.1\n"
]
},
Expand All @@ -442,7 +442,7 @@
"\n",
"# Construction a controller that cancels the pole\n",
"kp = 0.5\n",
"a = -P.poles()[0]\n",
"a = -P.poles()[0].real\n",
"b = np.real(P(0)) * a\n",
"ki = a * kp\n",
"control_pz = ct.TransferFunction(\n",
Expand Down