Skip to content

Commit 0ef95a3

Browse files
committed
Preserve complex StateSpace matrices
1 parent 146ccee commit 0ef95a3

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

control/statesp.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2302,7 +2302,8 @@ def _ssmatrix(data, axis=1, square=None, rows=None, cols=None, name=None):
23022302
name = "" if name is None else " " + name
23032303

23042304
# Convert the data into an array (always making a copy)
2305-
arr = np.array(data, dtype=float)
2305+
dtype = complex if np.iscomplexobj(data) else float
2306+
arr = np.array(data, dtype=dtype)
23062307
ndim = arr.ndim
23072308
shape = arr.shape
23082309

control/tests/statesp_test.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"""
99

1010
import operator
11+
import warnings
1112

1213
import numpy as np
1314
import pytest
@@ -136,7 +137,6 @@ def test_constructor(self, sys322ABCD, dt, argfun):
136137
((np.ones((3, 3)), np.ones((3, 2)),
137138
np.ones((2, 3)), np.ones((2, 3))), ValueError,
138139
r"Incompatible dimensions of D matrix; expected \(2, 2\)"),
139-
(([1j], 2, 3, 0), TypeError, "real number, not 'complex'"),
140140
])
141141
def test_constructor_invalid(self, args, exc, errmsg):
142142
"""Test invalid input to StateSpace() constructor"""
@@ -146,6 +146,22 @@ def test_constructor_invalid(self, args, exc, errmsg):
146146
with pytest.raises(exc, match=errmsg):
147147
ss(*args)
148148

149+
def test_constructor_complex_matrices(self):
150+
"""Test complex-valued matrices in StateSpace() constructor"""
151+
A = np.array([[1 + 1j, 2 - 3j], [3 + 2j, 4 - 1j]])
152+
B = np.array([[1 - 2j], [3 + 4j]])
153+
C = np.array([[5 + 6j, 7 - 8j]])
154+
D = np.array([[9 + 10j]])
155+
156+
with warnings.catch_warnings():
157+
warnings.simplefilter("error")
158+
sys = StateSpace(A, B, C, D)
159+
160+
np.testing.assert_allclose(sys.A, A)
161+
np.testing.assert_allclose(sys.B, B)
162+
np.testing.assert_allclose(sys.C, C)
163+
np.testing.assert_allclose(sys.D, D)
164+
149165
def test_constructor_warns(self, sys322ABCD):
150166
"""Test ambiguos input to StateSpace() constructor"""
151167
with pytest.warns(UserWarning, match="received multiple dt"):

0 commit comments

Comments
 (0)