Skip to content

Commit ccb9c75

Browse files
authored
Merge pull request #73 from JuliaDiff/ox/memoize_coeffs
Memoize Coeffs
2 parents 5366d31 + c0b67f1 commit ccb9c75

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
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.5"
3+
version = "0.9.6"
44

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

src/methods.jl

+11-6
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,20 @@ function _check_p_q(p::Integer, q::Integer)
156156
end
157157

158158
# Compute coefficients for the method
159+
160+
const _COEFFS_CACHE = Dict{Tuple{AbstractVector{<:Real}, Integer, Integer}, Vector{Float64}}()
159161
function _coefs(grid::AbstractVector{<:Real}, p::Integer, q::Integer)
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)
164-
x[q + 1] = factorial(q)
165-
return Float64.(C \ x)
162+
return get!(_COEFFS_CACHE, (grid, p, q)) do
163+
# For high precision on the \ we use Rational, and to prevent overflows we use Int128
164+
# At the end we go to Float64 for fast floating point math (rather than rational math)
165+
C = [Rational{Int128}(g^i) for i in 0:(p - 1), g in grid]
166+
x = zeros(Rational{Int128}, p)
167+
x[q + 1] = factorial(q)
168+
return Float64.(C \ x)
169+
end
166170
end
167171

172+
168173
# Estimate the bound on the function value and its derivatives at a point
169174
_estimate_bound(x, cond) = cond * maximum(abs, x) + TINY
170175

0 commit comments

Comments
 (0)