Skip to content

Fix numerical stability of factored controllability Gramian (gh-1123) - #1278

Open
DresdenGman wants to merge 1 commit into
python-control:mainfrom
DresdenGman:fix-1123-gram-cf-trans
Open

DresdenGman wants to merge 1 commit into
python-control:mainfrom
DresdenGman:fix-1123-gram-cf-trans

Conversation

@DresdenGman

Copy link
Copy Markdown

Fixes #1123.

gram(sys, 'cf') passed A.T with trans='N' to the SLICOT routine
sb03od. When A is already in (quasi-triangular) Schur form, its
transpose forces the routine to reduce the transposed matrix back to
Schur form, and the additional orthogonal transformations introduce
rounding errors that are significant for non-normal systems. This PR
passes A itself with trans='T' instead, stores B in the layout
that mode expects, and returns the transpose of the computed factor so
that the documented identity Wc = Rc' * Rc still holds.

One API-visible change

The factor returned by gram(sys, 'cf') is now lower triangular
instead of upper triangular (the 'of' factor is still upper
triangular). The documented identity Wc = Rc' * Rc is unchanged, so
code that reconstructs the Gramian from the factor is unaffected.
However, code that feeds the factor to triangular solvers is affected
silently. For the system in testGramRc (Wc = [[18.5, 24.5], [24.5, 32.5]]), solving Wc x = [1, 1] (exact answer [8, -6]) via
scipy.linalg.cho_solve((Rc, False), b) now returns [32.5, 0.0308],
and cho_solve((Rc, True), b) returns [50.22, -0.723] (it solves
Rc Rc' x = b), so such call sites need updating: cho_solve cannot
be used with the new factor as before (no flag setting solves
Wc x = b); triangular solves must be done in two steps, first with
Rc' and then with Rc. Returning the transposed factor is
deliberate: converting sb03od's U (with Wc = U U') back to an
upper triangular factor via QR would reintroduce ~eps·||W||-level
errors and discard the accuracy this fix gains (measured: Frobenius
relative error 1.08e-07 -> 7.04e-16 on the new regression test's
system). Whether this convention change is acceptable as-is, or
should go through a deprecation cycle, is a maintainer decision.

Measured improvement

New regression test testGramRcNonnormal (two cascaded lightly
damped resonant blocks in Schur form, poles at -0.001 +/- 100j,
Gramian known from a 60-digit mpmath computation), same system and
same reference before/after:

metric before after
||Rc'Rc - Wref||_F / ||Wref||_F 1.0788e-07 7.0370e-16
W[1,2] relative error (true value -4.99981e10) 1.164 (wrong sign: +8.21e9) 7.09e-07
max per-element relative error 6.09e+04 (W[0,3]) 3.71e-02 (W[0,3]; that entry is only 9.6e5, 23 orders of magnitude below ||W||, at the limit of float64)

Files changed

  • control/statefbk.py: 'cf' branch only — pass A with trans='T',
    store B as B[0:n, 0:m] = sys.B, return X.transpose(); gram
    docstring documents the new lower-triangular 'cf' factor and the
    triangular-solver impact.
  • control/tests/statefbk_test.py: updated expected factors in
    testGramRc; added testGramRcNonnormal with the extended-precision
    reference (60-digit mpmath, back-substitution residual 4.6e-26
    relative to ||A||·||W||) and an explicit lower-triangular assertion.
  • doc/releases/0.10.2-notes.rst: bug-fix entry for this change.

Tests

  • pytest control/tests/statefbk_test.py control/tests/modelsimp_test.py control/tests/mateqn_test.py -q → 153 passed, 5 skipped, 2 xfailed (baseline on upstream main: 152 passed, 5 skipped, 2 xfailed — the +1 is the new test, zero new failures).
  • pytest control/tests/statefbk_test.py -k Gram -q → 8 passed.
  • ruff check control/statefbk.py control/tests/statefbk_test.py → All checks passed!
  • Regression check: stashing only the statefbk.py change (keeping the new tests) makes testGramRc and testGramRcNonnormal fail; restoring it makes them pass.

Out of scope (pre-existing, unchanged by this PR)

  • The 'cf'/'of' factor buffer is nxn, so inputs with more inputs than states (m > n) fail as before.
  • gram discards the scale returned by sb03od, as before.

AI Disclosure

The approach and design of this fix are my own. AI assistance was used only to help implement.

Tools and use, per the NumPy AI Policy referenced by this repository:
ChatGPT and DeepSeek (deepseek-flash) were used for independent code
review of the diff; every finding was verified against local
reproduction or the source before adoption, and no AI-generated code
was included without that verification. This pull request description
was drafted with AI assistance and reviewed by me before submission.

…ontrolgh-1123)

gram(sys, 'cf') passed A.T with trans='N' to sb03od, which forces the
routine to recompute the Schur form whenever A is already in Schur
form (the transpose is lower quasi-triangular), introducing rounding errors
that are significant for non-normal systems.  Pass A with trans='T'
instead, store B in the layout that mode expects, and return the
transpose of the computed factor so that Wc = Rc' Rc still holds
(the returned factor is now lower triangular).

Update the expected factors in testGramRc and add
testGramRcNonnormal, a regression test with two cascaded lightly
damped resonant blocks in Schur form whose Gramian is known from an
extended-precision computation: the previous implementation has a
relative error of about 1e-7, the new one is at machine precision.

@slivingston slivingston left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is all extremely verbose: the PR description, the code comments, the docstring. I am assuming this was all generated by ChatGPT and DeepSeek, which you cite, without editing. Please just try to edit it yourself instead of asking the bots to make it more concise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve numerical stability in computation of Cholesky-factored controllability gramian

2 participants