scirs2_interpolate/extrapolation_modules/
types.rs

1//! Core type definitions for extrapolation methods
2//!
3//! This module contains all the foundational data types used throughout
4//! the extrapolation system.
5
6use scirs2_core::numeric::Float;
7
8/// Enhanced extrapolation methods for interpolation.
9///
10/// This enum provides advanced extrapolation capabilities that go beyond
11/// the basic ExtrapolateMode enum. It allows for more sophisticated boundary
12/// handling and domain extension methods, including:
13///
14/// - Physics-informed extrapolation based on boundary derivatives
15/// - Polynomial extrapolation of various orders
16/// - Decay/growth models for asymptotic behavior
17/// - Periodic extension of the domain
18/// - Reflection-based extrapolation
19/// - Domain-specific extrapolation models
20#[derive(Debug, Clone, Copy, PartialEq)]
21pub enum ExtrapolationMethod {
22    /// No extrapolation - return an error for points outside the domain
23    Error,
24
25    /// Use the nearest endpoint value (constant extrapolation)
26    Constant,
27
28    /// Linear extrapolation based on endpoint derivatives
29    Linear,
30
31    /// Quadratic extrapolation based on endpoint values and derivatives
32    Quadratic,
33
34    /// Cubic extrapolation preserving both values and derivatives at boundaries
35    Cubic,
36
37    /// Extend domain as if the function is periodic
38    Periodic,
39
40    /// Reflect the function at the boundaries
41    Reflection,
42
43    /// Exponential decay/growth model for asymptotic behavior
44    Exponential,
45
46    /// Power law decay/growth model for asymptotic behavior
47    PowerLaw,
48
49    /// Spline-based extrapolation using the full spline continuation
50    Spline,
51
52    /// Akima extrapolation for stable polynomial continuation
53    Akima,
54
55    /// Sinusoidal extrapolation for periodic data
56    Sinusoidal,
57
58    /// Rational function extrapolation for poles/zeros behavior
59    Rational,
60
61    /// Confidence-based extrapolation with uncertainty bands
62    Confidence,
63
64    /// Ensemble extrapolation combining multiple methods
65    Ensemble,
66
67    /// Adaptive extrapolation that selects the best method locally
68    Adaptive,
69
70    /// Autoregressive extrapolation using AR models
71    Autoregressive,
72
73    /// Return zeros for all out-of-bounds points (SciPy 'zeros' mode)
74    Zeros,
75
76    /// Use nearest boundary value (SciPy 'nearest'/'edge' mode)
77    Nearest,
78
79    /// Mirror reflection without repeating edge values (SciPy 'mirror' mode)
80    Mirror,
81
82    /// Periodic wrapping (SciPy 'wrap' mode)
83    Wrap,
84
85    /// Clamped boundary conditions with zero derivatives
86    Clamped,
87
88    /// Grid-specific mirror mode for structured grids
89    GridMirror,
90
91    /// Grid-specific constant mode for structured grids
92    GridConstant,
93
94    /// Grid-specific wrap mode for structured grids
95    GridWrap,
96}
97
98/// Direction for extrapolation
99#[derive(Debug, Clone, Copy, PartialEq)]
100pub enum ExtrapolationDirection {
101    /// Extrapolation below the lower boundary
102    Lower,
103
104    /// Extrapolation above the upper boundary
105    Upper,
106}
107
108/// Ensemble combination strategies for multiple extrapolation methods
109#[derive(Debug, Clone, Copy, PartialEq)]
110pub enum EnsembleCombinationStrategy {
111    /// Simple arithmetic mean of all method predictions
112    Mean,
113
114    /// Weighted average based on confidence scores
115    WeightedMean,
116
117    /// Use median of all predictions (robust to outliers)
118    Median,
119
120    /// Use method with highest confidence for local region
121    BestMethod,
122
123    /// Weighted combination optimized for minimum variance
124    MinimumVariance,
125
126    /// Bayesian model averaging
127    BayesianAveraging,
128
129    /// Use methods voted by majority (discrete classification)
130    Voting,
131
132    /// Stack multiple methods using a meta-learner
133    Stacking,
134}
135
136/// Adaptive selection criteria for choosing extrapolation methods
137#[derive(Debug, Clone, Copy, PartialEq)]
138pub enum AdaptiveSelectionCriterion {
139    /// Minimize cross-validation error on nearby data
140    CrossValidationError,
141
142    /// Maximize smoothness at boundary
143    BoundarySmoothness,
144
145    /// Minimize curvature discontinuity
146    CurvatureContinuity,
147
148    /// Use physics-informed metrics
149    PhysicsConsistency,
150
151    /// Minimize extrapolation uncertainty
152    UncertaintyMinimization,
153
154    /// Optimize for specific application domain
155    DomainSpecific,
156
157    /// Use information-theoretic criteria (AIC/BIC)
158    InformationCriterion,
159
160    /// Combine multiple criteria with weighted scoring
161    MultiCriteria,
162}
163
164/// Autoregressive model fitting methods
165#[derive(Debug, Clone, Copy, PartialEq)]
166pub enum ARFittingMethod {
167    /// Yule-Walker equations (method of moments)
168    YuleWalker,
169
170    /// Burg's method (maximum entropy)
171    Burg,
172
173    /// Least squares estimation
174    LeastSquares,
175
176    /// Maximum likelihood estimation
177    MaximumLikelihood,
178
179    /// Modified covariance method
180    ModifiedCovariance,
181
182    /// Forward-backward linear prediction
183    ForwardBackward,
184}
185
186/// Physics laws for informed extrapolation
187#[derive(Debug, Clone, Copy)]
188pub enum PhysicsLaw {
189    /// Mass conservation (non-negative, decay to zero)
190    MassConservation,
191    /// Energy conservation (quadratic behavior)
192    EnergyConservation,
193    /// Momentum conservation (linear behavior)
194    MomentumConservation,
195}
196
197/// Boundary condition types for physics-informed extrapolation
198#[derive(Debug, Clone, Copy)]
199pub enum BoundaryType {
200    /// Fixed value at boundary (Dirichlet)
201    Dirichlet,
202    /// Fixed derivative at boundary (Neumann)
203    Neumann,
204    /// Linear combination of value and derivative (Robin)
205    Robin,
206    /// Absorbing boundary with exponential decay
207    Absorbing,
208}
209
210/// Data characteristics for adaptive extrapolation
211#[derive(Debug, Clone)]
212pub struct DataCharacteristics<T: Float> {
213    /// Whether the data appears periodic
214    pub is_periodic: bool,
215    /// Estimated period if periodic
216    pub estimated_period: Option<T>,
217    /// Whether the data is monotonic
218    pub is_monotonic: bool,
219    /// Whether the data follows exponential-like growth/decay
220    pub is_exponential_like: bool,
221    /// Whether the data is oscillatory
222    pub is_oscillatory: bool,
223    /// Characteristic scale of the data
224    pub characteristic_scale: T,
225}
226
227impl<T: Float> Default for DataCharacteristics<T> {
228    fn default() -> Self {
229        Self {
230            is_periodic: false,
231            estimated_period: None,
232            is_monotonic: false,
233            is_exponential_like: false,
234            is_oscillatory: false,
235            characteristic_scale: T::one(),
236        }
237    }
238}
239
240impl<T: Float> DataCharacteristics<T> {
241    /// Create new data characteristics with default values
242    pub fn new() -> Self {
243        Self::default()
244    }
245
246    /// Set whether the data is periodic
247    pub fn with_periodic(mut self, periodic: bool, period: Option<T>) -> Self {
248        self.is_periodic = periodic;
249        self.estimated_period = period;
250        self
251    }
252
253    /// Set whether the data is monotonic
254    pub fn with_monotonic(mut self, monotonic: bool) -> Self {
255        self.is_monotonic = monotonic;
256        self
257    }
258
259    /// Set whether the data follows exponential behavior
260    pub fn with_exponential_like(mut self, exponential: bool) -> Self {
261        self.is_exponential_like = exponential;
262        self
263    }
264
265    /// Set whether the data is oscillatory
266    pub fn with_oscillatory(mut self, oscillatory: bool) -> Self {
267        self.is_oscillatory = oscillatory;
268        self
269    }
270
271    /// Set the characteristic scale
272    pub fn with_scale(mut self, scale: T) -> Self {
273        self.characteristic_scale = scale;
274        self
275    }
276}