JuliaDiff / JuliaDiff/ForwardDiff.jl
Bug (NaNs) when differentiating eigenvectors of Symmetric matrices
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 1k
- Forks
- 160
- PR merge metrics
- No merged PRs in 30d
Description
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):
function buggy(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
When differentiating with:
julia> vv = [82.62687031649999 0.0 82.62687031649999]
julia> ForwardDiff.jacobian(buggy, vv)
4×3 Matrix{Float64}:
NaN NaN NaN
NaN -Inf NaN
NaN Inf NaN
NaN NaN NaN
However, if I remove the specification that the matrix must be Symmetric, then it works.
function good(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(mat)
return pols
end
julia> ForwardDiff.jacobian(good, vv)
4×3 Matrix{Float64}:
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
0.0 0.0 0.0
I remember I had to specify Symmetric because, at a certain point, ForwardDiff was working only with that (I do not remember exactly).
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the report with ForwardDiff.jacobian on the provided buggy function and input, comparing eigen(Symmetric(mat)) with eigen(mat). Read the eigenvector differentiation path involved by the Symmetric wrapper and determine what behavior should avoid the reported NaN and Inf values; done means the reproduction no longer produces those values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100