37 template <
typename number>
40 , in_lagrange_product_form(false)
46 template <
typename number>
48 : coefficients(n + 1, 0.)
49 , in_lagrange_product_form(false)
55 template <
typename number>
58 : in_lagrange_product_form(true)
64 number tmp_lagrange_weight = 1.;
65 for (
unsigned int i = 0; i < supp.size(); ++i)
69 tmp_lagrange_weight *= supp[
center][0] - supp[i][0];
73 Assert(std::fabs(tmp_lagrange_weight) > std::numeric_limits<number>::min(),
74 ExcMessage(
"Underflow in computation of Lagrange denominator."));
75 Assert(std::fabs(tmp_lagrange_weight) < std::numeric_limits<number>::max(),
76 ExcMessage(
"Overflow in computation of Lagrange denominator."));
83 template <
typename number>
89 value(x, values.size() - 1, values.data());
94 template <
typename number>
103 coefficients.resize(lagrange_support_points.size() + 1);
104 if (lagrange_support_points.empty())
105 coefficients[0] = 1.;
108 coefficients[0] = -lagrange_support_points[0];
109 coefficients[1] = 1.;
110 for (
unsigned int i = 1; i < lagrange_support_points.size(); ++i)
112 coefficients[i + 1] = 1.;
113 for (
unsigned int j = i; j > 0; --j)
114 coefficients[j] = (-lagrange_support_points[i] * coefficients[j] +
115 coefficients[j - 1]);
116 coefficients[0] *= -lagrange_support_points[i];
119 for (
unsigned int i = 0; i < lagrange_support_points.size() + 1; ++i)
120 coefficients[i] *= lagrange_weight;
123 std::vector<number> new_points;
124 lagrange_support_points.swap(new_points);
125 in_lagrange_product_form =
false;
126 lagrange_weight = 1.;
131 template <
typename number>
137 for (
typename std::vector<number>::iterator c = coefficients.begin();
138 c != coefficients.end();
148 template <
typename number>
155 if (in_lagrange_product_form ==
true)
157 number inv_fact = number(1.) / factor;
158 number accumulated_fact = 1.;
159 for (
unsigned int i = 0; i < lagrange_support_points.size(); ++i)
161 lagrange_support_points[i] *= inv_fact;
162 accumulated_fact *= factor;
164 lagrange_weight *= accumulated_fact;
168 scale(coefficients, factor);
173 template <
typename number>
178 for (
typename std::vector<number>::iterator c = coefficients.begin();
179 c != coefficients.end();
186 template <
typename number>
190 if (in_lagrange_product_form ==
true)
191 lagrange_weight *= s;
194 for (
typename std::vector<number>::iterator c = coefficients.begin();
195 c != coefficients.end();
204 template <
typename number>
213 lagrange_support_points.insert(lagrange_support_points.end(),
219 else if (in_lagrange_product_form ==
true)
220 transform_into_standard_form();
225 std::unique_ptr<Polynomial<number>> q_data;
229 q_data = std::make_unique<Polynomial<number>>(p);
230 q_data->transform_into_standard_form();
237 unsigned int new_degree = this->degree() + q->
degree();
239 std::vector<number> new_coefficients(new_degree + 1, 0.);
241 for (
unsigned int i = 0; i < q->
coefficients.size(); ++i)
242 for (
unsigned int j = 0; j < this->coefficients.size(); ++j)
243 new_coefficients[i + j] += this->coefficients[j] * q->
coefficients[i];
244 this->coefficients = new_coefficients;
251 template <
typename number>
263 if (in_lagrange_product_form ==
true)
264 transform_into_standard_form();
269 std::unique_ptr<Polynomial<number>> q_data;
273 q_data = std::make_unique<Polynomial<number>>(p);
274 q_data->transform_into_standard_form();
285 for (
unsigned int i = 0; i < q->
coefficients.size(); ++i)
293 template <
typename number>
299 if (in_lagrange_product_form ==
true)
300 transform_into_standard_form();
305 std::unique_ptr<Polynomial<number>> q_data;
309 q_data = std::make_unique<Polynomial<number>>(p);
310 q_data->transform_into_standard_form();
321 for (
unsigned int i = 0; i < q->
coefficients.size(); ++i)
329 template <
typename number>
341 else if (in_lagrange_product_form ==
true)
359 template <
typename number>
360 template <
typename number2>
363 const number2 offset)
371 std::vector<number2> new_coefficients(coefficients.begin(),
377 for (
unsigned int d = 1; d < new_coefficients.size(); ++d)
379 const unsigned int n = d;
384 unsigned int binomial_coefficient = 1;
389 number2 offset_power = offset;
396 for (
unsigned int k = 0; k < d; ++k)
402 binomial_coefficient = (binomial_coefficient * (n - k)) / (k + 1);
404 new_coefficients[d - k - 1] +=
405 new_coefficients[d] * binomial_coefficient * offset_power;
406 offset_power *= offset;
416 coefficients.assign(new_coefficients.begin(), new_coefficients.end());
421 template <
typename number>
422 template <
typename number2>
429 if (in_lagrange_product_form ==
true)
431 for (
unsigned int i = 0; i < lagrange_support_points.size(); ++i)
432 lagrange_support_points[i] -= offset;
436 shift(coefficients, offset);
441 template <
typename number>
450 std::unique_ptr<Polynomial<number>> q_data;
452 if (in_lagrange_product_form ==
true)
454 q_data = std::make_unique<Polynomial<number>>(*this);
455 q_data->transform_into_standard_form();
461 std::vector<number> newcoefficients(q->
coefficients.size() - 1);
462 for (
unsigned int i = 1; i < q->
coefficients.size(); ++i)
463 newcoefficients[i - 1] = number(i) * q->
coefficients[i];
470 template <
typename number>
476 std::unique_ptr<Polynomial<number>> q_data;
478 if (in_lagrange_product_form ==
true)
480 q_data = std::make_unique<Polynomial<number>>(*this);
481 q_data->transform_into_standard_form();
487 std::vector<number> newcoefficients(q->
coefficients.size() + 1);
488 newcoefficients[0] = 0.;
489 for (
unsigned int i = 0; i < q->
coefficients.size(); ++i)
490 newcoefficients[i + 1] = q->
coefficients[i] / number(i + 1.);
497 template <
typename number>
501 if (in_lagrange_product_form ==
true)
503 out << lagrange_weight;
504 for (
unsigned int i = 0; i < lagrange_support_points.size(); ++i)
505 out <<
" (x-" << lagrange_support_points[i] <<
")";
509 for (
int i = degree(); i >= 0; --i)
511 out << coefficients[i] <<
" x^" << i << std::endl;
516 template <
typename number>
530 template <
typename number>
534 std::vector<number> result(n + 1, 0.);
535 result[n] = coefficient;
541 template <
typename number>
543 :
Polynomial<number>(make_vector(n, coefficient))
548 template <
typename number>
549 std::vector<Polynomial<number>>
552 std::vector<Polynomial<number>> v;
553 v.reserve(degree + 1);
554 for (
unsigned int i = 0; i <= degree; ++i)
565 namespace LagrangeEquidistantImplementation
567 std::vector<Point<1>>
570 std::vector<Point<1>> points(n + 1);
571 const double one_over_n = 1. / n;
572 for (
unsigned int k = 0; k <= n; ++k)
573 points[k][0] =
static_cast<double>(k) * one_over_n;
582 const unsigned int support_point)
584 generate_equidistant_unit_points(n),
595 std::vector<double> new_support_points;
606 const unsigned int support_point,
607 std::vector<double> &a)
611 unsigned int n_functions = n + 1;
613 const double *x =
nullptr;
619 static const double x1[4] = {1.0, -1.0, 0.0, 1.0};
625 static const double x2[9] = {
626 1.0, -3.0, 2.0, 0.0, 4.0, -4.0, 0.0, -1.0, 2.0};
632 static const double x3[16] = {1.0,
656 for (
unsigned int i = 0; i < n_functions; ++i)
657 a[i] = x[support_point * n_functions + i];
662 std::vector<Polynomial<double>>
667 return std::vector<Polynomial<double>>(
673 std::vector<Polynomial<double>> v;
674 for (
unsigned int i = 0; i <=
degree; ++i)
685 std::vector<Polynomial<double>>
688 std::vector<Polynomial<double>> p;
689 p.reserve(points.size());
691 for (
unsigned int i = 0; i < points.size(); ++i)
692 p.emplace_back(points, i);
714 for (
unsigned int i = 0; i < k; ++i)
721 for (
unsigned int i = 0; i < k; ++i)
728 std::vector<Polynomial<double>>
731 std::vector<Polynomial<double>> v;
733 for (
unsigned int i = 0; i <=
degree; ++i)
783 std::vector<double> legendre_coefficients_tmp1(p);
784 std::vector<double> legendre_coefficients_tmp2(p - 1);
788 legendre_coefficients_tmp1[0] = 1.0;
790 for (
unsigned int i = 2; i < p; ++i)
792 for (
unsigned int j = 0; j < i - 1; ++j)
793 legendre_coefficients_tmp2[j] = legendre_coefficients_tmp1[j];
795 for (
unsigned int j = 0; j < i; ++j)
800 ((1.0 - 2 * i) * legendre_coefficients_tmp1[0] /
802 (1.0 - i) * legendre_coefficients_tmp2[0] /
806 for (
unsigned int j = 1; j < i - 1; ++j)
810 (2.0 * legendre_coefficients_tmp1[j - 1] -
811 legendre_coefficients_tmp1[j]) +
812 (1.0 - i) * legendre_coefficients_tmp2[j] /
817 (2.0 * legendre_coefficients_tmp1[i - 2] -
818 legendre_coefficients_tmp1[i - 1]) /
821 legendre_coefficients_tmp1[i - 1] / i;
824 for (
int i = p; i > 0; --i)
833 std::vector<Polynomial<double>>
836 std::vector<Polynomial<double>> basis(p + 1);
838 for (
unsigned int i = 0; i <= p; ++i)
850 std::vector<std::unique_ptr<const std::vector<double>>>
924 std::vector<double> c0(2);
928 std::vector<double> c1(2);
935 std::make_unique<const std::vector<double>>(std::move(c0));
937 std::make_unique<const std::vector<double>>(std::move(c1));
945 std::vector<double> c2(3);
954 std::make_unique<const std::vector<double>>(std::move(c2));
970 std::vector<double> ck(k + 1);
976 for (
unsigned int i = 1; i <= k - 1; ++i)
998 std::make_unique<const std::vector<double>>(std::move(ck));
1004 const std::vector<double> &
1019 std::vector<Polynomial<double>>
1032 return std::vector<Polynomial<double>>(
1036 std::vector<Polynomial<double>> v;
1038 for (
unsigned int i = 0; i <=
degree; ++i)
1093 (*this) *= legendre;
1099 std::vector<Polynomial<double>>
1104 "degrees less than three"));
1105 std::vector<Polynomial<double>> basis(n + 1);
1107 for (
unsigned int i = 0; i <= n; ++i)
1123 find_support_point_x_star(
const std::vector<double> &jacobi_roots)
1129 double guess_left = 0;
1130 double guess_right = 0.5;
1131 const unsigned int degree = jacobi_roots.size() + 3;
1143 double integral_left = 0, integral_right = 0;
1144 for (
unsigned int q = 0; q < gauss.size(); ++q)
1146 const double x = gauss.point(q)[0];
1147 double poly_val_common = x;
1148 for (
unsigned int j = 0; j < degree - 3; ++j)
1152 gauss.weight(q) * (poly_val_common * (x - guess_left));
1154 gauss.weight(q) * (poly_val_common * (x - guess_right));
1159 return guess_right - (guess_right - guess_left) /
1160 (integral_right - integral_left) * integral_right;
1167 const unsigned int index)
1179 else if (degree == 1)
1192 else if (degree == 2)
1200 else if (index == 1)
1213 else if (degree == 3)
1230 else if (index == 1)
1240 else if (index == 2)
1247 else if (index == 3)
1278 std::vector<double> jacobi_roots =
1288 const double auxiliary_zero =
1289 find_support_point_x_star(jacobi_roots);
1291 for (
unsigned int m = 0; m <
degree - 3; ++m)
1299 else if (index == 1)
1302 for (
unsigned int m = 0; m <
degree - 3; ++m)
1315 std::vector<Point<1>> points(
degree);
1317 for (
unsigned int i = 0; i <
degree; ++i)
1324 std::vector<double> value_and_grad(2);
1325 helper.
value(0., value_and_grad);
1329 const double auxiliary_zero =
1330 find_support_point_x_star(jacobi_roots);
1332 (1. / auxiliary_zero - value_and_grad[1] / value_and_grad[0]) /
1335 else if (index >= 2 && index <
degree - 1)
1339 for (
unsigned int m = 0, c = 2; m <
degree - 3; ++m)
1349 else if (index ==
degree - 1)
1353 for (
unsigned int m = 0; m <
degree - 3; ++m)
1357 std::vector<Point<1>> points(
degree);
1359 for (
unsigned int i = 0; i <
degree; ++i)
1366 std::vector<double> value_and_grad(2);
1367 helper.
value(1., value_and_grad);
1371 const double auxiliary_zero =
1372 find_support_point_x_star(jacobi_roots);
1374 (-1. / auxiliary_zero - value_and_grad[1] / value_and_grad[0]) /
1377 else if (index ==
degree)
1379 const double auxiliary_zero =
1380 find_support_point_x_star(jacobi_roots);
1383 for (
unsigned int m = 0; m <
degree - 3; ++m)
1395 std::vector<Polynomial<double>>
1398 std::vector<Polynomial<double>> basis(
degree + 1);
1400 for (
unsigned int i = 0; i <=
degree; ++i)
1413 template class Polynomial<float>;
1414 template class Polynomial<double>;
1415 template class Polynomial<long double>;
1430 template class Monomial<float>;
1431 template class Monomial<double>;
1432 template class Monomial<long double>;
HermiteInterpolation(const unsigned int p)
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int p)
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int degree)
HermiteLikeInterpolation(const unsigned int degree, const unsigned int index)
static std::vector< std::unique_ptr< const std::vector< double > > > recursive_coefficients
Hierarchical(const unsigned int p)
static void compute_coefficients(const unsigned int p)
static const std::vector< double > & get_coefficients(const unsigned int p)
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int degree)
static std::shared_mutex coefficients_lock
static void compute_coefficients(const unsigned int n, const unsigned int support_point, std::vector< double > &a)
LagrangeEquidistant(const unsigned int n, const unsigned int support_point)
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int degree)
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int degree)
Legendre(const unsigned int p)
std::vector< double > compute_coefficients(const unsigned int p)
Lobatto(const unsigned int p=0)
static std::vector< Polynomial< double > > generate_complete_basis(const unsigned int p)
static std::vector< Polynomial< number > > generate_complete_basis(const unsigned int degree)
Monomial(const unsigned int n, const double coefficient=1.)
static std::vector< number > make_vector(unsigned int n, const double coefficient)
number value(const number x) const
bool operator==(const Polynomial< number > &p) const
std::vector< number > coefficients
Polynomial< number > primitive() const
Polynomial< number > & operator+=(const Polynomial< number > &p)
Polynomial< number > derivative() const
void transform_into_standard_form()
void scale(const number factor)
Polynomial< number > & operator-=(const Polynomial< number > &p)
std::vector< number > lagrange_support_points
void shift(const number2 offset)
void print(std::ostream &out) const
bool in_lagrange_product_form
static void multiply(std::vector< number > &coefficients, const number factor)
Polynomial< number > & operator*=(const double s)
virtual std::size_t memory_consumption() const
unsigned int degree() const
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcZero()
static ::ExceptionBase & ExcNotImplemented()
static ::ExceptionBase & ExcEmptyObject()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define DEAL_II_ASSERT_UNREACHABLE()
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
std::vector< Point< 1 > > generate_equidistant_unit_points(const unsigned int n)
std::vector< Polynomial< double > > generate_complete_Lagrange_basis(const std::vector< Point< 1 > > &points)
std::vector< Number > jacobi_polynomial_roots(const unsigned int degree, const int alpha, const int beta)
constexpr T fixed_power(const T t)
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)