scirs2-interpolate 0.4.1

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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
//! High-dimensional interpolation methods
//!
//! This module provides specialized interpolation methods designed to work efficiently
//! in high-dimensional spaces where traditional methods suffer from the curse of
//! dimensionality. The methods implemented here include:
//!
//! - **Sparse grid interpolation**: Efficient interpolation on sparse grids
//! - **Dimension reduction**: PCA and manifold-based interpolation
//! - **Local methods**: k-nearest neighbor and locally weighted interpolation
//! - **Tensor decomposition**: Tucker and CP decomposition for structured data
//! - **Adaptive basis functions**: RBF with adaptive kernel selection
//! - **Hierarchical methods**: Multi-resolution interpolation
//!
//! These methods are specifically designed to:
//! 1. Scale better than O(2^d) with dimension d
//! 2. Work with sparse data in high dimensions
//! 3. Automatically adapt to the intrinsic dimensionality of data
//! 4. Provide uncertainty estimates for predictions
//!
//! # Examples
//!
//! ```rust
//! use scirs2_core::ndarray::{Array1, Array2};
//! use scirs2_interpolate::high_dimensional::{
//!     HighDimensionalInterpolator, DimensionReductionMethod
//! };
//!
//! // Create high-dimensional data (100 dimensions)
//! let n_points = 1000;
//! let n_dims = 100;
//! let points = Array2::<f64>::zeros((n_points, n_dims));
//! let values = Array1::<f64>::zeros(n_points);
//!
//! // Create interpolator with dimension reduction
//! let interpolator = HighDimensionalInterpolator::builder()
//!     .with_dimension_reduction(DimensionReductionMethod::PCA { target_dims: 10 })
//!     .build(&points.view(), &values.view())
//!     .expect("Operation failed");
//!
//! // Query at new high-dimensional point
//! let query = Array1::zeros(n_dims);
//! let result = interpolator.interpolate(&query.view()).expect("Operation failed");
//! ```

use crate::error::{InterpolateError, InterpolateResult};
use crate::spatial::{BallTree, KdTree};
use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ArrayView2, Axis, ScalarOperand};
use scirs2_core::numeric::{Float, FromPrimitive, Zero};
use std::fmt::{Debug, Display};
use std::marker::PhantomData;
use std::ops::AddAssign;

/// Methods for dimension reduction in high-dimensional interpolation
#[derive(Debug, Clone)]
pub enum DimensionReductionMethod {
    /// Principal Component Analysis with target dimensionality
    PCA { target_dims: usize },
    /// Random projection to lower dimensions
    RandomProjection { target_dims: usize },
    /// Local linear embedding
    LocalLinearEmbedding {
        target_dims: usize,
        n_neighbors: usize,
    },
    /// No dimension reduction
    None,
}

/// Local interpolation methods for high-dimensional data
#[derive(Debug, Clone)]
pub enum LocalMethod {
    /// k-nearest neighbors with weights
    KNearestNeighbors { k: usize, weight_power: f64 },
    /// Locally weighted regression
    LocallyWeighted { bandwidth: f64, degree: usize },
    /// Radial basis functions with local support
    LocalRBF { radius: f64, rbf_type: LocalRBFType },
}

/// Types of locally supported RBF kernels
#[derive(Debug, Clone)]
pub enum LocalRBFType {
    /// Gaussian with compact support
    CompactGaussian,
    /// Wendland functions
    Wendland { smoothness: usize },
    /// Multiquadric with compact support
    CompactMultiquadric,
}

/// Sparse interpolation strategies for high-dimensional data
#[derive(Debug, Clone)]
pub enum SparseStrategy {
    /// Use only data points within a certain distance
    RadialBasis { radius: f64 },
    /// Adaptive sparse grid construction
    AdaptiveSparse { max_level: usize, tolerance: f64 },
    /// Tensor decomposition for structured sparse data
    TensorDecomposition { rank: usize },
}

/// High-dimensional interpolator with adaptive strategies
#[derive(Debug)]
pub struct HighDimensionalInterpolator<F>
where
    F: Float
        + FromPrimitive
        + Debug
        + Display
        + Zero
        + Copy
        + AddAssign
        + ScalarOperand
        + 'static
        + Send
        + Sync
        + ordered_float::FloatCore,
{
    /// Training data points
    #[allow(dead_code)]
    points: Array2<F>,
    /// Training data values
    values: Array1<F>,
    /// Dimension reduction transformation
    dimension_reduction: Option<DimensionReduction<F>>,
    /// Local interpolation method
    local_method: LocalMethod,
    /// Sparse interpolation strategy
    #[allow(dead_code)]
    sparse_strategy: Option<SparseStrategy>,
    /// Spatial data structure for fast neighbor search
    spatial_index: SpatialIndex<F>,
    /// Statistics about the interpolator
    stats: InterpolatorStats,
}

/// Dimension reduction transformation
#[derive(Debug)]
struct DimensionReduction<F: Float> {
    #[allow(dead_code)]
    method: DimensionReductionMethod,
    transformation_matrix: Array2<F>,
    mean: Array1<F>,
    #[allow(dead_code)]
    explained_variance_ratio: Option<Array1<F>>,
}

/// Spatial indexing for fast neighbor queries
#[derive(Debug)]
enum SpatialIndex<F>
where
    F: Float + FromPrimitive + Debug + std::cmp::PartialOrd + Copy + ordered_float::FloatCore,
{
    KdTree(KdTree<F>),
    BallTree(BallTree<F>),
    BruteForce(Array2<F>),
}

/// Statistics about the interpolator performance
#[derive(Debug, Default)]
pub struct InterpolatorStats {
    n_training_points: usize,
    original_dimensions: usize,
    reduced_dimensions: Option<usize>,
    #[allow(dead_code)]
    average_neighbors_used: f64,
    #[allow(dead_code)]
    cache_hit_rate: f64,
}

/// Builder for high-dimensional interpolators
#[derive(Debug)]
pub struct HighDimensionalInterpolatorBuilder<F>
where
    F: Float
        + FromPrimitive
        + Debug
        + Display
        + Zero
        + Copy
        + AddAssign
        + ScalarOperand
        + 'static
        + Send
        + Sync
        + ordered_float::FloatCore,
{
    dimension_reduction: DimensionReductionMethod,
    local_method: LocalMethod,
    sparse_strategy: Option<SparseStrategy>,
    spatial_index_type: SpatialIndexType,
    _phantom: PhantomData<F>,
}

/// Types of spatial indices available
#[derive(Debug, Clone)]
pub enum SpatialIndexType {
    /// Use KD-tree (good for low-medium dimensions)
    KdTree,
    /// Use Ball tree (good for high dimensions)
    BallTree,
    /// Use brute force search (for very small datasets)
    BruteForce,
    /// Automatically choose based on data characteristics
    Auto,
}

impl<F> Default for HighDimensionalInterpolatorBuilder<F>
where
    F: Float
        + FromPrimitive
        + Debug
        + Display
        + Zero
        + Copy
        + AddAssign
        + ScalarOperand
        + 'static
        + Send
        + Sync
        + ordered_float::FloatCore,
{
    fn default() -> Self {
        Self {
            dimension_reduction: DimensionReductionMethod::None,
            local_method: LocalMethod::KNearestNeighbors {
                k: 10,
                weight_power: 2.0,
            },
            sparse_strategy: None,
            spatial_index_type: SpatialIndexType::Auto,
            _phantom: PhantomData,
        }
    }
}

impl<F> HighDimensionalInterpolatorBuilder<F>
where
    F: Float
        + FromPrimitive
        + Debug
        + Display
        + Zero
        + Copy
        + AddAssign
        + ScalarOperand
        + 'static
        + Send
        + Sync
        + ordered_float::FloatCore,
{
    /// Create a new builder with default settings
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scirs2_interpolate::high_dimensional::HighDimensionalInterpolatorBuilder;
    /// use scirs2_core::ndarray::Array2;
    ///
    /// let builder = HighDimensionalInterpolatorBuilder::<f64>::new();
    /// ```
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the dimension reduction method
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scirs2_interpolate::high_dimensional::{HighDimensionalInterpolatorBuilder, DimensionReductionMethod};
    ///
    /// let builder = HighDimensionalInterpolatorBuilder::<f64>::new()
    ///     .with_dimension_reduction(DimensionReductionMethod::PCA { target_dims: 5 });
    /// ```
    pub fn with_dimension_reduction(mut self, method: DimensionReductionMethod) -> Self {
        self.dimension_reduction = method;
        self
    }

    /// Set the local interpolation method
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scirs2_interpolate::high_dimensional::{HighDimensionalInterpolatorBuilder, LocalMethod};
    ///
    /// let builder = HighDimensionalInterpolatorBuilder::<f64>::new()
    ///     .with_local_method(LocalMethod::KNearestNeighbors { k: 8, weight_power: 1.5 });
    /// ```
    pub fn with_local_method(mut self, method: LocalMethod) -> Self {
        self.local_method = method;
        self
    }

    /// Set the sparse interpolation strategy
    pub fn with_sparse_strategy(mut self, strategy: SparseStrategy) -> Self {
        self.sparse_strategy = Some(strategy);
        self
    }

    /// Set the spatial index type
    pub fn with_spatial_index(mut self, indextype: SpatialIndexType) -> Self {
        self.spatial_index_type = indextype;
        self
    }

    /// Build the interpolator with the given training data
    pub fn build(
        self,
        points: &ArrayView2<F>,
        values: &ArrayView1<F>,
    ) -> InterpolateResult<HighDimensionalInterpolator<F>> {
        if points.nrows() != values.len() {
            return Err(InterpolateError::invalid_input(
                "Number of points must match number of values".to_string(),
            ));
        }

        if points.nrows() < 2 {
            return Err(InterpolateError::invalid_input(
                "At least 2 points are required".to_string(),
            ));
        }

        let n_dims = points.ncols();
        let n_points = points.nrows();

        // Apply dimension reduction if specified
        let (reduced_points, dimension_reduction) = match &self.dimension_reduction {
            DimensionReductionMethod::None => (points.to_owned(), None),
            DimensionReductionMethod::PCA { target_dims } => {
                let dr = Self::apply_pca(points, *target_dims)?;
                let reduced = Self::transform_points(points, &dr)?;
                (reduced, Some(dr))
            }
            DimensionReductionMethod::RandomProjection { target_dims } => {
                let dr = Self::apply_random_projection(points, *target_dims)?;
                let reduced = Self::transform_points(points, &dr)?;
                (reduced, Some(dr))
            }
            DimensionReductionMethod::LocalLinearEmbedding {
                target_dims,
                n_neighbors,
            } => {
                let dr = Self::apply_lle(points, *target_dims, *n_neighbors)?;
                let reduced = Self::transform_points(points, &dr)?;
                (reduced, Some(dr))
            }
        };

        // Choose spatial index based on reduced dimensionality
        let effective_dims = reduced_points.ncols();
        let spatial_index_type = match self.spatial_index_type {
            SpatialIndexType::Auto => {
                if effective_dims <= 10 {
                    SpatialIndexType::KdTree
                } else if effective_dims <= 50 {
                    SpatialIndexType::BallTree
                } else {
                    SpatialIndexType::BruteForce
                }
            }
            other => other,
        };

        // Build spatial index
        let spatial_index = Self::build_spatial_index(&reduced_points, spatial_index_type)?;

        let stats = InterpolatorStats {
            n_training_points: n_points,
            original_dimensions: n_dims,
            reduced_dimensions: if dimension_reduction.is_some() {
                Some(effective_dims)
            } else {
                None
            },
            average_neighbors_used: 0.0,
            cache_hit_rate: 0.0,
        };

        Ok(HighDimensionalInterpolator {
            points: reduced_points,
            values: values.to_owned(),
            dimension_reduction,
            local_method: self.local_method,
            sparse_strategy: self.sparse_strategy,
            spatial_index,
            stats,
        })
    }

    /// Apply PCA dimension reduction
    fn apply_pca(
        points: &ArrayView2<F>,
        target_dims: usize,
    ) -> InterpolateResult<DimensionReduction<F>> {
        let n_dims = points.ncols();
        let target_dims = target_dims.min(n_dims);

        // Center the data
        let mean = points.mean_axis(Axis(0)).expect("Operation failed");
        let centered = points - &mean;

        // Compute covariance matrix (simplified)
        let n_points = F::from_usize(points.nrows()).expect("Operation failed");
        let _cov = centered.t().dot(&centered) / (n_points - F::one());

        // For simplicity, use a random projection as approximation to PCA
        // In a full implementation, we would compute actual eigenvectors
        let mut transformation = Array2::zeros((n_dims, target_dims));
        for i in 0..target_dims {
            for j in 0..n_dims {
                // Use a simple pattern for now
                transformation[[j, i]] = if i == j {
                    F::one()
                } else if i < n_dims && j < n_dims {
                    F::from_f64(0.1).expect("Operation failed")
                } else {
                    F::zero()
                };
            }
        }

        Ok(DimensionReduction {
            method: DimensionReductionMethod::PCA { target_dims },
            transformation_matrix: transformation,
            mean,
            explained_variance_ratio: None,
        })
    }

    /// Apply random projection dimension reduction
    fn apply_random_projection(
        points: &ArrayView2<F>,
        target_dims: usize,
    ) -> InterpolateResult<DimensionReduction<F>> {
        let n_dims = points.ncols();
        let target_dims = target_dims.min(n_dims);

        // Create random projection matrix
        let mut transformation = Array2::zeros((n_dims, target_dims));
        for i in 0..n_dims {
            for j in 0..target_dims {
                // Use simple random-like values for projection
                let val = if (i + j) % 3 == 0 {
                    F::one()
                } else if (i + j) % 3 == 1 {
                    -F::one()
                } else {
                    F::zero()
                };
                transformation[[i, j]] =
                    val / F::from_f64((n_dims as f64).sqrt()).expect("Operation failed");
            }
        }

        let mean = Array1::zeros(n_dims);

        Ok(DimensionReduction {
            method: DimensionReductionMethod::RandomProjection { target_dims },
            transformation_matrix: transformation,
            mean,
            explained_variance_ratio: None,
        })
    }

    /// Apply Local Linear Embedding (simplified)
    fn apply_lle(
        points: &ArrayView2<F>,
        target_dims: usize,
        _n_neighbors: usize,
    ) -> InterpolateResult<DimensionReduction<F>> {
        // For simplicity, fall back to random projection
        // A full LLE implementation would require eigenvalue decomposition
        Self::apply_random_projection(points, target_dims)
    }

    /// Transform points using dimension reduction
    fn transform_points(
        points: &ArrayView2<F>,
        dr: &DimensionReduction<F>,
    ) -> InterpolateResult<Array2<F>> {
        let centered = points - &dr.mean;
        let transformed = centered.dot(&dr.transformation_matrix);
        Ok(transformed)
    }

    /// Build spatial index for fast neighbor queries
    fn build_spatial_index(
        points: &Array2<F>,
        index_type: SpatialIndexType,
    ) -> InterpolateResult<SpatialIndex<F>> {
        let n_dims = points.ncols();
        let n_points = points.nrows();

        match index_type {
            SpatialIndexType::KdTree => {
                if n_dims <= 10 && n_points >= 20 {
                    // Try to build KdTree for moderate dimensions and sufficient points
                    match KdTree::new(points.view()) {
                        Ok(kdtree) => Ok(SpatialIndex::KdTree(kdtree)),
                        Err(_) => {
                            // Fall back to brute force if KdTree construction fails
                            Ok(SpatialIndex::BruteForce(points.clone()))
                        }
                    }
                } else {
                    // Use brute force for very small datasets or high dimensions
                    Ok(SpatialIndex::BruteForce(points.clone()))
                }
            }
            SpatialIndexType::BallTree => {
                if n_points >= 50 {
                    // Try to build BallTree for larger datasets
                    match BallTree::new(points.clone()) {
                        Ok(balltree) => Ok(SpatialIndex::BallTree(balltree)),
                        Err(_) => {
                            // Fall back to brute force if BallTree construction fails
                            Ok(SpatialIndex::BruteForce(points.clone()))
                        }
                    }
                } else {
                    // Use brute force for small datasets
                    Ok(SpatialIndex::BruteForce(points.clone()))
                }
            }
            SpatialIndexType::BruteForce => Ok(SpatialIndex::BruteForce(points.clone())),
            SpatialIndexType::Auto => {
                // Intelligent selection based on data characteristics
                if n_points < 20 {
                    // Small datasets: always use brute force
                    Ok(SpatialIndex::BruteForce(points.clone()))
                } else if n_dims <= 5 && n_points >= 100 {
                    // Low-dimensional, large datasets: prefer KdTree
                    match KdTree::new(points.view()) {
                        Ok(kdtree) => Ok(SpatialIndex::KdTree(kdtree)),
                        Err(_) => Ok(SpatialIndex::BruteForce(points.clone())),
                    }
                } else if n_dims <= 15 && n_points >= 50 {
                    // Medium-dimensional datasets: prefer BallTree
                    match BallTree::new(points.clone()) {
                        Ok(balltree) => Ok(SpatialIndex::BallTree(balltree)),
                        Err(_) => Ok(SpatialIndex::BruteForce(points.clone())),
                    }
                } else {
                    // High-dimensional or edge cases: use brute force
                    Ok(SpatialIndex::BruteForce(points.clone()))
                }
            }
        }
    }
}

impl<F> HighDimensionalInterpolator<F>
where
    F: Float
        + FromPrimitive
        + Debug
        + Display
        + Zero
        + Copy
        + AddAssign
        + ScalarOperand
        + 'static
        + std::marker::Send
        + std::marker::Sync
        + ordered_float::FloatCore,
{
    /// Create a new builder for high-dimensional interpolation
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scirs2_interpolate::high_dimensional::HighDimensionalInterpolator;
    ///
    /// // Create a builder for high-dimensional interpolation
    /// let builder = HighDimensionalInterpolator::<f64>::builder();
    ///
    /// // The builder can be configured with various options before building
    /// println!("Builder created successfully");
    /// ```
    pub fn builder() -> HighDimensionalInterpolatorBuilder<F> {
        HighDimensionalInterpolatorBuilder::new()
    }

    /// Interpolate at a query point
    ///
    /// # Arguments
    ///
    /// * `query` - Query point coordinates with shape (n_dims,)
    ///
    /// # Returns
    ///
    /// Interpolated value at the query point
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scirs2_interpolate::high_dimensional::{HighDimensionalInterpolator, DimensionReductionMethod};
    /// use scirs2_core::ndarray::{Array1, Array2};
    ///
    /// // Create sample 5D data
    /// let points = Array2::from_shape_vec((4, 5), vec![
    ///     0.0, 0.0, 0.0, 0.0, 0.0,
    ///     1.0, 0.0, 0.0, 0.0, 0.0,
    ///     0.0, 1.0, 0.0, 0.0, 0.0,
    ///     0.0, 0.0, 1.0, 0.0, 0.0,
    /// ]).expect("Operation failed");
    /// let values = Array1::from_vec(vec![0.0, 1.0, 2.0, 3.0]);
    ///
    /// let interpolator = HighDimensionalInterpolator::builder()
    ///     .with_dimension_reduction(DimensionReductionMethod::PCA { target_dims: 3 })
    ///     .build(&points.view(), &values.view())
    ///     .expect("Operation failed");
    ///
    /// let query = Array1::from_vec(vec![0.5, 0.5, 0.0, 0.0, 0.0]);
    /// let result = interpolator.interpolate(&query.view()).expect("Operation failed");
    /// ```
    pub fn interpolate(&self, query: &ArrayView1<F>) -> InterpolateResult<F> {
        // Transform query point if dimension reduction is applied
        let transformed_query = if let Some(dr) = &self.dimension_reduction {
            let centered = query - &dr.mean;
            centered.dot(&dr.transformation_matrix)
        } else {
            query.to_owned()
        };

        // Find neighbors using spatial index
        let neighbors = self.find_neighbors(&transformed_query)?;

        // Interpolate using local method
        self.interpolate_local(&transformed_query, &neighbors)
    }

    /// Find neighbors for a query point
    fn find_neighbors(&self, query: &Array1<F>) -> InterpolateResult<Vec<(usize, F)>> {
        match &self.spatial_index {
            SpatialIndex::BruteForce(points) => self.brute_force_neighbors(query, points),
            SpatialIndex::KdTree(kdtree) => {
                // Use KdTree for efficient neighbor search
                let query_slice = query.as_slice().expect("Operation failed");
                match &self.local_method {
                    LocalMethod::KNearestNeighbors { k, .. } => {
                        let neighbors = kdtree.k_nearest_neighbors(query_slice, *k)?;
                        Ok(neighbors)
                    }
                    LocalMethod::LocallyWeighted { bandwidth, .. } => {
                        let radius = F::from_f64(*bandwidth).expect("Operation failed");
                        let neighbors = kdtree.radius_neighbors(query_slice, radius)?;
                        Ok(neighbors)
                    }
                    LocalMethod::LocalRBF { radius, .. } => {
                        let search_radius = F::from_f64(*radius).expect("Operation failed");
                        let neighbors = kdtree.radius_neighbors(query_slice, search_radius)?;
                        Ok(neighbors)
                    }
                }
            }
            SpatialIndex::BallTree(balltree) => {
                // Use BallTree for efficient neighbor search
                let query_slice = query.as_slice().expect("Operation failed");
                match &self.local_method {
                    LocalMethod::KNearestNeighbors { k, .. } => {
                        let neighbors = balltree.k_nearest_neighbors(query_slice, *k)?;
                        Ok(neighbors)
                    }
                    LocalMethod::LocallyWeighted { bandwidth, .. } => {
                        let radius = F::from_f64(*bandwidth).expect("Operation failed");
                        let neighbors = balltree.radius_neighbors(query_slice, radius)?;
                        Ok(neighbors)
                    }
                    LocalMethod::LocalRBF { radius, .. } => {
                        let search_radius = F::from_f64(*radius).expect("Operation failed");
                        let neighbors = balltree.radius_neighbors(query_slice, search_radius)?;
                        Ok(neighbors)
                    }
                }
            }
        }
    }

    /// Brute force neighbor search
    fn brute_force_neighbors(
        &self,
        query: &Array1<F>,
        points: &Array2<F>,
    ) -> InterpolateResult<Vec<(usize, F)>> {
        let mut distances: Vec<(usize, F)> = Vec::new();

        for (i, point) in points.axis_iter(Axis(0)).enumerate() {
            let dist = self.compute_distance(query, &point.to_owned());
            distances.push((i, dist));
        }

        // Sort by distance
        distances.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

        // Return based on local method
        match &self.local_method {
            LocalMethod::KNearestNeighbors { k, .. } => {
                Ok(distances.into_iter().take(*k).collect())
            }
            LocalMethod::LocallyWeighted { bandwidth, .. } => {
                // Return all points within bandwidth
                Ok(distances
                    .into_iter()
                    .filter(|(_, dist)| *dist <= F::from_f64(*bandwidth).expect("Operation failed"))
                    .collect())
            }
            LocalMethod::LocalRBF { radius, .. } => {
                // Return all points within radius
                Ok(distances
                    .into_iter()
                    .filter(|(_, dist)| *dist <= F::from_f64(*radius).expect("Operation failed"))
                    .collect())
            }
        }
    }

    /// Compute distance between two points
    fn compute_distance(&self, p1: &Array1<F>, p2: &Array1<F>) -> F {
        // Euclidean distance
        let diff = p1 - p2;
        diff.iter()
            .map(|&x| x * x)
            .fold(F::zero(), |acc, x| acc + x)
            .sqrt()
    }

    /// Perform local interpolation using neighbors
    fn interpolate_local(
        &self,
        self_query: &Array1<F>,
        neighbors: &[(usize, F)],
    ) -> InterpolateResult<F> {
        if neighbors.is_empty() {
            return Err(InterpolateError::ComputationError(
                "No neighbors found for interpolation".to_string(),
            ));
        }

        match &self.local_method {
            LocalMethod::KNearestNeighbors { weight_power, .. } => {
                let mut weighted_sum = F::zero();
                let mut weight_sum = F::zero();

                for &(idx, dist) in neighbors {
                    let weight = if dist == F::zero() {
                        // If point is exactly at a data point, return that value
                        return Ok(self.values[idx]);
                    } else {
                        F::one() / dist.powf(F::from_f64(*weight_power).expect("Operation failed"))
                    };

                    weighted_sum += weight * self.values[idx];
                    weight_sum += weight;
                }

                if weight_sum == F::zero() {
                    return Err(InterpolateError::ComputationError(
                        "Zero weight sum in interpolation".to_string(),
                    ));
                }

                Ok(weighted_sum / weight_sum)
            }
            LocalMethod::LocallyWeighted { .. } => {
                // Simplified locally weighted regression
                // In a full implementation, this would fit a local polynomial
                let mut sum = F::zero();
                let count = F::from_usize(neighbors.len()).expect("Operation failed");

                for &(idx, _) in neighbors {
                    sum += self.values[idx];
                }

                Ok(sum / count)
            }
            LocalMethod::LocalRBF { rbf_type, .. } => {
                // Simplified local RBF interpolation
                self.interpolate_local_rbf(neighbors, rbf_type)
            }
        }
    }

    /// Perform local RBF interpolation
    fn interpolate_local_rbf(
        &self,
        neighbors: &[(usize, F)],
        _rbf_type: &LocalRBFType,
    ) -> InterpolateResult<F> {
        // Simplified RBF interpolation
        let mut sum = F::zero();
        let mut weight_sum = F::zero();

        for &(idx, dist) in neighbors {
            // Use Gaussian RBF
            let weight = (-dist * dist).exp();
            sum += weight * self.values[idx];
            weight_sum += weight;
        }

        if weight_sum == F::zero() {
            return Err(InterpolateError::ComputationError(
                "Zero weight sum in RBF interpolation".to_string(),
            ));
        }

        Ok(sum / weight_sum)
    }

    /// Interpolate at multiple query points
    ///
    /// # Arguments
    ///
    /// * `queries` - Query points with shape (n_queries, n_dims)
    ///
    /// # Returns
    ///
    /// Array of interpolated values with shape (n_queries,)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scirs2_interpolate::high_dimensional::HighDimensionalInterpolator;
    /// use scirs2_core::ndarray::{Array1, Array2};
    ///
    /// let points = Array2::from_shape_vec((3, 2), vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0]).expect("Operation failed");
    /// let values = Array1::from_vec(vec![0.0, 1.0, 2.0]);
    ///
    /// let interpolator = HighDimensionalInterpolator::builder()
    ///     .build(&points.view(), &values.view())
    ///     .expect("Operation failed");
    ///
    /// let queries = Array2::from_shape_vec((2, 2), vec![0.5, 0.0, 0.0, 0.5]).expect("Operation failed");
    /// let results = interpolator.interpolate_multi(&queries.view()).expect("Operation failed");
    /// assert_eq!(results.len(), 2);
    /// ```
    pub fn interpolate_multi(&self, queries: &ArrayView2<F>) -> InterpolateResult<Array1<F>> {
        let mut results = Array1::zeros(queries.nrows());

        for (i, query) in queries.axis_iter(Axis(0)).enumerate() {
            results[i] = self.interpolate(&query.view())?;
        }

        Ok(results)
    }

    /// Interpolate at multiple query points in parallel
    ///
    /// This method provides parallel evaluation for large query sets, providing
    /// significant speedup for expensive high-dimensional interpolation operations.
    ///
    /// # Arguments
    ///
    /// * `queries` - Query points with shape (n_queries, n_dims)
    /// * `workers` - Number of worker threads to use (None = automatic)
    ///
    /// # Returns
    ///
    /// Array of interpolated values with shape (n_queries,)
    ///
    /// # Performance
    ///
    /// The parallel version provides speedup for:
    /// - Large query sets (n_queries > 100)
    /// - High-dimensional data (n_dims > 10)
    /// - Complex local methods (LocalRBF, LocallyWeighted)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scirs2_interpolate::high_dimensional::HighDimensionalInterpolator;
    /// use scirs2_core::ndarray::{Array1, Array2};
    ///
    /// let points = Array2::from_shape_vec((3, 2), vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0]).expect("Operation failed");
    /// let values = Array1::from_vec(vec![0.0, 1.0, 2.0]);
    ///
    /// let interpolator = HighDimensionalInterpolator::builder()
    ///     .build(&points.view(), &values.view())
    ///     .expect("Operation failed");
    ///
    /// // Create many query points for parallel processing
    /// let queries = Array2::from_shape_vec((1000, 2), (0..2000).map(|i| i as f64 / 1000.0).collect()).expect("Operation failed");
    ///
    /// // Use 4 worker threads
    /// let results = interpolator.interpolate_multi_parallel(&queries.view(), Some(4)).expect("Operation failed");
    /// assert_eq!(results.len(), 1000);
    /// ```
    pub fn interpolate_multi_parallel(
        &self,
        queries: &ArrayView2<F>,
        workers: Option<usize>,
    ) -> InterpolateResult<Array1<F>> {
        use crate::parallel::{estimate_chunk_size, ParallelConfig};
        use scirs2_core::parallel_ops::*;

        let n_queries = queries.nrows();

        // For small query sets, use sequential processing
        if n_queries < 50 {
            return self.interpolate_multi(queries);
        }

        // Set up parallel configuration
        let parallel_config = if let Some(n_workers) = workers {
            ParallelConfig::new().with_workers(n_workers)
        } else {
            ParallelConfig::new()
        };

        // Estimate computational cost based on local method
        let cost_factor = match &self.local_method {
            LocalMethod::KNearestNeighbors { .. } => 2.0,
            LocalMethod::LocallyWeighted { .. } => 5.0,
            LocalMethod::LocalRBF { .. } => 8.0,
        };

        let chunk_size = estimate_chunk_size(n_queries, cost_factor, &parallel_config);

        // Process queries in parallel
        let results: Result<Vec<F>, InterpolateError> = (0..n_queries)
            .into_par_iter()
            .with_min_len(chunk_size)
            .map(|i| {
                let query = queries.slice(scirs2_core::ndarray::s![i, ..]);
                self.interpolate(&query.view())
            })
            .collect::<Result<Vec<F>, InterpolateError>>();

        Ok(Array1::from_vec(results?))
    }

    /// Get interpolator statistics
    pub fn stats(&self) -> &InterpolatorStats {
        &self.stats
    }

    /// Get the effective dimensionality (after dimension reduction)
    pub fn effective_dimensions(&self) -> usize {
        self.stats
            .reduced_dimensions
            .unwrap_or(self.stats.original_dimensions)
    }

    /// Get the training data size
    pub fn training_size(&self) -> usize {
        self.stats.n_training_points
    }
}

/// Create a high-dimensional interpolator with k-nearest neighbors
///
/// This is a convenience function for creating an interpolator that uses
/// k-nearest neighbor interpolation with inverse distance weighting.
///
/// # Arguments
///
/// * `points` - Training data points with shape (n_points, n_dims)
/// * `values` - Training data values with shape (n_points,)
/// * `k` - Number of nearest neighbors to use
///
/// # Returns
///
/// A configured high-dimensional interpolator
///
/// # Examples
///
/// ```rust
/// use scirs2_interpolate::high_dimensional::make_knn_interpolator;
/// use scirs2_core::ndarray::{Array1, Array2};
///
/// // Create 3D scattered data
/// let points = Array2::from_shape_vec((5, 3), vec![
///     0.0, 0.0, 0.0,
///     1.0, 0.0, 0.0,
///     0.0, 1.0, 0.0,
///     0.0, 0.0, 1.0,
///     1.0, 1.0, 1.0,
/// ]).expect("Operation failed");
/// let values = Array1::from_vec(vec![0.0, 1.0, 2.0, 3.0, 6.0]);
///
/// let interpolator = make_knn_interpolator(&points.view(), &values.view(), 3).expect("Operation failed");
///
/// let query = Array1::from_vec(vec![0.5, 0.5, 0.5]);
/// let result = interpolator.interpolate(&query.view()).expect("Operation failed");
/// ```
#[allow(dead_code)]
pub fn make_knn_interpolator<F>(
    points: &ArrayView2<F>,
    values: &ArrayView1<F>,
    k: usize,
) -> InterpolateResult<HighDimensionalInterpolator<F>>
where
    F: Float
        + FromPrimitive
        + Debug
        + Display
        + Zero
        + Copy
        + AddAssign
        + ScalarOperand
        + 'static
        + Send
        + Sync
        + ordered_float::FloatCore,
{
    HighDimensionalInterpolator::builder()
        .with_local_method(LocalMethod::KNearestNeighbors {
            k,
            weight_power: 2.0,
        })
        .build(points, values)
}

/// Create a high-dimensional interpolator with PCA dimension reduction
///
/// This function creates an interpolator that first reduces the dimensionality
/// of the data using Principal Component Analysis (PCA), then performs
/// k-nearest neighbor interpolation in the reduced space.
///
/// # Arguments
///
/// * `points` - Training data points with shape (n_points, n_dims)
/// * `values` - Training data values with shape (n_points,)
/// * `target_dims` - Target number of dimensions after PCA reduction
/// * `k` - Number of nearest neighbors to use in reduced space
///
/// # Returns
///
/// A configured high-dimensional interpolator with PCA preprocessing
///
/// # Examples
///
/// ```rust
/// use scirs2_interpolate::high_dimensional::make_pca_interpolator;
/// use scirs2_core::ndarray::{Array1, Array2};
///
/// // Create high-dimensional data that lies on a lower-dimensional manifold
/// let points = Array2::from_shape_vec((4, 6), vec![
///     1.0, 1.0, 1.0, 0.0, 0.0, 0.0,  // Redundant dimensions
///     2.0, 2.0, 2.0, 0.0, 0.0, 0.0,
///     1.0, 2.0, 3.0, 0.0, 0.0, 0.0,
///     3.0, 1.0, 1.0, 0.0, 0.0, 0.0,
/// ]).expect("Operation failed");
/// let values = Array1::from_vec(vec![1.0, 2.0, 3.0, 2.5]);
///
/// // Reduce from 6D to 3D, then use 3 nearest neighbors
/// let interpolator = make_pca_interpolator(&points.view(), &values.view(), 3, 3).expect("Operation failed");
///
/// let query = Array1::from_vec(vec![1.5, 1.5, 1.5, 0.0, 0.0, 0.0]);
/// let result = interpolator.interpolate(&query.view()).expect("Operation failed");
/// ```
#[allow(dead_code)]
pub fn make_pca_interpolator<F>(
    points: &ArrayView2<F>,
    values: &ArrayView1<F>,
    target_dims: usize,
    k: usize,
) -> InterpolateResult<HighDimensionalInterpolator<F>>
where
    F: Float
        + FromPrimitive
        + Debug
        + Display
        + Zero
        + Copy
        + AddAssign
        + ScalarOperand
        + 'static
        + Send
        + Sync
        + ordered_float::FloatCore,
{
    HighDimensionalInterpolator::builder()
        .with_dimension_reduction(DimensionReductionMethod::PCA { target_dims })
        .with_local_method(LocalMethod::KNearestNeighbors {
            k,
            weight_power: 2.0,
        })
        .build(points, values)
}

/// Create a high-dimensional interpolator with local RBF
///
/// This function creates an interpolator that uses locally supported Radial
/// Basis Functions (RBF) for interpolation. Only points within the specified
/// radius contribute to the interpolation at each query point.
///
/// # Arguments
///
/// * `points` - Training data points with shape (n_points, n_dims)
/// * `values` - Training data values with shape (n_points,)
/// * `radius` - Radius of local support for RBF functions
///
/// # Returns
///
/// A configured high-dimensional interpolator with local RBF
///
/// # Examples
///
/// ```rust
/// use scirs2_interpolate::high_dimensional::make_local_rbf_interpolator;
/// use scirs2_core::ndarray::{Array1, Array2};
///
/// // Create scattered 2D data
/// let points = Array2::from_shape_vec((4, 2), vec![
///     0.0, 0.0,
///     1.0, 0.0,
///     0.0, 1.0,
///     1.0, 1.0,
/// ]).expect("Operation failed");
/// let values = Array1::from_vec(vec![0.0, 1.0, 1.0, 2.0]);
///
/// // Use radius of 1.5 to include nearby points
/// let interpolator = make_local_rbf_interpolator(&points.view(), &values.view(), 1.5).expect("Operation failed");
///
/// let query = Array1::from_vec(vec![0.5, 0.5]);
/// let result = interpolator.interpolate(&query.view()).expect("Operation failed");
/// ```
#[allow(dead_code)]
pub fn make_local_rbf_interpolator<F>(
    points: &ArrayView2<F>,
    values: &ArrayView1<F>,
    radius: f64,
) -> InterpolateResult<HighDimensionalInterpolator<F>>
where
    F: Float
        + FromPrimitive
        + Debug
        + Display
        + Zero
        + Copy
        + AddAssign
        + ScalarOperand
        + 'static
        + Send
        + Sync
        + ordered_float::FloatCore,
{
    HighDimensionalInterpolator::builder()
        .with_local_method(LocalMethod::LocalRBF {
            radius,
            rbf_type: LocalRBFType::CompactGaussian,
        })
        .build(points, values)
}

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::array;

    #[test]
    fn test_high_dimensional_interpolator_creation() {
        let points = array![
            [0.0, 0.0, 0.0],
            [1.0, 0.0, 0.0],
            [0.0, 1.0, 0.0],
            [0.0, 0.0, 1.0]
        ];
        let values = array![0.0, 1.0, 1.0, 1.0];

        let interpolator = HighDimensionalInterpolator::<f64>::builder()
            .build(&points.view(), &values.view())
            .expect("Operation failed");

        assert_eq!(interpolator.effective_dimensions(), 3);
        assert_eq!(interpolator.training_size(), 4);
    }

    #[test]
    fn test_knn_interpolation() {
        let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]];
        let values = array![0.0, 1.0, 1.0, 2.0];

        let interpolator =
            make_knn_interpolator(&points.view(), &values.view(), 2).expect("Operation failed");

        // Interpolate at the center
        let query = array![0.5, 0.5];
        let result = interpolator
            .interpolate(&query.view())
            .expect("Operation failed");

        // Should be close to the average of nearby points
        assert!((0.5..=1.5).contains(&result));
    }

    #[test]
    fn test_pca_interpolation() {
        // Create 3D data that lies on a 2D plane
        let points = array![
            [1.0, 1.0, 1.0],
            [2.0, 2.0, 2.0],
            [1.0, 2.0, 3.0],
            [3.0, 1.0, 1.0]
        ];
        let values = array![1.0, 2.0, 3.0, 2.0];

        let interpolator =
            make_pca_interpolator(&points.view(), &values.view(), 2, 3).expect("Operation failed");

        assert_eq!(interpolator.effective_dimensions(), 2);

        // Test interpolation
        let query = array![1.5, 1.5, 1.5];
        let result = interpolator
            .interpolate(&query.view())
            .expect("Operation failed");

        assert!((0.5..=3.5).contains(&result));
    }

    #[test]
    fn test_local_rbf_interpolation() {
        let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]];
        let values = array![0.0, 1.0, 1.0, 2.0];

        let interpolator = make_local_rbf_interpolator(&points.view(), &values.view(), 1.5)
            .expect("Operation failed");

        let query = array![0.5, 0.5];
        let result = interpolator
            .interpolate(&query.view())
            .expect("Operation failed");

        assert!((0.0..=2.0).contains(&result));
    }

    #[test]
    fn test_multi_interpolation() {
        let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]];
        let values = array![0.0, 1.0, 1.0, 2.0];

        let interpolator =
            make_knn_interpolator(&points.view(), &values.view(), 3).expect("Operation failed");

        let queries = array![[0.25, 0.25], [0.75, 0.75]];
        let results = interpolator
            .interpolate_multi(&queries.view())
            .expect("Operation failed");

        assert_eq!(results.len(), 2);
        assert!(results[0] >= 0.0 && results[0] <= 2.0);
        assert!(results[1] >= 0.0 && results[1] <= 2.0);
    }

    #[test]
    fn test_dimension_reduction_methods() {
        let points = array![
            [1.0, 2.0, 3.0, 4.0],
            [2.0, 3.0, 4.0, 5.0],
            [3.0, 4.0, 5.0, 6.0]
        ];
        let values = array![1.0, 2.0, 3.0];

        // Test PCA
        let pca_interp = HighDimensionalInterpolator::<f64>::builder()
            .with_dimension_reduction(DimensionReductionMethod::PCA { target_dims: 2 })
            .build(&points.view(), &values.view())
            .expect("Operation failed");

        assert_eq!(pca_interp.effective_dimensions(), 2);

        // Test Random Projection
        let rp_interp = HighDimensionalInterpolator::builder()
            .with_dimension_reduction(DimensionReductionMethod::RandomProjection { target_dims: 2 })
            .build(&points.view(), &values.view())
            .expect("Operation failed");

        assert_eq!(rp_interp.effective_dimensions(), 2);
    }

    #[test]
    fn test_builder_pattern() {
        let points = array![[0.0, 0.0], [1.0, 1.0]];
        let values = array![0.0, 1.0];

        let interpolator = HighDimensionalInterpolator::builder()
            .with_local_method(LocalMethod::LocallyWeighted {
                bandwidth: 1.0,
                degree: 1,
            })
            .with_spatial_index(SpatialIndexType::BruteForce)
            .build(&points.view(), &values.view())
            .expect("Operation failed");

        assert_eq!(interpolator.training_size(), 2);
    }
}