FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(xferfcn): support complex dtypes with negligible imaginary parts … · python-control/python-control@2c6ee69 · GitHub

Commit 2c6ee69

Browse files
marko1olo
committed
fix(xferfcn): support complex dtypes with negligible imaginary parts and safe polynomial stringification (fixes #1188)
1 parent 2743c6e commit 2c6ee69

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎control/tests/xferfcn_test.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ class TestXferFcn:
2828

2929
# Tests for raising exceptions.
3030

31+
def test_zpk_complex_dtypes(self):
32+
"""Test zpk with complex64 and complex128 zeros/poles (Issue #1188)."""
33+
P = ct.rss(5)
34+
q1 = ct.zpk(zeros=P.zeros().astype(np.complex64),
35+
poles=P.poles().astype(np.complex64),
36+
gain=1, dt=0)
37+
assert isinstance(q1, ct.TransferFunction)
38+
assert "s^5" in str(q1)
39+
40+
q2 = ct.zpk(zeros=P.zeros().astype(np.complex128),
41+
poles=P.poles().astype(np.complex128),
42+
gain=1, dt=0)
43+
assert isinstance(q2, ct.TransferFunction)
44+
assert "s^5" in str(q2)
45+
3146
def test_constructor_bad_input_type(self):
3247
"""Give the constructor invalid input types."""
3348
# Single argument of the wrong type

‎control/xferfcn.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,9 +1378,8 @@ def _tf_polynomial_to_string(coeffs, var='s'):
13781378
"""Convert a transfer function polynomial to a string."""
13791379
thestr = "0"
13801380

1381-
# Apply NumPy formatting
1382-
with np.printoptions(threshold=sys.maxsize):
1383-
coeffs = eval(repr(coeffs))
1381+
# Convert coefficients to Python list of floats/numbers
1382+
coeffs = np.asarray(coeffs).tolist()
13841383

13851384
# Compute the number of coefficients
13861385
N = len(coeffs) - 1
@@ -2015,6 +2014,12 @@ def _clean_part(data, name="<unknown>"):
20152014
# Check for coefficients that are ints and convert to floats
20162015
for i in range(out.shape[0]):
20172016
for j in range(out.shape[1]):
2017+
if np.iscomplexobj(out[i, j]):
2018+
if np.allclose(np.imag(out[i, j]), 0.0, atol=1e-10):
2019+
out[i, j] = np.real(out[i, j]).astype(float)
2020+
else:
2021+
raise TypeError(
2022+
f"unsupported data type: {type(out[i, j][0])}")
20182023
for k in range(len(out[i, j])):
20192024
if isinstance(out[i, j][k], (int, np.integer)):
20202025
out[i, j][k] = float(out[i, j][k])

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL