JuliaDiff / JuliaDiff/ForwardDiff.jl
one(::type(Dual))
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 1k
- Forks
- 160
- PR merge metrics
- No merged PRs in 30d
Description
Currently, one returns a Dual number. It seems like it could simply return the underlying value type instead. i.e. change these lines to
Base.one(d::Dual) = one(typeof(d))
Base.oneunit(d::Dual) = oneunit(typeof(d))
Base.one(::Type{Dual{T,V,N}}) where {T,V,N} = one(V)
Base.oneunit(::Type{Dual{T,V,N}}) where {T,V,N} = Dual{T}(oneunit(V), zero(Partials{N,V}))
which is still correct since one(V) is still a multiplicative identity for Dual, and type promotion takes care of the rest. In general, one(x) in Julia need not return the same type as x — if you want the same type, you use oneunit, which is why I added a method for Dual (since they otherwise fall back to calling one).
The advantage of this is that code that is designed to "strip the units" from numbers will then work with Dual numbers as well. For example, this code in QuadGK.jl will work, and you will suddenly be able to differentiate numerical integrals:
julia> ForwardDiff.derivative(x -> quadgk(y -> y^2, 0, x)[1], 4)
16.0
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/dual.jl at the Base.one and Base.oneunit methods around lines 356-357. Check the linked QuadGK example and verify that differentiating the shown numerical integral works while the Dual identity behavior remains correct; the issue is done when those calls produce the expected derivative.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100