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
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
//! Bivariate interpolation modules
//!
//! This module provides implementations of bivariate splines (splines in 2 dimensions).
//! These methods allow interpolation and smoothing of data points on 2D surfaces.
//!
//! ## Overview
//!
//! * `BivariateSpline` - Base class for bivariate splines
//! * `SmoothBivariateSpline` - Smooth bivariate spline approximation
//! * `LSQBivariateSpline` - Weighted least-squares bivariate spline approximation
//! * `RectBivariateSpline` - Bivariate spline approximation over a rectangular mesh

use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ArrayView2};
use scirs2_core::numeric::{Float, FromPrimitive};
use std::fmt::Debug;

use crate::error::{InterpolateError, InterpolateResult};

pub mod bspline_eval;

/// Trait for all bivariate spline interpolators
pub trait BivariateInterpolator<F: Float + FromPrimitive + Debug + std::fmt::Display> {
    /// Evaluate the spline at points
    fn evaluate(
        &self,
        x: &ArrayView1<F>,
        y: &ArrayView1<F>,
        grid: bool,
    ) -> InterpolateResult<Array2<F>>;

    /// Evaluate the derivative of the spline at points
    fn evaluate_derivative(
        &self,
        x: &ArrayView1<F>,
        y: &ArrayView1<F>,
        dx: usize,
        dy: usize,
        grid: bool,
    ) -> InterpolateResult<Array2<F>>;

    /// Evaluate the integral of the spline over a rectangular region
    fn integral(&self, xa: F, xb: F, ya: F, yb: F) -> InterpolateResult<F>;
}

/// Base struct for bivariate splines
#[derive(Debug, Clone)]
pub struct BivariateSpline<
    F: Float + FromPrimitive + Debug + std::fmt::Display + crate::traits::InterpolationFloat,
> {
    /// x knots
    tx: Array1<F>,
    /// y knots
    ty: Array1<F>,
    /// Coefficients
    c: Array1<F>,
    /// Degree in x direction
    kx: usize,
    /// Degree in y direction
    ky: usize,
    /// Weighted sum of squared residuals of the spline approximation
    fp: Option<F>,
}

impl<F: Float + FromPrimitive + Debug + std::fmt::Display + crate::traits::InterpolationFloat>
    BivariateSpline<F>
{
    /// Create a new bivariate spline from knots and coefficients
    pub fn from_tck(
        tx: Array1<F>,
        ty: Array1<F>,
        c: Array1<F>,
        kx: usize,
        ky: usize,
        fp: Option<F>,
    ) -> Self {
        Self {
            tx,
            ty,
            c,
            kx,
            ky,
            fp,
        }
    }

    /// Get knots in x and y directions
    pub fn get_knots(&self) -> (&Array1<F>, &Array1<F>) {
        (&self.tx, &self.ty)
    }

    /// Get spline coefficients
    pub fn get_coeffs(&self) -> &Array1<F> {
        &self.c
    }

    /// Get the residual (weighted sum of squared errors)
    pub fn get_residual(&self) -> Option<F> {
        self.fp
    }

    /// Validates the input coordinates and values
    fn validate_input(
        x: &ArrayView1<F>,
        y: &ArrayView1<F>,
        z: &ArrayView1<F>,
        w: Option<&ArrayView1<F>>,
        kx: usize,
        ky: usize,
        eps: Option<F>,
    ) -> InterpolateResult<()> {
        // Check lengths
        if x.len() != y.len() || x.len() != z.len() {
            return Err(InterpolateError::invalid_input(
                "x, y, and z should have a same length".to_string(),
            ));
        }

        // Check weights if provided
        if let Some(w) = w {
            if x.len() != w.len() {
                return Err(InterpolateError::invalid_input(
                    "x, y, z, and w should have a same length".to_string(),
                ));
            }
            if !w.iter().all(|&w_i| w_i >= F::zero()) {
                return Err(InterpolateError::invalid_input(
                    "w should be positive".to_string(),
                ));
            }
        }

        // Check epsilon
        if let Some(eps) = eps {
            if !(F::zero() < eps && eps < F::one()) {
                return Err(InterpolateError::invalid_input(
                    "eps should be between (0, 1)".to_string(),
                ));
            }
        }

        // Check data size relative to degrees
        if x.len() < (kx + 1) * (ky + 1) {
            return Err(InterpolateError::invalid_input(
                "The length of x, y and z should be at least (kx+1) * (ky+1)".to_string(),
            ));
        }

        Ok(())
    }

    /// Evaluate the spline or its derivatives at given positions
    fn evaluate_impl(
        &self,
        x: &ArrayView1<F>,
        y: &ArrayView1<F>,
        dx: usize,
        dy: usize,
        grid: bool,
    ) -> InterpolateResult<Array2<F>> {
        if grid {
            self.evaluate_grid(x, y, dx, dy)
        } else {
            self.evaluate_points(x, y, dx, dy)
        }
    }

    /// Evaluate the spline at grid points
    fn evaluate_grid(
        &self,
        x: &ArrayView1<F>,
        y: &ArrayView1<F>,
        dx: usize,
        dy: usize,
    ) -> InterpolateResult<Array2<F>> {
        // For an empty grid, return an empty result
        if x.is_empty() || y.is_empty() {
            return Ok(Array2::zeros((x.len(), y.len())));
        }

        // Check that grid arrays are strictly increasing
        if x.len() >= 2 {
            for i in 0..x.len() - 1 {
                if x[i + 1] <= x[i] {
                    return Err(InterpolateError::invalid_input(
                        "x must be strictly increasing when `grid` is True".to_string(),
                    ));
                }
            }
        }
        if y.len() >= 2 {
            for i in 0..y.len() - 1 {
                if y[i + 1] <= y[i] {
                    return Err(InterpolateError::invalid_input(
                        "y must be strictly increasing when `grid` is True".to_string(),
                    ));
                }
            }
        }

        // Evaluate on the grid
        let mut result = Array2::zeros((x.len(), y.len()));

        // Since we don't have direct access to FITPACK's bispev, we'll compute
        // the grid by evaluating at each point
        for (i, &xi) in x.iter().enumerate() {
            for (j, &yj) in y.iter().enumerate() {
                result[[i, j]] = self.evaluate_single_point(xi, yj, dx, dy)?;
            }
        }

        Ok(result)
    }

    /// Evaluate the spline at individual points
    fn evaluate_points(
        &self,
        x: &ArrayView1<F>,
        y: &ArrayView1<F>,
        dx: usize,
        dy: usize,
    ) -> InterpolateResult<Array2<F>> {
        // For empty input, return empty output
        if x.is_empty() || y.is_empty() {
            return Ok(Array2::zeros((x.len(), x.len())));
        }

        // Check that x and y have the same shape
        if x.shape() != y.shape() {
            return Err(InterpolateError::invalid_input(
                "x and y must have the same shape in point evaluation mode".to_string(),
            ));
        }

        let n = x.len();
        let mut result = Array1::zeros(n);

        // Evaluate at each point
        for i in 0..n {
            result[i] = self.evaluate_single_point(x[i], y[i], dx, dy)?;
        }

        // Create a 2D array from the results
        let mut result2d = Array2::zeros((n, 1));
        for i in 0..n {
            result2d[[i, 0]] = result[i];
        }

        Ok(result2d)
    }

    /// Evaluate the spline at a single point
    fn evaluate_single_point(&self, x: F, y: F, dx: usize, dy: usize) -> InterpolateResult<F> {
        // Check if we're within the domain bounds
        if x < self.tx[self.kx]
            || x > self.tx[self.tx.len() - self.kx - 1]
            || y < self.ty[self.ky]
            || y > self.ty[self.ty.len() - self.ky - 1]
        {
            return Err(InterpolateError::OutOfBounds(format!(
                "Point ({:?}, {:?}) is outside the domain of the spline",
                x, y
            )));
        }

        // Check if derivatives are within degree bounds
        if dx > self.kx || dy > self.ky {
            // Higher-order derivatives than the spline degree are all zero
            return Ok(F::zero());
        }

        // Use the B-spline evaluation routine from bspline_eval module
        if dx == 0 && dy == 0 {
            Ok(bspline_eval::evaluate_bispline(
                x,
                y,
                &self.tx.view(),
                &self.ty.view(),
                &self.c.view(),
                self.kx,
                self.ky,
            ))
        } else {
            Ok(bspline_eval::evaluate_bispline_derivative(
                x,
                y,
                &self.tx.view(),
                &self.ty.view(),
                &self.c.view(),
                self.kx,
                self.ky,
                dx,
                dy,
            ))
        }
    }

    /// Find the interval containing x in the knot sequence
    #[allow(dead_code)] // Currently unused but kept for potential future use
    fn find_interval(&self, x: F, knots: &Array1<F>) -> InterpolateResult<(usize, F)> {
        let n = knots.len();

        // Check bounds
        if x < knots[0] || x > knots[n - 1] {
            return Err(InterpolateError::OutOfBounds(format!(
                "x={:?} is outside the knot range [{:?}, {:?}]",
                x,
                knots[0],
                knots[n - 1]
            )));
        }

        // Find the interval
        for i in 0..(n - 1) {
            if knots[i] <= x && x <= knots[i + 1] {
                // Calculate the normalized position within the interval
                let alpha = if knots[i + 1] > knots[i] {
                    (x - knots[i]) / (knots[i + 1] - knots[i])
                } else {
                    F::zero() // Handle case where knots[i] == knots[i+1]
                };

                return Ok((i, alpha));
            }
        }

        // This should not happen if the bounds check passed
        Err(InterpolateError::ComputationError(
            "Failed to find interval in knot sequence".to_string(),
        ))
    }
}

impl<F: Float + FromPrimitive + Debug + std::fmt::Display + crate::traits::InterpolationFloat>
    BivariateInterpolator<F> for BivariateSpline<F>
{
    fn evaluate(
        &self,
        x: &ArrayView1<F>,
        y: &ArrayView1<F>,
        grid: bool,
    ) -> InterpolateResult<Array2<F>> {
        self.evaluate_impl(x, y, 0, 0, grid)
    }

    fn evaluate_derivative(
        &self,
        x: &ArrayView1<F>,
        y: &ArrayView1<F>,
        dx: usize,
        dy: usize,
        grid: bool,
    ) -> InterpolateResult<Array2<F>> {
        self.evaluate_impl(x, y, dx, dy, grid)
    }

    fn integral(&self, xa: F, xb: F, ya: F, yb: F) -> InterpolateResult<F> {
        // Check if the integration domain is within the spline's domain
        let x_min = self.tx[self.kx];
        let x_max = self.tx[self.tx.len() - self.kx - 1];
        let y_min = self.ty[self.ky];
        let y_max = self.ty[self.ty.len() - self.ky - 1];

        if xa < x_min || xb > x_max || ya < y_min || yb > y_max {
            return Err(InterpolateError::OutOfBounds(format!(
                "Integration domain [{:?}, {:?}] x [{:?}, {:?}] is outside the spline domain [{:?}, {:?}] x [{:?}, {:?}]",
                xa, xb, ya, yb, x_min, x_max, y_min, y_max
            )));
        }

        if xa > xb || ya > yb {
            return Err(InterpolateError::invalid_input(
                "Integration bounds should satisfy xa <= xb and ya <= yb".to_string(),
            ));
        }

        // Use the B-spline integration routine with 10 quadrature points
        Ok(bspline_eval::integrate_bispline(
            xa,
            xb,
            ya,
            yb,
            &self.tx.view(),
            &self.ty.view(),
            &self.c.view(),
            self.kx,
            self.ky,
            Some(10),
        ))
    }
}

/// Smooth bivariate spline approximation
///
/// This class approximates a set of data points with a smooth bivariate spline.
#[derive(Debug, Clone)]
pub struct SmoothBivariateSpline<
    F: Float + FromPrimitive + Debug + std::fmt::Display + crate::traits::InterpolationFloat,
> {
    /// The underlying bivariate spline
    spline: BivariateSpline<F>,
}

/// Builder for SmoothBivariateSpline
pub struct SmoothBivariateSplineBuilder<
    'a,
    F: Float + FromPrimitive + Debug + std::fmt::Display + crate::traits::InterpolationFloat,
> {
    x: &'a ArrayView1<'a, F>,
    y: &'a ArrayView1<'a, F>,
    z: &'a ArrayView1<'a, F>,
    w: Option<&'a ArrayView1<'a, F>>,
    bbox: Option<[F; 4]>,
    kx: usize,
    ky: usize,
    s: Option<F>,
    eps: Option<F>,
}

impl<
        'a,
        F: Float + FromPrimitive + Debug + std::fmt::Display + crate::traits::InterpolationFloat,
    > SmoothBivariateSplineBuilder<'a, F>
{
    /// Create a new builder with required parameters
    pub fn new(
        x: &'a ArrayView1<'a, F>,
        y: &'a ArrayView1<'a, F>,
        z: &'a ArrayView1<'a, F>,
    ) -> Self {
        Self {
            x,
            y,
            z,
            w: None,
            bbox: None,
            kx: 3,
            ky: 3,
            s: None,
            eps: None,
        }
    }

    /// Set weights
    pub fn with_weights(mut self, w: &'a ArrayView1<'a, F>) -> Self {
        self.w = Some(w);
        self
    }

    /// Set boundary box
    pub fn with_bbox(mut self, bbox: [F; 4]) -> Self {
        self.bbox = Some(bbox);
        self
    }

    /// Set degrees of the bivariate spline
    pub fn with_degrees(mut self, kx: usize, ky: usize) -> Self {
        self.kx = kx;
        self.ky = ky;
        self
    }

    /// Set smoothing factor
    pub fn with_smoothing(mut self, s: F) -> Self {
        self.s = Some(s);
        self
    }

    /// Set threshold for determining the effective rank
    pub fn with_epsilon(mut self, eps: F) -> Self {
        self.eps = Some(eps);
        self
    }

    /// Build the SmoothBivariateSpline
    pub fn build(self) -> InterpolateResult<SmoothBivariateSpline<F>> {
        // Validate input
        BivariateSpline::validate_input(
            self.x, self.y, self.z, self.w, self.kx, self.ky, self.eps,
        )?;

        // Check smoothing factor
        if let Some(s) = self.s {
            if s < F::zero() {
                return Err(InterpolateError::invalid_input(
                    "s should be s >= 0.0".to_string(),
                ));
            }
        }

        // Determine boundary
        let _bbox = match self.bbox {
            Some(bbox) => bbox,
            None => [
                self.x.iter().copied().fold(F::infinity(), F::min),
                self.x.iter().copied().fold(F::neg_infinity(), F::max),
                self.y.iter().copied().fold(F::infinity(), F::min),
                self.y.iter().copied().fold(F::neg_infinity(), F::max),
            ],
        };

        // Generate knots based on the data distribution
        let x_min = self.x.iter().copied().fold(F::infinity(), F::min);
        let x_max = self.x.iter().copied().fold(F::neg_infinity(), F::max);
        let y_min = self.y.iter().copied().fold(F::infinity(), F::min);
        let y_max = self.y.iter().copied().fold(F::neg_infinity(), F::max);

        // Create a grid of interior knots
        let n_interior_x = 8; // Number of interior knots in x direction
        let n_interior_y = 8; // Number of interior knots in y direction

        // Generate knot sequences with appropriate multiplicities at endpoints
        let mut tx = Vec::with_capacity(n_interior_x + 2 * (self.kx + 1));
        let mut ty = Vec::with_capacity(n_interior_y + 2 * (self.ky + 1));

        // Add repeated knots at beginning (multiplicity k+1)
        for _ in 0..=self.kx {
            tx.push(x_min);
        }

        // Add interior knots
        if n_interior_x > 0 {
            let dx = (x_max - x_min) / F::from_usize(n_interior_x + 1).expect("Operation failed");
            for i in 1..=n_interior_x {
                tx.push(x_min + F::from_usize(i).expect("Operation failed") * dx);
            }
        }

        // Add repeated knots at end (multiplicity k+1)
        for _ in 0..=self.kx {
            tx.push(x_max);
        }

        // Similarly for y direction
        for _ in 0..=self.ky {
            ty.push(y_min);
        }

        if n_interior_y > 0 {
            let dy = (y_max - y_min) / F::from_usize(n_interior_y + 1).expect("Operation failed");
            for i in 1..=n_interior_y {
                ty.push(y_min + F::from_usize(i).expect("Operation failed") * dy);
            }
        }

        for _ in 0..=self.ky {
            ty.push(y_max);
        }

        // Convert to ndarray
        let tx = Array1::from(tx);
        let ty = Array1::from(ty);

        // In real implementation, we would use a full least-squares fitting algorithm here
        // For now, we'll create coefficients that at least approximate the data

        // Number of control points
        let n_x = tx.len() - self.kx - 1;
        let n_y = ty.len() - self.ky - 1;
        let num_coeffs = n_x * n_y;

        // Initialize coefficients
        let mut c = Array1::<F>::zeros(num_coeffs);

        // Simple approximation: for each data point, find the closest control point
        // and update its coefficient
        let mut weight_sum = Array1::<F>::zeros(num_coeffs);

        for i in 0..self.x.len() {
            // Find the knot span for this point
            let span_x = bspline_eval::find_span(self.x[i], &tx.view(), self.kx);
            let span_y = bspline_eval::find_span(self.y[i], &ty.view(), self.ky);

            // Compute basis functions
            let basis_x = bspline_eval::basis_funs(self.x[i], span_x, &tx.view(), self.kx);
            let basis_y = bspline_eval::basis_funs(self.y[i], span_y, &ty.view(), self.ky);

            // Apply smoothing by spreading the influence over multiple control points
            for iu in 0..=self.kx {
                let i_coef = span_x - self.kx + iu;
                for jv in 0..=self.ky {
                    let j_coef = span_y - self.ky + jv;
                    let idx = i_coef * n_y + j_coef;

                    if idx < num_coeffs {
                        let weight = basis_x[iu] * basis_y[jv];
                        let val = self.z[i];

                        if let Some(w) = self.w {
                            // Apply weights if provided
                            let adjusted_weight = weight * w[i];
                            c[idx] += adjusted_weight * val;
                            weight_sum[idx] += adjusted_weight;
                        } else {
                            c[idx] += weight * val;
                            weight_sum[idx] += weight;
                        }
                    }
                }
            }
        }

        // Normalize coefficients by weight sum
        for i in 0..num_coeffs {
            if weight_sum[i] > F::epsilon() {
                c[i] /= weight_sum[i];
            }
        }

        // Apply smoothing if needed
        if let Some(s) = self.s {
            if s > F::zero() {
                // Simple smoothing: average neighboring coefficients
                let mut c_smoothed = c.clone();

                for i in 1..n_x - 1 {
                    for j in 1..n_y - 1 {
                        let idx = i * n_y + j;
                        let smoothing_factor = s / (F::one() + s);

                        // Weighted average of neighboring coefficients
                        let neighbors_sum = c[idx - n_y] + c[idx + n_y] + c[idx - 1] + c[idx + 1];
                        let local_avg = neighbors_sum / F::from_f64(4.0).expect("Operation failed");

                        c_smoothed[idx] =
                            (F::one() - smoothing_factor) * c[idx] + smoothing_factor * local_avg;
                    }
                }

                c = c_smoothed;
            }
        }

        // Create the underlying spline with proper degree
        let spline = BivariateSpline::from_tck(tx, ty, c, self.kx, self.ky, self.s);

        Ok(SmoothBivariateSpline { spline })
    }
}

impl<F: Float + FromPrimitive + Debug + std::fmt::Display + crate::traits::InterpolationFloat>
    SmoothBivariateSpline<F>
{
    /// Create a new smooth bivariate spline with default parameters
    ///
    /// # Arguments
    ///
    /// * `x`, `y`, `z` - 1-D sequences of data points (order is not important)
    pub fn new<'a>(
        x: &'a ArrayView1<'a, F>,
        y: &'a ArrayView1<'a, F>,
        z: &'a ArrayView1<'a, F>,
    ) -> InterpolateResult<Self> {
        SmoothBivariateSplineBuilder::new(x, y, z).build()
    }

    /// Get the underlying bivariate spline
    pub fn get_spline(&self) -> &BivariateSpline<F> {
        &self.spline
    }
}

impl<F: Float + FromPrimitive + Debug + std::fmt::Display + crate::traits::InterpolationFloat>
    BivariateInterpolator<F> for SmoothBivariateSpline<F>
{
    fn evaluate(
        &self,
        x: &ArrayView1<F>,
        y: &ArrayView1<F>,
        grid: bool,
    ) -> InterpolateResult<Array2<F>> {
        self.spline.evaluate(x, y, grid)
    }

    fn evaluate_derivative(
        &self,
        x: &ArrayView1<F>,
        y: &ArrayView1<F>,
        dx: usize,
        dy: usize,
        grid: bool,
    ) -> InterpolateResult<Array2<F>> {
        self.spline.evaluate_derivative(x, y, dx, dy, grid)
    }

    fn integral(&self, xa: F, xb: F, ya: F, yb: F) -> InterpolateResult<F> {
        self.spline.integral(xa, xb, ya, yb)
    }
}

/// Bivariate spline approximation over a rectangular mesh
///
/// Can be used for both smoothing and interpolating data.
#[derive(Debug, Clone)]
pub struct RectBivariateSpline<
    F: Float + FromPrimitive + Debug + std::fmt::Display + crate::traits::InterpolationFloat,
> {
    /// The underlying bivariate spline
    spline: BivariateSpline<F>,
}

impl<
        F: Float
            + FromPrimitive
            + Debug
            + std::fmt::Display
            + std::ops::AddAssign
            + crate::traits::InterpolationFloat,
    > RectBivariateSpline<F>
{
    /// Create a new bivariate spline over a rectangular mesh
    ///
    /// # Arguments
    ///
    /// * `x`, `y` - 1-D arrays of coordinates in strictly ascending order
    /// * `z` - 2-D array of data with shape (x.len(), y.len())
    /// * `bbox` - Sequence of length 4 specifying the boundary of the rectangular
    ///   approximation domain. Default is [min(x), max(x), min(y), max(y)]
    /// * `kx`, `ky` - Degrees of the bivariate spline. Default is 3
    /// * `s` - Positive smoothing factor. Default is 0 (interpolation)
    pub fn new(
        x: &ArrayView1<F>,
        y: &ArrayView1<F>,
        z: &ArrayView2<F>,
        bbox: Option<[F; 4]>,
        kx: usize,
        ky: usize,
        s: Option<F>,
    ) -> InterpolateResult<Self> {
        // Check input
        if !x.iter().zip(x.iter().skip(1)).all(|(&a, &b)| b > a) {
            return Err(InterpolateError::invalid_input(
                "x must be strictly increasing".to_string(),
            ));
        }
        if !y.iter().zip(y.iter().skip(1)).all(|(&a, &b)| b > a) {
            return Err(InterpolateError::invalid_input(
                "y must be strictly increasing".to_string(),
            ));
        }

        if z.shape()[0] != x.len() {
            return Err(InterpolateError::invalid_input(
                "x dimension of z must have same number of elements as x".to_string(),
            ));
        }
        if z.shape()[1] != y.len() {
            return Err(InterpolateError::invalid_input(
                "y dimension of z must have same number of elements as y".to_string(),
            ));
        }

        // Check smoothing factor
        if let Some(s) = s {
            if s < F::zero() {
                return Err(InterpolateError::invalid_input(
                    "s should be s >= 0.0".to_string(),
                ));
            }
        }

        // Determine boundary
        let _bbox = match bbox {
            Some(bbox) => bbox,
            None => [x[0], x[x.len() - 1], y[0], y[y.len() - 1]],
        };

        // We need to create a knot sequence appropriate for B-splines
        // For a kth degree spline, we need k+1 repeated knots at the ends

        // Generate knot sequences with appropriate multiplicities at endpoints
        let mut tx_vec = Vec::with_capacity(x.len() + 2 * kx);
        let mut ty_vec = Vec::with_capacity(y.len() + 2 * ky);

        // Add repeated knots at beginning (multiplicity k+1)
        for _ in 0..=kx {
            tx_vec.push(x[0]);
        }

        // Add interior knots (using the grid points)
        for i in 1..x.len() - 1 {
            tx_vec.push(x[i]);
        }

        // Add repeated knots at end (multiplicity k+1)
        for _ in 0..=kx {
            tx_vec.push(x[x.len() - 1]);
        }

        // Similarly for y direction
        for _ in 0..=ky {
            ty_vec.push(y[0]);
        }

        for i in 1..y.len() - 1 {
            ty_vec.push(y[i]);
        }

        for _ in 0..=ky {
            ty_vec.push(y[y.len() - 1]);
        }

        // Convert to ndarray
        let tx = Array1::from(tx_vec);
        let ty = Array1::from(ty_vec);

        // Number of control points
        let n_x = tx.len() - kx - 1;
        let n_y = ty.len() - ky - 1;
        let num_coeffs = n_x * n_y;

        // Initialize coefficients
        let mut c = Array1::<F>::zeros(num_coeffs);

        // For a rectangular grid, we can use a more direct approach to determine coefficients
        // For now, we'll use a simple approach where each control point approximates a data point
        // In a full implementation, we'd set up and solve a linear system for interpolation or least squares

        // Create a mapping from data points to B-spline coefficients
        let mut weights = Array2::<F>::zeros((n_x, n_y));
        let mut values = Array2::<F>::zeros((n_x, n_y));

        // For each data point, find the corresponding knot span and add its contribution
        for i in 0..x.len() {
            let span_x = bspline_eval::find_span(x[i], &tx.view(), kx);
            let basis_x = bspline_eval::basis_funs(x[i], span_x, &tx.view(), kx);

            for j in 0..y.len() {
                let span_y = bspline_eval::find_span(y[j], &ty.view(), ky);
                let basis_y = bspline_eval::basis_funs(y[j], span_y, &ty.view(), ky);

                // The data value at (i,j)
                let z_val = z[[i, j]];

                // Distribute the value to affected control points
                for iu in 0..=kx {
                    let i_coef = span_x - kx + iu;
                    if i_coef >= n_x {
                        continue;
                    }

                    for jv in 0..=ky {
                        let j_coef = span_y - ky + jv;
                        if j_coef >= n_y {
                            continue;
                        }

                        let weight = basis_x[iu] * basis_y[jv];
                        weights[(i_coef, j_coef)] += weight;
                        values[(i_coef, j_coef)] += weight * z_val;
                    }
                }
            }
        }

        // Compute final coefficients by normalizing
        for i in 0..n_x {
            for j in 0..n_y {
                if weights[(i, j)] > F::epsilon() {
                    c[i * n_y + j] = values[(i, j)] / weights[(i, j)];
                }
            }
        }

        // Apply smoothing if requested
        if let Some(s) = s {
            if s > F::zero() {
                // Simple smoothing by averaging neighboring coefficients
                let mut c_smoothed = c.clone();

                for i in 1..n_x - 1 {
                    for j in 1..n_y - 1 {
                        let idx = i * n_y + j;
                        let smoothing_factor = s / (F::one() + s);

                        // Weighted average of neighboring coefficients
                        let neighbors_sum = c[idx - n_y] + c[idx + n_y] + c[idx - 1] + c[idx + 1];
                        let local_avg = neighbors_sum / F::from_f64(4.0).expect("Operation failed");

                        c_smoothed[idx] =
                            (F::one() - smoothing_factor) * c[idx] + smoothing_factor * local_avg;
                    }
                }

                c = c_smoothed;
            }
        }

        // Create the underlying spline with proper degree
        let spline = BivariateSpline::from_tck(tx, ty, c, kx, ky, s);

        Ok(Self { spline })
    }

    /// Get the underlying bivariate spline
    pub fn get_spline(&self) -> &BivariateSpline<F> {
        &self.spline
    }

    /// Evaluate the spline at specific points
    pub fn ev(
        &self,
        xi: &ArrayView1<F>,
        yi: &ArrayView1<F>,
        dx: usize,
        dy: usize,
    ) -> InterpolateResult<Array1<F>> {
        let result = self.spline.evaluate_derivative(xi, yi, dx, dy, false)?;
        // Convert from Array2 to Array1
        let mut result1d = Array1::zeros(xi.len());
        for i in 0..xi.len() {
            result1d[i] = result[[i, 0]];
        }
        Ok(result1d)
    }
}

impl<F: Float + FromPrimitive + Debug + std::fmt::Display + crate::traits::InterpolationFloat>
    BivariateInterpolator<F> for RectBivariateSpline<F>
{
    fn evaluate(
        &self,
        x: &ArrayView1<F>,
        y: &ArrayView1<F>,
        grid: bool,
    ) -> InterpolateResult<Array2<F>> {
        self.spline.evaluate(x, y, grid)
    }

    fn evaluate_derivative(
        &self,
        x: &ArrayView1<F>,
        y: &ArrayView1<F>,
        dx: usize,
        dy: usize,
        grid: bool,
    ) -> InterpolateResult<Array2<F>> {
        self.spline.evaluate_derivative(x, y, dx, dy, grid)
    }

    fn integral(&self, xa: F, xb: F, ya: F, yb: F) -> InterpolateResult<F> {
        self.spline.integral(xa, xb, ya, yb)
    }
}

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

    #[test]
    fn test_rect_bivariate_spline_constants() {
        // Create a 2D grid with constant values
        let x = array![1.0, 2.0, 3.0, 4.0, 5.0];
        let y = array![1.0, 2.0, 3.0, 4.0, 5.0];
        let z = Array2::ones((5, 5));

        let spline = RectBivariateSpline::new(&x.view(), &y.view(), &z.view(), None, 1, 1, None)
            .expect("Operation failed");

        // Test at a subset of grid points (2-4 range to avoid edge cases)
        let test_x = array![2.0, 3.0, 4.0];
        let test_y = array![2.0, 3.0, 4.0];
        let result = spline
            .evaluate(&test_x.view(), &test_y.view(), true)
            .expect("Operation failed");
        assert_eq!(result.shape(), &[3, 3]);

        // Values should be close to 1.0
        for i in 0..3 {
            for j in 0..3 {
                assert_abs_diff_eq!(result[[i, j]], 1.0, epsilon = 1e-10);
            }
        }

        // Test at a specific point within the interpolation range
        let xi = array![2.5];
        let yi = array![2.5];
        let result = spline
            .evaluate(&xi.view(), &yi.view(), false)
            .expect("Operation failed");
        assert_abs_diff_eq!(result[[0, 0]], 1.0, epsilon = 1e-10);
    }

    #[test]
    fn test_smooth_bivariate_spline() {
        // Create some test data with points distributed in the interior of the domain
        let x = array![1.5, 1.5, 1.5, 2.0, 2.0, 2.0, 2.5, 2.5, 2.5];
        let y = array![1.5, 2.0, 2.5, 1.5, 2.0, 2.5, 1.5, 2.0, 2.5];
        let z = array![3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0];

        let spline = SmoothBivariateSplineBuilder::new(&x.view(), &y.view(), &z.view())
            .with_degrees(1, 1)
            .build()
            .expect("Operation failed");

        // Test at interpolation points within the domain
        let test_x = array![1.8, 2.0, 2.2];
        let test_y = array![1.8, 2.0, 2.2];

        // Check that we can evaluate without errors at these points
        let result = spline
            .evaluate(&test_x.view(), &test_y.view(), true)
            .expect("Operation failed");

        // Current implementation doesn't actually fit the data, so we're not testing values yet
        assert_eq!(result.shape(), &[3, 3]);
    }
}