CubicSpline.cpp 2.81 KB
Newer Older
Cédric Mezrag's avatar
refs#16  
Cédric Mezrag committed
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
#include "../../../include/NumA/interpolation/CubicSpline.h"

#include <iostream>
#include <sstream>
#include <vector>
#include <cmath>

/*
 * Cubic spline algorithm based on wikipedia :
 * https://en.wikipedia.org/w/index.php?title=Spline_%28mathematics%29&oldid=288288033#Algorithm_for_computing_natural_cubic_splines
 *
 *
 */

namespace NumA {

	CubicSpline::CubicSpline(std::vector<double> x, std::vector<double> y):
			m_X(x),
			m_Y(y),
			m_N(x.size()-1),
			m_aCoeff(y),
			m_bCoeff(0),
			m_cCoeff(0),
			m_dCoeff(0),
			m_SplineDefined(false)
			{
				for(unsigned int i = 0; i <= m_N; i++){

					m_bCoeff.push_back(0.) ;
					m_cCoeff.push_back(0.) ;
					m_dCoeff.push_back(0.) ;

				};

			}

	CubicSpline::~CubicSpline(){}


	void CubicSpline::ConstructSpline(){

		std::vector<double> h(0) ;
		std::vector<double> alpha(0) ;
		std::vector<double> l(0) ;
		std::vector<double> mu(0) ;
		std::vector<double> z(0) ;


		for(unsigned int i = 0; i < m_N; i++){
			h.push_back(m_X.at(i+1)-m_X.at(i));
		};

		alpha.push_back(0.) ;
		for(unsigned int i=1; i < m_N; i++){
			alpha.push_back(  3. / h.at(i) * ( m_aCoeff.at(i+1) - m_aCoeff.at(i) ) - 3. / h.at(i-1) * (m_aCoeff.at(i) - m_aCoeff.at(i-1))  );
		};

		//std::cout << " alpha is ok" << std::endl ;

		l.push_back(1.);
		mu.push_back(0.);
		z.push_back(0.);

		for(unsigned int i=1 ; i< m_N ; i++){

			l.push_back( 2. * (m_X.at(i+1)-m_X.at(i-1) ) - h.at(i-1) * mu.at(i-1) );

			mu.push_back( h.at(i) / l.at(i) );

			z.push_back( ( alpha.at(i) - h.at(i-1) * z.at( i-1 ) ) / l.at(i)  );

		} ;

		l.push_back(1) ;
		z.push_back(0) ;
		m_cCoeff.at(m_N) = 0. ;


		for(int i = m_N - 1; i >= 0; i--){


			m_cCoeff.at(i) = z.at(i) - mu.at(i)* m_cCoeff.at(i+1);


			m_bCoeff.at(i) = ( m_aCoeff.at(i+1) - m_aCoeff.at(i) ) / h.at(i)
									-  1./3. * h.at(i) * ( m_cCoeff.at(i+1) + 2 * m_cCoeff.at(i) ) ;


			m_dCoeff.at(i) = ( m_cCoeff.at(i+1) - m_cCoeff.at(i) ) / 3. / h.at(i) ;


		} ;


		m_SplineDefined = true ;

	}



	double CubicSpline::getSplineInsideValue(double z){

		// Test if splines have been computed before
		if(m_SplineDefined){

			if(z < m_X.at(0) or z > m_X.at(m_N)){
				std::cout << "Warning X is outside the interpolation domain " << std::endl ;
				std::cout << " Interpolation wil return zero " << std::endl ;
				return 0. ;
			}
			else{

				for(unsigned int i = 0 ; i < m_N ; i++ ){

					if(z <= m_X.at(i+1) ){

						return m_aCoeff.at(i) + m_bCoeff.at(i)*(z-m_X.at(i)) + m_cCoeff.at(i)* pow( (z-m_X.at(i)) , 2.)
									+ m_dCoeff.at(i) * pow( (z-m_X.at(i)) , 3.) ;

					};

				};

			};

		}
		else{

			std::cout << "Warning splines have not been defined " << std::endl;
			std::cout << " Return is set to zero " << std::endl;

			return 0. ;

		};

		std::cout << " If this point is reached, there is a problem in computing the spline" << std::endl ;
137
		return 0;
Cédric Mezrag's avatar
refs#16  
Cédric Mezrag committed
138
139
140
141
142
	}



}