-
Notifications
You must be signed in to change notification settings - Fork 30
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 Differentiation #92
Comments
Just use a Note DualNumbers.jl takes the other approach you advocate but is planned to be redesigned as |
What could be added is support for |
Thanks for the response. I tried that solution and there were some problems.
function sin(z::Complex{T}) where T
F = float(T)
zr, zi = reim(z)
if zr == 0
Complex(F(zr), sinh(zi))
elseif !isfinite(zr)
if zi == 0 || isinf(zi)
Complex(F(NaN), F(zi))
else
Complex(F(NaN), F(NaN))
end
else
s, c = sincos(zr)
Complex(s * cosh(zi), c * sinh(zi))
end
end So if we call Simply passing a function Base.sin(d::Dual{T}) where T
s, c = sincos(value(d))
return Dual{T}(s, c * partials(d))
end If we wanted to use As for
To be clear, I tried this when I was experimenting with |
Changing I don't think you would have to add julia> exp(Complex(Dual(0,1),Dual(1,0)))
Dual{Nothing}(0.5403023058681398,0.5403023058681398) + Dual{Nothing}(0.8414709848078965,0.8414709848078965)*im |
Okay, fair enough. Also, I just noticed that I posted this issue on Taking a closer look at |
I'd really like to be able to differentiate complex functions with ForwardDiff. For almost all analytic functions, the derivative can be taken in the same way as in the real case, so Julia's dynamic type system should make complex diferentiation work out of the box. It seems to me that the only reason it doesn't is because many functions in
ForwardDiff
acceptReal
arguments, but if we simply changed these to acceptNumber
s then we should be most of the way there. Functions that are not analytic such asreal
,imag
,abs
,conj
,abs2
,reim
should all return errors when their derivatives are taken at a complex input. For example, consider these two functions:These functions are equal and both analytic, but it is not obvious by looking at
g
that it is analytic. In my opinion, it would be acceptable ifderivative(f, im)
worked whilederivative(g, im)
threw an error.I already started experimenting with this. I copied the definitions of several functions in
ForwardDiff
and just replacedReal
withComplex
, like so:This was enough for me to correctly differentiate
sin
. I expect that if we just do this throughoutForwardDiff
(and replace theReal
definitions withNumber
, instead of addingComplex
definitions like I did here), then we'll be most of the way there. Here's my plan:Real
withNumber
throughout the repositoryI'm tempted to actually do this and make a pull request as soon as I have some time to do so.
The text was updated successfully, but these errors were encountered: