-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdynamic_strategy.rs
More file actions
48 lines (44 loc) · 1.5 KB
/
dynamic_strategy.rs
File metadata and controls
48 lines (44 loc) · 1.5 KB
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
use ndarray::prelude::*;
use ninterp::prelude::*;
fn main() {
using_enum();
using_boxdyn();
}
/// Use a provided strategy enum to allow strategy swapping.
/// - serde compatible
/// - Statically dispatched (faster runtime)
/// - **NOT** compatible with custom strategies
fn using_enum() {
// Create mutable interpolator
let mut interp: Interp1D<_, strategy::enums::Strategy1DEnum> = Interp1D::new(
array![0., 1., 2.],
array![0., 3., 6.],
// Provide the strategy as an enum
strategy::Linear.into(),
Extrapolate::Error,
)
.unwrap();
assert_eq!(interp.interpolate(&[1.75]).unwrap(), 5.25);
// Change strategy to `Nearest`
interp.set_strategy(strategy::Nearest).unwrap();
assert_eq!(interp.interpolate(&[1.75]).unwrap(), 6.);
}
/// Use a provided strategy enum to allow strategy swapping.
/// - **NOT** serde compatible
/// - Dynamically dispatched (slower runtime)
/// - Compatible with custom strategies
fn using_boxdyn() {
// Create mutable interpolator
let mut interp = Interp1D::new(
array![0., 1., 2.],
array![0., 3., 6.],
// Provide the strategy as a trait object
Box::new(strategy::Linear) as Box<dyn strategy::traits::Strategy1D<_>>,
Extrapolate::Error,
)
.unwrap();
assert_eq!(interp.interpolate(&[1.75]).unwrap(), 5.25);
// Change strategy to `Nearest`
interp.set_strategy(Box::new(strategy::Nearest)).unwrap();
assert_eq!(interp.interpolate(&[1.75]).unwrap(), 6.);
}