Skip to content

Preserve complex StateSpace matrices#1230

Open
marko1olo wants to merge 1 commit into
python-control:mainfrom
marko1olo:fix-statespace-complex-matrices
Open

Preserve complex StateSpace matrices#1230
marko1olo wants to merge 1 commit into
python-control:mainfrom
marko1olo:fix-statespace-complex-matrices

Conversation

@marko1olo

Copy link
Copy Markdown
Contributor

Fixes #1058.

StateSpace(A, B, C, D) currently coerces every state-space matrix through np.array(..., dtype=float), which silently discards imaginary parts from complex-valued matrices and emits ComplexWarning. State-space operations already evaluate complex frequencies and NumPy can safely keep real matrices as floats, so this changes _ssmatrix() to preserve complex dtype only when the input is complex-valued.

The regression test covers complex A/B/C/D matrices and treats constructor warnings as errors, so the old ComplexWarning path fails directly.

Validation:

  • Reproduced the issue on current main before the fix: complex matrices were stored as real matrices and emitted four ComplexWarnings.
  • python -m pytest control/tests/statesp_test.py::TestStateSpace::test_constructor_complex_matrices control/tests/statesp_test.py::TestStateSpace::test_constructor_invalid -q
  • python -m pytest control/tests/statesp_test.py -q
  • python -m pytest control/tests/convert_test.py control/tests/type_conversion_test.py control/tests/xferfcn_test.py control/tests/bdalg_test.py -q
  • python -m ruff check control/statesp.py control/tests/statesp_test.py
  • python -m compileall -q control
  • git diff --check

Note: I also attempted python -m pytest control/tests -q, but the local Windows run exceeded a 5-minute timeout before producing useful output.

Assisted-by: OpenAI Codex

@marko1olo

marko1olo commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

Local verification on 0ef95a33:

python -m pytest control/tests/statesp_test.py -q
828 passed, 112 skipped, 1 xfailed

python -m ruff check control/statesp.py control/tests/statesp_test.py
All checks passed

git diff --check origin/main..HEAD
passed

The only local warning was pytest being unable to write .pytest_cache in this Windows worktree; the tests themselves passed.

@slivingston

Copy link
Copy Markdown
Member

@marko1olo thanks for this and the other recent PRs that you have opened. I have several requests that apply to all of these (including this one):

  1. Can you mark when you used AI tools like Codex? You marked this PR, but I wonder if the others involved such a tool.
  2. If an AI tool was used, can you summarize how it was used? E.g., code editing, PR description text writing, etc.
  3. Can you stop adding the "Checks" section that has a dump of various commands. Generally, we use CI testing, so unless there is some special requirement for running tests with this PR, it should all be automatic. Furthermore, trivial checks like git diff --check will be noticed during review easily and need not be listed as a suggested check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Complex numbers in state space matrices get cast to real in control.StateSpace(...) constructor - a bug?

2 participants