Skip to content

Commit 616cfe2

Browse files
committed
avoid recursion in extrapolate
1 parent 41dc041 commit 616cfe2

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Richardson.jl

+8-2
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,15 @@ function extrapolate(f, h_::Number; contract::Number=oftype(float(real(h_)), 0.1
7575
atol::Real=0, rtol::Real = atol > zero(atol) ? zero(one(float(real(x0+h_)))) : sqrt(eps(typeof(one(float(real(x0+h_)))))),
7676
maxeval::Integer=typemax(Int), breaktol::Real=2)
7777
if isinf(x0)
78-
# use a change of variables x = 1/u
79-
return extrapolate(u -> f(inv(u)), inv(h_); rtol=rtol, atol=atol, maxeval=maxeval, contract = abs(contract) > 1 ? inv(contract) : contract, x0=inv(x0), power=power)
78+
# use a change of variables x = 1/u
79+
contract = abs(contract) > 1 ? inv(contract) : contract
80+
_extrapolate(u -> f(inv(u)), inv(h_), contract, inv(x0), power, atol, rtol, maxeval, breaktol)
81+
else
82+
_extrapolate(f, h_, contract, x0, power, atol, rtol, maxeval, breaktol)
8083
end
84+
end
85+
86+
function _extrapolate(f, h_::Number, contract, x0, power, atol, rtol, maxeval, breaktol)
8187
(rtol 0 && atol zero(atol)) || throw(ArgumentError("rtol and atol must be nonnegative"))
8288
breaktol > 0 || throw(ArgumentError("breaktol must be positive"))
8389
0 < abs(contract) < 1 || throw(ArgumentError("contract must be in (0,1)"))

test/runtests.jl

+6
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,9 @@ end
127127
@test_throws ArgumentError extrapolate!([(sin(h)/h,h) for h in [1, 0.8, 1.3]])
128128
@test_throws ArgumentError extrapolate!([(sin(h)/h,h) for h in [1, 0.8, 0.8]])
129129
end
130+
131+
132+
@testset "type stability" begin
133+
@inferred extrapolate(x -> sin(x)/x, 1)
134+
@inferred extrapolate(x -> sin(x)/x, 1.0)
135+
end

0 commit comments

Comments
 (0)