Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
LagrangeInterpolant.cpp
Go to the documentation of this file.
1/* This file is part of MoDELib, the Mechanics Of Defects Evolution Library.
2 *
3 *
4 * MoDELib is distributed without any warranty under the
5 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
6 */
7
8#ifndef model_LagrangeInterpolant_cpp_
9#define model_LagrangeInterpolant_cpp_
10
11#include <LagrangeInterpolant.h>
12#include <iterator>
13
14namespace model
15{
16
18 /* init*/ InterpolantBase(data,extrap)
19 {
20
21 }
22
24/* init*/ InterpolantBase(data,extrap)
25{
26
27}
28
29 double LagrangeInterpolant::_f(const double& x) const
30 {
31 switch (this->size())
32 {
33 case 0:
34 {
35 throw std::runtime_error("LinearInterpolant::operator() data is empty.");
36 return 0.0;
37 break;
38 }
39
40 default:
41 {
42 switch (this->extrapolation.type)
43 {
44 case 1:
45 {// Constant Extarpolation
46 const auto iterLower(this->lower_bound(x)); // first element in the map whose key is >= x
47 const auto iterUpper(this->upper_bound(x)); // first element in the map whose key is > x
48
49 if(iterUpper==this->begin())
50 {
51 return this->begin()->second;
52
53 }
54 else if(iterUpper==this->end())
55 {
56 return std::prev(this->end())->second;
57 }
58 else
59 {
60 return lagrangePoly(x);
61 }
62 break;
63 }
64
65 case 2:
66 {// Periodic Extarpolation
67 InterpolantBase::MapType exMap(this->map());
68 exMap.emplace(this->map().rbegin()->first-this->extrapolation.period,this->map().rbegin()->second);
69 exMap.emplace(this->map(). begin()->first+this->extrapolation.period,this->map(). begin()->second);
70 return LagrangeInterpolant(exMap).f(x);
71 break;
72 }
73
74 default:
75 {// Polynomial Extrapolation
76 return lagrangePoly(x);
77 break;
78 }
79 }
80 break;
81 }
82 }
83 }
84
85 double LagrangeInterpolant::lagrangePoly(const double& x) const
86 {
87 double temp(0.0);
88 for (InterpolantBase::const_iterator iterI=this->begin();iterI!= this->end();++iterI)
89 {
90 temp+=iterI->second*p(iterI,x);
91 }
92 return temp;
93 }
94
95 double LagrangeInterpolant::p(const InterpolantBase::MapType::const_iterator& iterI,const double& x) const
96 {
97 double temp(1.0);
98 for (InterpolantBase::MapType::const_iterator iterJ=this->begin();iterJ!=this->end();++iterJ)
99 {
100 if(iterJ!=iterI)
101 {
102 temp*=(x-iterJ->first)/(iterI->first-iterJ->first);
103 }
104 }
105 return temp;
106 }
107}
108#endif
Eigen::Matrix< double, Eigen::Dynamic, 2 > MatrixType
InterpolantBase(const MapType &data, const ExtrapolationMethod &extrap)
std::map< double, double > MapType
ExtrapolationMethod extrapolation
LagrangeInterpolant(const InterpolantBase::MatrixType &data, const ExtrapolationMethod &extrap=ExtrapolationMethod())
x
Definition int_R.m:12
int end[2]
vertex p[3]
vertex temp
vertex first