-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathnumerical.rs
70 lines (55 loc) · 1.29 KB
/
numerical.rs
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
extern crate peroxide;
use peroxide::fuga::*;
macro_rules! rnd {
( $x:expr ) => {
($x * 100.0).round() / 100.0
};
}
fn f(x: f64) -> f64 {
1.0 / (1.0 + x * x)
}
#[test]
fn test_cubic_spline_initialization() -> Result<(), Box<dyn std::error::Error>> {
let mut vx = Vec::new();
let mut vy = Vec::new();
for i in 0..11 {
let x = i as f64;
vx.push(x);
vy.push(f(x));
}
let spline = cubic_spline(&vx, &vy)?;
for i in 0..11 {
let x = i as f64;
let y = spline.eval(x);
assert_eq!(rnd!(y), rnd!(f(x)));
}
Ok(())
}
#[test]
fn test_cubic_spline_extension() -> Result<(), Box<dyn std::error::Error>> {
let mut vx = Vec::new();
let mut vy = Vec::new();
for i in 0..11 {
let x = (i - 10) as f64;
vx.push(x);
vy.push(f(x));
}
let mut spline = cubic_spline(&vx, &vy)?;
vx = Vec::new();
vy = Vec::new();
for i in 11..21 {
let x = (i - 10) as f64;
vx.push(x);
vy.push(f(x));
}
spline.extend_with_nodes(vx, vy)?;
for i in 0..21 {
let x = (i - 10) as f64;
let y = spline.eval(x);
assert_eq!(
format!("{} = {}", x, rnd!(y)),
format!("{} = {}", x, rnd!(f(x)))
);
}
Ok(())
}