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);
28 return polynomial<T>({T(
nan())});
33 for (
unsigned int i = 0; i < points.size() - 1; ++i) {
34 if(points[i][0] == points[i + 1][0]) {
35 TH_MATH_ERROR(
"lagrange_polynomial", points[i][0], INVALID_ARGUMENT);
36 return polynomial<T>({T(
nan())});
41 polynomial<T> L = {0};
43 for (
unsigned int j = 0; j < points.size(); ++j) {
47 polynomial<T> l_j = {1};
49 for (
unsigned int m = 0; m < points.size(); ++m) {
55 l_j *= polynomial<T>({-points[m][0], 1});
56 l_j /= points[j][0] - points[m][0];
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])};
#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:225
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:198
VectorType chebyshev_nodes(real a, real b, unsigned int n)
Compute the n Chebyshev nodes on a given interval.
Definition polynomial.h:73
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:54
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
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition dual2_functions.h:86
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
polynomial< T > lagrange_polynomial(const std::vector< vec< T, 2 > > &points)
Compute the Lagrange polynomial interpolating a set of points.
Definition polynomial.h:24
std::function< real(real)> real_function
Function pointer to a real function of real variable.
Definition function.h:20
constexpr real PI
The Pi mathematical constant.
Definition constants.h:216