From 81a120bda50a2c787adf9c5f7e04050fd1b7fbfb Mon Sep 17 00:00:00 2001 From: Akshay Sridhar Date: Wed, 17 Aug 2022 09:41:24 -0700 Subject: [PATCH] Add RelativeSolutionTolerance option + up tests, docs modified: src/RootSolvers.jl modified: test/test_helper.jl modified: docs/src/API.md + Bump minor version -> 0.3.4 modified: Project.toml --- Project.toml | 2 +- docs/src/API.md | 1 + src/RootSolvers.jl | 18 +++++++++++++++++- test/test_helper.jl | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index b82e24b..1226b86 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "RootSolvers" uuid = "7181ea78-2dcb-4de3-ab41-2b8ab5a31e74" authors = ["CliMA Contributors "] -version = "0.3.3" +version = "0.3.4" [deps] DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" diff --git a/docs/src/API.md b/docs/src/API.md index 4c1810a..6d8b753 100644 --- a/docs/src/API.md +++ b/docs/src/API.md @@ -34,6 +34,7 @@ CompactSolutionResults ```@docs ResidualTolerance SolutionTolerance +RelativeSolutionTolerance ``` ## Internal helper methods diff --git a/src/RootSolvers.jl b/src/RootSolvers.jl index b789097..8841a83 100644 --- a/src/RootSolvers.jl +++ b/src/RootSolvers.jl @@ -26,7 +26,7 @@ using DocStringExtensions: FIELDS export find_zero, SecantMethod, RegulaFalsiMethod, NewtonsMethodAD, NewtonsMethod export CompactSolution, VerboseSolution -export AbstractTolerance, ResidualTolerance, SolutionTolerance +export AbstractTolerance, ResidualTolerance, SolutionTolerance, RelativeSolutionTolerance import ForwardDiff @@ -199,6 +199,22 @@ Evaluates solution tolerance, based on ``|x2-x1|`` """ (tol::SolutionTolerance)(x1, x2, y) = abs(x2 - x1) < tol.tol +""" + RelativeSolutionTolerance + +A tolerance type based on consecutive iterations of solution ``x`` of the equation ``f(x) = 0`` +""" +struct RelativeSolutionTolerance{FT} <: AbstractTolerance{FT} + tol::FT +end + +""" + (tol::RelativeSolutionTolerance)(x1, x2, y) + +Evaluates solution tolerance, based on ``|(x2-x1)/x1|`` +""" +(tol::RelativeSolutionTolerance)(x1, x2, y) = abs((x2 - x1)/x1) < tol.tol + # TODO: CuArrays.jl has trouble with isapprox on 1.1 # we use simple checks for now, will switch to relative checks later. diff --git a/test/test_helper.jl b/test/test_helper.jl index c762d45..738ca58 100644 --- a/test/test_helper.jl +++ b/test/test_helper.jl @@ -31,7 +31,7 @@ end problem_size() = 5 problem_size(::RootSolvingProblem{S,F,F′,FT,FTA,N}) where {S,F,F′,FT,FTA,N} = N float_types() = [Float32, Float64] -get_tolerances(FT) = [ResidualTolerance{FT}(1e-6), SolutionTolerance{FT}(1e-3), nothing] +get_tolerances(FT) = [ResidualTolerance{FT}(1e-6), SolutionTolerance{FT}(1e-3), RelativeSolutionTolerance{FT}(sqrt(eps(FT))), nothing] test_verbose!(::CompactSolution, sol, problem, tol, converged) = nothing function test_verbose!(::VerboseSolution, sol, problem, tol, converged)