feat: add analytical derivative to all 1D shapes#92
Open
lpatiny wants to merge 2 commits into
Open
Conversation
Every Shape1D class now implements a required `derivative(x)` method returning the value `fct`, the partial derivative with respect to `x`, and the partial derivatives with respect to each shape parameter (in `getParameters()` order), exposed via the new `Shape1DDerivative` type. Standalone `<shape>Derivative(...)` functions are added next to each `fct` (gaussian, lorentzian, pseudoVoigt, lorentzianDispersive, generalizedLorentzian, pseudoVoigtTCH). The pseudoVoigt and pseudoVoigtTCH derivatives inline their component math so the hot path allocates a single object. Each derivative is validated against central finite differences. This lets consumers (e.g. ml-spectra-fitting curve fitting) build an analytical Jacobian instead of approximating it numerically. Assisted-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #92 +/- ##
==========================================
+ Coverage 91.88% 93.18% +1.29%
==========================================
Files 23 23
Lines 813 968 +155
Branches 194 206 +12
==========================================
+ Hits 747 902 +155
Misses 65 65
Partials 1 1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
What
Adds an analytical
derivative(x)to everyShape1Dclass plus standalone<shape>Derivative(...)functions, so consumers can build an exact Jacobian instead of approximating it with finite differences.Shape1DClass.derivative(x)returning{ fct, dx, parameters }, whereparametersare the partials w.r.t. each shape parameter ingetParameters()order. Exposed via the new exportedShape1DDerivativetype.fct:gaussianDerivative,lorentzianDerivative,pseudoVoigtDerivative,lorentzianDispersiveDerivative,generalizedLorentzianDerivative,pseudoVoigtTCHDerivative.pseudoVoigtDerivativeandpseudoVoigtTCHDerivativeinline their component math so the hot path allocates a single object (instead of 3 / 2).Why
ml-spectra-fitting'soptimize()can pass these as an analyticaljacobianFunctionto Levenberg–Marquardt. Measured end-to-end speedup vs the current finite-difference behavior: ~4× (mixed shapes, 42 params) to ~5.8× (all pseudoVoigt, 48 params), same iteration count and final error.Tests
Each derivative is validated against central finite differences, and each class's
derivative()is checked to return parameters ingetParameters()order.npm testpasses (108 tests, lint, types, prettier).