ProjectTorreyPines / ProjectTorreyPines/FastInterpolations.jl

Dispatch error in `cubic_interp!` for N-D interpolation when N = 1

Open Beginner friendly
#204 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
68
Forks
6
PR merge metrics
No merged PRs in 30d

Description

Hi,

I noticed a dispatch error in cubic_interp! in some N-dimensional code where I set N=1. Here is a mwe:

using FastInterpolations
ndim = 1
nquery = 7
ndata = 5
output = ones(nquery)
grids = ntuple(n -> range(0, 1, length=ndata), ndim)
data = [sum(sin, x) for x in Iterators.product(ntuple(n -> range(0, 1, length=ndata), ndim)...)]
query = [ntuple(n -> y, ndim) for y in range(0, 1, length=nquery)]
cubic_interp!(output, grids, data, query)

While this code runs perfectly fine with ndim=2, the full stacktrace for ndim=1 is

ERROR: MethodError: no method matching cubic_interp!(::Vector{…}, ::StepRangeLen{…}, ::Vector{…}, ::Vector{…})
The function `cubic_interp!` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  cubic_interp!(::AbstractArray, ::AbstractVector{Tg}, ::AbstractVector{Tv}, ::AbstractArray{Tq}; bc, extrap, autocache, deriv, search, hint) where {Tg<:Number, Tv, Tq<:Number}
   @ FastInterpolations ~/.julia/packages/FastInterpolations/me4ge/src/cubic/cubic_oneshot.jl:239
  cubic_interp!(::AbstractArray, ::Tuple{AbstractVector}, ::AbstractVector, ::AbstractArray; coeffs, kwargs...)
   @ FastInterpolations ~/.julia/packages/FastInterpolations/me4ge/src/cubic/nd/cubic_nd_interpolant.jl:178
  cubic_interp!(::AbstractArray, ::NTuple{N, AbstractVector}, ::AbstractArray{<:Any, N}, ::Any; kwargs...) where N
   @ FastInterpolations ~/.julia/packages/FastInterpolations/me4ge/src/cubic/nd/cubic_nd_oneshot.jl:277
  ...

Stacktrace:
 [1] #cubic_interp!#519
   @ ~/.julia/packages/FastInterpolations/me4ge/src/cubic/nd/cubic_nd_interpolant.jl:180 [inlined]
 [2] cubic_interp!(output::Vector{Float64}, grids::Tuple{StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}, Int64}}, data::Vector{Float64}, q::Vector{Tuple{Float64}})
   @ FastInterpolations ~/.julia/packages/FastInterpolations/me4ge/src/cubic/nd/cubic_nd_interpolant.jl:178
 [3] top-level scope
   @ REPL[73]:1
Some type information was truncated. Use `show(err)` to see complete types.

I was using FastInterpolations v0.4.18.

Taking a look at the first method in the stacktrace, it unwraps the grids tuple using only, and the MethodError seems to be caused by not matching the element type of the query points as Numbers due to the fact they were one-element tuples. It seems like applying map(only, queries) would solve the issue, but create unwanted allocations. Perhaps there is a better solution already within the package? Thanks

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the N-dimensional dispatch in src/cubic/nd/cubic_nd_interpolant.jl:178 and compare it with the one-dimensional candidate in src/cubic/cubic_oneshot.jl:239. Reproduce the issue with the provided N=1 MWE, then verify that cubic_interp! dispatches successfully without introducing unwanted allocations and that the existing N=2 case still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
performance
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.