24 using Eigen::VectorXd;
25 using Eigen::MatrixXd;
31 const int N = Size - 1;
34 std::vector<std::vector<double>> coef(Size);
35 for (
auto& el : coef) el.resize(Size);
38 for (
int k = 0; k <= N / 2; ++k) {
39 coef[2 * k][N] = 1. / N;
40 coef[2 * k][0] = 1. / N ;
42 coef[2 * k][N / 2] = 2. / N * (2 * ((k + 1) % 2) - 1);
44 for (
int n = 1; n <= N / 2 - 1; ++n)
45 coef[2 * k][n] = coef[2 * k][N - n] = 2. / N * cos(n * k * M_PI * 2 / N);
48 VectorXd wgt = VectorXd::Zero(Size);
51 for (
int i = 0; i < Size; ++i) {
53 wgt[i] += coef[N][i] / (1. - N * N);
54 for (
int k = 1; k <= N / 2 - 1; ++k) {
55 double w = 2. / (1 - 4 * k * k);
56 wgt[i] += w * coef[2 * k][i];
67 assert((Size - 1) % 2 == 0);
68 VectorXd xi = VectorXd::Zero(Size);
69 for (
int i = 0; i < Size; ++i) {
70 double Cos = cos(i / (Size - 1.) * M_PI);
71 xi[i] = (1 - Cos) / 2;
80 double C = 2 * (2 * x - 1);
82 if (Size >= 1) pol[0] = 1;
83 if (Size >= 2) pol[1] = C / 2;
85 for (
int i = 2; i < Size; ++i)
86 pol[i] = C * pol[i - 1] - pol[i - 2];
100 VectorXd polSum(Size);
102 VectorXd pol0 = 0 * x.array() + 1;
103 VectorXd pol1 = 2 * x.array() - 1;
104 VectorXd C = 2 * pol1;
106 VectorXd pol2(x.size());
107 for (
int i = 2; i < Size; ++i) {
108 polSum(i - 2) = pol0.sum();
110 pol2 = C.array() * pol1.array() - pol0.array();
116 polSum(Size - 2) = pol0.sum();
117 polSum(Size - 1) = pol1.sum();
127 MatrixXd
getCoefs(
int Size,
bool isInverse =
false)
129 const int N = Size - 1;
132 MatrixXd coef(Size, Size);
136 if (isInverse ==
true) {C = 1. / 2; }
138 for (
int k = 0; k <= N; ++k) {
141 coef(k, 0) = C * (k % 2 == 1 ? -1 : 1);
143 mul = k % 2 == 1 ? -1 : 1;
144 coef(N - k, N) = C * mul;
148 for (
int n = 1; n <= N - 1; ++n) {
149 double el = cos(n * k * M_PI / N) * 2.*C * mul;
150 if (!isInverse) coef(k, N - n) = el;
151 else coef(N - k, N - n) = el;
167 coef.row(coef.rows() - 1) *= 0.5;
176 double evalPol(
const VectorXd& polCoef,
double x)
178 VectorXd pols =
getPols(polCoef.size(), x);
180 double s = pols.dot(polCoef);
191 double Norm = (xi[xi.size() - 1] - xi[0]) / 2;
192 VectorXd coefs(xi.size());
193 for (
int i = 0; i < xi.size(); ++i) {
194 double num = 1, den = 1;
195 for (
int j = 0; j < xi.size(); ++j)
197 num *= (x - xi(j)) / Norm;
198 den *= (xi(i) - xi(j)) / Norm;
200 coefs(i) = num / den;
210 double interpol(VectorXd xi, VectorXd vals,
double x)
213 return coefs.dot(vals);
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 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::MatrixXd getCoefs(int Size, bool isInverse=false)
Transformation matrix between Cheb nodes and coefficients of the Cheb polynomials Notice,...
double evalPol(const Eigen::VectorXd &polCoef, double x)
Evaluate Cheb.
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::VectorXd getNodes(int Size)
Get the vector of positions of the Chebyshev nodes The nodes are by definition between 0 and 1,...
Abstract base class for different kinds of events.