Derivatives

FastInterpolations.jl provides analytical derivatives computed directly from spline coefficients—no finite differences.

Visual Comparison

See Visual Comparison for side-by-side derivative plots.

Overview

Interpolation1st Derivative2nd Derivative3rd Derivative
Constant000
LinearPiecewise constant00
QuadraticContinuous (C¹)Piecewise constant0
CubicSmooth (C¹)Continuous (C²)Piecewise constant

Usage

One-Shot API

using FastInterpolations

x = range(0.0, 2π, 50)
y = sin.(x)

cubic_interp(x, y, 1.0)           # value at x=1.0
cubic_interp(x, y, 1.0; deriv=1)  # 1st derivative at x=1.0
cubic_interp(x, y, 1.0; deriv=2)  # 2nd derivative at x=1.0

Interpolant API

itp = cubic_interp(x, y)

itp(1.0; deriv=1)  # 1st derivative at x=1.0
itp(1.0; deriv=2)  # 2nd derivative at x=1.0

DerivativeView

deriv1(itp), deriv2(itp), deriv3(itp) create lightweight callable wrappers with the derivative order fixed at construction. Calling d1(x) is equivalent to itp(x; deriv=1). Same performance, cleaner syntax.

All interpolants support all derivative orders

deriv1, deriv2, deriv3 work with all interpolant types (constant, linear, quadratic, cubic). Higher-order derivatives simply return 0 for lower-order methods.

# Create derivative views (callable objects, not values)
d1 = deriv1(itp)
d2 = deriv2(itp)
d3 = deriv3(itp)

d1(1.0)  # 1st derivative at x=1.0 (same as itp(1.0; deriv=1))
d2(1.0)  # 2nd derivative at x=1.0
d3(1.0)  # 3rd derivative at x=1.0

Keyword Forwarding

DerivativeView forwards all keyword arguments to the parent interpolant (deriv excepted—it's fixed at construction):

d1 = deriv1(itp)

# Override search policy
d1(0.5; search=LinearBinary())

# Use hint for sequential access
hint = Ref(1)
for xq in sorted_queries
    val = d1(xq; hint=hint)
end

Broadcasting & Fused Operations

xq = range(0.0, 2π, 100)

slopes = d1.(xq)                    # broadcast over query points
result = @. itp(xq) + 0.1 * d1(xq)  # fused broadcast (zero allocations)

In-Place Evaluation

output = zeros(5)
xq_small = [0.5, 1.0, 1.5, 2.0, 2.5]

d1(output, xq_small)  # in-place: writes 1st derivatives into output
output
5-element Vector{Float64}:
  0.8775875895263162
  0.5403162013526956
  0.07075186552691143
 -0.41613953407644466
 -0.8011442897429127

Higher-Order Functions

DerivativeView works with any function expecting a callable:

using Roots
d1 = deriv1(itp)
root = find_zero(d1, 0.5)  # Find where derivative = 0

Boundary Conditions & Extrapolation

  • Boundary conditions affect derivative behavior at endpoints. See Cubic Splines.
  • Extrapolation settings are inherited by derivatives:
itp = cubic_interp(x, y; extrap=:extension)
d1 = deriv1(itp)
d1(-0.5)  # Uses :extension extrapolation

API Summary

MethodDescription
itp(xq; deriv=N)Direct derivative evaluation
deriv1(itp), deriv2(itp), deriv3(itp)Convenience wrappers (all interpolants)
d.(xq)Broadcast evaluation
d(output, xq)In-place vector evaluation
d(xq; search=..., hint=...)With keyword arguments

API Reference

FastInterpolations.deriv1Function
deriv1(itp::AbstractInterpolant)
deriv2(itp::AbstractInterpolant)
deriv3(itp::AbstractInterpolant)

deriv_view(itp::AbstractInterpolant, order::Int)
deriv_view(itp::AbstractInterpolantND, order::Int)
deriv_view(itp::AbstractInterpolantND, order::NTuple{N,Int})

Create a callable, zero-allocation derivative view of the interpolant.

  • 1D convenience: deriv1/deriv2/deriv3 for the 1st/2nd/3rd derivatives.
  • Generic: deriv_view(itp, order) for 1D derivative orders and ND mixed partials.

DerivativeView is a lightweight wrapper that delegates all evaluation calls to the underlying interpolant using the deriv keyword argument (e.g., itp(xq; deriv=1)). This enables a more functional syntax without the overhead of full object copying.

Keyword Arguments

All keyword arguments are forwarded to the parent interpolant. Common options include:

  • search: Search policy for interval lookup (default: parent's search_policy)
  • hint::Union{Nothing,Base.RefValue{Int}}: Mutable hint for sequential access patterns (default: nothing)

Features

  • Functional API: Pass derivative views into high-order functions like map or find_zeros.
  • Broadcast Fusion: Enables evaluation without temporary allocations when combined with other operations using dot syntax (e.g., @. d1(xs) + d2(xs)).
  • Unified Interface: Supports both single-series and multi-series (SeriesInterpolant) objects.
    • For AbstractInterpolant, returns a scalar derivative.
    • For AbstractSeriesInterpolant, returns a vector of derivatives.
  • In-place Evaluation: Supports in-place evaluation for both single-series and multi-series interpolants.

Notes

  • Order 3: For cubic splines, the 3rd derivative is piecewise constant and may jump at knot points. For lower-order interpolants, it returns zero.

Example

itp = cubic_interp(x, y)
d1 = deriv1(itp)

# Evaluate at a point
val = d1(0.5)

# Override search policy
val = d1(0.5; search=LinearBinary())

# Use hint for sequential access
hint = Ref(1)
for xq in sorted_queries
    val = d1(xq; hint=hint)
end

# Use in higher-order functions
using Roots
root = find_zero(d1, 0.5)

# Fused broadcast (no temporary allocations)
query_pts = range(0.0, 1.0, 100)
results = @. itp(query_pts) + 0.1 * d1(query_pts)

# In-place evaluation (single-series)
output = similar(query_pts)
d1(output, query_pts)
d1(output, query_pts; search=LinearBinary())
source
FastInterpolations.deriv2Function
deriv1(itp::AbstractInterpolant)
deriv2(itp::AbstractInterpolant)
deriv3(itp::AbstractInterpolant)

deriv_view(itp::AbstractInterpolant, order::Int)
deriv_view(itp::AbstractInterpolantND, order::Int)
deriv_view(itp::AbstractInterpolantND, order::NTuple{N,Int})

Create a callable, zero-allocation derivative view of the interpolant.

  • 1D convenience: deriv1/deriv2/deriv3 for the 1st/2nd/3rd derivatives.
  • Generic: deriv_view(itp, order) for 1D derivative orders and ND mixed partials.

DerivativeView is a lightweight wrapper that delegates all evaluation calls to the underlying interpolant using the deriv keyword argument (e.g., itp(xq; deriv=1)). This enables a more functional syntax without the overhead of full object copying.

Keyword Arguments

All keyword arguments are forwarded to the parent interpolant. Common options include:

  • search: Search policy for interval lookup (default: parent's search_policy)
  • hint::Union{Nothing,Base.RefValue{Int}}: Mutable hint for sequential access patterns (default: nothing)

Features

  • Functional API: Pass derivative views into high-order functions like map or find_zeros.
  • Broadcast Fusion: Enables evaluation without temporary allocations when combined with other operations using dot syntax (e.g., @. d1(xs) + d2(xs)).
  • Unified Interface: Supports both single-series and multi-series (SeriesInterpolant) objects.
    • For AbstractInterpolant, returns a scalar derivative.
    • For AbstractSeriesInterpolant, returns a vector of derivatives.
  • In-place Evaluation: Supports in-place evaluation for both single-series and multi-series interpolants.

Notes

  • Order 3: For cubic splines, the 3rd derivative is piecewise constant and may jump at knot points. For lower-order interpolants, it returns zero.

Example

itp = cubic_interp(x, y)
d1 = deriv1(itp)

# Evaluate at a point
val = d1(0.5)

# Override search policy
val = d1(0.5; search=LinearBinary())

# Use hint for sequential access
hint = Ref(1)
for xq in sorted_queries
    val = d1(xq; hint=hint)
end

# Use in higher-order functions
using Roots
root = find_zero(d1, 0.5)

# Fused broadcast (no temporary allocations)
query_pts = range(0.0, 1.0, 100)
results = @. itp(query_pts) + 0.1 * d1(query_pts)

# In-place evaluation (single-series)
output = similar(query_pts)
d1(output, query_pts)
d1(output, query_pts; search=LinearBinary())
source
FastInterpolations.deriv3Function
deriv1(itp::AbstractInterpolant)
deriv2(itp::AbstractInterpolant)
deriv3(itp::AbstractInterpolant)

deriv_view(itp::AbstractInterpolant, order::Int)
deriv_view(itp::AbstractInterpolantND, order::Int)
deriv_view(itp::AbstractInterpolantND, order::NTuple{N,Int})

Create a callable, zero-allocation derivative view of the interpolant.

  • 1D convenience: deriv1/deriv2/deriv3 for the 1st/2nd/3rd derivatives.
  • Generic: deriv_view(itp, order) for 1D derivative orders and ND mixed partials.

DerivativeView is a lightweight wrapper that delegates all evaluation calls to the underlying interpolant using the deriv keyword argument (e.g., itp(xq; deriv=1)). This enables a more functional syntax without the overhead of full object copying.

Keyword Arguments

All keyword arguments are forwarded to the parent interpolant. Common options include:

  • search: Search policy for interval lookup (default: parent's search_policy)
  • hint::Union{Nothing,Base.RefValue{Int}}: Mutable hint for sequential access patterns (default: nothing)

Features

  • Functional API: Pass derivative views into high-order functions like map or find_zeros.
  • Broadcast Fusion: Enables evaluation without temporary allocations when combined with other operations using dot syntax (e.g., @. d1(xs) + d2(xs)).
  • Unified Interface: Supports both single-series and multi-series (SeriesInterpolant) objects.
    • For AbstractInterpolant, returns a scalar derivative.
    • For AbstractSeriesInterpolant, returns a vector of derivatives.
  • In-place Evaluation: Supports in-place evaluation for both single-series and multi-series interpolants.

Notes

  • Order 3: For cubic splines, the 3rd derivative is piecewise constant and may jump at knot points. For lower-order interpolants, it returns zero.

Example

itp = cubic_interp(x, y)
d1 = deriv1(itp)

# Evaluate at a point
val = d1(0.5)

# Override search policy
val = d1(0.5; search=LinearBinary())

# Use hint for sequential access
hint = Ref(1)
for xq in sorted_queries
    val = d1(xq; hint=hint)
end

# Use in higher-order functions
using Roots
root = find_zero(d1, 0.5)

# Fused broadcast (no temporary allocations)
query_pts = range(0.0, 1.0, 100)
results = @. itp(query_pts) + 0.1 * d1(query_pts)

# In-place evaluation (single-series)
output = similar(query_pts)
d1(output, query_pts)
d1(output, query_pts; search=LinearBinary())
source
FastInterpolations.deriv_viewFunction
deriv_view(itp::AbstractInterpolant, order::Int)
deriv_view(itp::AbstractInterpolantND, order::Int)
deriv_view(itp::AbstractInterpolantND, order::NTuple{N,Int})

Create a derivative view for 1D or N-dimensional interpolants.

  • 1D: order::Int maps to deriv=order.
  • ND: order::Int applies the same order to all axes (e.g., 1(1,1,...,1)), i.e. a mixed partial with order order along every axis.
  • ND: order::NTuple{N,Int} specifies mixed partials.

The ND view forwards deriv=Val(order) to enable compile-time dispatch.

Examples

itp = cubic_interp((x, y), data)

# In 2D, `order::Int` is shorthand for the mixed partial `(order, order)`.
dxy = deriv_view(itp, 1)        # same as (1, 1) => ∂²f/∂x∂y
dx = deriv_view(itp, (1, 0))
dy = deriv_view(itp, (0, 1))

dx((0.5, 0.5))                         # ∂f/∂x
dxy.([(0.1, 0.2), (0.3, 0.4)])          # broadcast over points
source
FastInterpolations.DerivativeViewType
DerivativeView{Order, ITP} <: AbstractDerivativeView

Lightweight callable wrapper for derivative evaluation with a fixed derivative order. Enables broadcast and higher-order function composition.

Type Parameters

  • Order: Derivative order
    • Int for 1D derivatives (1, 2, or 3)
    • NTuple{N,Int} for ND partials (e.g., (1,0))
  • ITP: Parent interpolant type

Construction

Use deriv1(itp), deriv2(itp), or deriv3(itp) to create instances for 1D. Use deriv_view(itp, (d1, d2, ...)) for ND mixed partials.

Type Dispatch Patterns

# Match any derivative view
function foo(d::AbstractDerivativeView) ... end

# Match specific derivative order (any interpolant)
function foo(d::DerivativeView{1}) ... end

# Match specific order AND interpolant type
function foo(d::DerivativeView{1, <:CubicInterpolant}) ... end

Example

itp = cubic_interp(x, y)
d1 = deriv1(itp)     # DerivativeView{1, ...}
d2 = deriv2(itp)     # DerivativeView{2, ...}

# Broadcast evaluation
slopes = d1.(query_points)

# Fused broadcast (zero allocation)
result = @. coef * d1(xs) * other_func(xs)

See also: AbstractDerivativeView, deriv1, deriv2, deriv3, deriv_view

source

See Also