### 1D Linear Interpolation Setup Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/convenience-construction.md Set up input and output arrays for 1D linear interpolation. Requires the Interpolations package. ```julia using Interpolations # hide f(x) = log(x) xs = 1:0.2:5 A = [f(x) for x in xs] nothing # hide ``` -------------------------------- ### Install Interpolations.jl Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/index.md Install the Interpolations.jl package using the Julia package manager. ```julia using Pkg Pkg.add("Interpolations") ``` -------------------------------- ### Basic Extrapolation Examples Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/extrapolation.md Demonstrates using `extrapolate` with `Flat()` for edge values and a constant `0` for out-of-bounds evaluation. ```julia itp = interpolate(1:7, BSpline(Linear())) etpf = extrapolate(itp, Flat()) # gives 1 on the left edge and 7 on the right edge etp0 = extrapolate(itp, 0) # gives 0 everywhere outside [1,7] ``` -------------------------------- ### Extrapolation Setup with Linear Boundary Conditions Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/convenience-construction.md Set up linear interpolation with linear boundary conditions for extrapolation. Requires the Interpolations package. ```julia using Interpolations # hide f(x) = log(x) xs = 1:0.2:5 A = [f(x) for x in xs] nothing # hide ``` ```julia # extrapolation with linear boundary conditions extrap = linear_interpolation(xs, A, extrapolation_bc = Line()) nothing # hide ``` -------------------------------- ### Multidimensional Linear Interpolation Setup Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/convenience-construction.md Set up multidimensional input and output arrays for linear interpolation. Requires the Interpolations package. ```julia using Interpolations # hide f(x,y) = log(x+y) xs = 1:0.2:5 ys = 2:0.1:5 A = [f(x,y) for x in xs, y in ys] nothing # hide ``` -------------------------------- ### Periodic Extrapolation Setup Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/extrapolation.md Sets up various B-Spline interpolations with periodic boundary conditions for uniformly sampled periodic data. ```julia f(x) = sin((x-3)*2pi/7 - 1) A = Float64[f(x) for x in 1:7] # Does not include the periodic image # Constant(Periodic())) is an alias for Constant{Nearest}(Periodic(OnCell())) itp0 = interpolate(A, BSpline(Constant(Periodic()))) # Linear(Periodic())) is an alias for Linear(Periodic(OnCell())) itp1 = interpolate(A, BSpline(Linear(Periodic()))) itp2 = interpolate(A, BSpline(Quadratic(Periodic(OnCell())))) itp3 = interpolate(A, BSpline(Cubic(Periodic(OnCell())))) ``` -------------------------------- ### Iterating Knots Greater Than a Start Value Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/iterate.md Demonstrates using `knotsbetween` to iterate over knots greater than a specified start value. This is achieved by omitting the `stop` argument. ```julia using Interpolations itp = interpolate(rand(4), options...) krange = knotsbetween(itp; start=1.2, stop=3.0) collect(kiter) # Array of knots between 1.2 and 3.0 ``` ```jldoctest julia> x = [1.0, 1.5, 1.75, 2.0] julia> etp = linear_interpolation(x, x.^2, extrapolation_bc=Periodic()) julia> krange = knotsbetween(etp; start=4.0) julia> Iterators.take(krange, 5) |> collect 5-element Vector{Float64}: 4.5 4.75 5.0 5.5 5.75 ``` -------------------------------- ### Extrapolation Setup with Fill Value Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/convenience-construction.md Set up linear interpolation with a fill value for extrapolation. Any out-of-range values will return NaN. Requires the Interpolations package. ```julia extrap = linear_interpolation(xs, A, extrapolation_bc = NaN) nothing # hide ``` -------------------------------- ### Irregular Grid Linear Interpolation Setup Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/convenience-construction.md Set up input and output arrays for linear interpolation on an irregular grid. Note that only constant and linear interpolation support irregular grids currently. Requires the Interpolations package. ```julia using Interpolations # hide f(x) = log(x) # hide xs = [x^2 for x = 1:0.2:5] A = [f(x) for x in xs] # linear interpolation interp_linear = linear_interpolation(xs, A) nothing # hide ``` -------------------------------- ### Iterating Knots Up to a Stop Value Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/iterate.md Illustrates how to use `knotsbetween` to iterate over knots from the first knot up to a specified stop value by omitting the `start` argument. ```jldoctest julia> krange = knotsbetween(etp; stop=4.0) julia> collect(krange) 9-element Vector{Float64}: 1.0 1.5 1.75 2.0 2.5 2.75 3.0 3.5 3.75 ``` -------------------------------- ### Scaled Cubic interpolation with boundary conditions Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Use Scaled BSplines for data on a non-uniformly spaced grid. This example uses Cubic splines with Line boundary conditions on the grid. ```julia A_x = 1.:2.:40. A = [log(x) for x in A_x] itp = interpolate(A, BSpline(Cubic(Line(OnGrid())))) sitp = scale(itp, A_x) sitp(3.) # exactly log(3.) sitp(3.5) # approximately log(3.5) ``` -------------------------------- ### Monotonic Interpolation Example Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Create a monotonic interpolating function, useful for cumulative distribution functions. Uses `SteffenMonotonicInterpolation` and `extrapolate` with `Flat()` extrapolation. ```julia percentile_values = [ 0.0, 0.01, 0.1, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, 99.9, 99.99, 100.0 ]; y = sort(randn(length(percentile_values))); # some random data itp_cdf = extrapolate( interpolate(y, percentile_values, SteffenMonotonicInterpolation()), Flat() ); t = -3.0:0.01:3.0 # just a range for calculating values of the interpolating function interpolated_cdf = map(itp_cdf, t) # interpolating the CDF ``` -------------------------------- ### Error for `knotsbetween` Without Start or Stop Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/iterate.md Shows the error that occurs when `knotsbetween` is called without providing either a `start` or `stop` argument, as at least one is required. ```jldoctest julia> knotsbetween(etp) ERROR: ArgumentError: At least one of `start` or `stop` must be specified [...] ``` -------------------------------- ### Scaled Cubic interpolation for multidimensional grids Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Apply Scaled BSplines to multidimensional data with uniformly spaced grids. This example uses Cubic splines with Line boundary conditions on the grid for a 2D case. ```julia A_x1 = 1:.1:10 A_x2 = 1:.5:20 f(x1, x2) = log(x1+x2) A = [f(x1,x2) for x1 in A_x1, x2 in A_x2] itp = interpolate(A, BSpline(Cubic(Line(OnGrid())))) sitp = scale(itp, A_x1, A_x2) sitp(5., 10.) # exactly log(5 + 10) sitp(5.6, 7.1) # approximately log(5.6 + 7.1) ``` -------------------------------- ### Handle Infinite Knot Sequences in Multi-dimensional Extrapolation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/iterate.md When boundary conditions result in infinite knot sequences along certain axes, rearranging the axes or using appropriate boundary conditions can prevent iteration from getting stuck. ```julia x = [1.0, 1.5, 1.75, 2.0]; etp = linear_interpolation((x, x), x.*x', extrapolation_bc=(Periodic(), Throw())); Iterators.take(knots(etp), 6) |> collect ``` ```julia x = [1.0, 1.5, 1.75, 2.0]; etp = linear_interpolation((x, x), x.*x', extrapolation_bc=(Throw(), Periodic())); Iterators.take(knots(etp), 6) |> collect ``` -------------------------------- ### Get Length and Size of Knot Iterator Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/iterate.md The `length()` and `size()` functions can be used to determine the number of elements and dimensions of the knot iterator for standard interpolations. ```julia length(kiter) size(kiter) ``` -------------------------------- ### Iterate Over Knots of an Interpolation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/iterate.md Get an iterator over the knots of an `AbstractInterpolation` object using `knots(itp)`. Use `collect()` to view all knots as an array. ```julia using Interpolations itp = interpolate(rand(4), options...) kiter = knots(itp) # Iterator over knots collect(kiter) # Array of knots [1, 2, 3, 4] ``` -------------------------------- ### GPU Support with Interpolations.jl Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/devdocs.md Demonstrates how to use interpolants on GPU via broadcasting. Requires Interpolations, Adapt, and a GPU package like CUDA. The interpolant is constructed on the CPU, adapted to GPU memory, and then called via broadcast. ```julia using Interpolations, Adapt, CUDA # Or any other GPU package itp = Interpolations.interpolate([1, 2, 3], BSpline(Linear())); # construct the interpolant object on CPU cuitp = adapt(CuArray{Float32}, itp); # adapt it to GPU memory cuitp.(1:0.5:2) # call interpolant object via broadcast gradient.(Ref(cuitp), 1:0.5:2) ``` -------------------------------- ### Simplify Interpolation Object Creation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/convenience-construction.md Use convenience constructors to simplify the creation of interpolation objects, making expressions more readable. ```julia extrap_full = extrapolate(scale(interpolate(A, BSpline(Linear())), xs), Line()) ``` ```julia extrap = linear_interpolation(xs, A, extrapolation_bc = Line()) ``` -------------------------------- ### Finite Knot Sequence with Swapped Directional Boundary Conditions Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/iterate.md Shows how swapping the order of directional boundary conditions results in a finite sequence of knots. This is useful when a bounded range of knots is desired. ```jldoctest julia> x = [1.0, 1.2, 1.3, 2.0] julia> etp = linear_interpolation(x, x.^2, extrapolation_bc=((Periodic(), Throw()),)) julia> kiter = knots(etp) julia> collect(kiter) 4-element Vector{Float64}: 1.0 1.2 1.3 2.0 ``` ```jldoctest julia> Base.IteratorSize(kiter) Base.HasLength() ``` ```jldoctest julia> length(kiter) 4 ``` ```jldoctest julia> size(kiter) (4,) ``` -------------------------------- ### Infinite Knot Sequence with Directional Boundary Conditions Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/iterate.md Demonstrates how directional boundary conditions can lead to an infinite sequence of knots. The forward boundary condition determines if the iterator generates an infinite sequence. ```jldoctest julia> x = [1.0, 1.2, 1.3, 2.0] julia> etp = linear_interpolation(x, x.^2, extrapolation_bc=((Throw(), Periodic()),)) julia> kiter = knots(etp) julia> Iterators.take(kiter, 5) |> collect 5-element Vector{Float64}: 1.0 1.2 1.3 2.0 2.2 ``` ```jldoctest julia> Base.IteratorSize(kiter) Base.IsInfinite() ``` -------------------------------- ### Iterating Knots in Multi-dimensional Interpolants Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/iterate.md Demonstrates the use of `knotsbetween` with multi-dimensional interpolants to iterate over knots within specified ranges for each dimension. ```jldoctest julia> x = [1.0, 1.5, 1.75, 2.0] julia> etp = linear_interpolation((x, x), x.*x') julia> knotsbetween(etp; start=(1.2, 1.5), stop=(1.8, 3.0)) |> collect 2×2 Matrix{Tuple{Float64, Float64}}: (1.5, 1.75) (1.5, 2.0) (1.75, 1.75) (1.75, 2.0) ``` -------------------------------- ### Quadratic interpolation with reflecting boundary conditions Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Use Quadratic BSpline with Reflecting boundary conditions applied on the cell. Quadratic splines offer continuous gradients. ```julia # Quadratic interpolation with reflecting boundary conditions # Quadratic is the lowest order that has continuous gradient itp = interpolate(A, BSpline(Quadratic(Reflect(OnCell())))) ``` -------------------------------- ### In-place quadratic interpolation with BSplines Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Use Quadratic BSpline with InPlace boundary conditions. This method modifies the input array directly, reducing memory allocation but destroying the original data. ```julia # In-place interpolation itp = interpolate!(A, BSpline(Quadratic(InPlace(OnCell())))) ``` -------------------------------- ### Plotting Interpolations with Plots.jl Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/convenience-construction.md Demonstrates how to plot linear and cubic spline interpolations alongside original data points using Plots.jl. Requires Interpolations and Plots packages. ```julia using Interpolations, Plots # Lower and higher bound of interval a = 1.0 b = 10.0 # Interval definition x = a:1.0:b # This can be any sort of array data, as long as # length(x) == length(y) y = @. cos(x^2 / 9.0) # Function application by broadcasting # Interpolations itp_linear = linear_interpolation(x, y) itp_cubic = cubic_spline_interpolation(x, y) # Interpolation functions f_linear(x) = itp_linear(x) f_cubic(x) = itp_cubic(x) # Plots width, height = 1500, 800 # not strictly necessary x_new = a:0.1:b # smoother interval, necessary for cubic spline scatter(x, y, markersize=10,label="Data points") plot!(f_linear, x_new, w=3,label="Linear interpolation") plot!(f_cubic, x_new, linestyle=:dash, w=3, label="Cubic Spline interpolation") plot!(size = (width, height)) plot!(legend = :bottomleft) ``` -------------------------------- ### Create Linear Interpolation Object (With Extrapolation) Source: https://github.com/juliamath/interpolations.jl/blob/master/README.md Create a linear interpolation object that supports extrapolation. Values outside the grid are extrapolated linearly. ```julia interp_linear_extrap = linear_interpolation(xs, A,extrapolation_bc=Line()) interp_linear_extrap(0.9) # outside grid: linear extrapolation ``` -------------------------------- ### Iterate Over Knots with Reflect Extrapolation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/iterate.md With `Reflect` boundary conditions, `knots()` produces an infinite sequence following a specific reflection pattern. Use `Iterators.take()` to limit the output. ```julia x = [1.0, 1.5, 1.75, 2.0]; etp = linear_interpolation(x, x.^2, extrapolation_bc=Reflect()); kiter = knots(etp); k = Iterators.take(kiter, 6) |> collect ``` ```julia etp.(k) ``` -------------------------------- ### Create Interpolation Grid and Data Source: https://github.com/juliamath/interpolations.jl/blob/master/README.md Define the grid points `xs` and the corresponding data values `A` for interpolation. ```julia xs = 1:0.2:5 A = log.(xs) ``` -------------------------------- ### Monotonic Interpolation Algorithms Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md List of available monotonic interpolation algorithms. Some guarantee no overshooting (`FritschButlandMonotonicInterpolation`, `SteffenMonotonicInterpolation`), while others may overshoot. ```julia LinearMonotonicInterpolation() ``` ```julia FiniteDifferenceMonotonicInterpolation() ``` ```julia CardinalMonotonicInterpolation() ``` ```julia FritschCarlsonMonotonicInterpolation() ``` ```julia FritschButlandMonotonicInterpolation() ``` ```julia SteffenMonotonicInterpolation() ``` -------------------------------- ### Zygote Gradient Calculation with Interpolations.jl Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/chainrules.md Demonstrates using Zygote to compute the gradient of an interpolated function. The pullback reuses the existing gradient function by scaling it by the perturbation in y, enabling autodiff. ```julia x = 1:10 y = sin.(x) itp = interpolate(y, BSpline(Linear())) Zygote.gradient(itp, 2) #([-0.7681774187658145],) ``` -------------------------------- ### Applying Periodic Extrapolation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/extrapolation.md Applies periodic extrapolation to interpolation objects that were set up with periodic boundary conditions. ```julia etp0 = extrapolate(itp0, Periodic()) etp1 = extrapolate(itp1, Periodic()) etp2 = extrapolate(itp2, Periodic()) etp3 = extrapolate(itp3, Periodic()) ``` -------------------------------- ### Iterate Over Knots with Periodic Extrapolation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/iterate.md For `Periodic` boundary conditions, `knots()` generates an infinite sequence of knots. Use `Iterators.take()` to limit the output. ```julia x = [1.0, 1.5, 1.75, 2.0]; etp = linear_interpolation(x, x.^2, extrapolation_bc=Periodic()); kiter = knots(etp); k = Iterators.take(kiter, 6) |> collect ``` ```julia etp.(k) ``` -------------------------------- ### Interpolate Using Ranges Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/interpolations.md Create an interpolation 'fine' by evaluating the object 'itp' over specified ranges for each dimension. This utilizes Julia's indexing rules for convenience. ```julia fine = itp(range(1,stop=10,length=1001), range(1,stop=15,length=201)) ``` -------------------------------- ### Extrapolation with Linear Boundary Conditions Usage Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/convenience-construction.md Evaluate an interpolation object with linear boundary conditions outside the original data range to observe extrapolation behavior. ```julia extrap(1 - 0.2) ≈ f(1) - (f(1.2) - f(1)) extrap(5 + 0.2) ≈ f(5) + (f(5) - f(4.8)) ``` -------------------------------- ### Interpolate and Evaluate Value Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/devdocs.md Demonstrates interpolating an array and evaluating the interpolated value at a given point. ```jldoctest julia> A = reshape(1:27, 3, 3, 3) 3×3×3 reshape(::UnitRange{Int64}, 3, 3, 3) with eltype Int64: [:, :, 1] = 1 4 7 2 5 8 3 6 9 [:, :, 2] = 10 13 16 11 14 17 12 15 18 [:, :, 3] = 19 22 25 20 23 26 21 24 27 julia> itp = interpolate(A, BSpline(Linear())); julia> x = (1.2, 1.4, 1.7) (1.2, 1.4, 1.7) julia> itp(x...) 8.7 ``` -------------------------------- ### Plotting Parametric Spline Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Plot the original knots and the constructed spline using the `Plots` package. Requires `using Plots`. ```julia using Plots scatter(x, y, label="knots") plot!(xs, ys, label="spline") ``` -------------------------------- ### Create Linear Interpolation Object (No Extrapolation) Source: https://github.com/juliamath/interpolations.jl/blob/master/README.md Create a linear interpolation object. Accessing values outside the grid will result in an error. ```julia interp_linear = linear_interpolation(xs, A) interp_linear(3) # exactly log(3) interp_linear(3.1) # approximately log(3.1) interp_linear(0.9) # outside grid: error ``` -------------------------------- ### Previous-neighbor interpolation with BSplines Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Use Constant BSpline with Previous neighbor for interpolation. The value is retrieved by taking the floor of the coordinate to the nearest integer index. ```julia # Previous-neighbor interpolation itp = interpolate(a, BSpline(Constant(Previous))) v = itp(1.8) # returns a[1] ``` -------------------------------- ### Next-neighbor interpolation with BSplines Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Use Constant BSpline with Next neighbor for interpolation. The value is retrieved by taking the ceiling of the coordinate to the nearest integer index. ```julia # Next-neighbor interpolation itp = interpolate(a, BSpline(Constant(Next))) v = itp(5.4) # returns a[6] ``` -------------------------------- ### Multilinear interpolation with BSplines Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Use Linear BSpline for multilinear interpolation. This method interpolates linearly between the surrounding grid points. ```julia # (Multi)linear interpolation itp = interpolate(A, BSpline(Linear())) v = itp(3.2, 4.1) # returns 0.9*(0.8*A[3,4]+0.2*A[4,4]) + 0.1*(0.8*A[3,5]+0.2*A[4,5]) ``` -------------------------------- ### Constant Interpolation Variants Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Use `Constant{Previous}()` for previous neighbor interpolation or `Constant{Next}()` for next neighbor interpolation. Be mindful of potential rounding issues. ```julia Constant{Previous}() ``` ```julia Constant{Next}() ``` -------------------------------- ### Nearest-neighbor interpolation with BSplines Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Use Constant BSpline for nearest-neighbor interpolation. The value is retrieved by rounding the coordinate to the nearest integer index. ```julia # Nearest-neighbor interpolation itp = interpolate(a, BSpline(Constant())) v = itp(5.4) # returns a[5] ``` -------------------------------- ### Parametric Spline Construction Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Construct a parametric spline from knots `(x(t), y(t))`. Uses `Interpolations.scale` to map the parameter `t` to the spline. Requires `using Interpolations`. ```julia using Interpolations t = 0:.1:1 x = sin.(2π*t) y = cos.(2π*t) A = hcat(x,y) itp = Interpolations.scale( interpolate(A, (BSpline(Cubic(Natural(OnGrid()))), NoInterp())), t, 1:2 ) tfine = 0:.01:1 xs, ys = [itp(t,1) for t in tfine], [itp(t,2) for t in tfine] ``` -------------------------------- ### Bibliography Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/api.md References and citations used within the documentation. ```APIDOC ## Bibliography References and citations used within the documentation. ### Pages - api.md ### Canonical - false ``` -------------------------------- ### Iterate Over Knots in Multi-dimensional Extrapolation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/iterate.md Iterating over knots is supported for multi-dimensional extrapolations. The behavior depends on the specified boundary conditions for each dimension. ```julia x = [1.0, 1.5, 1.75, 2.0]; etp = linear_interpolation((x, x), x.*x'); knots(etp) |> collect ``` -------------------------------- ### Create Linear Interpolation Object Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/index.md Create a linear interpolation object for a given set of points and values. Extrapolation can be enabled using `extrapolation_bc=Line()`. ```julia using Interpolations xs = 1:0.2:5 A = log.(xs) interp_linear = linear_interpolation(xs, A); interp_linear(3) interp_linear(3.1) interp_linear(0.9) # outside grid: error ``` ```julia interp_linear_extrap = linear_interpolation(xs, A, extrapolation_bc=Line()); interp_linear_extrap(0.9) # outside grid: linear extrapolation ``` -------------------------------- ### Advanced Interpolation Composition Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/index.md Construct interpolation objects by composing `interpolate`, `scale`, and `extrapolate` for fine-grained control over performance and features. ```julia interp_linear = extrapolate(scale(interpolate(A, BSpline(Linear())), xs), Line()) ``` ```julia scaled_itp = scale(interpolate(A, BSpline(Linear())), xs) ``` ```julia itp = interpolate(A, BSpline(Linear())) ``` -------------------------------- ### Public API Documentation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/api.md This section documents the public API of the Interpolations.jl package, including exported functions and types. ```APIDOC ## Public API This section documents the public API of the Interpolations.jl package, including exported functions and types. ### Modules - Interpolations ### Order - function - type ``` -------------------------------- ### Construct Interpolation Object Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/interpolations.md Construct an interpolation object from an AbstractArray. The options control the interpolation type. Assumes samples in A are equally-spaced. ```julia itp = interpolate(A, options...) ``` -------------------------------- ### In-place Gridded Interpolation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Perform in-place gridded interpolation using `interpolate!`. The data array `y` is modified directly. Ensure the `using Interpolations` import. ```julia using Interpolations # hide x = 1:4 y = view(rand(4), :) itp = interpolate!((x,), y, Gridded(Linear())) y .= 0 itp(2.5) ``` -------------------------------- ### Store On-Grid Values Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/interpolations.md Iterate through the grid points of an interpolation object 'itp' and store the on-grid value at each point into the destination array 'dest'. ```julia function ongrid!(dest, itp) for I in CartesianIndices(itp) dest[I] = itp(I) end end ``` -------------------------------- ### Internal API Documentation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/api.md This section documents the internal API of the Interpolations.jl package, which is intended for internal use and may change without notice. ```APIDOC ## Internal API This section documents the internal API of the Interpolations.jl package, which is intended for internal use and may change without notice. ### Modules - Interpolations ### Order - function - type ``` -------------------------------- ### Compute Weighted Indexes for Value and Gradient Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/devdocs.md Calculates weighted indexes for both the interpolated value and its gradient. This is used for derivative computations. ```jldoctest julia> wis = Interpolations.weightedindexes((Interpolations.value_weights, Interpolations.gradient_weights), Interpolations.itpinfo(itp)..., x) ((Interpolations.WeightedAdjIndex{2, Float64}(1, (-1.0, 1.0)), Interpolations.WeightedAdjIndex{2, Float64}(1, (0.6000000000000001, 0.3999999999999999)), Interpolations.WeightedAdjIndex{2, Float64}(1, (0.30000000000000004, 0.7))), (Interpolations.WeightedAdjIndex{2, Float64}(1, (0.8, 0.19999999999999996)), Interpolations.WeightedAdjIndex{2, Float64}(1, (-1.0, 1.0)), Interpolations.WeightedAdjIndex{2, Float64}(1, (0.30000000000000004, 0.7))), (Interpolations.WeightedAdjIndex{2, Float64}(1, (0.8, 0.19999999999999996)), Interpolations.WeightedAdjIndex{2, Float64}(1, (0.6000000000000001, 0.3999999999999999)), Interpolations.WeightedAdjIndex{2, Float64}(1, (-1.0, 1.0)))) julia> wis[1] (Interpolations.WeightedAdjIndex{2, Float64}(1, (-1.0, 1.0)), Interpolations.WeightedAdjIndex{2, Float64}(1, (0.6000000000000001, 0.3999999999999999)), Interpolations.WeightedAdjIndex{2, Float64}(1, (0.30000000000000004, 0.7))) julia> wis[2] (Interpolations.WeightedAdjIndex{2, Float64}(1, (0.8, 0.19999999999999996)), Interpolations.WeightedAdjIndex{2, Float64}(1, (-1.0, 1.0)), Interpolations.WeightedAdjIndex{2, Float64}(1, (0.30000000000000004, 0.7))) julia> wis[3] (Interpolations.WeightedAdjIndex{2, Float64}(1, (0.8, 0.19999999999999996)), Interpolations.WeightedAdjIndex{2, Float64}(1, (0.6000000000000001, 0.3999999999999999)), Interpolations.WeightedAdjIndex{2, Float64}(1, (-1.0, 1.0))) julia> Interpolations.InterpGetindex(A)[wis[1]...] 1.0 julia> Interpolations.InterpGetindex(A)[wis[2]...] 3.000000000000001 julia> Interpolations.InterpGetindex(A)[wis[3]...] 9.0 ``` -------------------------------- ### Evaluate Interpolation Object Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/interpolations.md Evaluate the constructed interpolation object 'itp' at the given position (x, y, ...). ```julia v = itp(x, y, ...) ``` -------------------------------- ### Compute Gradient In-Place Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/interpolations.md Compute the gradient of the interpolation object 'itp' and store it in the pre-allocated vector 'g'. ```julia Interpolations.gradient!(g, itp, x, y, ...) ``` -------------------------------- ### Gridded Interpolation Modes Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Available modes for gridded interpolation: `Gridded(Linear())` for linear, `Gridded(Constant())` for nearest neighbor, and `NoInterp()` for exact grid point lookup. ```julia Gridded(Linear()) ``` ```julia Gridded(Constant()) ``` ```julia NoInterp() ``` -------------------------------- ### Gridded Interpolation with Missing Data Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Handle missing data in gridded interpolation by filtering out `missing` values before creating the interpolator. Requires specifying node positions `xf` and filtered data `Af`. ```julia x = 1:6 A = [i == 3 ? missing : i for i in x] xf = [xi for (xi,a) in zip(x, A) if !ismissing(a)] Af = filter(!ismissing, A) itp = interpolate((xf, ), Af, Gridded(Linear())) ``` -------------------------------- ### 1D Linear Interpolation Usage Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/convenience-construction.md Perform 1D linear interpolation and evaluate the interpolated function at specific points. The result at an existing data point should be exact. ```julia interp_linear = linear_interpolation(xs, A) interp_linear(3) # exactly log(3) interp_linear(3.1) # approximately log(3.1) ``` -------------------------------- ### Compute Weighted Indexes for Value Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/devdocs.md Calculates the weighted indexes required to compute the interpolated value at a point. ```jldoctest julia> wis = Interpolations.weightedindexes((Interpolations.value_weights,), Interpolations.itpinfo(itp)..., x) (Interpolations.WeightedAdjIndex{2, Float64}(1, (0.8, 0.19999999999999996)), Interpolations.WeightedAdjIndex{2, Float64}(1, (0.6000000000000001, 0.3999999999999999)), Interpolations.WeightedAdjIndex{2, Float64}(1, (0.30000000000000004, 0.7))) julia> wis[1] Interpolations.WeightedAdjIndex{2, Float64}(1, (0.8, 0.19999999999999996)) julia> wis[2] Interpolations.WeightedAdjIndex{2, Float64}(1, (0.6000000000000001, 0.3999999999999999)) julia> wis[3] Interpolations.WeightedAdjIndex{2, Float64}(1, (0.30000000000000004, 0.7)) julia> Interpolations.InterpGetindex(A)[wis...] 8.7 ``` -------------------------------- ### Iterate Over Knots in Multi-dimensional Interpolation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/iterate.md For multi-dimensional interpolations, the `knots()` iterator returns tuples of positions. The first coordinate changes fastest. ```julia itp = interpolate(ones(3,3), BSpline(Linear())); kiter = knots(itp); collect(kiter) ``` -------------------------------- ### Mixed interpolation with BSplines Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Combine different interpolation types for different dimensions. Here, Linear BSpline is used for the first dimension, and no interpolation (direct lookup) for the second. ```julia # Linear interpolation in the first dimension, and no interpolation # (just lookup) in the second itp = interpolate(A, (BSpline(Linear()), NoInterp())) v = itp(3.65, 5) # returns 0.35*A[3,5] + 0.65*A[4,5] ``` -------------------------------- ### 1D Cubic Spline Interpolation Usage Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/convenience-construction.md Perform 1D cubic spline interpolation and evaluate the interpolated function at specific points. The result at an existing data point should be exact. ```julia interp_cubic = cubic_spline_interpolation(xs, A) interp_cubic(3) # exactly log(3) interp_cubic(3.1) # approximately log(3.1) ``` -------------------------------- ### Define Custom rrule for Interpolations.jl Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/chainrules.md A custom rrule is defined to facilitate integration with autodiff libraries like Zygote. This rrule takes a perturbation on the interpolated value and returns its effect on each input dimension. ```julia y, itp_pullback = rrule(itp, 1) ``` -------------------------------- ### Multidimensional Cubic Spline Interpolation Usage Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/convenience-construction.md Perform multidimensional cubic spline interpolation and evaluate the function at specific coordinates. The result at an existing data point should be exact. ```julia interp_cubic = cubic_spline_interpolation((xs, ys), A) interp_cubic(3, 2) # exactly log(3 + 2) interp_cubic(3.1, 2.1) # approximately log(3.1 + 2.1) ``` -------------------------------- ### Mixed Gridded Interpolation Modes Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Apply different interpolation modes to different dimensions of gridded data by providing a tuple of modes. ```julia itp = interpolate(nodes, A, (Gridded(Linear()), Gridded(Constant()))) ``` -------------------------------- ### Compute Gradient of Interpolation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/interpolations.md Compute the gradient of the interpolation object 'itp' at the given position (x, y, ...). ```julia g = Interpolations.gradient(itp, x, y, ...) ``` -------------------------------- ### Compute Hessian In-Place Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/interpolations.md Compute the hessian of the interpolation object 'itp' and store it in the pre-allocated matrix 'h'. ```julia Interpolations.hessian!(h, itp, x, y, ...) ``` -------------------------------- ### Multidimensional Linear Interpolation Usage Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/convenience-construction.md Perform multidimensional linear interpolation and evaluate the function at specific coordinates. The result at an existing data point should be exact. ```julia interp_linear = linear_interpolation((xs, ys), A) interp_linear(3, 2) # exactly log(3 + 2) interp_linear(3.1, 2.1) # approximately log(3.1 + 2.1) ``` -------------------------------- ### Irregular Grid Linear Interpolation Usage Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/convenience-construction.md Perform linear interpolation on an irregular grid and evaluate the function at specific points. The result at an existing data point should be exact. ```julia interp_linear(1) # exactly log(1) interp_linear(1.05) # approximately log(1.05) ``` -------------------------------- ### 1D Gridded Interpolation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Use for 1D gridded data interpolation. Specify node positions and data array. `Gridded(Linear())` is used for linear interpolation. ```julia A = rand(20) A_x = 1.0:2.0:40.0 nodes = (A_x,) itp = interpolate(nodes, A, Gridded(Linear())) itp(2.0) ``` -------------------------------- ### Compute Hessian of Interpolation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/interpolations.md Compute the hessian of the interpolation object 'itp' at the given position (x, y, ...). ```julia h = Interpolations.hessian(itp, x, y, ...) ``` -------------------------------- ### Multi-dimensional Gridded Interpolation Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/control.md Interpolate multi-dimensional gridded data. `nodes` should be a tuple of coordinate arrays for each dimension. `itp(x, y, ...)` queries the interpolated value. ```julia A = rand(8,20) nodes = ([x^2 for x = 1:8], [0.2y for y = 1:20]) itp = interpolate(nodes, A, Gridded(Linear())) itp(4,1.2) # approximately A[2,6] ``` -------------------------------- ### Extrapolation with Fill Value Usage Source: https://github.com/juliamath/interpolations.jl/blob/master/docs/src/convenience-construction.md Check if an out-of-range evaluation returns NaN when a fill value is specified for extrapolation. ```julia isnan(extrap(5.2)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.