From 2aafe2060f7702383458e77d38ec4dc19f0cd5c5 Mon Sep 17 00:00:00 2001 From: Bart van de Lint <33758964+1-Bart-1@users.noreply.github.com> Date: Tue, 21 Jan 2025 15:01:07 +0100 Subject: [PATCH] Add the relstep and absstep parameters to AutoFiniteDiff (#102) * add relstep and absstep to AutoFiniteDiff * add relstep and absstep documentation * add tests for relstep and absstep * Fix typo in doc Co-authored-by: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> * improve step test Co-authored-by: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> * improve step test Co-authored-by: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> * update minor version --------- Co-authored-by: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> --- Project.toml | 2 +- src/dense.jl | 12 ++++++++++-- test/dense.jl | 6 +++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Project.toml b/Project.toml index b8cd5cf..b65a756 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ADTypes" uuid = "47edcb42-4c32-4615-8424-f2b9edc5f35b" authors = ["Vaibhav Dixit , Guillaume Dalle and contributors"] -version = "1.11.0" +version = "1.12.0" [deps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" diff --git a/src/dense.jl b/src/dense.jl index 9359726..bbc327e 100644 --- a/src/dense.jl +++ b/src/dense.jl @@ -106,18 +106,22 @@ Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl). # Constructors - AutoFiniteDiff(; fdtype=Val(:forward), fdjtype=fdtype, fdhtype=Val(:hcentral)) + AutoFiniteDiff(; fdtype=Val(:forward), fdjtype=fdtype, fdhtype=Val(:hcentral), relstep=nothing, absstep=nothing) # Fields - `fdtype::T1`: finite difference type - `fdjtype::T2`: finite difference type for the Jacobian - `fdhtype::T3`: finite difference type for the Hessian + - `relstep`: relative finite difference step size + - `absstep`: absolute finite difference step size """ Base.@kwdef struct AutoFiniteDiff{T1, T2, T3} <: AbstractADType fdtype::T1 = Val(:forward) fdjtype::T2 = fdtype fdhtype::T3 = Val(:hcentral) + relstep = nothing + absstep = nothing end mode(::AutoFiniteDiff) = ForwardMode() @@ -129,7 +133,11 @@ function Base.show(io::IO, backend::AutoFiniteDiff) backend.fdjtype != backend.fdtype && print(io, "fdjtype=", repr(backend.fdjtype; context = io), ", ") backend.fdhtype != Val(:hcentral) && - print(io, "fdhtype=", repr(backend.fdhtype; context = io)) + print(io, "fdhtype=", repr(backend.fdhtype; context = io), ", ") + !isnothing(backend.relstep) && + print(io, "relstep=", repr(backend.relstep; context = io), ", ") + !isnothing(backend.absstep) && + print(io, "absstep=", repr(backend.absstep; context = io)) print(io, ")") end diff --git a/test/dense.jl b/test/dense.jl index f724b27..1f1bce9 100644 --- a/test/dense.jl +++ b/test/dense.jl @@ -67,14 +67,18 @@ end @test ad.fdtype === Val(:forward) @test ad.fdjtype === Val(:forward) @test ad.fdhtype === Val(:hcentral) + @test ad.relstep === nothing + @test ad.absstep === nothing - ad = AutoFiniteDiff(; fdtype = Val(:central), fdjtype = Val(:forward)) + ad = AutoFiniteDiff(; fdtype = Val(:central), fdjtype = Val(:forward), relstep = 1e-3, absstep = 1e-4) @test ad isa AbstractADType @test ad isa AutoFiniteDiff @test mode(ad) isa ForwardMode @test ad.fdtype === Val(:central) @test ad.fdjtype === Val(:forward) @test ad.fdhtype === Val(:hcentral) + @test ad.relstep == 1e-3 + @test ad.absstep == 1e-4 end @testset "AutoFiniteDifferences" begin