JuliaDiff / JuliaDiff/ForwardDiff.jl

one(::type(Dual))

Open
#624 1 comment 0 reactions 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.