Skip to content
Open
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
32 changes: 29 additions & 3 deletions control/statefbk.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,19 @@ def gram(sys, type):
Returns
-------
gram : 2D array
Gramian of system.
Gramian of system. For `type` 'cf' and 'of', the Cholesky
factor of the Gramian is returned, satisfying
``Wc = Rc' * Rc`` (respectively ``Wo = Ro' * Ro``). The 'cf'
factor is lower triangular, while the 'of' factor is upper
triangular. Note that the 'cf' factor was upper triangular in
earlier versions; the identity ``Wc = Rc' * Rc`` is unchanged,
but code that passes the factor to routines sensitive to the
triangular direction or the multiplication order (for example
`scipy.linalg.cho_solve` or `scipy.linalg.solve_triangular`)
needs to be updated: `cho_solve` cannot be used with the new
factor as before (no flag setting solves ``Wc x = b``), and
triangular solves must be done in two steps, first with
``Rc'`` and then with ``Rc``.

Raises
------
Expand Down Expand Up @@ -1207,9 +1219,23 @@ def gram(sys, type):
if type == 'cf':
m = sys.B.shape[1]
B = np.zeros_like(A)
B[0:m, 0:n] = sys.B.transpose()
B[0:n, 0:m] = sys.B
# Pass A itself with trans='T' instead of passing A.T with
# trans='N' (gh-1123): with fact='N', sb03od computes a
# Schur factorization of the matrix it is given, so if A is
# already in Schur form, passing A.T makes the routine
# reduce the transposed (lower quasi-triangular) matrix back to
# Schur form; the additional orthogonal transformations
# introduce rounding errors that can be significant for
# non-normal systems. Passing A itself avoids that extra
# transformation. With trans='T', sb03od returns an upper
# triangular factor U satisfying Wc = U @ U.T; return U.T
# so that the factor R returned here still satisfies
# Wc = R.T @ R (note that R is lower triangular, while the
# factor returned for 'of' is upper triangular).
X, scale, w = sb03od(
n, m, A.transpose(), Q, B, dico, fact='N', trans=tra)
n, m, A, Q, B, dico, fact='N', trans='T')
X = X.transpose()
elif type == 'of':
m = sys.C.shape[0]
C = np.zeros_like(A)
Expand Down
63 changes: 60 additions & 3 deletions control/tests/statefbk_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,72 @@ def testGramRc(self):
C = np.array([[4., 5.], [6., 7.]])
D = np.array([[13., 14.], [15., 16.]])
sys = ss(A, B, C, D)
Rctrue = np.array([[4.30116263, 5.6961343], [0., 0.23249528]])
# gh-1123: the 'cf' factor is lower triangular (Wc = Rc' Rc)
Rctrue = np.array([[0.175412, 0.], [4.297584, 5.700877]])
Rc = gram(sys, 'cf')
np.testing.assert_array_almost_equal(Rc, Rctrue)
sysd = ct.c2d(sys, 0.2)
Rctrue = np.array([[1.91488054, 2.53468814],
[0. , 0.10290372]])
Rctrue = np.array([[0.077677, 0. ],
[1.913304, 2.536776]])
Rc = gram(sysd, 'cf')
np.testing.assert_array_almost_equal(Rc, Rctrue)

@pytest.mark.slycot
def testGramRcNonnormal(self):
# gh-1123: for a system in Schur form with large off-diagonal
# entries, passing A.T to sb03od (the previous implementation)
# makes the routine reduce the transposed matrix back to Schur
# form, and the additional transformations introduce
# significant errors in the factored controllability Gramian.
# The system below consists of two cascaded, lightly damped
# resonant blocks (poles at -0.001 +/- 100j) with strong
# coupling to a slow pole. Its Gramian, computed in extended
# precision, has entries over a wide range of magnitudes; the
# previous implementation gives a relative error of about 1e-7
# in Wc = Rc' Rc (and an O(1) relative error in the (1, 2)
# entry), while the current one is normwise accurate to
# machine precision.
A = np.array([[-1e-3, 1e6, 1e6, 0., 0.],
[-0.01, -1e-3, 0., 0., 0.],
[0., 0., -1e-3, 1e6, 1e6],
[0., 0., -0.01, -1e-3, 0.],
[0., 0., 0., 0., -0.5]])
B = np.ones((5, 1))
sys = ss(A, B, np.eye(5), np.zeros((5, 1)))
# Reference values for Wtrue: computed by solving the
# continuous Lyapunov equation A W + W A' = -B B' in extended
# precision. The equation was assembled in Kronecker (vec)
# form and solved by LU decomposition using mpmath with 60
# significant digits (mp.dps = 60), with the float64 entries
# of A and B B' converted exactly (mp.mpf of each entry).
# Back-substitution of the result into the equation gives a
# residual of 4.6e-26 relative to ||A|| ||W||, so the values
# below are exact at float64 precision.
Wtrue = np.array([
[1.2499769131085409e+28, -1.2499769133585267e+19,
2.4999538264670675e+19, -9.5611639087090350e+05,
7.6196325582424242e+03],
[-1.2499769133585267e+19, 1.2499769133585267e+20,
-4.9998120322969727e+10, 2.4999538269680298e+11,
-1.5009246623238371e+02],
[2.4999538264670675e+19, -4.9998120322969727e+10,
9.9998145349069382e+10, -5.0098137819226004e+01,
1.5009628266829540e+02],
[-9.5611639087090350e+05, 2.4999538269680298e+11,
-5.0098137819226004e+01, 1.0009813781922600e+03,
-9.9992580176238322e-01],
[7.6196325582424242e+03, -1.5009246623238371e+02,
1.5009628266829540e+02, -9.9992580176238322e-01,
1.0000000000000000e+00]])
Rc = gram(sys, 'cf')
# The returned factor is lower triangular (gh-1123 changed it
# from upper triangular); the norm check below alone would
# also pass for an upper triangular factor.
np.testing.assert_array_equal(Rc, np.tril(Rc))
Wc = Rc.T @ Rc
assert np.linalg.norm(Wc - Wtrue) / np.linalg.norm(Wtrue) < 1e-12
np.testing.assert_allclose(Wc[1, 2], Wtrue[1, 2], rtol=1e-4)

@pytest.mark.slycot
def testGramWo(self):
A = np.array([[1., -2.], [3., -4.]])
Expand Down
11 changes: 11 additions & 0 deletions doc/releases/0.10.2-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ The following bugs have been fixed in this release:
* Fix some internal bugs that cropped up when using NumPy 2.3.1 but
were latent prior to that.

* `gram`: improved the numerical accuracy of the factored
controllability Gramian (type 'cf') for non-normal systems by
passing the system matrix to the underlying SLICOT routine without
transposing it. Note that the factor returned for 'cf' is now
lower triangular (it was previously upper triangular), although it
still satisfies ``Wc = Rc' * Rc``; code that uses the factor with
triangular solvers, such as ``scipy.linalg.cho_solve``, needs to
be updated for the new direction (``cho_solve`` cannot be used
with the factor as before; triangular solves must be done in two
steps, first with ``Rc'`` and then with ``Rc``).


Improvements
............
Expand Down