Description
One of the issues with Grid.jl was that it's hard to specify what type the interpolation object should return - Interpolations.jl should be able to do better if we want it to be easily usable with any number type.
I'm leaning toward an interface with an optional first type argument, that specifies the output type, similar to what round
and friends do in 0.4:
julia> itp = Interpolation(Float64, 1:10, Linear(OnGrid()), ExtrapNaN())
julia> typeof(itp[3.2])
Float64
Of course, we need to provide some sensible default, and there are a couple of options:
-
Use the element type of the input array, i.e.
Interpolation(A, config...) = Interpolation(eltype(A), A, config...)
Pros:- very predictable for the end user; effectively,
eltype(Interpolation(A,...)) == eltype(A)
Cons:
- will have the same problems as Grid.jl, especially for integer inputs (but these are avoided if the user explicitly sets a type)
- we have to
convert
each return value, so there must be a conversion defined from whatever our calculations promote to, to the return type.
- very predictable for the end user; effectively,
-
Use use some sensible promotion result, e.g.
promote_type(eltype(A), Float64, typeof(x)...)
(where theFloat64
is from the literals in the prefiltering step - we might be able to avoid those, if we can require aconvert
method for converting floats toeltype(A)
)Pros:
- very similar to what we do today
- won't require anything extra from the user to work with integers, e.g.
itp = Interpolation(1:10, ...)
Cons:
- might be difficult to get it right, since there are lots of places where numbers enter the calculations
- might seem unpredictable to the user, especially since e.g.
promote_type(Rational{Int}, Float64) == Float64
will make us return floats instead of rationals forInterpolation(A::Array{T<:Rational}, ...)
I honestly don't know what's best here, but before we start implementing this, we need some sort of decisions on:
- What should the API look like? (Is the above suggestion good or ugly?)
- How should we choose a default?