6 #ifndef THEORETICA_INTERP_POLYNOMIAL_H
7 #define THEORETICA_INTERP_POLYNOMIAL_H
10 #include "../core/real_analysis.h"
11 #include "../polynomial/polynomial.h"
12 #include "../algebra/algebra_types.h"
13 #include "../core/function.h"
23 template<
typename T = real>
27 TH_MATH_ERROR(
"lagrange_polynomial", points.size(), INVALID_ARGUMENT);
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);
43 for (
unsigned int j = 0; j < points.size(); ++j) {
49 for (
unsigned int m = 0; m < points.size(); ++m) {
56 l_j /= points[j].
get(0) - points[m].get(0);
60 l_j *= points[j].
get(1);
72 template<
typename VectorType = std::vector<real>>
78 real m = (b + a) / 2.0;
79 real c = (b - a) / 2.0;
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);
97 std::vector<vec2> points(order + 1);
100 for (
unsigned int i = 0; i < order + 1; ++i) {
102 points[i] = {x, f(x)};
122 std::vector<vec2> points(order + 1);
127 for (
unsigned int i = 0; i < order + 1; ++i)
128 points[i] = {nodes[i], f(nodes[i])};
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