Skip to content

Commit 433c136

Browse files
committed
return frequency response for 0 and 1-state systems directly
1 parent 5ab0a1c commit 433c136

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

control/statesp.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ def slycot_laub(self, x):
851851
# transformed state matrices, at, bt, ct.
852852

853853
# Start at the second frequency, already have the first.
854-
for kk, x_kk in enumerate(x_arr[1:len(x_arr)]):
854+
for kk, x_kk in enumerate(x_arr[1:]):
855855
result = tb05ad(n, m, p, x_kk, at, bt, ct, job='NH')
856856
# When job='NH', result = (g_i, hinvb, info)
857857

@@ -885,15 +885,27 @@ def horner(self, x, warn_infinite=True):
885885
Attempts to use Laub's method from Slycot library, with a
886886
fall-back to python code.
887887
"""
888+
# Make sure the argument is a 1D array of complex numbers
889+
x_arr = np.atleast_1d(x).astype(complex, copy=False)
890+
891+
# return fast on systems with 0 or 1 state
892+
if self.nstates == 0:
893+
return self.D[:, :, np.newaxis] \
894+
* np.ones_like(x_arr, dtype=complex)
895+
if self.nstates == 1:
896+
with np.errstate(divide='ignore', invalid='ignore'):
897+
out = (self.C[:, :, np.newaxis]
898+
* (self.B[:, :, np.newaxis] / (x_arr - self.A[0, 0]))
899+
+ self.D[:, :, np.newaxis])
900+
out[np.isnan(out)] = complex(np.inf, np.nan)
901+
return out
902+
888903
try:
889-
out = self.slycot_laub(x)
904+
out = self.slycot_laub(x_arr)
890905
except (ImportError, Exception):
891906
# Fall back because either Slycot unavailable or cannot handle
892907
# certain cases.
893908

894-
# Make sure the argument is a 1D array of complex numbers
895-
x_arr = np.atleast_1d(x).astype(complex, copy=False)
896-
897909
# Make sure that we are operating on a simple list
898910
if len(x_arr.shape) > 1:
899911
raise ValueError("input list must be 1D")

0 commit comments

Comments
 (0)