Skip to content

Commit c9cd542

Browse files
authored
Merge pull request #65 from JuliaDiff/ox/accurate_coeffs
Compute coeffs using Rationals
2 parents 86f6207 + 7eed57f commit c9cd542

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FiniteDifferences"
22
uuid = "26cc04aa-876d-5657-8c51-4c34ba976000"
3-
version = "0.9.3"
3+
version = "0.9.4"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/methods.jl

+5-3
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,12 @@ end
157157

158158
# Compute coefficients for the method
159159
function _coefs(grid::AbstractVector{<:Real}, p::Integer, q::Integer)
160-
C = [g^i for i in 0:(p - 1), g in grid]
161-
x = zeros(Int, p)
160+
# For high precision on the \ we use Rational, and to prevent overflows we use Int128
161+
# At the end we go to Float64 for fast floating point math (rather than rational math)
162+
C = [Rational{Int128}(g^i) for i in 0:(p - 1), g in grid]
163+
x = zeros(Rational{Int128}, p)
162164
x[q + 1] = factorial(q)
163-
return C \ x
165+
return Float64.(C \ x)
164166
end
165167

166168
# Estimate the bound on the function value and its derivatives at a point

test/methods.jl

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ using FiniteDifferences: Forward, Backward, Central, Nonstandard
3737
@test central_fdm(5, 1)(abs, 0.001) 1.0
3838
end
3939

40+
@testset "Accuracy at high orders, with high adapt" begin
41+
# Regression test against issues with precision during computation of _coeffs
42+
# see https://github.com/JuliaDiff/FiniteDifferences.jl/issues/64
43+
44+
@test fdm(central_fdm(9, 5), exp, 1.0, adapt=4) exp(1) atol=1e-7
45+
46+
poly(x) = 4x^3 + 3x^2 + 2x + 1
47+
@test fdm(central_fdm(9, 3), poly, 1.0, adapt=4) 24 atol=1e-11
48+
end
49+
50+
4051
@testset "Printing FiniteDifferenceMethods" begin
4152
@test sprint(show, central_fdm(2, 1)) == """
4253
FiniteDifferenceMethod:

0 commit comments

Comments
 (0)