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

use oneunit(T) in construct_seeds #659

Open
wants to merge 6 commits into
base: release-0.10
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ForwardDiff"
uuid = "f6369f11-7733-5829-9624-2563aa707210"
version = "0.10.36"
version = "0.10.37"

[deps]
CommonSubexpressions = "bbf7d656-a473-5ed7-a52c-81e309532950"
Expand Down Expand Up @@ -35,12 +35,13 @@ ForwardDiffStaticArraysExt = "StaticArrays"
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
DiffTests = "de460e47-3fe3-5279-bb4a-814414816d5d"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Measurements = "eff96d63-e80a-5855-80a2-b1b0885c5ab7"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Calculus", "DiffTests", "SparseArrays", "Test", "InteractiveUtils", "StaticArrays"]
test = ["Calculus", "DiffTests", "SparseArrays", "Test", "InteractiveUtils", "StaticArrays", "Measurements"]

[weakdeps]
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
3 changes: 3 additions & 0 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ end
@inline Base.one(d::Dual) = one(typeof(d))
@inline Base.one(::Type{Dual{T,V,N}}) where {T,V,N} = Dual{T}(one(V), zero(Partials{N,V}))

@inline Base.oneunit(d::Dual) = oneunit(typeof(d))
devmotion marked this conversation as resolved.
Show resolved Hide resolved
@inline Base.oneunit(::Type{Dual{T,V,N}}) where {T,V,N} = Dual{T}(oneunit(V), zero(Partials{N,V}))

@inline function Base.Int(d::Dual)
all(iszero, partials(d)) || throw(InexactError(:Int, Int, d))
Int(value(d))
Expand Down
2 changes: 1 addition & 1 deletion src/partials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ end
##############################

@generated function single_seed(::Type{Partials{N,V}}, ::Val{i}) where {N,V,i}
ex = Expr(:tuple, [ifelse(i === j, :(one(V)), :(zero(V))) for j in 1:N]...)
ex = Expr(:tuple, [ifelse(i === j, :(oneunit(V)), :(zero(V))) for j in 1:N]...)
return :(Partials($(ex)))
end

Expand Down
5 changes: 5 additions & 0 deletions test/MiscTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Test
using ForwardDiff
using DiffTests
using SparseArrays: sparse
using Measurements: Measurements

include(joinpath(dirname(@__FILE__), "utils.jl"))

Expand Down Expand Up @@ -158,4 +159,8 @@ end
@test ForwardDiff.derivative(x -> rem2pi(x, RoundUp), rand()) == 1
@test ForwardDiff.derivative(x -> rem2pi(x, RoundDown), rand()) == 1

#issue 651, using Measurements
f651(x) = 2.1*x + 1
@test ForwardDiff.derivative(f651,Measurements.measurement(1.0, 0.001)) == 2.1

end # module
Loading