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

ForwardDiff with complex numbers ode sometimes results in NaN derivatives #861

Open
AmitRotem opened this issue Jan 7, 2023 · 2 comments

Comments

@AmitRotem
Copy link
Contributor

Found a small issue with ForwardDiff on complex numbers ode #860
The Dual of time in the ode function has NaN values. Meaning, in the ode function (u,p,t) -> ... t is Dual with derivative NaN
Maybe a problem with the promotion of tspan ? affecting the initial dt value ?

Example;

using LinearAlgebra, DifferentialEquations
import ForwardDiff as FD

H0 = randn(2,2)
u0 = [1.0, 0im]
Ht(u,p,t) = (H0*u)*cos(p*t)
prob0 = ODEProblem(Ht, u0, (0.0,1.0))

function loss(p)
    prob = remake(prob0; p)
    sol = solve(prob)
    lo = abs2(tr(first(sol.u)'last(sol.u)))
    lo
end

loss(rand())
FD.derivative(loss, rand()) # NaN's

This does not show up in other cases, such as;

if the initial difference is small; e.g., Ht(u,p,t) = (H0*u)*sin(p*t)
other random initial state; u0 = randn(ComplexF64,2)
real initial state u0 = [1.0, 0.0]
use some finite dt in ODEProblem(...; dt=1)
Also nan safe mode for ForwardDiff solve this.

@ChrisRackauckas
Copy link
Member

Also nan safe mode for ForwardDiff solve this.

Interesting, why does that make a difference?

@AmitRotem
Copy link
Contributor Author

https://juliadiff.org/ForwardDiff.jl/v0.10.2/user/advanced.html#Fixing-NaN/Inf-Issues-1
It solves NaN of Inf derivatives with 0 perturbation, meaning it makes sure that NaN*dt is zero when dt=0
I'm not sure where this situation happens in the DiffEq

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

2 participants