Expand description
Piecewise Chebyshev polynomial fitting for bevy_autodiff.
This crate fits smooth piecewise Chebyshev polynomials to tabulated data
and integrates them with bevy_autodiff’s symbolic differentiation.
Derivatives are computed exactly via the chain rule — the Clenshaw evaluation
is built as graph nodes, so differentiate() just works.
§Important
The fit is an approximation, not exact. Derivatives of the fit approximate derivatives of the underlying function, with accuracy limited by:
- Polynomial degree (higher = more accurate, but amplifies noise)
- Data quality (noisy data → unreliable higher derivatives)
- Segment width (narrower segments → less polynomial bending)
Use FitResult::reliability to check how many derivatives are trustworthy.
§Quick Start
use bevy_autodiff_fit::{fit_dense, FitOptions, uniform_breakpoints, PiecewiseCompiled};
// Your data
let x: Vec<f64> = (0..=100).map(|i| i as f64 / 100.0).collect();
let y: Vec<f64> = x.iter().map(|&x| x.sin()).collect();
// Fit with a single segment, degree 20
let result = fit_dense(&x, &y, &[0.0, 1.0], &FitOptions { degree: 20 }).unwrap();
// Check reliability
println!("reliable up to order {}", result.reliability[0].max_reliable_order);
// Compile for fast evaluation with first derivatives
let mut compiled = PiecewiseCompiled::new(&result.fit, 1).unwrap();
compiled.eval(0.5).unwrap();
let value = compiled.value();
let derivative = compiled.partial(&[1]).unwrap();§Integration with bevy_autodiff
For composing fits with other AD operations:
use bevy_autodiff::AutoDiff;
use bevy_autodiff_fit::{fit_dense, FitOptions};
// Fit sin(x) on [0, 1]
let x_data: Vec<f64> = (0..=50).map(|i| i as f64 / 50.0).collect();
let y_data: Vec<f64> = x_data.iter().map(|&x| x.sin()).collect();
let result = fit_dense(&x_data, &y_data, &[0.0, 1.0], &FitOptions { degree: 10 }).unwrap();
let mut ad = AutoDiff::new();
let x = ad.var(0.5).unwrap();
// Build Clenshaw graph for segment 0
let f = result.fit.build_segment_graph(&mut ad, x, 0).unwrap();
// Compose with other operations
let g = ad.exp(f); // g = exp(fit(x))
// Differentiate — chain rule is automatic
let dg = ad.differentiate(g, x).unwrap();Re-exports§
pub use error::FitError;pub use piecewise::PiecewiseFit;pub use reliability::SegmentReliability;pub use continuity::ContinuityOptions;pub use continuity::fit_sparse_continuous;pub use piecewise2d::PiecewiseFit2D;pub use reliability2d::SegmentReliability2D;
Modules§
- chebyshev
- Core Chebyshev polynomial math.
- continuity
- Continuity constraints for piecewise Chebyshev fits.
- error
- Error types for the fitting crate.
- piecewise
- Piecewise Chebyshev fit management.
- piecewise2d
- Piecewise 2D Chebyshev fit management.
- reliability
- Derivative reliability estimation from Chebyshev coefficient decay.
- reliability2d
- Per-axis derivative reliability estimation for 2D fits.
Structs§
- Chebyshev
Segment - A single Chebyshev segment: coefficients on a domain [a, b].
- Chebyshev
Segment2D - A single 2D Chebyshev segment on [a_x, b_x] × [a_y, b_y].
- FitOptions
- Options controlling the fitting process.
- FitOptions2D
- Options controlling the 2D fitting process.
- FitResult
- The result of a fitting operation: fit + reliability metadata.
- FitResult2D
- The result of a 2D fitting operation: fit + reliability metadata.
- Piecewise
Compiled - Pre-compiled piecewise fit for fast repeated evaluation.
- Piecewise
Compiled2D - Pre-compiled 2D piecewise fit for fast repeated evaluation.
Functions§
- fit_
dense - Fit dense or regularly sampled data with user-specified breakpoints.
- fit_
dense_ 2d - Fit dense 2D data on a rectangular grid.
- fit_
sparse - Fit sparse or scattered data with user-specified breakpoints.
- fit_
sparse_ 2d - Fit scattered 2D data via least-squares.
- uniform_
breakpoints - Generate n equally-spaced breakpoints covering [x_min, x_max].