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

complex number support #455

Closed
wants to merge 7 commits into from
Closed

Conversation

GiggleLiu
Copy link

@GiggleLiu GiggleLiu commented May 24, 2020

Related Issues

#157
#324
#364

This is a quick fix for some complex valued functions. Any thoughts?

NOTE: I copied some functions from Base, because they fixed the type to foating point numbers.

Copy link
Member

@YingboMa YingboMa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if it makes sense to define these methods in ForwardDiff.jl, since they should all be generic callbacks that are independent of Dual, right?

src/complex.jl Show resolved Hide resolved
src/complex.jl Outdated Show resolved Hide resolved
src/complex.jl Show resolved Hide resolved
src/complex.jl Show resolved Hide resolved
test/ComplexTest.jl Show resolved Hide resolved
src/complex.jl Outdated Show resolved Hide resolved
GiggleLiu and others added 4 commits May 25, 2020 23:41
Co-authored-by: Yingbo Ma <mayingbo5@gmail.com>
Co-authored-by: Yingbo Ma <mayingbo5@gmail.com>
@GiggleLiu
Copy link
Author

@YingboMa
Although we get only 3 votes in #456 , but I need to differentiate complex number in one of my recent projects.
Do you think we should take the chance to polish and get this PR merged?

@ChrisRackauckas
Copy link
Member

Is this not all covered by JuliaLang/julia#36030 ?

@ChrisRackauckas
Copy link
Member

Looking at the two PRs, it seems like @YingboMa upstreamed it all to Base where it should go (correct me if I'm wrong and anything was left out). So complex AD is thus fixed on v1.6. However, the tests are still definitely useful from here. So I'd suggest the following:

  • Move these overloads to an internal module. Document in the README using ForwardDiff.Complex for v1.4 as a compatibility workaround.
  • Use this to in the tests

and everything is good to go. The other alternative would be to move these behind an @static if definition, but I would be weary of having such central dispatches modified by such a core package outside of the system image. If you want invalidations, this is how you get invalidations.

@ChrisRackauckas
Copy link
Member

Handled by JuliaLang/julia#36030

@GiggleLiu
Copy link
Author

Thanks! @ChrisRackauckas

@DEKOHG
Copy link

DEKOHG commented Nov 3, 2022

Hello,
I just installed the newest version of DifferentialEquations.jl . However, I still got an error with stiff solvers (eg. Rodas4P) for complex numbers calculation. I have tried on both julia 1.8.2 and julia 1.7.2 and my DifferentialEquations is v7.6.0

ERROR: ArgumentError: Cannot create a dual over scalar type ComplexF64. If the type behaves as a scalar, define ForwardDiff.can_dual(::Type{ComplexF64}) = true.

@petar-andrejic
Copy link

Handled by JuliaLang/julia#36030

Not entirely handled, complex valued functions of multiple real arguments don't work, e.g.

julia> ForwardDiff.gradient(x->exp(im * (x[1] + x[2])), [1.0,2.0])

ERROR: DimensionMismatch: gradient(f, x) expects that f(x) is a real number. Perhaps you meant jacobian(f, x)?

@davidschlegel
Copy link

Hello, I just installed the newest version of DifferentialEquations.jl . However, I still got an error with stiff solvers (eg. Rodas4P) for complex numbers calculation. I have tried on both julia 1.8.2 and julia 1.7.2 and my DifferentialEquations is v7.6.0

ERROR: ArgumentError: Cannot create a dual over scalar type ComplexF64. If the type behaves as a scalar, define ForwardDiff.can_dual(::Type{ComplexF64}) = true.

I also still get the same error. Is there a way to manually change the way forward differentiation is handled?

@ChrisRackauckas
Copy link
Member

For DiffEq? You just set autodiff=false (Rodas5P(autodiff=false)) or pass a jac function into ODEFunction if you want full control of it.

@davidschlegel
Copy link

Sorry, it was not clear from my question. Of course, setting autodiff=false works but is not efficient.
Can I autodiff a complex ODE in DiffEq? Currently it does not work out-of-the-box, but perhaps one can manually pass a Jacobian that is calculated using ForwardDiff...

@ChrisRackauckas
Copy link
Member

It uses ForwardDiff by default, and you can pass any Jacobian function you want via jac in the ODEFunction.

@davidschlegel
Copy link

Exactly, but right now, this throws an error, when trying to autodiff a complex ODE.

@ChrisRackauckas
Copy link
Member

So then I don't get the question?

@davidschlegel
Copy link

Right now, afaik, ForwardDiff does not handle automatic differentiation for complex functions out-of-the-box. However, DiffEq relies on ForwardDiff when computing jacobians and more. Is there any solution to still use automatic differentiation (eg using a different package) for DiffEq?

@ChrisRackauckas
Copy link
Member

Yes, just use jac in the ODEFunction.

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

Successfully merging this pull request may close these issues.

None yet

6 participants