From b0259dc9726c5bb8bc87ab240b0d14c42b5bc032 Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Sat, 28 Mar 2020 14:20:10 +0000 Subject: [PATCH 1/4] Compute coeffs using Rationals --- src/methods.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/methods.jl b/src/methods.jl index 20b56ea9..1400134f 100644 --- a/src/methods.jl +++ b/src/methods.jl @@ -157,10 +157,12 @@ end # Compute coefficients for the method function _coefs(grid::AbstractVector{<:Real}, p::Integer, q::Integer) - C = [g^i for i in 0:(p - 1), g in grid] - x = zeros(Int, p) + # For high precision on the \ we use Rational, and to prevent overfloats we use Int128 + # At the end we go to Float64 for fast floating point math (rather than rational math) + C = [Rational{Int128}(g^i) for i in 0:(p - 1), g in grid] + x = zeros(Rational{Int128}, p) x[q + 1] = factorial(q) - return C \ x + return Float64.(C \ x) end # Estimate the bound on the function value and its derivatives at a point From d6e9ddbab645a90aaa38dd39333f598ba07dc32c Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Sat, 4 Apr 2020 22:23:19 +0100 Subject: [PATCH 2/4] add tests of accuracy at high orders --- test/methods.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/methods.jl b/test/methods.jl index 4b46d33e..fe886626 100644 --- a/test/methods.jl +++ b/test/methods.jl @@ -37,6 +37,17 @@ using FiniteDifferences: Forward, Backward, Central, Nonstandard @test central_fdm(5, 1)(abs, 0.001) ≈ 1.0 end + @testset "Accuracy at high orders, with high adapt" begin + # Regression test against issues with precision during computation of _coeffs + # see https://github.com/JuliaDiff/FiniteDifferences.jl/issues/64 + + @test fdm(central_fdm(9, 5), exp, 1.0, adapt=4) ≈ exp(1) atol=1e-7 + + poly(x) = 4x^3 + 3x^2 + 2x + 1 + @test fdm(central_fdm(9, 3), poly, 1.0, adapt=4) ≈ 24 atol=1e-11 + end + + @testset "Printing FiniteDifferenceMethods" begin @test sprint(show, central_fdm(2, 1)) == """ FiniteDifferenceMethod: From 2a92c613776250bc82befccb18030fe1117e14ee Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Mon, 6 Apr 2020 13:02:07 +0100 Subject: [PATCH 3/4] Update src/methods.jl Co-Authored-By: willtebbutt --- src/methods.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/methods.jl b/src/methods.jl index 1400134f..41485561 100644 --- a/src/methods.jl +++ b/src/methods.jl @@ -157,7 +157,7 @@ end # Compute coefficients for the method function _coefs(grid::AbstractVector{<:Real}, p::Integer, q::Integer) - # For high precision on the \ we use Rational, and to prevent overfloats we use Int128 + # For high precision on the \ we use Rational, and to prevent overflows we use Int128 # At the end we go to Float64 for fast floating point math (rather than rational math) C = [Rational{Int128}(g^i) for i in 0:(p - 1), g in grid] x = zeros(Rational{Int128}, p) From 7eed57f280d0f6da96d19e22ba4945f4dc530d0d Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Mon, 6 Apr 2020 13:02:47 +0100 Subject: [PATCH 4/4] update version number --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 7388759d..5b1df0e1 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "FiniteDifferences" uuid = "26cc04aa-876d-5657-8c51-4c34ba976000" -version = "0.9.3" +version = "0.9.4" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"