From 9afe916a6635e2427e3e789faff5fb5801d0c04c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Riedemann?= <38795484+longemen3000@users.noreply.github.com> Date: Tue, 13 May 2025 22:38:37 -0400 Subject: [PATCH 1/2] use `oneunit(T)` in `construct_seeds` --- Project.toml | 3 ++- src/dual.jl | 3 +++ src/partials.jl | 2 +- test/MiscTest.jl | 5 +++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index e43900af..14bd787b 100644 --- a/Project.toml +++ b/Project.toml @@ -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"] diff --git a/src/dual.jl b/src/dual.jl index 7e8ec110..6a3c9344 100644 --- a/src/dual.jl +++ b/src/dual.jl @@ -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)) diff --git a/src/partials.jl b/src/partials.jl index a5316e3e..ddec4ebc 100644 --- a/src/partials.jl +++ b/src/partials.jl @@ -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 diff --git a/test/MiscTest.jl b/test/MiscTest.jl index e7e0d91a..df26a8cc 100644 --- a/test/MiscTest.jl +++ b/test/MiscTest.jl @@ -9,6 +9,7 @@ using SparseArrays: sparse using StaticArrays using IrrationalConstants using LinearAlgebra +using Measurements: Measurements include(joinpath(dirname(@__FILE__), "utils.jl")) @@ -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 From 13ceac8bb2ba047fc2efb209c577ac1cbcba66f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Riedemann?= <38795484+longemen3000@users.noreply.github.com> Date: Tue, 13 May 2025 22:59:59 -0400 Subject: [PATCH 2/2] remove test_broken that now pass --- test/AllocationsTest.jl | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/AllocationsTest.jl b/test/AllocationsTest.jl index ad1832d0..42f694c3 100644 --- a/test/AllocationsTest.jl +++ b/test/AllocationsTest.jl @@ -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()