Skip to content

ENH: Add derivative point evaluation via coefficient differentiation#98

Merged
oyamad merged 1 commit into
mainfrom
deriv-point-eval
Jul 5, 2026
Merged

ENH: Add derivative point evaluation via coefficient differentiation#98
oyamad merged 1 commit into
mainfrom
deriv-point-eval

Conversation

@oyamad

@oyamad oyamad commented Jul 4, 2026

Copy link
Copy Markdown
Member

Summary

Extend the point-evaluation kernel (#93) to partial derivatives of the interpolant, as preparation for derivative-based inner optimization (#7, #73) and as part of the functionality proposed for upstreaming (#94, QuantEcon/BasisMatrices.jl#82).

Rather than adding per-point derivative recurrences, derivatives are obtained by coefficient differentiation: for each basis family, the derivative of an interpolant is itself an interpolant on the differentiated parameters (Chebyshev of degree n-1, spline of degree k-1, piecewise linear on midpoints), with coefficients given by a sparse linear operator — both provided by BasisMatrices.derivative_op. This is the same construction evalbase uses internally, so agreement with BasisMatrices is by construction, and the already-tested order-0 kernels are reused verbatim.

  • DerivFunEvalCache(basis, order::NTuple{N,Int}): workspace for the partial derivative of orders order (one per dimension; mixed and higher orders supported, e.g. (1, 1) or (2, 0)). For SplineParams the order must be less than the spline degree, mirroring evalbase's restriction.
  • set_coefs!(dfec, C): transforms the coefficients with a single precomputed sparse matvec (kron of the per-dimension operators); call once whenever C changes (e.g. once per sweep in a solver).
  • funeval_point!(dfec, x): non-allocating single-point evaluation of the derivative interpolant.
  • Project.toml: add the SparseArrays stdlib dependency.

No solver code is touched; this PR is purely additive to src/point_eval.jl.

A BasisMatrices limitation found along the way

funeval with derivative orders routes through the Direct/SplineSparse path, whose LinParams method errors with "derivatives un-supported right now" — so BasisMatrices' own funeval cannot evaluate LinParams derivatives, although the dense evalbase(p, x, order) supports them. The agreement tests therefore use the expanded derivative basis row built from per-dimension evalbase calls as the reference. Worth mentioning in the QuantEcon/BasisMatrices.jl#82 discussion.

Verification

  • 124 new agreement tests: Cheb (orders 1, 2), splines of degree 2-3 with even and uneven breaks (orders 1, 2), Lin (order 1), and mixed 2-D/3-D tensor bases with gradient, mixed-partial (1,1), and second-order (2,0) combinations, at points inside and outside the domain; plus coefficient-update and zero-allocation checks and error cases (spline order >= degree, negative order).
  • Full test suite passes (384 point-eval tests total).

🤖 Generated with Claude Code (Claude Fable 5)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the existing non-allocating point-evaluation kernel to support evaluating partial derivatives of tensor-product interpolants by differentiating coefficients (via BasisMatrices.derivative_op) and then reusing the existing order-0 point-evaluation machinery.

Changes:

  • Add DerivFunEvalCache plus set_coefs! for precomputing derivative-interpolant coefficients using a sparse (Kronecker-composed) linear operator.
  • Add a funeval_point!(dfec, x) entry point for allocation-free single-point derivative evaluation.
  • Expand the point-eval test suite with derivative agreement, coefficient-update, and allocation checks; add SparseArrays as a stdlib dependency.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.

File Description
src/point_eval.jl Adds derivative point-evaluation support via coefficient differentiation and sparse precomputed operators.
test/test_point_eval.jl Adds derivative agreement/reference tests and allocation checks for derivative point evaluation.
Project.toml Adds SparseArrays stdlib dependency and compat entry to support sparse operators used by derivative caches.

@oyamad oyamad merged commit c49db87 into main Jul 5, 2026
6 checks passed
@oyamad oyamad deleted the deriv-point-eval branch July 5, 2026 03:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants