Comments (5)
Just use a Complex{<:Dual}
.
Note DualNumbers.jl takes the other approach you advocate but is planned to be redesigned as ForwardDiff.Dual
from dualnumbers.jl.
What could be added is support for derivative(f, im)
but that doesn't require a redesign of Dual
.
from dualnumbers.jl.
Thanks for the response. I tried that solution and there were some problems.
Complex{Dual}
worked for some functions like polynomials, but didn't work for functions like sin
. If we look at the source code for sin(::Complex)
, which gets called when we try sin(::Complex{Dual})
:
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 derivative(sin, im)
(after overloading derivative
and extract_derivative
to use Complex{Dual}
) we end up in the first code path because zr === Dual(0, 1) == 0
. However, this gives the wrong answer because zr != sin(zr) * cosh(zi)
. These expressions have the same real part, but different infinitesimal parts.
Simply passing a Dual
into the implementation of sin(::Real)
from Base
wouldn't work for similarish reasons, which is why ForwardDiff
provides a method for sin(::Dual)
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 Complex{Dual}
, we would have to make this method and methods like it take both Dual
and Complex{Dual}
. Alternatively, we could use Dual{Complex}
. It seems to me that the trade off is between replacing Dual
with Union{Dual, Complex{Dual}}
throughout the codebase or replacing Real
with Number
. The second option seems preferable, but the first might work too with some trickery.
As for DualNumbers.jl
, that is an option, but it doesn't have a derivative
interface to hide the dual numbers, and it isn't actively maintained. Maybe I could look into using TaylorSeries.jl
. In any case I think ForwardDiff
would benefit from this feature.
What could be added is support for
derivative(f, im)
but that doesn't require a redesign ofDual
.
To be clear, I tried this when I was experimenting with Complex{Dual}
, and ran into the problems described above.
from dualnumbers.jl.
Changing Dual
s supertype to Number
will be incredibly disruptive and break a lot of code. I think there is no chance a PR doing this will be merged so I would recommend not putting time into that.
I don't think you would have to add Union
s everywhere, only special cases where the default breaks (such as sin
). For example, exp
already works:
julia> exp(Complex(Dual(0,1),Dual(1,0)))
Dual{Nothing}(0.5403023058681398,0.5403023058681398) + Dual{Nothing}(0.8414709848078965,0.8414709848078965)*im
from dualnumbers.jl.
Changing
Dual
s supertype toNumber
will be incredibly disruptive and break a lot of code. I think there is no chance a PR doing this will be merged so I would recommend not putting time into that.
Okay, fair enough. Also, I just noticed that I posted this issue on DualNumbers
and not ForwardDiff
, my bad
Taking a closer look at ForwardDiff
, we wouldn't actually have to change that many places to take Complex{<:Dual}
in addition to Dual
. Most of the special function definitions for Dual
s are done with unary_dual_definition
and binary_dual_definition
, so we just have to change those and a couple other places. I might give that a go.
from dualnumbers.jl.
Related Issues (20)
- 0.4: @compat value.(v) for v::Vector{Dual{Float64}} returns Vector{Any}
- Replace this package with ForwardDiff's `Dual`? HOT 13
- Tag a new version
- isapprox throws an error
- max when values agree?
- erf function broken in julia Nightly HOT 1
- / HOT 1
- / HOT 1
- Support for fft of DualComplex Numbers
- @show imĪµ output
- Package compatibility caps HOT 1
- Odd behavior: `sparse(M::Array{Dual{Float64}})` zeroes dual values HOT 2
- isless broken for mixed types
- TagBot trigger issue HOT 13
- Consider renaming realpart to value or primalpart or similar
- Should we export Ļµ ā” Īµ?
- bitmix not defined
- div() and rem()
- Consider replacing SpecialFunctions.jl by pure Julia alternatives HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dualnumbers.jl.