Skip to content

Add the relstep and absstep parameters to AutoFiniteDiff #102

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

Merged
merged 7 commits into from
Jan 21, 2025
Merged
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ADTypes"
uuid = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
authors = ["Vaibhav Dixit <[email protected]>, Guillaume Dalle and contributors"]
version = "1.11.0"
version = "1.12.0"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
12 changes: 10 additions & 2 deletions src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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

Expand Down
6 changes: 5 additions & 1 deletion test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading