You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug (NaNs) when differentiating eigenvectors of Symmetric matrices #684
Hello,
I noticed at a certain point my code started to crash because of some NaNs introduced by Forward diff when performing derivatives of the eigenvectors of matrix.
Here was my previous code (it was working at a certain point):
functionbuggy(x)
mat =zeros(eltype(x),(2, 2))
mat[1,1] = x[1]
mat[1,2] = x[2]
mat[2,1] = x[2]
mat[2,2] = x[3]
(ω, pols) =eigen(Symmetric(mat))
return pols
end
At the values in the example the matrix is exactly a*I, so both eigenvalues are a = 82.62687031649999. There is no unique first eigenvector at that point. For example, these two symmetric matrices approach the same input:
The result was 0.70710678 for h from 1.0 down to 1e-10. The directions stay 45° apart, even allowing for an eigenvector sign flip. A zero Jacobian from the unwrapped call would also be misleading. On Julia 1.13.4 and ForwardDiff 1.5.0 the unwrapped call gives a MethodError.
If the calculation needs the two modes together, an invariant such as tr(A) has a well-defined gradient ([1, 0, 1] for your three inputs). For an isolated mode, differentiating q*q' avoids sign flips. I compared that derivative to central differences on 36 matrices with distinct eigenvalues, with agreement in all three input directions.
Activity
dleather commentedon Jul 26, 2024
Im getting this issues too.
yucheng-du commentedon Dec 2, 2026
At the values in the example the matrix is exactly
a*I, so both eigenvalues area = 82.62687031649999. There is no unique first eigenvector at that point. For example, these two symmetric matrices approach the same input:The result was
0.70710678forhfrom1.0down to1e-10. The directions stay 45° apart, even allowing for an eigenvector sign flip. A zero Jacobian from the unwrapped call would also be misleading. On Julia 1.13.4 and ForwardDiff 1.5.0 the unwrapped call gives aMethodError.If the calculation needs the two modes together, an invariant such as
tr(A)has a well-defined gradient ([1, 0, 1]for your three inputs). For an isolated mode, differentiatingq*q'avoids sign flips. I compared that derivative to central differences on 36 matrices with distinct eigenvalues, with agreement in all three input directions.