Skip to content

one(::type(Dual)) #624

Open
Open
@stevengj

Description

@stevengj

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions