Skip to content

Commit 971070c

Browse files
authored
fix(Unitful): find_zero on <:Quantity interval chooses Bisection method (#475)
* fix(Unitful): `find_zero` on `<:Quantity` interval chooses `Bisection` method * fix(test): Test with `isapprox` instead of equivalence for round-off errors
1 parent 3040289 commit 971070c

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1414
IntervalRootFinding = "d2bf35a9-74e0-55ec-b149-d360ff49b807"
1515
SymPy = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6"
1616
SymPyPythonCall = "bc8888f7-b21e-4b7c-a06a-5d9c9496438c"
17+
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
1718

1819
[extensions]
1920
RootsChainRulesCoreExt = "ChainRulesCore"
2021
RootsForwardDiffExt = "ForwardDiff"
2122
RootsIntervalRootFindingExt = "IntervalRootFinding"
2223
RootsSymPyExt = "SymPy"
2324
RootsSymPyPythonCallExt = "SymPyPythonCall"
25+
RootsUnitfulExt = "Unitful"
2426

2527
[compat]
2628
Accessors = "0.1.33"

ext/RootsUnitfulExt.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module RootsUnitfulExt
2+
using Unitful
3+
using Roots
4+
5+
function Roots.find_zero_default_method(x0::Tuple{<:Quantity,<:Quantity})
6+
T = eltype(ustrip.(float.(Roots._extrema(x0))))
7+
T <: Union{Float16,Float32,Float64} ? Roots.Bisection() : Roots.A42()
8+
end
9+
10+
end # module

test/test_extensions.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ using ForwardDiff
7070
end
7171
end
7272

73+
using Unitful
74+
@testset "Unitful" begin
75+
f = x -> -exp(ustrip(x)) + 3
76+
@test find_zero(f, (0u"nm", Inf * 1u"nm"))
77+
find_zero(x -> f(x * 1u"nm"), (0, Inf)) * 1u"nm"
78+
end
79+
7380
#=
7481
using IntervalRootFinding
7582
@testset "IntervalRootFinding" begin

0 commit comments

Comments
 (0)