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

Implement partials/value for complex duals #732

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dlfivefifty
Copy link
Contributor

At the moment values/partials give useless (and probably "wrong") answers for complex-duals:

julia> ForwardDiff.value(ForwardDiff.Dual(1,2) + im*ForwardDiff.Dual(3,4))
Dual{Nothing}(1,2) + Dual{Nothing}(3,4)*im


julia> ForwardDiff.value(ForwardDiff.Dual(1,2) + im*ForwardDiff.Dual(3,4))
Dual{Nothing}(1,2) + Dual{Nothing}(3,4)*im

julia>  ForwardDiff.partials(ForwardDiff.Dual(1,2) + im*ForwardDiff.Dual(3,4))
0-element ForwardDiff.Partials{0, Complex{ForwardDiff.Dual{Nothing, Int64, 1}}}

julia>  ForwardDiff.partials(ForwardDiff.Dual(1,2) + im*ForwardDiff.Dual(3,4),1)
Dual{Nothing}(0,0) + Dual{Nothing}(0,0)*im

This PR implements correct versions of these functions.

@mcabbott
Copy link
Member

mcabbott commented Dec 12, 2024

Are these the desired return types?

julia> ForwardDiff.partials(ForwardDiff.Dual(1,2) + im*ForwardDiff.Dual(3,4))
1-element Vector{Complex{Int64}}:
 2 + 4im

julia> ForwardDiff.partials(1 + im)
0-element ForwardDiff.Partials{0, Complex{Int64}}

One alternative is

julia> @eval ForwardDiff @inline partials(d::Complex{<:Dual}) = Partials(map(complex, d.re.partials.values, d.im.partials.values))
partials (generic function with 13 methods)

julia> ForwardDiff.partials(ForwardDiff.Dual(1,2) + im*ForwardDiff.Dual(3,4))
1-element Partials{1, Complex{Int64}}:
 2 + 4im

Copy link

codecov bot commented Dec 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.90%. Comparing base (c310fb5) to head (d05bb57).
Report is 13 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #732      +/-   ##
==========================================
- Coverage   89.57%   85.90%   -3.68%     
==========================================
  Files          11       10       -1     
  Lines         969      922      -47     
==========================================
- Hits          868      792      -76     
- Misses        101      130      +29     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dlfivefifty
Copy link
Contributor Author

Ah yes you are right, it is better to return a Partials. It's fixed now

@mcabbott
Copy link
Member

I don't swear this is better, it just needs some thought. Are there any paths by which Partials{3, ComplexF64} can result in a Dual{Complex} instead of Complex{Dual}? That would be bad.

@dlfivefifty
Copy link
Contributor Author

Given that nothing is currently calling partials(::Complex) I don't see how this change can result in that happening, but yes there is a question about what Partials can represent.

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.

2 participants