Skip to content

safer eigendecomposition for defective matrices (fix #96) - #97

Open
bbolker wants to merge 1 commit into
kaskr:masterfrom
bbolker:eigen-nan
Open

bbolker wants to merge 1 commit into
kaskr:masterfrom
bbolker:eigen-nan

Conversation

@bbolker

@bbolker bbolker commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

More help from Claude in sorting out #96.

Trade-off: returning NaN when eigenvalues are tied also affects cases that happened to come out correct before. Example: a diagonal matrix perturbed only along its diagonal, passed with get NaN derivatives at exact ties. That seems the safer default, but the maintainer may prefer an option or a warning.

  1. RTMB/R/eigen.R:
    • A new helper, nan_complex(), returns a matrix of complex NaNs.
    • solve_complex(), eigen_rescaled(), and the solve(V) step in eigen_rescaled_adj() now return NaN matrices instead of raising an R error when the decomposition fails.
    • eigen_rescaled_adj() returns NaN derivatives when any two eigenvalues are within sqrt(.Machine$double.eps) * max(Mod(D)) of each other. Previously it silently dropped those terms.
    • I left the symmetric case (eigen_symmetric_adj) alone. It uses the same 1e-300 trick, but in that case the result is correct whenever the input perturbation doesn't mix the repeated eigenspace.
  2. RTMB/inst/tinytest/test-factorize.R: seven new tests:
    • defective Q: the value matches away from the tie, and at the tie the value and derivatives are NaN with no error;
    • repeated eigenvalues: the value is correct and the derivatives are NaN;
    • distinct eigenvalues: the derivatives match numerical ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant