-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearSpline.hpp
55 lines (37 loc) · 1.17 KB
/
LinearSpline.hpp
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
#ifndef LinearSpline_head
#define LinearSpline_head
#include<cmath>
#include<vector>
#include"Interpolation.hpp"
template<class LD>
class LinearSpline: public Interpolation<LD>{
using VecLD=std::vector<LD>;
using un_int=unsigned int;
public:
LinearSpline()=default;
LinearSpline(VecLD *x,VecLD *y): Interpolation<LD>(x,y){};
LD operator()(LD x){
/*
1st order spline
*/
un_int i=this->bSearch(x);
LD a=((*this->X)[i+1]-x)/((*this->X)[i+1]-(*this->X)[i]);
return a*(*this->Y)[i]+(1-a)*(*this->Y)[i+1];
}
virtual LD derivative_1(LD x){
/*
1st order spline of the first derivative.
It is better than taking the derivative of operator()
*/
un_int i=this->bSearch(x);
LD xi=(*this->X)[i],yi=(*this->Y)[i];
LD xj=(*this->X)[i+1],yj=(*this->Y)[i+1];
if (i==this->N-2) {return (yi-yj)/(xi-xj);}
LD a=(xj-x)/(xj-xi);
LD dydx0=(yi-yj)/(xi-xj);
LD dydx1=(yj-(*this->Y)[i+2])/(xj-(*this->X)[i+2]);
return a*dydx0+(1-a)*dydx1;
}
virtual LD derivative_2(LD x){return 0;}
};
#endif