Interpolations
The package Interpolations.jl implements a variety of interpolation schemes for the Julia language. It has the goals of ease-of-use, broad algorithmic support, and exceptional performance.
Currently this package supports B-splines and irregular grids. The API has been designed with intent to support more options. Pull-requests are more than welcome! It should be noted that the API may continue to evolve over time.
There are many other interpolation packages implemented in Julia. For a listing, see Other Interpolation Packages.
Some of these packages support methods that Interpolations
does not, so if you can't find what you need here, check one of them or submit a pull request here.
Installation
Interpolations.jl can be installed via the following invocation since it is a registered Julia package.
julia> using Pkg
julia> Pkg.add("Interpolations")
Example Usage
Create a grid xs
and an array A
of values to be interpolated
using Interpolations
xs = 1:0.2:5
A = log.(xs)
Create linear interpolation object without extrapolation
julia> interp_linear = linear_interpolation(xs, A);
julia> interp_linear(3) # exactly log(3)
1.0986122886681098
julia> interp_linear(3.1) # approximately log(3.1)
1.1308815492368953
julia> interp_linear(0.9) # outside grid: error
ERROR: BoundsError: attempt to access 21-element extrapolate(scale(interpolate(::Vector{Float64}, BSpline(Linear())), (1.0:0.2:5.0,)), Throw()) with element type Float64 at index [0.9]
Create linear interpolation object with extrapolation
julia> interp_linear_extrap = linear_interpolation(xs, A, extrapolation_bc=Line());
julia> interp_linear_extrap(0.9) # outside grid: linear extrapolation
-0.09116077839697727
Performant Example Usage
The above use of LinearInterpolation
is actually a short hand for a composition of interpolate
, scale
, and extrapolate
. You may not need all of the the scaling and extrapolation features.
interp_linear = extrapolate(scale(interpolate(A, BSpline(Linear())), xs))
If we know we do not need the extrapolation portion, we can use the following.
scaled_itp = scale(interpolate(A, BSpline(Linear())), xs)
We can also remove the scaling for further performance if integer valued knots and regular grids are sufficient.
itp = interpolate(A, BSpline(Linear()))
Removing the scaling or extrapolation will help accelerate interpolation by removing unneeded operations and branches. This can permit the use of advanced processor Single Instruction/Multiple Data (SIMD) capabilities.
Regular Grids
Interpolations.jl is optimized for use with regular grids with uniform spacing. The highest performance is achieved when the knots are an AbstractUnitRange
such as 2:5
or Base.OneTo(9)
. The default case if no knots are specified is to assign the knots as a UnitRange
starting at 1
.
Scaling
If the knots are not unit spaced or start at a distinct value other than 1
, then the scale
function can be used. While this increases the flexibility of the interpolation, some performance penalty is acquired. See Scaled BSplines for further information.
Irregular Grids
If the knots are irregularly spaced, then the ranges between knots will have to be scaled as in the Gridded
interpolation type. See Gridded interpolation for additional details.
Points outside the knots
For points not between knots, extrapolation can be used. This introduces a branch into the code that checks whether the point to be queried is inside or outside of the knots. This branch can inhibit the use of vectorized SIMD computation, resulting in a reduction of performance. See Extrapolation.