scirs2-interpolate 0.4.0

Interpolation module for SciRS2 (scirs2-interpolate)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Common traits for interpolation types
//!
//! This module defines standard trait bounds used throughout the interpolation library
//! to ensure API consistency and reduce repetition.

use scirs2_core::ndarray::{ArrayView1, ArrayView2};
use scirs2_core::numeric::{Float, FromPrimitive};
use std::fmt::{Debug, Display, LowerExp};
use std::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign};

/// Type alias for confidence intervals result
pub type ConfidenceIntervals<T> = Vec<(T, Vec<(T, T)>)>;

/// Standard floating-point type for interpolation operations
///
/// This trait combines all the common bounds needed for interpolation algorithms,
/// providing a single consistent constraint across the library.
pub trait InterpolationFloat:
    Float
    + FromPrimitive
    + Debug
    + Display
    + LowerExp
    + AddAssign
    + SubAssign
    + MulAssign
    + DivAssign
    + RemAssign
    + Send
    + Sync
    + Default
    + Copy
    + std::iter::Sum
    + 'static
{
    /// Default epsilon value for this floating-point type
    fn default_epsilon() -> Self {
        Self::from_f64(1e-9).unwrap_or_else(|| Self::epsilon())
    }

    /// Default tolerance for iterative algorithms
    fn default_tolerance() -> Self {
        Self::from_f64(1e-12)
            .unwrap_or_else(|| Self::epsilon() * Self::from_f64(100.0).expect("Operation failed"))
    }
}

// Implement for standard floating-point types
impl InterpolationFloat for f32 {}
impl InterpolationFloat for f64 {}

/// Standard input data format for interpolation
///
/// This trait defines the expected format for input data points and values
pub trait InterpolationData<T: InterpolationFloat> {
    /// Get the spatial coordinates of the data points
    fn points(&self) -> ArrayView2<T>;

    /// Get the function values at the data points
    fn values(&self) -> ArrayView1<T>;

    /// Get the number of data points
    fn len(&self) -> usize {
        self.values().len()
    }

    /// Check if the data is empty
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get the spatial dimension of the data
    fn dim(&self) -> usize {
        self.points().ncols()
    }
}

/// Standard query interface for interpolators
pub trait Interpolator<T: InterpolationFloat> {
    /// Evaluate the interpolator at given query points
    fn evaluate(&self, querypoints: &ArrayView2<T>) -> crate::InterpolateResult<Vec<T>>;

    /// Evaluate the interpolator at a single point
    fn evaluate_single(&self, point: &ArrayView1<T>) -> crate::InterpolateResult<T> {
        let query = point.view().insert_axis(scirs2_core::ndarray::Axis(0));
        self.evaluate(&query).map(|v| v[0])
    }

    /// Evaluate derivatives at query points (if supported)
    fn evaluate_derivatives(
        &self,
        query_points: &ArrayView2<T>,
        order: usize,
    ) -> crate::InterpolateResult<Vec<Vec<T>>> {
        let _ = (query_points, order);
        Err(crate::InterpolateError::NotImplemented(
            "Derivative evaluation not implemented for this interpolator".to_string(),
        ))
    }

    /// Evaluate with options for advanced control
    fn evaluate_with_options(
        &self,
        query_points: &ArrayView2<T>,
        options: &EvaluationOptions,
    ) -> crate::InterpolateResult<BatchEvaluationResult<T>> {
        let _ = options;
        let values = self.evaluate(query_points)?;
        Ok(BatchEvaluationResult {
            values,
            uncertainties: None,
            out_of_bounds: Vec::new(),
        })
    }

    /// Get the spatial dimension of the interpolator
    fn dimension(&self) -> usize;

    /// Get the number of data points used to construct this interpolator
    fn len(&self) -> usize;

    /// Check if the interpolator is empty (no data points)
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Advanced interpolator interface for methods with uncertainty quantification
pub trait UncertaintyInterpolator<T: InterpolationFloat>: Interpolator<T> {
    /// Evaluate with uncertainty estimates
    fn evaluate_with_uncertainty(
        &self,
        query_points: &ArrayView2<T>,
    ) -> crate::InterpolateResult<(Vec<T>, Vec<T>)>;

    /// Evaluate confidence intervals at specified levels
    fn evaluate_confidence_intervals(
        &self,
        query_points: &ArrayView2<T>,
        confidence_levels: &[T],
    ) -> crate::InterpolateResult<ConfidenceIntervals<T>>;
}

/// Adaptive interpolator interface for methods that can refine their approximation
pub trait AdaptiveInterpolator<T: InterpolationFloat>: Interpolator<T> {
    /// Add new data _points to refine the interpolation
    fn add_points(
        &mut self,
        new_points: &ArrayView2<T>,
        new_values: &ArrayView1<T>,
    ) -> crate::InterpolateResult<()>;

    /// Remove data _points from the interpolation
    fn remove_points(&mut self, indices: &[usize]) -> crate::InterpolateResult<()>;

    /// Update the interpolation with new data
    fn update(
        &mut self,
        points: &ArrayView2<T>,
        values: &ArrayView1<T>,
    ) -> crate::InterpolateResult<()>;
}

/// Configuration trait for interpolation methods
pub trait InterpolationConfig: Clone + Debug {
    /// Validate the configuration
    fn validate(&self) -> crate::InterpolateResult<()>;

    /// Get default configuration
    fn default() -> Self;
}

/// Builder pattern trait for consistent API
pub trait InterpolatorBuilder<T: InterpolationFloat> {
    /// The interpolator type this builder creates
    type Interpolator: Interpolator<T>;

    /// The configuration type for this builder
    type Config: InterpolationConfig;

    /// Build the interpolator with the given data and configuration
    fn build(
        points: &ArrayView2<T>,
        values: &ArrayView1<T>,
        config: Self::Config,
    ) -> crate::InterpolateResult<Self::Interpolator>;
}

/// Standard evaluation options
#[derive(Debug, Clone)]
pub struct EvaluationOptions {
    /// Number of parallel workers (None for automatic)
    pub workers: Option<usize>,

    /// Whether to use caching for repeated evaluations
    pub use_cache: bool,

    /// Whether to validate input bounds
    pub validate_bounds: bool,

    /// Fill value for out-of-bounds queries
    pub fill_value: Option<f64>,
}

impl Default for EvaluationOptions {
    fn default() -> Self {
        Self {
            workers: None,
            use_cache: false,
            validate_bounds: true,
            fill_value: None,
        }
    }
}

/// Standard result for batch evaluation
pub struct BatchEvaluationResult<T: InterpolationFloat> {
    /// The interpolated values
    pub values: Vec<T>,

    /// Optional uncertainty estimates (for methods that support it)
    pub uncertainties: Option<Vec<T>>,

    /// Indices of out-of-bounds points (if any)
    pub out_of_bounds: Vec<usize>,
}

/// Spline-specific interface for methods that support derivatives and integrals
pub trait SplineInterpolator<T: InterpolationFloat>: Interpolator<T> {
    /// Evaluate the nth derivative at query points
    fn derivative(
        &self,
        query_points: &ArrayView2<T>,
        order: usize,
    ) -> crate::InterpolateResult<Vec<T>>;

    /// Evaluate the definite integral over specified bounds
    fn integrate(&self, bounds: &[(T, T)]) -> crate::InterpolateResult<Vec<T>>;

    /// Get the antiderivative as a new spline
    fn antiderivative(&self) -> crate::InterpolateResult<Box<dyn SplineInterpolator<T>>>;

    /// Find roots of the spline within given bounds
    fn find_roots(&self, bounds: &[(T, T)], tolerance: T) -> crate::InterpolateResult<Vec<T>>;

    /// Find extrema (local minima and maxima) within given bounds
    fn find_extrema(
        &self,
        bounds: &[(T, T)],
        tolerance: T,
    ) -> crate::InterpolateResult<Vec<(T, T, ExtremaType)>>;
}

/// Type of extrema for spline analysis
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ExtremaType {
    /// Local minimum
    Minimum,
    /// Local maximum
    Maximum,
    /// Inflection point
    InflectionPoint,
}

/// Performance monitoring interface for interpolation methods
pub trait PerformanceMonitoring {
    /// Get timing statistics for the last operation
    fn get_timing_stats(&self) -> Option<TimingStats>;

    /// Get memory usage statistics
    fn get_memory_stats(&self) -> Option<MemoryStats>;

    /// Enable or disable performance monitoring
    fn set_monitoring_enabled(&mut self, enabled: bool);

    /// Reset performance counters
    fn reset_stats(&mut self);
}

/// Timing statistics for performance monitoring
#[derive(Debug, Clone)]
pub struct TimingStats {
    /// Time spent in construction (microseconds)
    pub construction_time_us: u64,
    /// Time spent in evaluation (microseconds)
    pub evaluation_time_us: u64,
    /// Number of evaluations performed
    pub evaluation_count: u64,
    /// Average time per evaluation (microseconds)
    pub avg_evaluation_time_us: f64,
}

/// Memory usage statistics
#[derive(Debug, Clone)]
pub struct MemoryStats {
    /// Memory used by the interpolator (bytes)
    pub interpolator_memory_bytes: usize,
    /// Memory used by cached data (bytes)  
    pub cache_memory_bytes: usize,
    /// Total allocations
    pub total_allocations: usize,
    /// Peak memory usage (bytes)
    pub peak_memory_bytes: usize,
}

/// Serialization support for interpolators
pub trait SerializableInterpolator<T: InterpolationFloat> {
    /// Serialize the interpolator to bytes
    fn serialize(&self) -> crate::InterpolateResult<Vec<u8>>;

    /// Deserialize the interpolator from bytes
    fn deserialize(data: &[u8]) -> crate::InterpolateResult<Self>
    where
        Self: Sized;

    /// Get the serialization format version
    fn serialization_version(&self) -> u32;
}

/// Parallel evaluation support for interpolators
pub trait ParallelInterpolator<T: InterpolationFloat>: Interpolator<T> + Sync + Send {
    /// Evaluate at query points using parallel execution
    fn evaluate_parallel(
        &self,
        query_points: &ArrayView2<T>,
        num_threads: Option<usize>,
    ) -> crate::InterpolateResult<Vec<T>>;

    /// Check if this interpolator supports parallel evaluation
    fn supports_parallel(&self) -> bool {
        true
    }

    /// Get the recommended number of threads for this interpolator
    fn recommended_thread_count(&self, query_size: usize) -> usize {
        (query_size / 1000).max(1).min(num_cpus::get())
    }
}

/// Common validation utilities
pub mod validation {
    use super::*;

    /// Validate that points and values have consistent dimensions
    pub fn validate_data_consistency<T: InterpolationFloat>(
        points: &ArrayView2<T>,
        values: &ArrayView1<T>,
    ) -> crate::InterpolateResult<()> {
        if points.nrows() != values.len() {
            return Err(crate::InterpolateError::invalid_input(format!(
                "Inconsistent data dimensions: {} points but {} values",
                points.nrows(),
                values.len()
            )));
        }

        if points.is_empty() || values.is_empty() {
            return Err(crate::InterpolateError::invalid_input("Empty input data"));
        }

        Ok(())
    }

    /// Validate query points have correct dimension
    pub fn validate_query_dimension<T: InterpolationFloat>(
        data_dim: usize,
        query_points: &ArrayView2<T>,
    ) -> crate::InterpolateResult<()> {
        if query_points.ncols() != data_dim {
            return Err(crate::InterpolateError::invalid_input(format!(
                "Query dimension {} does not match data dimension {}",
                query_points.ncols(),
                data_dim
            )));
        }
        Ok(())
    }

    /// Validate that query points are within valid bounds (if bounds checking is enabled)
    pub fn validate_query_bounds<T: InterpolationFloat>(
        query_points: &ArrayView2<T>,
        bounds: &[(T, T)],
        options: &EvaluationOptions,
    ) -> crate::InterpolateResult<Vec<usize>> {
        if !options.validate_bounds {
            return Ok(Vec::new());
        }

        let mut out_of_bounds = Vec::new();

        for (row_idx, point) in query_points.outer_iter().enumerate() {
            for (&coord, &(min_bound, max_bound)) in point.iter().zip(bounds.iter()) {
                if coord < min_bound || coord > max_bound {
                    out_of_bounds.push(row_idx);
                    break;
                }
            }
        }

        Ok(out_of_bounds)
    }

    /// Validate interpolator configuration parameters
    pub fn validate_config<C: InterpolationConfig>(config: &C) -> crate::InterpolateResult<()> {
        config.validate()
    }
}