Class ChebyshevSpline
- Namespace
- ChebyshevSharp
- Assembly
- ChebyshevSharp.dll
Piecewise Chebyshev interpolation with user-specified knots. Partitions the domain into sub-intervals at interior knots and builds an independent ChebyshevApproximation on each piece. Query points are routed to the appropriate piece for evaluation.
public class ChebyshevSpline
- Inheritance
-
ChebyshevSpline
- Inherited Members
Remarks
This is the correct approach when the target function has known singularities (kinks, discontinuities) at specific locations: place knots at those locations so that each piece is smooth, restoring spectral convergence.
Constructors
ChebyshevSpline(Func<double[], object?, double>, int, double[][], int[], double[][], int)
Create a new ChebyshevSpline.
public ChebyshevSpline(Func<double[], object?, double> function, int numDimensions, double[][] domain, int[] nNodes, double[][] knots, int maxDerivativeOrder = 2)
Parameters
functionFunc<double[], object, double>Function to approximate: f(point, data) -> double.
numDimensionsintNumber of input dimensions.
domaindouble[][]Bounds for each dimension as double[ndim][2].
nNodesint[]Number of Chebyshev nodes per dimension per piece.
knotsdouble[][]Interior knots for each dimension. Empty array for no knots.
maxDerivativeOrderintMaximum derivative order to support (default 2).
Properties
BuildTime
Wall-clock time (seconds) for the most recent Build() call.
public double BuildTime { get; }
Property Value
Built
Whether Build() has been called.
public bool Built { get; }
Property Value
Domain
Domain bounds for each dimension, as list of [lo, hi].
public double[][] Domain { get; }
Property Value
- double[][]
Function
The function to approximate. Null after load or from_values.
public Func<double[], object?, double>? Function { get; }
Property Value
Knots
Interior knots per dimension. Each sub-array is sorted ascending.
public double[][] Knots { get; }
Property Value
- double[][]
MaxDerivativeOrder
Maximum supported derivative order.
public int MaxDerivativeOrder { get; }
Property Value
NNodes
Number of Chebyshev nodes per dimension per piece.
public int[] NNodes { get; }
Property Value
- int[]
NumDimensions
Number of input dimensions.
public int NumDimensions { get; }
Property Value
NumPieces
Total number of pieces (Cartesian product of per-dimension intervals).
public int NumPieces { get; }
Property Value
TotalBuildEvals
Total number of function evaluations used during build.
public int TotalBuildEvals { get; }
Property Value
Methods
Build(bool)
Build all pieces by evaluating the function on each sub-domain.
public void Build(bool verbose = true)
Parameters
verboseboolIf true, print build progress.
ErrorEstimate()
Estimate the supremum-norm interpolation error. Returns the maximum error estimate across all pieces.
public double ErrorEstimate()
Returns
Eval(double[], int[])
Evaluate the spline approximation at a point.
public double Eval(double[] point, int[] derivativeOrder)
Parameters
pointdouble[]Evaluation point in the full domain.
derivativeOrderint[]Derivative order for each dimension (0 = function value).
Returns
- double
Approximated function value or derivative.
EvalBatch(double[][], int[])
Evaluate at multiple points, grouping by piece for efficiency.
public double[] EvalBatch(double[][] points, int[] derivativeOrder)
Parameters
pointsdouble[][]Evaluation points (N x numDimensions).
derivativeOrderint[]Derivative order for each dimension.
Returns
- double[]
Approximated values at each point.
EvalMulti(double[], int[][])
Evaluate multiple derivative orders at one point, sharing weights.
public double[] EvalMulti(double[] point, int[][] derivativeOrders)
Parameters
pointdouble[]Evaluation point in the full domain.
derivativeOrdersint[][]Each inner array specifies derivative order per dimension.
Returns
- double[]
One result per derivative order.
Extrude(params (int dimIndex, double[] bounds, int nNodes)[])
Add new dimensions where the function is constant.
public ChebyshevSpline Extrude(params (int dimIndex, double[] bounds, int nNodes)[] extrudeParams)
Parameters
Returns
- ChebyshevSpline
A new, higher-dimensional spline (already built).
FromValues(double[][], int, double[][], int[], double[][], int)
Create a spline from pre-computed function values on each piece.
public static ChebyshevSpline FromValues(double[][] pieceValues, int numDimensions, double[][] domain, int[] nNodes, double[][] knots, int maxDerivativeOrder = 2)
Parameters
pieceValuesdouble[][]Function values for each piece. Length must equal total pieces.
numDimensionsintNumber of dimensions.
domaindouble[][]Lower and upper bounds for each dimension.
nNodesint[]Number of Chebyshev nodes per dimension per piece.
knotsdouble[][]Knot positions for each dimension.
maxDerivativeOrderintMaximum derivative order (default 2).
Returns
- ChebyshevSpline
A fully built spline with Function=null.
Integrate(int[]?, (double lo, double hi)[]?)
Integrate the spline over one or more dimensions.
public object Integrate(int[]? dims = null, (double lo, double hi)[]? bounds = null)
Parameters
dimsint[]Dimensions to integrate out. Null = all.
bounds(double lo, double hi)[]Sub-interval bounds per dim. Null = full domain.
Returns
- object
Scalar if all dims integrated, otherwise a lower-dimensional spline.
Load(string)
Load a previously saved spline from a file.
public static ChebyshevSpline Load(string path)
Parameters
pathstringPath to the saved file.
Returns
- ChebyshevSpline
The restored spline.
Maximize(int?, Dictionary<int, double>?)
Find the maximum value of the spline along a dimension.
public (double value, double location) Maximize(int? dim = null, Dictionary<int, double>? fixedDims = null)
Parameters
dimint?Dimension along which to maximize.
fixedDimsDictionary<int, double>For multi-D, dict of dim_index -> value for all other dims.
Returns
Minimize(int?, Dictionary<int, double>?)
Find the minimum value of the spline along a dimension.
public (double value, double location) Minimize(int? dim = null, Dictionary<int, double>? fixedDims = null)
Parameters
dimint?Dimension along which to minimize.
fixedDimsDictionary<int, double>For multi-D, dict of dim_index -> value for all other dims.
Returns
Nodes(int, double[][], int[], double[][])
Generate Chebyshev nodes for every piece without evaluating any function.
public static SplineNodeInfo Nodes(int numDimensions, double[][] domain, int[] nNodes, double[][] knots)
Parameters
numDimensionsintNumber of dimensions.
domaindouble[][]Lower and upper bounds for each dimension.
nNodesint[]Number of Chebyshev nodes per dimension per piece.
knotsdouble[][]Knot positions for each dimension (may be empty).
Returns
- SplineNodeInfo
A SplineNodeInfo with per-piece node info.
Roots(int?, Dictionary<int, double>?)
Find all roots of the spline along a specified dimension.
public double[] Roots(int? dim = null, Dictionary<int, double>? fixedDims = null)
Parameters
dimint?Dimension along which to find roots.
fixedDimsDictionary<int, double>For multi-D, dict of dim_index -> value for all other dims.
Returns
- double[]
Sorted array of root locations.
Save(string)
Save the built spline to a file using JSON serialization.
public void Save(string path)
Parameters
pathstringDestination file path.
Slice(params (int dimIndex, double value)[])
Fix one or more dimensions at given values, reducing dimensionality.
public ChebyshevSpline Slice(params (int dimIndex, double value)[] sliceParams)
Parameters
Returns
- ChebyshevSpline
A new, lower-dimensional spline (already built).
ToReprString()
Compact string representation.
public string ToReprString()
Returns
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.
Operators
operator +(ChebyshevSpline, ChebyshevSpline)
Add two splines with the same grid and knots.
public static ChebyshevSpline operator +(ChebyshevSpline a, ChebyshevSpline b)
Parameters
Returns
operator /(ChebyshevSpline, double)
Divide spline by a scalar.
public static ChebyshevSpline operator /(ChebyshevSpline a, double scalar)
Parameters
aChebyshevSplinescalardouble
Returns
operator *(ChebyshevSpline, double)
Multiply spline by a scalar.
public static ChebyshevSpline operator *(ChebyshevSpline a, double scalar)
Parameters
aChebyshevSplinescalardouble
Returns
operator *(double, ChebyshevSpline)
Multiply scalar by spline.
public static ChebyshevSpline operator *(double scalar, ChebyshevSpline a)
Parameters
scalardoubleaChebyshevSpline
Returns
operator -(ChebyshevSpline, ChebyshevSpline)
Subtract two splines with the same grid and knots.
public static ChebyshevSpline operator -(ChebyshevSpline a, ChebyshevSpline b)
Parameters
Returns
operator -(ChebyshevSpline)
Negate spline.
public static ChebyshevSpline operator -(ChebyshevSpline a)