Repository navigation
Propagate a NaN through xLAE2 and xLAEV2 instead of returning finite eigenvalues - #1405
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1405 +/- ##
========================================
Coverage 70.20% 70.21%
========================================
Files 6237 6237
Lines 500437 500585 +148
Branches 23536 23536
========================================
+ Hits 351336 351476 +140
- Misses 148639 148647 +8
Partials 462 462
Continue to review full report in Codecov by Harness.
|
349f09f to
833d60b
Compare
833d60b to
e5c7b9c
Compare
#1389, #1403, #1404 and #1405 add checks to xCHKST that build a NaN as SQRT( -RONE ) or an infinity as ONE / RZERO, the construction that b5dbdf5 exempted in the xERRBD and xERRST error-exit tests: nagfor folds it at compile time and rejects the result. Add chkst to the pattern, which matches ?chkst.f and its generated _64 copies but not ?chkst2stg.f. Other compilers are unaffected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
e5c7b9c to
2ab657f
Compare
…eigenvalues
xLAE2 and xLAEV2 compute the eigenvalues of the symmetric 2 by 2
matrix [a b; b c] from sm = a + c, adf = |a - c| and ab = |2b|:
IF( ADF.GT.AB ) THEN
RT = ADF*SQRT( ONE+( AB / ADF )**2 )
ELSE IF( ADF.LT.AB ) THEN
RT = AB*SQRT( ONE+( ADF / AB )**2 )
ELSE
RT = AB*SQRT( TWO )
END IF
The last branch is meant for adf = ab, but a NaN diagonal entry makes
adf a NaN, both comparisons false, and rt = |2b| sqrt(2), finite. The
sign test on sm that follows is false for a NaN as well, so the
routines returned rt1 = -rt2 = |b| sqrt(2) with no trace of the NaN.
Every 2 by 2 tridiagonal eigenproblem goes through this code: xSTEQR,
xSTERF, xSTEV, xSTEVD, xSTEDC and xSTEMR returned INFO = 0 and finite
eigenvalues for a 2 by 2 matrix with a NaN on the diagonal, while a
NaN off the diagonal was propagated.
Take the last branch only for adf = ab and let a NaN in either operand
propagate, rt = adf + ab, so that the eigenvalues, and in xLAEV2 the
eigenvector, come out as NaN. Finite arguments are not affected: the
branch they take is unchanged and so is the value it computes.
xCHKST gets the case as a regression test: it calls xSTEQR, xSTERF
and xSTEMR on the 2 by 2 matrix [1 1; 1 2] with a NaN in place of
either diagonal entry and reports a finite eigenvalue as a failure.
On the parent commit all six calls return INFO = 0 and finite
eigenvalues in every precision; here every eigenvalue is a NaN. Over
the 1 by 1 and 2 by 2 NaN cases of a sweep of every tridiagonal solver
the parent returned an all-finite result in 62 of 111 cases and this
branch in 16, all of them calls that return no eigenvalue at all
(RANGE = 'V' or 'I' with M = 0) or bisection paths, which do the same
for larger n; every finite case is bit-identical.
The full LAPACK test suite passes: 0 numerical errors, 0 other errors,
120 tests more than the parent from the new calls.
The regression test fails on the parent and passes with the fix, and the
reproducer prints the same before and after output, with gfortran 13
(x86-64 Release and Debug with -fcheck=all, and under QEMU on aarch64,
ppc64le, s390x and riscv64), flang-19 and Intel ifx 2025.3.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Add STEQR(I) and STEMR(V,A) cases for both diagonal NaN positions in all four precisions. Include the job option in diagnostics and check the real workspace capacity for the complex drivers. Validation: 8 sep/se2 driver runs and 8 AddressSanitizer cases passed. All four precisions detect reverting only SLAEV2/DLAEV2, and all four also retain the expected failures against the parent kernels.
2ab657f to
9794c79
Compare
|
Good catch. I like it. Thanks. Very important to get this one right. I checked with various NaN positions on the D(1), D(2) and E(1). Tried all 2^3 combinations and all work great. Indeed as long as D(1) or D(2) contained a NaN, the output was not a NaN. Not good. And this PR fixes this. I also checked the behavior with Inf, and it is all good. Tried all 3^3 combinations there too. (With +/- Inf.) All good with me. If there is an Inf on the diagonal of D and E is finite, the infinite nicely propagates as an eigenvalue. Great. And the sort is correct too. I tried some of the 4^3 combination by mixing (+Inf, -Inf, NaN, and finite) behavior is much better now. (Like 100% correct.) For example, for something like: [ +∞, 1.; 1., 2. ], LAPACK returns [ 2, +∞]. This makes sense. Same for [ +∞, 1e10; 1e10, 2. ], LAPACK also returns [ 2, +∞]. Great. I tried various things and it always make sense and +/- infinity is nicely sorted on the real line. The one thing where I paused when E is infinite, then LAPACK returns NaN. One could argue that the eigenvalues of the symmetric matrix [ 1, ∞; . , 2 ] should be +∞ and -∞. I think returning NaN is better. I can see someone arguing that LAPACK should return +∞ and -∞. |
|
Two other ones that are not obvious to me:
Opinions welcome. |
|
I looked a tat more at it with Julia, Numpy and Matlab to see what conventions these high programming languages use.
import numpy as np
A = np.array([[np.nan, 1.0],[1.0, 2.0]])
eigenvalues = np.linalg.eigvalsh(A)
print(eigenvalues)returns the infamous [-1.41421356 1.41421356]So it is good that this will be fixed. Thanks @rmlarsen.
[NaN NaN]More generally, as soon there is an Inf or a NaN in the input, Matlab returns NaNs as eigenvalues. This is a legitimate solution.
using LinearAlgebra
eigen(Symmetric([ NaN 1; 1 2]))errors out with ERROR: ArgumentError: matrix contains Infs or NaNs |
|
I want to come back to the three examples I gave. ( 1 / 3 ) When E is infinite and D is finite, for example [ 1, +∞; . , 2 ], (where the . is assumed by symmetry,) then LAPACK returns [ NaN, NaN]. => It could be better to return the eigenvalues [ -∞, +∞ ]. ( 2 / 3 ) For the symmetric 2x2 matrix [ +∞, +∞; . , +∞ ]. Right now, LAPACK returns [ +∞, +∞ ]. I do not think this is the good answer. [ NaN, +∞ ] would be better. This might be hard to get, so maybe we settle for [ NaN, NaN ]. => if the matrix is [ t, t; t t ] (and t goes to infinity) then we get [ 0, +∞ ] => The largest eigenvalue is always +∞, the smallest can be anything. So returning [ NaN, +∞ ] makes sense. (Note: One could argue that IEEE 754 ( 3 / 3 ) For the symmetric 2x2 matrix [ +∞, -∞; . , +∞ ]. Right now, LAPACK returns [ -∞, +∞ ]. For the same reasons as (2/3), I do not think this is the good answer. [ NaN, +∞ ] would be better. This might be hard to get, so maybe we settle for [ NaN, NaN ]. |
|
@rmlarsen's PR is about NaNs. If a NaN is anywhere in the 2x2, I agree with returning [ NaN, NaN ] as the answer. I am changing the topic to Inf. I checked all 2x2 symmetric matrices with finite or infinite entries and then looked at what we would expect mathematically and what LAPACK returns. Here is what I got.
I have ❌❌ for the ones that bother the most. I would rather return [ NaN, NaN ] than the current answer. For all ❌, we can argue what the correct answer should be, but returning [ NaN, NaN ] could be OK. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
AI disclosure: The initial changes were prepared with Claude Code; review and subsequent test fixes used Codex.
Summary
xLAE2andxLAEV2compute the eigenvalues of the symmetric 2 by 2 matrix[a b; b c]through a three-way comparison of|a-c|with|2b|, whose last branch is meant for the equal case. For a NaN diagonal entry both comparisons are false, the last branch computesrt = |2b| sqrt(2), finite, and the sign test ona+cthat follows is false too, so the routines returnedrt1 = -rt2 = |b| sqrt(2)with no trace of the NaN. Every 2 by 2 tridiagonal eigenproblem goes through this code:xSTEQR,xSTERF,xSTEV,xSTEVD,xSTEDCandxSTEMRreturnedINFO = 0and finite eigenvalues for a 2 by 2 matrix with a NaN on its diagonal (a NaN off the diagonal was propagated). This PR takes the equal-case branch only when the operands are equal and propagates a NaN otherwise. The numerical changes are in{s,d}lae2.fand{s,d}laev2.f, with regression coverage in{s,d,c,z}chkst.f;cLAEV2andzLAEV2call the real routine.Description
With
aa NaN,sm = a + candadf = |a - c|are NaN,ab = |2b|is finite, both branches of the first test are skipped,rt = ab sqrt(2), and thesmtest lands in its last branch as well.Fix. The last branch of the first test is taken only for
ADF.EQ.AB; otherwise one of the two is a NaN andRT = ADF + ABpropagates it, after whichsmis a NaN too (a NaN diagonal entry makes bothsmandadfNaN), soRT1andRT2come out as NaN. Finite arguments take exactly the branch they took before and get the same value.Minimal reproducer
+/- sqrt(2) = +/- |b| sqrt(2): the diagonal, one entry of which is a NaN, played no part in the result.Regression test.
xCHKSTcallsxSTEQR('N'),xSTERF,xSTEMR('N','A'),xSTEQR('I'), andxSTEMR('V','A')on the 2 by 2 matrix[1 1; 1 2]with a NaN in either diagonal entry. The eigenvalue-only calls exercisexLAE2, and the eigenvector-producing calls exercisexLAEV2. A successful call must return NaN eigenvalues; diagnostics identify the routine and job option. Reverting onlySLAEV2andDLAEV2makes the new vector cases fail in every precision, while the complete fix passes.Validation
-O2 -fcheck=all; all 8 focusedsep.in/se2.indriver runs pass.SLAEV2andDLAEV2are reverted.Found while auditing the symmetric tridiagonal eigensolvers for the NaN and overflow handling of #1377-#1391.
Update: Codex review identified that the eigenvalue-only regression calls did not reach LAEV2. STEQR(I) and STEMR(V,A) now exercise that path, and reverting only LAEV2 makes the added cases fail.