16 #include <Eigen/Dense>
25 using Eigen::VectorXd;
26 using Eigen::MatrixXd;
32 const int N = Size - 1;
35 vector<vector<double>> coef(Size);
36 for (
auto& el : coef) el.resize(Size);
39 for (
int k = 0; k <= N / 2; ++k) {
40 coef[2 * k][N] = 1. / N;
41 coef[2 * k][0] = 1. / N ;
43 coef[2 * k][N / 2] = 2. / N * (2 * ((k + 1) % 2) - 1);
45 for (
int n = 1; n <= N / 2 - 1; ++n)
46 coef[2 * k][n] = coef[2 * k][N - n] = 2. / N * cos(n * k * M_PI * 2 / N);
49 VectorXd wgt = VectorXd::Zero(Size);
52 for (
int i = 0; i < Size; ++i) {
54 wgt[i] += coef[N][i] / (1. - N * N);
55 for (
int k = 1; k <= N / 2 - 1; ++k) {
56 double w = 2. / (1 - 4 * k * k);
57 wgt[i] += w * coef[2 * k][i];
68 assert((Size - 1) % 2 == 0);
69 VectorXd xi = VectorXd::Zero(Size);
70 for (
int i = 0; i < Size; ++i) {
71 double Cos = cos(i / (Size - 1.) * M_PI);
72 xi[i] = (1 - Cos) / 2;
81 double C = 2 * (2 * x - 1);
83 if (Size >= 1) pol[0] = 1;
84 if (Size >= 2) pol[1] = C / 2;
86 for (
int i = 2; i < Size; ++i)
87 pol[i] = C * pol[i - 1] - pol[i - 2];
101 VectorXd polSum(Size);
103 VectorXd pol0 = 0 * x.array() + 1;
104 VectorXd pol1 = 2 * x.array() - 1;
105 VectorXd C = 2 * pol1;
107 VectorXd pol2(x.size());
108 for (
int i = 2; i < Size; ++i) {
109 polSum(i - 2) = pol0.sum();
111 pol2 = C.array() * pol1.array() - pol0.array();
117 polSum(Size - 2) = pol0.sum();
118 polSum(Size - 1) = pol1.sum();
128 MatrixXd
getCoefs(
int Size,
bool isInverse =
false)
130 const int N = Size - 1;
133 MatrixXd coef(Size, Size);
137 if (isInverse ==
true) {C = 1. / 2; }
139 for (
int k = 0; k <= N; ++k) {
142 coef(k, 0) = C * (k % 2 == 1 ? -1 : 1);
144 mul = k % 2 == 1 ? -1 : 1;
145 coef(N - k, N) = C * mul;
149 for (
int n = 1; n <= N - 1; ++n) {
150 double el = cos(n * k * M_PI / N) * 2.*C * mul;
151 if (!isInverse) coef(k, N - n) = el;
152 else coef(N - k, N - n) = el;
168 coef.row(coef.rows() - 1) *= 0.5;
177 double evalPol(
const VectorXd& polCoef,
double x)
179 VectorXd pols =
getPols(polCoef.size(), x);
181 double s = pols.dot(polCoef);
192 double Norm = (xi[xi.size() - 1] - xi[0]) / 2;
193 VectorXd coefs(xi.size());
194 for (
int i = 0; i < xi.size(); ++i) {
195 double num = 1, den = 1;
196 for (
int j = 0; j < xi.size(); ++j)
198 num *= (x - xi(j)) / Norm;
199 den *= (xi(i) - xi(j)) / Norm;
201 coefs(i) = num / den;
211 double interpol(VectorXd xi, VectorXd vals,
double x)
214 return coefs.dot(vals);
Eigen::VectorXd interpol(const Eigen::VectorXd &xi, double x)
Get Interpolation vector k_i for point x from the function values at points xi (polynomial interpolat...
Eigen::VectorXd getWeights(int Size)
Get the vector of weights to calculate the integral over the Chebyshev nodes The nodes are by definit...
Eigen::VectorXd getPols(int Size, double x)
Evaluate Chebyshev polynomials up to Size at point x It returns a vector of the P_i(x) for i=0....
Eigen::MatrixXd getCoefsCheb(int oldSize)
Transformation matrix between Cheb nodes and Cheb coefficients with better normalization of the borde...
Eigen::VectorXd getPolsSum(int Size, Eigen::VectorXd x)
Calculate the Chebyshev polynomials of order i=0..Size-1 at points given in vector x_j and sum it ove...
Eigen::MatrixXd getCoefs(int Size, bool isInverse=false)
Transformation matrix between Cheb nodes and coefficients of the Cheb polynomials Notice,...
Eigen::VectorXd getNodes(int Size)
Get the vector of positions of the Chebyshev nodes The nodes are by definition between 0 and 1,...
double evalPol(const Eigen::VectorXd &polCoef, double x)
Evaluate Cheb.
Abstract base class for different kinds of events.