Theoretica
A C++ numerical and automatic mathematical library
polynomial.h
Go to the documentation of this file.
1 
5 
6 #ifndef THEORETICA_INTERP_POLYNOMIAL_H
7 #define THEORETICA_INTERP_POLYNOMIAL_H
8 
9 #include <vector>
10 #include "../core/real_analysis.h"
11 #include "../polynomial/polynomial.h"
12 #include "../algebra/algebra_types.h"
13 #include "../core/function.h"
14 
15 
16 namespace theoretica {
17 
18 
23  template<typename T = real>
24  inline polynomial<T> lagrange_polynomial(const std::vector<vec<T, 2>>& points) {
25 
26  if(!points.size()) {
27  TH_MATH_ERROR("lagrange_polynomial", points.size(), INVALID_ARGUMENT);
28  return polynomial<T>({T(nan())});
29  }
30 
31  // Check that all x_i are different to prevent
32  // division by zero
33  for (unsigned int i = 0; i < points.size() - 1; ++i) {
34  if(points[i].get(0) == points[i + 1].get(0)) {
35  TH_MATH_ERROR("lagrange_polynomial", points[i].get(0), INVALID_ARGUMENT);
36  return polynomial<T>({T(nan())});
37  }
38  }
39 
40  // Lagrange polynomial to construct
41  polynomial<T> L = {0};
42 
43  for (unsigned int j = 0; j < points.size(); ++j) {
44 
45  // The Lagrange polynomial is a linear
46  // combination of all l_j
47  polynomial<T> l_j = {1};
48 
49  for (unsigned int m = 0; m < points.size(); ++m) {
50 
51  if(m == j)
52  continue;
53 
54  // l_j = product(x - x_m / x_j - x_m)
55  l_j *= polynomial<T>({-points[m].get(0), 1});
56  l_j /= points[j].get(0) - points[m].get(0);
57  }
58 
59  // L = sum(y_j * l_j)
60  l_j *= points[j].get(1);
61  L += l_j;
62  }
63 
64  return L;
65  }
66 
67 
72  template<typename VectorType = std::vector<real>>
73  inline VectorType chebyshev_nodes(real a, real b, unsigned int n) {
74 
75  VectorType nodes;
76  nodes.resize(n);
77 
78  real m = (b + a) / 2.0;
79  real c = (b - a) / 2.0;
80 
81  for (unsigned int i = 1; i < n + 1; ++i)
82  nodes[i - 1] = m + c * cos(real(2 * i - 1) / real(2 * n) * PI);
83 
84  return nodes;
85  }
86 
87 
95  inline polynomial<real> interpolate_grid(real_function f, real a, real b, unsigned int order) {
96 
97  std::vector<vec2> points(order + 1);
98 
99  // Sample <order + 1> equidistant points
100  for (unsigned int i = 0; i < order + 1; ++i) {
101  real x = (b - a) / real(order) * real(i) + a;
102  points[i] = {x, f(x)};
103  }
104 
105  return lagrange_polynomial(points);
106  }
107 
108 
120  inline polynomial<real> interpolate_chebyshev(real_function f, real a, real b, unsigned int order) {
121 
122  std::vector<vec2> points(order + 1);
123 
124  std::vector<real> nodes = chebyshev_nodes(a, b, order + 1);
125 
126  // Sample <order + 1> equidistant points
127  for (unsigned int i = 0; i < order + 1; ++i)
128  points[i] = {nodes[i], f(nodes[i])};
129 
130  return lagrange_polynomial(points);
131  }
132 
133 }
134 
135 #endif
A polynomial of arbitrary order.
Definition: polynomial.h:25
Type get(int i) const
Get the i-th by value.
Definition: polynomial.h:52
A statically allocated N-dimensional vector with elements of the given type.
Definition: vec.h:88
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
double real
A real number, defined as a floating point type.
Definition: constants.h:188
polynomial< real > interpolate_grid(real_function f, real a, real b, unsigned int order)
Compute the interpolating polynomial of a real function on an equidistant point sample.
Definition: polynomial.h:95
VectorType chebyshev_nodes(real a, real b, unsigned int n)
Compute the n Chebyshev nodes on a given interval.
Definition: polynomial.h:73
polynomial< T > lagrange_polynomial(const std::vector< vec< T, 2 >> &points)
Compute the Lagrange polynomial interpolating a set of points.
Definition: polynomial.h:24
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition: dual2_functions.h:82
std::function< real(real)> real_function
Function pointer to a real function of real variable.
Definition: function.h:20
polynomial< real > interpolate_chebyshev(real_function f, real a, real b, unsigned int order)
Compute the interpolating polynomial of a real function using Chebyshev nodes as sampling points.
Definition: polynomial.h:120
constexpr real PI
The Pi mathematical constant.
Definition: constants.h:206
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54