Skip to content

Commit 3b84cc2

Browse files
committed
tests for periodic cubic spline interpolation
1 parent eb7a6d6 commit 3b84cc2

1 file changed

Lines changed: 144 additions & 0 deletions

File tree

tests/cubic_spline_strat.rs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,4 +462,148 @@ fn extrapolate_periodic() {
462462
)
463463
.build()
464464
.unwrap();
465+
466+
let q = Array1::linspace(-3.0, 15.0, 30);
467+
let res = interp.interp_array(&q).unwrap();
468+
let expect = array![
469+
3.,
470+
4.45171164,
471+
5.5978812,
472+
6.54905092,
473+
3.79486808,
474+
0.76011398,
475+
1.36656494,
476+
2.4432986,
477+
2.50822019,
478+
2.40158688,
479+
2.63514361,
480+
3.01451693,
481+
2.59950279,
482+
1.96267846,
483+
1.65029582,
484+
-0.22831889,
485+
-2.04318459,
486+
0.41031552,
487+
3.63201944,
488+
4.66215778,
489+
6.05245899,
490+
6.19632834,
491+
2.68818585,
492+
0.64246067,
493+
1.77979077,
494+
2.52789822,
495+
2.46676892,
496+
2.41681682,
497+
2.76866398,
498+
3.
499+
];
500+
assert_relative_eq!(res, expect, epsilon = f64::EPSILON, max_relative = 0.001);
501+
}
502+
503+
#[test]
504+
fn extrapolate_periodic_multidim() {
505+
let y = array![[0.5, 1.0], [0.0, 1.5], [0.0, 1.5], [0.5, 1.0],];
506+
let x = array![-1.0, 0.0, 2.0, 3.0];
507+
let interp = Interp1D::builder(y)
508+
.x(x)
509+
.strategy(
510+
CubicSpline::new()
511+
.extrapolate(true)
512+
.boundary(BoundaryCondition::Periodic),
513+
)
514+
.build()
515+
.unwrap();
516+
517+
let q = Array1::linspace(-1.5, 3.5, 15);
518+
let res = interp.interp_array(&q).unwrap();
519+
let expect = array![
520+
[0.325, 1.175],
521+
[0.48279883, 1.01720117],
522+
[0.46260933, 1.03739067],
523+
[0.28075802, 1.21924198],
524+
[0.04424198, 1.45575802],
525+
[-0.14693878, 1.64693878],
526+
[-0.26173469, 1.76173469],
527+
[-0.3, 1.8],
528+
[-0.26173469, 1.76173469],
529+
[-0.14693878, 1.64693878],
530+
[0.04424198, 1.45575802],
531+
[0.28075802, 1.21924198],
532+
[0.46260933, 1.03739067],
533+
[0.48279883, 1.01720117],
534+
[0.325, 1.175]
535+
];
536+
assert_relative_eq!(res, expect, epsilon = f64::EPSILON, max_relative = 0.001);
537+
}
538+
539+
#[test]
540+
fn extrapolate_periodic_len3() {
541+
let y = array![0.5, 0.0, 0.5];
542+
let x = array![-1.0, 0.0, 3.0];
543+
let interp = Interp1D::builder(y)
544+
.x(x)
545+
.strategy(
546+
CubicSpline::new()
547+
.extrapolate(true)
548+
.boundary(BoundaryCondition::Periodic),
549+
)
550+
.build()
551+
.unwrap();
552+
553+
let q = Array1::linspace(-1.5, 3.5, 15);
554+
let res = interp.interp_array(&q).unwrap();
555+
let expect = array![
556+
0.55555556,
557+
0.53773891,
558+
0.40889213,
559+
0.20845481,
560+
0.02623907,
561+
-0.05701328,
562+
-0.03717201,
563+
0.05555556,
564+
0.19080013,
565+
0.33819242,
566+
0.46736314,
567+
0.54794299,
568+
0.54956268,
569+
0.44314869,
570+
0.25
571+
];
572+
assert_relative_eq!(res, expect, epsilon = f64::EPSILON, max_relative = 0.001);
573+
}
574+
575+
#[test]
576+
fn extrapolate_periodic_len3_multidim() {
577+
let y = array![[0.5, 1.0], [0.0, 2.5], [0.5, 1.0],];
578+
let x = array![-1.0, 0.0, 3.0];
579+
let interp = Interp1D::builder(y)
580+
.x(x)
581+
.strategy(
582+
CubicSpline::new()
583+
.extrapolate(true)
584+
.boundary(BoundaryCondition::Periodic),
585+
)
586+
.build()
587+
.unwrap();
588+
589+
let q = Array1::linspace(-1.5, 3.5, 15);
590+
let res = interp.interp_array(&q).unwrap();
591+
let expect = array![
592+
[0.55555556, 0.83333333],
593+
[0.53773891, 0.88678328],
594+
[0.40889213, 1.27332362],
595+
[0.20845481, 1.87463557],
596+
[0.02623907, 2.4212828],
597+
[-0.05701328, 2.67103984],
598+
[-0.03717201, 2.61151603],
599+
[0.05555556, 2.33333333],
600+
[0.19080013, 1.92759961],
601+
[0.33819242, 1.48542274],
602+
[0.46736314, 1.09791059],
603+
[0.54794299, 0.85617104],
604+
[0.54956268, 0.85131195],
605+
[0.44314869, 1.17055394],
606+
[0.25, 1.75]
607+
];
608+
assert_relative_eq!(res, expect, epsilon = f64::EPSILON, max_relative = 0.001);
465609
}

0 commit comments

Comments
 (0)