Repository navigation
Fix numerical stability of factored controllability Gramian (gh-1123) - #1278
Open
DresdenGman wants to merge 1 commit into
Open
DresdenGman wants to merge 1 commit into
DresdenGman wants to merge 1 commit into
Conversation
…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
requested changes
Nov 18, 2026
slivingston
left a comment
Member
There was a problem hiding this comment.
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.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1123.
gram(sys, 'cf')passedA.Twithtrans='N'to the SLICOT routinesb03od. WhenAis already in (quasi-triangular) Schur form, itstranspose 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
Aitself withtrans='T'instead, storesBin the layoutthat mode expects, and returns the transpose of the computed factor so
that the documented identity
Wc = Rc' * Rcstill holds.One API-visible change
The factor returned by
gram(sys, 'cf')is now lower triangularinstead of upper triangular (the
'of'factor is still uppertriangular). The documented identity
Wc = Rc' * Rcis unchanged, socode 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]]), solvingWc x = [1, 1](exact answer[8, -6]) viascipy.linalg.cho_solve((Rc, False), b)now returns[32.5, 0.0308],and
cho_solve((Rc, True), b)returns[50.22, -0.723](it solvesRc Rc' x = b), so such call sites need updating:cho_solvecannotbe used with the new factor as before (no flag setting solves
Wc x = b); triangular solves must be done in two steps, first withRc'and then withRc. Returning the transposed factor isdeliberate: converting
sb03od'sU(withWc = U U') back to anupper 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 lightlydamped resonant blocks in Schur form, poles at -0.001 +/- 100j,
Gramian known from a 60-digit
mpmathcomputation), same system andsame reference before/after:
Files changed
control/statefbk.py:'cf'branch only — passAwithtrans='T',store
BasB[0:n, 0:m] = sys.B, returnX.transpose();gramdocstring documents the new lower-triangular
'cf'factor and thetriangular-solver impact.
control/tests/statefbk_test.py: updated expected factors intestGramRc; addedtestGramRcNonnormalwith the extended-precisionreference (60-digit
mpmath, back-substitution residual 4.6e-26relative 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!statefbk.pychange (keeping the new tests) makestestGramRcandtestGramRcNonnormalfail; restoring it makes them pass.Out of scope (pre-existing, unchanged by this PR)
'cf'/'of'factor buffer isnxn, so inputs with more inputs than states (m > n) fail as before.gramdiscards thescalereturned bysb03od, 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 codereview 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.