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
pub struct Spline{
x: Vec<f64>,
coefficients: Vec<(f64, f64, f64, f64)>
}
impl Spline{
pub fn from_vec(points: Vec<(f64, f64)>)->Spline{
assert!(points.len() > 1, "Only one point. No Interpolation possible");
let mut points = points;
points.sort_by(|(x0, _), (x1, _)| x0.partial_cmp(x1).unwrap());
let mut m = Vec::<f64>::with_capacity(points.len()+3);
for i in 0..points.len()-1{
m.push((points[i+1].1 - points[i].1)/(points[i+1].0-points[i].0));
}
if m.len() == 1{
let p1 = points[0];
let p2 = points[1];
let coefficients = vec![(p1.1, m[0], 0., 0.)];
return Spline{x: vec![p1.0, p2.0], coefficients}
}
m.insert(0, 2.0*m[0]-m[1]);
m.insert(0, 2.0*m[0]-m[1]);
m.push(2.0*m[m.len()-1]-m[m.len()-2]);
m.push(2.0*m[m.len()-1]-m[m.len()-2]);
let m = m;
let mut t= Vec::<f64>::with_capacity(points.len()-1);
for i in 0..points.len(){
let i = i + 2;
let w1 = (m[i+1]-m[i]).abs() + (m[i+1]+m[i]).abs()/2.;
let w2 = (m[i-1]-m[i-2]).abs() + (m[i-1]+m[i-2]).abs()/2.;
let nominator = w1*m[i-1] + w2*m[i];
if nominator == 0.0{
t.push(0.0)
}else{
t.push(nominator/(w1+w2))
}
}
let t = t;
let mut coefficients = Vec::<(f64, f64, f64, f64)>::with_capacity(points.len()-1);
for i in 0..points.len()-1{
let (xi, yi) = points[i];
let (xi1, _) = points[i+1];
let z = xi1-xi;
let a = yi;
let b = t[i];
let c = (3.*m[i+2] - 2.*t[i]-t[i+1])/z;
let d = (t[i]+t[i+1] - 2.*m[i+2])/z.powi(2);
coefficients.push((a, b, c, d));
}
let x = points.iter().map(|p|{
p.0
}).collect();
Spline{x, coefficients}
}
fn segment(&self, pos: f64) -> (f64, f64, f64, f64, f64){
if pos <= self.x[0]{
let (a, b, _, _) = self.coefficients[0];
let xi = self.x[0];
return (a, b, 0.0, 0.0, xi);
}
if pos >= self.x[self.x.len()-1]{
let (a, b, c, d) = self.coefficients[self.coefficients.len()-1];
let xi = self.x[self.x.len()-2];
let xi1 = self.x[self.x.len()-1];
let dx = xi1-xi;
let y1 = a + b*dx + c*dx*dx + d*dx*dx*dx;
let m = b + 2.*c*dx + 3.*d*dx*dx;
return (y1, m, 0.0, 0.0, xi1);
}
let mut upper = self.x.len();
let mut lower = 0;
while upper-lower > 1{
let center = (upper+lower)/2;
if pos > self.x[center]{
lower = center;
}else{
upper = center;
}
}
let coef = self.coefficients[lower];
(coef.0, coef.1, coef.2, coef.3, self.x[lower])
}
pub fn sample(&self, pos: f64)-> f64{
let (a, b, c, d, xi) = self.segment(pos);
let dx = pos-xi;
a + b*dx + c*dx*dx + d*dx*dx*dx
}
pub fn derivative_1(&self, pos: f64) -> f64{
let (_, b, c, d, xi) = self.segment(pos);
let dx = pos-xi;
b + 2.*c*dx + 3.*d*dx*dx
}
pub fn derivative_2(&self, pos: f64) -> f64{
let (_, _, c, d, xi) = self.segment(pos);
let dx = pos-xi;
2.*c + 6.*d*dx
}
pub fn derivative_3(&self, pos: f64) -> f64{
let (_, _, _, d, _) = self.segment(pos);
6.0*d
}
}
pub fn vec_to_points(x: &Vec<f64>, y: &Vec<f64>) -> Vec<(f64, f64)>{
assert!(x.len() == y.len());
let mut points = Vec::<(f64, f64)>::with_capacity(x.len());
for i in 0..x.len(){
points.push((x[i], y[i]))
}
points
}
#[cfg(feature = "n_dimensional")]
pub mod n_dimensional;
mod tests;