ENH: Add derivative point evaluation via coefficient differentiation#98
Merged
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
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
DerivFunEvalCacheplusset_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
SparseArraysas 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 degreek-1, piecewise linear on midpoints), with coefficients given by a sparse linear operator — both provided byBasisMatrices.derivative_op. This is the same constructionevalbaseuses 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 ordersorder(one per dimension; mixed and higher orders supported, e.g.(1, 1)or(2, 0)). ForSplineParamsthe order must be less than the spline degree, mirroringevalbase's restriction.set_coefs!(dfec, C): transforms the coefficients with a single precomputed sparse matvec (kronof the per-dimension operators); call once wheneverCchanges (e.g. once per sweep in a solver).funeval_point!(dfec, x): non-allocating single-point evaluation of the derivative interpolant.Project.toml: add theSparseArraysstdlib dependency.No solver code is touched; this PR is purely additive to
src/point_eval.jl.A BasisMatrices limitation found along the way
funevalwith derivative orders routes through theDirect/SplineSparsepath, whoseLinParamsmethod errors with "derivatives un-supported right now" — so BasisMatrices' ownfunevalcannot evaluateLinParamsderivatives, although the denseevalbase(p, x, order)supports them. The agreement tests therefore use the expanded derivative basis row built from per-dimensionevalbasecalls as the reference. Worth mentioning in the QuantEcon/BasisMatrices.jl#82 discussion.Verification
(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).🤖 Generated with Claude Code (Claude Fable 5)