Skip to content

Commit 1fad4c6

Browse files
committed
Added a check for unreachable systems to canonical
And a corresponding unit test
1 parent b9e4455 commit 1fad4c6

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

control/canonical.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .statefbk import ctrb, obsv
88

99
from numpy import zeros, shape, poly
10-
from numpy.linalg import solve
10+
from numpy.linalg import solve, matrix_rank
1111

1212
__all__ = ['canonical_form', 'reachable_form', 'observable_form']
1313

@@ -80,9 +80,15 @@ def reachable_form(xsys):
8080
Wrx = ctrb(xsys.A, xsys.B)
8181
Wrz = ctrb(zsys.A, zsys.B)
8282

83+
if matrix_rank(Wrx) != xsys.states:
84+
raise ValueError("System not controllable to working precision.")
85+
8386
# Transformation from one form to another
8487
Tzx = solve(Wrx.T, Wrz.T).T # matrix right division, Tzx = Wrz * inv(Wrx)
8588

89+
if matrix_rank(Tzx) != xsys.states:
90+
raise ValueError("Transformation matrix singular to working precision.")
91+
8692
# Finally, compute the output matrix
8793
zsys.C = solve(Tzx.T, xsys.C.T).T # matrix right division, zsys.C = xsys.C * inv(Tzx)
8894

control/tests/canonical_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,16 @@ def test_reachable_form(self):
3939
np.testing.assert_array_almost_equal(sys_check.C, C_true)
4040
np.testing.assert_array_almost_equal(sys_check.D, D_true)
4141
np.testing.assert_array_almost_equal(T_check, T_true)
42+
43+
def test_unreachable_system(self):
44+
"""Test reachable canonical form with an unreachable system"""
45+
46+
# Create an unreachable system
47+
A = np.matrix("1.0 2.0 2.0; 4.0 5.0 5.0; 7.0 8.0 8.0")
48+
B = np.matrix("1.0 1.0 1.0").T
49+
C = np.matrix("1.0 1.0 1.0")
50+
D = 42.0
51+
sys = ss(A, B, C, D)
52+
53+
# Check if an exception is raised
54+
np.testing.assert_raises(ValueError, canonical_form, sys, "reachable")

0 commit comments

Comments
 (0)