Quadratic Interpolation API

Overview

One-shot (construction + evaluation)

FunctionDescription
quadratic_interp(x, y, xq)Quadratic interpolation at point(s) xq
quadratic_interp(x, y, xq; bc=...)With boundary condition
quadratic_interp!(out, x, y, xq)In-place quadratic interpolation
quadratic_interp!(out, x, y, xq; bc)In-place with BC

Re-usable interpolant

FunctionDescription
itp = quadratic_interp(x, y)Create quadratic interpolant
itp = quadratic_interp(x, y; bc=...)Create with boundary condition
itp(xq)Evaluate at point(s) xq
itp(out, xq)Evaluate at xq, store result in out

Derivatives

FunctionDescription
quadratic_interp(x, y, xq; deriv=1)First derivative (continuous)
quadratic_interp(x, y, xq; deriv=2)Second derivative (piecewise constant)
deriv1(itp)First derivative view
deriv2(itp)Second derivative view

Functions

FastInterpolations.quadratic_interpFunction
quadratic_interp(x, y, xi; bc=Left(ParabolaFit()), extrap=:none, deriv=0)

C1 piecewise quadratic spline interpolation at a single point.

Arguments

  • x::AbstractVector: x-coordinates (sorted, length ≥ 2)
  • y::AbstractVector: y-values (same length as x)
  • xi::Real: Query point
  • bc: Boundary condition (one of):
    • Left(ParabolaFit()): 3-point parabola fit at left (default, exact for polynomials)
    • Right(ParabolaFit()): 3-point parabola fit at right
    • Left(Deriv1(v)): First derivative = v at left endpoint
    • Left(Deriv2(v)): Second derivative = v at left endpoint
    • Right(Deriv1(v)): First derivative = v at right endpoint
    • Right(Deriv2(v)): Second derivative = v at right endpoint
    • MinCurvFit(): Minimize total curvature (globally smooth)
  • extrap::Symbol: Extrapolation mode
    • :none (default): throws DomainError if outside domain
    • :constant: clamp to boundary values
    • :extension: extend the boundary polynomial
  • deriv::Int: Derivative order (0, 1, or 2)

Returns

  • Interpolated value (Float type)

Example

x = [0.0, 1.0, 2.0, 3.0]
y = x.^2  # [0, 1, 4, 9]

# Default: ParabolaFit (exact for quadratic polynomials)
quadratic_interp(x, y, 1.5)  # ≈ 2.25 (exact)

# With specific BC
quadratic_interp(x, y, 1.5; bc=Left(Deriv1(0.0)))  # zero slope at left
quadratic_interp(x, y, 1.5; bc=MinCurvFit())        # minimize curvature

# Derivatives
quadratic_interp(x, y, 1.5; deriv=1)  # ≈ 3.0 (slope at x=1.5)
quadratic_interp(x, y, 1.5; deriv=2)  # ≈ 2.0 (curvature)
source
quadratic_interp(x, y, x_targets; bc=Left(ParabolaFit()), extrap=:none, deriv=0)

Quadratic spline interpolation for multiple query points (allocating version).

Example

x = [0.0, 1.0, 2.0, 3.0]
y = x.^2
result = quadratic_interp(x, y, [0.5, 1.5, 2.5])
# result ≈ [0.25, 2.25, 6.25]
source
quadratic_interp(x, y; bc=Left(ParabolaFit()), extrap=:none) -> QuadraticInterpolant

Create a callable interpolant for broadcast fusion and reuse.

Arguments

  • x::AbstractVector: x-coordinates (sorted, length ≥ 2)
  • y::AbstractVector: y-values
  • bc: Boundary condition (Left, Right, MinCurvFit, or Left/Right with ParabolaFit)
  • extrap::Symbol: Extrapolation mode

Returns

QuadraticInterpolant object for scalar/broadcast evaluation.

Example

x = [0.0, 1.0, 2.0, 3.0]
y = x.^2

# Default BC (ParabolaFit) gives exact polynomial reproduction
itp = quadratic_interp(x, y)
itp(1.5)           # 2.25 (exact)
itp.([0.5, 1.5])   # [0.25, 2.25]

# Fused broadcast (optimal)
result = @. coef * itp(query)
source
quadratic_interp(x, ys::AbstractVector{<:AbstractVector}; bc=Left(ParabolaFit()), extrap=:none)

Create a multi-Y quadratic series interpolant for multiple y-data series sharing the same x-grid.

Arguments

  • x::AbstractVector: x-coordinates (sorted, length ≥ 2)
  • ys: Vector of y-value vectors (all same length as x)
  • bc: Boundary condition (Left/Right with ParabolaFit, Deriv1, Deriv2, MinCurvFit)
  • extrap: Extrapolation mode (:none, :constant, :extension)

Returns

QuadraticSeriesInterpolant object with unified matrix storage.

Example

x = collect(range(0.0, 1.0, 101))
y1 = sin.(2π .* x)
y2 = cos.(2π .* x)
y3 = exp.(-x)

sitp = quadratic_interp(x, [y1, y2, y3])
vals = sitp(0.5)  # Returns [val1, val2, val3]
source
quadratic_interp(x, Y::AbstractMatrix; bc=Left(ParabolaFit()), extrap=:none)

Create a multi-Y quadratic series interpolant from a matrix where each column is a y-series.

Arguments

  • x::AbstractVector: x-coordinates (length n)
  • Y::AbstractMatrix: n×m matrix, each column is a y-series
  • bc, extrap: Same as vector form

Example

x = collect(range(0.0, 1.0, 101))
Y = hcat(sin.(2π .* x), cos.(2π .* x))  # 101×2 matrix

sitp = quadratic_interp(x, Y)
source
FastInterpolations.quadratic_interp!Function
quadratic_interp!(output, x, y, x_targets; bc=Left(ParabolaFit()), extrap=:none, deriv=0)

In-place quadratic spline interpolation for multiple query points.

Arguments

  • output: Pre-allocated output vector
  • Other arguments same as quadratic_interp

Example

x = [0.0, 1.0, 2.0, 3.0]
y = x.^2
out = zeros(3)
quadratic_interp!(out, x, y, [0.5, 1.5, 2.5])
# out ≈ [0.25, 2.25, 6.25]
source

Interpolant Type

FastInterpolations.QuadraticInterpolantType
QuadraticInterpolant{T,X,Y}

Lightweight callable interpolant for quadratic spline interpolation. Returned by quadratic_interp(x, y) (2-argument form).

Fields

  • x::X: x-coordinates (sorted)
  • y::Y: y-values
  • h::Vector{T}: Grid spacing (precomputed)
  • a::Vector{T}: Quadratic coefficients
  • d::Vector{T}: Slope coefficients
  • mode::ExtrapVal: Extrapolation mode

Usage

itp = quadratic_interp(x, y; bc=Right(Deriv1(6.0)))
val = itp(0.5)               # scalar evaluation
vals = itp.([0.5, 1.5])      # broadcast
vals = itp([0.5, 1.5])       # vector call

# Derivatives
d1 = itp(0.5; deriv=1)       # first derivative
d2 = itp(0.5; deriv=2)       # second derivative
source

Boundary Condition Types

FastInterpolations.ParabolaFitType
ParabolaFit{T<:AbstractFloat} <: PointBC{T}

Parabola-fit boundary condition for quadratic splines. Computes the initial slope d[1] (or d[n]) using a 3-point derivative formula that exactly reproduces any polynomial up to degree 2.

This is the recommended BC for quadratic splines when the underlying function is polynomial-like or smooth. It uses the first (or last) 3 points to fit a parabola and computes the derivative at the endpoint.

Mathematical Background

For the first 3 points (x₀, y₀), (x₁, y₁), (x₂, y₂), the 3-point derivative formula is:

  • d[1] = y₀·(−(2h₁+h₂))/(h₁(h₁+h₂)) + y₁·(h₁+h₂)/(h₁·h₂) − y₂·h₁/((h₁+h₂)·h₂)

where h₁ = x₁ - x₀ and h₂ = x₂ - x₁.

For uniform grids (h₁ = h₂ = h), this simplifies to:

  • d[1] = (−3y₀ + 4y₁ − y₂) / (2h)

Key Property

Polynomial Reproduction: For any quadratic polynomial f(x) = ax² + bx + c, ParabolaFit BC produces exact interpolation at all query points.

Example

x = [0.0, 1.0, 2.0, 3.0, 4.0]
y = x.^2  # f(x) = x²

# ParabolaFit gives exact reproduction
itp = quadratic_interp(x, y; bc=Left(ParabolaFit()))
itp(1.5)  # ≈ 2.25 (exact)

# Works at both endpoints
itp_right = quadratic_interp(x, y; bc=Right(ParabolaFit()))
source
FastInterpolations.MinCurvFitType
MinCurvFit{T<:AbstractFloat} <: AbstractBC{T}

Minimum curvature boundary condition for quadratic splines. Minimizes total curvature (∫S''² dx) by optimizing the initial slope d[1].

This produces globally smooth interpolation by finding the optimal d[1] that minimizes the integrated squared second derivative. The optimal d[1] is computed using a closed-form solution in O(n) time with no additional allocation.

Mathematical Background

For quadratic splines, the slope d[i] depends on d[1] via recurrence:

  • d[i] = α[i] * d[1] + β[i] where α[i] = (-1)^(i+1)

The optimal d[1] minimizes:

  • ∫(S'')² dx = Σ 4a[i]²h[i] = Σ (s[i] - d[i])²/h[i]

Closed-form solution:

  • d[1] = [Σ α[i]*(s[i] - β[i])/h[i]] / [Σ 1/h[i]]

Example

x = [0.0, 0.3, 0.8, 1.5, 2.5, 3.0, 4.0]
y = [0.0, 0.8, 1.2, 0.9, 0.3, 0.6, 1.0]

# Default BC uses ParabolaFit
itp_default = quadratic_interp(x, y)

# MinCurvFit gives globally smooth result via curvature minimization
itp_smooth = quadratic_interp(x, y; bc=MinCurvFit())
source