Derivatives
FastInterpolations.jl provides analytical derivatives computed directly from spline coefficients—no finite differences.
See Visual Comparison for side-by-side derivative plots.
Overview
| Interpolation | 1st Derivative | 2nd Derivative | 3rd Derivative |
|---|---|---|---|
| Constant | 0 | 0 | 0 |
| Linear | Piecewise constant | 0 | 0 |
| Quadratic | Continuous (C¹) | Piecewise constant | 0 |
| Cubic | Smooth (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.0Interpolant 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.0DerivativeView
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.
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.0Keyword 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)
endBroadcasting & 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
output5-element Vector{Float64}:
0.8775875895263162
0.5403162013526956
0.07075186552691143
-0.41613953407644466
-0.8011442897429127Higher-Order Functions
DerivativeView works with any function expecting a callable:
using Roots
d1 = deriv1(itp)
root = find_zero(d1, 0.5) # Find where derivative = 0Boundary 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 extrapolationAPI Summary
| Method | Description |
|---|---|
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.deriv1 — Function
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/deriv3for 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'ssearch_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
maporfind_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.
- For
- 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())FastInterpolations.deriv2 — Function
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/deriv3for 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'ssearch_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
maporfind_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.
- For
- 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())FastInterpolations.deriv3 — Function
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/deriv3for 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'ssearch_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
maporfind_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.
- For
- 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())FastInterpolations.deriv_view — Function
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::Intmaps toderiv=order. - ND:
order::Intapplies the same order to all axes (e.g.,1→(1,1,...,1)), i.e. a mixed partial with orderorderalong 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 pointsFastInterpolations.AbstractDerivativeView — Type
AbstractDerivativeViewAbstract supertype for all derivative view wrappers.
Useful for dispatch when you want to accept any derivative view regardless of order or interpolant type.
Example
# Accept any derivative view
function integrate(d::AbstractDerivativeView, a, b)
# works with deriv1, deriv2, deriv3 of any interpolant
endSee also: DerivativeView, deriv1, deriv2, deriv3, deriv_view
FastInterpolations.DerivativeView — Type
DerivativeView{Order, ITP} <: AbstractDerivativeViewLightweight callable wrapper for derivative evaluation with a fixed derivative order. Enables broadcast and higher-order function composition.
Type Parameters
Order: Derivative orderIntfor 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}) ... endExample
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
See Also
- Visual Comparison: Side-by-side derivative plots
- Constant | Linear | Quadratic | Cubic