-
Notifications
You must be signed in to change notification settings - Fork 438
Add unit test illustrating issue #935 + add method keyword for tf2ss #937
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
murrayrm
merged 3 commits into
python-control:main
from
murrayrm:tf2ss_unstable_bug-21Oct2023
Nov 8, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,10 +60,10 @@ | |
from scipy.signal import StateSpace as signalStateSpace | ||
from warnings import warn | ||
|
||
from .exception import ControlSlycot | ||
from .exception import ControlSlycot, slycot_check, ControlMIMONotImplemented | ||
from .frdata import FrequencyResponseData | ||
from .lti import LTI, _process_frequency_response | ||
from .iosys import InputOutputSystem, common_timebase, isdtime, \ | ||
from .iosys import InputOutputSystem, common_timebase, isdtime, issiso, \ | ||
_process_iosys_keywords, _process_dt_keyword, _process_signal_list | ||
from .nlsys import NonlinearIOSystem, InterconnectedSystem | ||
from . import config | ||
|
@@ -1583,6 +1583,13 @@ def ss(*args, **kwargs): | |
-------- | ||
tf, ss2tf, tf2ss | ||
|
||
Notes | ||
----- | ||
If a transfer function is passed as the sole positional argument, the | ||
system will be converted to state space form in the same way as calling | ||
:func:`~control.tf2ss`. The `method` keyword can be used to select the | ||
method for conversion. | ||
|
||
Examples | ||
-------- | ||
Create a Linear I/O system object from matrices. | ||
|
@@ -1615,10 +1622,13 @@ def ss(*args, **kwargs): | |
warn("state labels specified for " | ||
"non-unique state space realization") | ||
|
||
# Allow method to be specified (eg, tf2ss) | ||
method = kwargs.pop('method', None) | ||
|
||
# Create a state space system from an LTI system | ||
sys = StateSpace( | ||
_convert_to_statespace( | ||
sys, | ||
sys, method=method, | ||
use_prefix_suffix=not sys._generic_name_check()), | ||
**kwargs) | ||
|
||
|
@@ -1765,6 +1775,10 @@ def tf2ss(*args, **kwargs): | |
name : string, optional | ||
System name. If unspecified, a generic name <sys[id]> is generated | ||
with a unique integer id. | ||
method : str, optional | ||
Set the method used for computing the result. Current methods are | ||
'slycot' and 'scipy'. If set to None (default), try 'slycot' first | ||
and then 'scipy' (SISO only). | ||
|
||
Raises | ||
------ | ||
|
@@ -1781,6 +1795,13 @@ def tf2ss(*args, **kwargs): | |
tf | ||
ss2tf | ||
|
||
Notes | ||
----- | ||
The ``slycot`` routine used to convert a transfer function into state | ||
space form appears to have a bug and in some (rare) instances may not | ||
return a system with the same poles as the input transfer function. | ||
For SISO systems, setting ``method=scipy`` can be used as an alternative. | ||
|
||
Examples | ||
-------- | ||
>>> num = [[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]]] | ||
|
@@ -2189,7 +2210,7 @@ def _f2s(f): | |
return s | ||
|
||
|
||
def _convert_to_statespace(sys, use_prefix_suffix=False): | ||
def _convert_to_statespace(sys, use_prefix_suffix=False, method=None): | ||
"""Convert a system to state space form (if needed). | ||
|
||
If sys is already a state space, then it is returned. If sys is a | ||
|
@@ -2213,13 +2234,17 @@ def _convert_to_statespace(sys, use_prefix_suffix=False): | |
raise ValueError("transfer function is non-proper; can't " | ||
"convert to StateSpace system") | ||
|
||
try: | ||
if method is None and slycot_check() or method == 'slycot': | ||
if not slycot_check(): | ||
raise ValueError("method='slycot' requires slycot") | ||
|
||
from slycot import td04ad | ||
|
||
# Change the numerator and denominator arrays so that the transfer | ||
# function matrix has a common denominator. | ||
# matrices are also sized/padded to fit td04ad | ||
num, den, denorder = sys.minreal()._common_den() | ||
num, den, denorder = sys._common_den() | ||
|
||
# transfer function to state space conversion now should work! | ||
ssout = td04ad('C', sys.ninputs, sys.noutputs, | ||
|
@@ -2230,9 +2255,8 @@ def _convert_to_statespace(sys, use_prefix_suffix=False): | |
ssout[1][:states, :states], ssout[2][:states, :sys.ninputs], | ||
ssout[3][:sys.noutputs, :states], ssout[4], sys.dt) | ||
|
||
except ImportError: | ||
# No Slycot. Scipy tf->ss can't handle MIMO, but static | ||
# MIMO is an easy special case we can check for here | ||
elif method in [None, 'scipy']: | ||
# Scipy tf->ss can't handle MIMO, but SISO is OK | ||
maxn = max(max(len(n) for n in nrow) | ||
for nrow in sys.num) | ||
maxd = max(max(len(d) for d in drow) | ||
|
@@ -2244,12 +2268,15 @@ def _convert_to_statespace(sys, use_prefix_suffix=False): | |
D[i, j] = sys.num[i][j][0] / sys.den[i][j][0] | ||
newsys = StateSpace([], [], [], D, sys.dt) | ||
else: | ||
if sys.ninputs != 1 or sys.noutputs != 1: | ||
raise TypeError("No support for MIMO without slycot") | ||
if not issiso(sys): | ||
raise ControlMIMONotImplemented( | ||
"MIMO system conversion not supported without Slycot") | ||
|
||
A, B, C, D = \ | ||
sp.signal.tf2ss(squeeze(sys.num), squeeze(sys.den)) | ||
newsys = StateSpace(A, B, C, D, sys.dt) | ||
else: | ||
raise ValueError(f"unknown {method=}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First instantiation of self-documenting F-strings in python-control? : ) |
||
|
||
# Copy over the signal (and system) names | ||
newsys._copy_names( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggested change for the two lines above: