Skip to content

BugFix: tf2ss now handles static gains #129

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
merged 1 commit into from
Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions control/statesp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# Python 3 compatibility (needs to go here)
from __future__ import print_function
from __future__ import division # for _convertToStateSpace

"""Copyright (c) 2010 by California Institute of Technology
All rights reserved.
Expand Down Expand Up @@ -647,6 +648,7 @@ def _convertToStateSpace(sys, **kw):
"""

from .xferfcn import TransferFunction
import itertools
if isinstance(sys, StateSpace):
if len(kw):
raise TypeError("If sys is a StateSpace, _convertToStateSpace \
Expand Down Expand Up @@ -679,16 +681,27 @@ def _convertToStateSpace(sys, **kw):
ssout[3][:sys.outputs, :states],
ssout[4], sys.dt)
except ImportError:
# If slycot is not available, use signal.lti (SISO only)
if (sys.inputs != 1 or sys.outputs != 1):
raise TypeError("No support for MIMO without slycot")

# TODO: do we want to squeeze first and check dimenations?
# I think this will fail if num and den aren't 1-D after
# the squeeze
lti_sys = lti(squeeze(sys.num), squeeze(sys.den))
return StateSpace(lti_sys.A, lti_sys.B, lti_sys.C, lti_sys.D,
sys.dt)
# No Slycot. Scipy tf->ss can't handle MIMO, but static
# MIMO is an easy special case we can check for here
maxn = max(max(len(n) for n in nrow)
for nrow in sys.num)
maxd = max(max(len(d) for d in drow)
for drow in sys.den)
if 1==maxn and 1==maxd:
D = empty((sys.outputs,sys.inputs),dtype=float)
for i,j in itertools.product(range(sys.outputs),range(sys.inputs)):
D[i,j] = sys.num[i][j][0] / sys.den[i][j][0]
return StateSpace([], [], [], D, sys.dt)
else:
if (sys.inputs != 1 or sys.outputs != 1):
raise TypeError("No support for MIMO without slycot")

# TODO: do we want to squeeze first and check dimenations?
Copy link
Member

@slivingston slivingston Feb 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should "dimenations" be "dimensions"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't take the credit for this one, the comment was there already ;). Not sure it's applicable anymore: the check that sys.inputs and sys.outputs are both 1 implies that squeeze will bring num and den to 1-D?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree. The comment is obsolete after the addition in commit c90a399 of the check that sys.inputs = sys.outputs = 1.

I am planning to review and perform maintenance on documentation and code comments, so I can delete this comment at that time.

# I think this will fail if num and den aren't 1-D after
# the squeeze
lti_sys = lti(squeeze(sys.num), squeeze(sys.den))
return StateSpace(lti_sys.A, lti_sys.B, lti_sys.C, lti_sys.D,
sys.dt)

elif isinstance(sys, (int, float, complex)):
if "inputs" in kw:
Expand Down
22 changes: 22 additions & 0 deletions control/tests/convert_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,28 @@ def testConvertMIMO(self):
if (not slycot_check()):
self.assertRaises(TypeError, control.tf2ss, tfcn)

def testTf2ssStaticSiso(self):
"""Regression: tf2ss for SISO static gain"""
import control
gsiso = control.tf2ss(control.tf(23, 46))
self.assertEqual(0, gsiso.states)
self.assertEqual(1, gsiso.inputs)
self.assertEqual(1, gsiso.outputs)
# in all cases ratios are exactly representable, so assert_array_equal is fine
np.testing.assert_array_equal([[0.5]], gsiso.D)

def testTf2ssStaticMimo(self):
"""Regression: tf2ss for MIMO static gain"""
import control
# 2x3 TFM
gmimo = control.tf2ss(control.tf([[ [23], [3], [5] ], [ [-1], [0.125], [101.3] ]],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you select these coefficients?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an arbitrary selection of numbers over a (small) range of magnitudes and signs; in all cases the ratios are exactly representable with low precision (see d just below), so I could use np.assert_array_equal().

[[ [46], [0.1], [80] ], [ [2], [-0.1], [1] ]]))
self.assertEqual(0, gmimo.states)
self.assertEqual(3, gmimo.inputs)
self.assertEqual(2, gmimo.outputs)
d = np.matrix([[0.5, 30, 0.0625], [-0.5, -1.25, 101.3]])
np.testing.assert_array_equal(d, gmimo.D)


def suite():
return unittest.TestLoader().loadTestsFromTestCase(TestConvert)
Expand Down