Theoretica
Scientific Computing
Loading...
Searching...
No Matches
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.h"
13#include "../core/function.h"
14
15
16namespace theoretica {
17
18
29 template <
30 typename Vector1, typename Vector2,
34 >
35 inline polynomial<Type> lagrange(const Vector1& x, const Vector2& y) {
36
37 if(!x.size()) {
38 TH_MATH_ERROR("lagrange", x.size(), MathError::InvalidArgument);
40 }
41
42 if(x.size() != y.size()) {
43 TH_MATH_ERROR("lagrange", y.size(), MathError::InvalidArgument);
45 }
46
47 // Check that all x_i are different to prevent division by zero
48 for (unsigned int i = 0; i < x.size() - 1; ++i) {
49
50 if(abs(x[i] - x[i + 1]) < MACH_EPSILON) {
53 }
54 }
55
56 // Lagrange polynomial to construct
57 polynomial<Type> L = {0};
58
59 for (unsigned int j = 0; j < x.size(); ++j) {
60
61 // The Lagrange polynomial is a linear combination of all l_j
62 polynomial<real> l_j = {1.0};
63
64 for (unsigned int m = 0; m < x.size(); ++m) {
65
66 if(m == j)
67 continue;
68
69 // l_j = product(x - x_m / x_j - x_m)
70 l_j *= polynomial<real>({-x[m], 1.0}) / (x[j] - x[m]);
71 }
72
73 // L = sum(y_j * l_j)
75 term.resize(l_j.size());
76
77 for (size_t i = 0; i < l_j.size(); ++i)
78 term[i] = y[j] * l_j[i];
79
80 L += term;
81 }
82
83 return L;
84 }
85
86
95 template<typename Vector = vec<real>>
96 inline Vector chebyshev_nodes(real a, real b, unsigned int n) {
97
98 Vector nodes;
99 nodes.resize(n);
100
101 real m = (b + a) / 2.0;
102 real c = (b - a) / 2.0;
103
104 for (unsigned int i = 1; i < n + 1; ++i)
105 nodes[i - 1] = m + c * cos(real(2 * i - 1) / real(2 * n) * PI);
106
107 return nodes;
108 }
109
110
119 template <
120 typename RealFunction,
122 >
124
125 vec<real> x (order + 1);
126 vec<Type> y (order + 1);
127
128 // Sample <order + 1> equidistant points
129 for (unsigned int i = 0; i < order + 1; ++i) {
130 x[i] = (b - a) / real(order) * real(i) + a;
131 y[i] = f(x[i]);
132 }
133
134 return lagrange(x, y);
135 }
136
137
150 template <
151 typename RealFunction,
153 >
155
156 vec<real> x = chebyshev_nodes(a, b, order + 1);
157 vec<Type> y (order + 1);
158
159 // Sample <order + 1> equidistant points
160 for (unsigned int i = 0; i < order + 1; ++i)
161 y[i] = f(x[i]);
162
163 return lagrange(x, y);
164 }
165
166
174 template <
175 typename RealFunction,
177 >
178 inline polynomial<Type> interpolate(RealFunction f, real a, real b, unsigned int order) {
179 return interpolate_chebyshev(f, a, b, order);
180 }
181
182}
183
184#endif
A polynomial of arbitrary order with coefficients of a specified type.
Definition polynomial.h:28
void resize(size_t sz)
Change the number of coefficients of the polynomial.
Definition polynomial.h:168
A statically allocated N-dimensional vector with elements of the given type.
Definition vec.h:92
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
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:207
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:242
Vector chebyshev_nodes(real a, real b, unsigned int n)
Compute the n Chebyshev nodes on a given interval.
Definition polynomial.h:96
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
polynomial< Type > interpolate_grid(RealFunction f, real a, real b, unsigned int order)
Compute the interpolating polynomial of a real function on an equidistant grid.
Definition polynomial.h:123
polynomial< Type > lagrange(const Vector1 &x, const Vector2 &y)
Compute the Lagrange interpolating polynomial of a set of points.
Definition polynomial.h:35
polynomial< Type > interpolate(RealFunction f, real a, real b, unsigned int order)
Use the best available method to compute the interpolating polynomial of a real function.
Definition polynomial.h:178
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition dual2_functions.h:86
@ InvalidArgument
Invalid argument.
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:216
polynomial< Type > interpolate_chebyshev(RealFunction 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:154
constexpr real PI
The Pi mathematical constant.
Definition constants.h:225