Skip to content

Commit 900ae01

Browse files
committed
Memoize Coeffs
1 parent 5366d31 commit 900ae01

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/methods.jl

Lines changed: 11 additions & 6 deletions
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)