Skip to content
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

one(::type(Dual)) #624

Open
stevengj opened this issue Feb 14, 2023 · 1 comment · May be fixed by #664
Open

one(::type(Dual)) #624

stevengj opened this issue Feb 14, 2023 · 1 comment · May be fixed by #664

Comments

@stevengj
Copy link
Contributor

stevengj commented Feb 14, 2023

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
@longemen3000
Copy link

longemen3000 commented Jun 8, 2023

Not completely related, #651 , but facing the same one/oneunit distinción problem

@stevengj stevengj linked a pull request Sep 18, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants