11use ndarray:: { NdFloat , Dimension } ;
22use almost:: AlmostEqual ;
33
4- use crate :: { Result , CsapsError :: ReshapeError , CubicSmoothingSpline } ;
4+ use crate :: { Result , CubicSmoothingSpline } ;
5+ use crate :: ndarrayext:: to_2d_simple;
6+
57use super :: { GridCubicSmoothingSpline , NdGridSpline } ;
68
79
@@ -15,10 +17,10 @@ impl<'a, T, D> GridCubicSmoothingSpline<'a, T, D>
1517 let ndim_m1 = ndim - 1 ;
1618
1719 let breaks = self . x . to_vec ( ) ;
18- let mut coeffs = self . y . view ( ) ;
20+ let mut coeffs = self . y . to_owned ( ) ;
1921 let mut coeffs_shape = coeffs. shape ( ) . to_vec ( ) ;
2022
21- let mut smooth: Vec < Option < T > > = Vec :: new ( ) ;
23+ let mut smooth: Vec < Option < T > > = vec ! [ None ; ndim ] ;
2224
2325 let mut permute_axes = D :: zeros ( ndim) ;
2426 permute_axes[ 0 ] = ndim_m1;
@@ -28,41 +30,47 @@ impl<'a, T, D> GridCubicSmoothingSpline<'a, T, D>
2830
2931 for ax in ( 0 ..ndim) . rev ( ) {
3032 let x = breaks[ ax] . view ( ) ;
33+ let y = to_2d_simple ( coeffs. view ( ) ) ?;
34+
35+ let weights = self . weights [ ax] . map ( |v| v. reborrow ( ) ) ;
36+ let s = self . smooth [ ax] ;
37+
38+ println ! ( "\n x shape: {:?}" , x. shape( ) ) ;
39+ println ! ( "y shape: {:?}" , y. shape( ) ) ;
40+
41+ println ! ( "\n x: {:?}" , x) ;
42+ println ! ( "y: {:?}" , y) ;
43+
44+ let sp = CubicSmoothingSpline :: new ( x, y)
45+ . with_optional_weights ( weights)
46+ . with_optional_smooth ( s)
47+ . make ( ) ?;
48+
49+ smooth[ ax] = sp. smooth ( ) ;
50+
51+ coeffs = {
52+ let spline = sp. spline ( ) . unwrap ( ) ;
53+
54+ coeffs_shape[ ndim_m1] = spline. pieces ( ) * spline. order ( ) ;
55+ let mut new_shape = D :: zeros ( ndim) ;
56+ for ( ax, & sz) in coeffs_shape. iter ( ) . enumerate ( ) {
57+ new_shape[ ax] = sz
58+ }
59+
60+ spline. coeffs ( )
61+ . into_shape ( new_shape) . unwrap ( )
62+ . permuted_axes ( permute_axes. clone ( ) )
63+ . to_owned ( )
64+ } ;
65+
66+ coeffs_shape = coeffs. shape ( ) . to_vec ( ) ;
3167
32- if ndim > 2 {
33- let coeffs_2d = {
34- let shape = coeffs. shape ( ) . to_vec ( ) ;
35- let new_shape = [ shape[ 0 ..ndim_m1] . iter ( ) . product ( ) , shape[ ndim_m1] ] ;
36-
37- match coeffs. view ( ) . into_shape ( new_shape) {
38- Ok ( coeffs_2d) => coeffs_2d,
39- Err ( err) => {
40- return Err (
41- ReshapeError (
42- format ! ( "Cannot reshape data array with shape {:?} to 2-d array with shape {:?}\n {}" ,
43- shape, new_shape, err)
44- )
45- )
46- }
47- }
48- } ;
49-
50- // CubicSmoothingSpline::new(x, coeffs_2d.view())
51- // .make()?
52- // .spline().unwrap()
53- // .coeffs().to_owned()
54- //
55- // } else {
56- //
57- // CubicSmoothingSpline::new(x, coeffs.view())
58- // .make()?
59- // .spline().unwrap()
60- // .coeffs().to_owned()
61- }
68+ println ! ( "\n coeffs shape: {:?}" , coeffs. shape( ) ) ;
69+ println ! ( "coeffs: {:?}" , coeffs) ;
6270 }
6371
64- self . smooth = Some ( smooth) ;
65- self . spline = Some ( NdGridSpline :: new ( breaks, coeffs. to_owned ( ) ) ) ;
72+ self . smooth = smooth;
73+ self . spline = Some ( NdGridSpline :: new ( breaks, coeffs) ) ;
6674
6775 Ok ( ( ) )
6876 }
0 commit comments