Skip to content

use oneunit(T) in construct_seeds #750

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
DiffTests = "de460e47-3fe3-5279-bb4a-814414816d5d"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6"
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", "IrrationalConstants", "SparseArrays", "StaticArrays", "Test", "InteractiveUtils"]
test = ["Calculus", "DiffTests", "IrrationalConstants", "Measurements", "SparseArrays", "StaticArrays", "Test", "InteractiveUtils"]
3 changes: 3 additions & 0 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,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{T}) where {T} = Dual{T}(oneunit(value(d)), zero(partials(d)))
@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
6 changes: 1 addition & 5 deletions test/AllocationsTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ convert_test_574() = convert(ForwardDiff.Dual{Nothing,ForwardDiff.Dual{Nothing,F
index = 1
alloc = @allocated ForwardDiff.seed!(duals, x, index, seed)
alloc = @allocated ForwardDiff.seed!(duals, x, index, seed)
if VERSION < v"1.9" || VERSION >= v"1.11"
@test alloc == 0
else
@test_broken alloc == 0
end
@test alloc == 0

alloc = @allocated convert_test_574()
alloc = @allocated convert_test_574()
Expand Down
5 changes: 5 additions & 0 deletions test/MiscTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using SparseArrays: sparse
using StaticArrays
using IrrationalConstants
using LinearAlgebra
using Measurements: Measurements

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

Expand Down Expand Up @@ -219,4 +220,8 @@ end
@test B_mmatrix == B
end

#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