Skip to content

Commit

Permalink
Add the relstep and absstep parameters to AutoFiniteDiff (#102)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* improve step test

Co-authored-by: Guillaume Dalle <[email protected]>

* improve step test

Co-authored-by: Guillaume Dalle <[email protected]>

* update minor version

---------

Co-authored-by: Guillaume Dalle <[email protected]>
  • Loading branch information
1-Bart-1 and gdalle authored Jan 21, 2025
1 parent e3e2d1a commit 2aafe20
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
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

2 comments on commit 2aafe20

@gdalle
Copy link
Collaborator

@gdalle gdalle commented on 2aafe20 Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/123408

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.12.0 -m "<description of version>" 2aafe2060f7702383458e77d38ec4dc19f0cd5c5
git push origin v1.12.0

Please sign in to comment.