-
Notifications
You must be signed in to change notification settings - Fork 141
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
Fix DiffRules-based definitions for complex-valued functions #577
Conversation
Bump 🙂 |
Bump 🙂 It would be very helpful, in DiffRules and downstream packages, to fix this bug. |
Codecov Report
@@ Coverage Diff @@
## master #577 +/- ##
==========================================
- Coverage 85.43% 84.80% -0.63%
==========================================
Files 9 9
Lines 865 882 +17
==========================================
+ Hits 739 748 +9
- Misses 126 134 +8
Continue to review full report at Codecov.
|
There have been a couple of discussions of support for complex numbers. Might be good to address the issues here. |
Which issues do you have in mind? Complex numbers (in intermediate calculations!) are already supported, e.g., julia> ForwardDiff.gradient([1.0, 2.0]) do x
abs(log(x[1] + im * x[2]))
end
2-element Vector{Float64}:
-0.20597271407900614
0.39695748015993854
julia> (x -> log(x[1] + im*x[2]))([ForwardDiff.Dual(1.0), ForwardDiff.Dual(2.0)])
Dual{Nothing}(0.8047189562170501) + Dual{Nothing}(1.1071487177940904)*im This PR just fixes incorrect ForwardDiff-definitions of rules present in DiffRules but doesn't change the support for complex numbers in general. In particular the PR does not add support for complex numbers as inputs, it just makes it possible to use e.g. |
Okay. Then let's get this one in. |
Currently, complex-valued functions (with real inputs!) for which derivatives are defined in DiffRules don't work with ForwardDiff since the return values for DiffRules-based definitions is enforced to be a
Dual
but should be aComplex{<:Dual}
in these cases. E.g., the following example fails currently (and is therefore excluded from the tests):This PR fixes this issue and returns values of type
Complex{<:Dual}
if the primal function value is aComplex
number. This fixes the example above: