Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug (NaNs) when differentiating eigenvectors of Symmetric matrices #684

Open
mesonepigreco opened this issue Jan 18, 2024 · 0 comments
Open

Comments

@mesonepigreco
Copy link

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).

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

No branches or pull requests

1 participant