15#ifndef dealii_polynomial_h
16#define dealii_polynomial_h
29#include <shared_mutex>
64 template <
typename number>
91 const unsigned int evaluation_point);
121 value(
const number x, std::vector<number> &values)
const;
141 template <
typename Number2>
144 const unsigned int n_derivatives,
145 Number2 *values)
const;
159 template <std::
size_t n_entries,
typename Number2>
165 const unsigned int n_derivatives,
166 std::array<Number2, n_entries> *values)
const;
184 scale(
const number factor);
201 template <
typename number2>
203 shift(
const number2 offset);
252 print(std::ostream &out)
const;
259 template <
class Archive>
279 template <
typename number2>
331 template <
typename number>
339 Monomial(
const unsigned int n,
const double coefficient = 1.);
347 static std::vector<Polynomial<number>>
354 static std::vector<number>
355 make_vector(
unsigned int n,
const double coefficient);
392 static std::vector<Polynomial<double>>
402 const unsigned int support_point,
403 std::vector<double> &a);
414 std::vector<Polynomial<double>>
446 static std::vector<Polynomial<double>>
476 Lobatto(
const unsigned int p = 0);
482 static std::vector<Polynomial<double>>
551 static std::vector<Polynomial<double>>
565 static const std::vector<double> &
576 static std::vector<std::unique_ptr<const std::vector<double>>>
629 static std::vector<Polynomial<double>>
744 const unsigned int index);
750 static std::vector<Polynomial<double>>
765 template <
typename Number>
785 template <
typename Number>
799 template <
typename number>
801 : in_lagrange_product_form(false)
802 , lagrange_weight(1.)
807 template <
typename number>
811 if (in_lagrange_product_form ==
true)
813 return lagrange_support_points.size();
818 return coefficients.size() - 1;
824 template <
typename number>
828 if (in_lagrange_product_form ==
false)
833 const unsigned int m = coefficients.size();
834 number value = coefficients.back();
835 for (
int k = m - 2; k >= 0; --k)
836 value = value * x + coefficients[k];
842 const unsigned int m = lagrange_support_points.size();
844 for (
unsigned int j = 0; j < m; ++j)
845 value *= x - lagrange_support_points[j];
846 value *= lagrange_weight;
853 template <
typename number>
854 template <
typename Number2>
857 const unsigned int n_derivatives,
858 Number2 *values)
const
860 values_of_array(std::array<Number2, 1ul>{{x}},
862 reinterpret_cast<std::array<Number2, 1ul> *
>(values));
867 template <
typename number>
868 template <std::
size_t n_entries,
typename Number2>
875 const std::array<Number2, n_entries> &x,
876 const unsigned int n_derivatives,
877 std::array<Number2, n_entries> *values)
const
880 if (in_lagrange_product_form ==
true)
885 const unsigned int n_supp = lagrange_support_points.size();
886 const number weight = lagrange_weight;
887 switch (n_derivatives)
890 for (
unsigned int e = 0; e < n_entries; ++e)
891 values[0][e] = weight;
892 for (
unsigned int k = 1; k <= n_derivatives; ++k)
893 for (
unsigned int e = 0; e < n_entries; ++e)
895 for (
unsigned int i = 0; i < n_supp; ++i)
897 std::array<Number2, n_entries> v = x;
898 for (
unsigned int e = 0; e < n_entries; ++e)
899 v[e] -= lagrange_support_points[i];
907 for (
unsigned int k = n_derivatives; k > 0; --k)
908 for (
unsigned int e = 0; e < n_entries; ++e)
909 values[k][e] = (values[k][e] * v[e] + values[k - 1][e]);
910 for (
unsigned int e = 0; e < n_entries; ++e)
911 values[0][e] *= v[e];
916 number k_factorial = 2;
917 for (
unsigned int k = 2; k <= n_derivatives; ++k)
919 for (
unsigned int e = 0; e < n_entries; ++e)
920 values[k][e] *= k_factorial;
921 k_factorial *=
static_cast<number
>(k + 1);
933 std::array<Number2, n_entries> value;
934 for (
unsigned int e = 0; e < n_entries; ++e)
936 for (
unsigned int i = 0; i < n_supp; ++i)
937 for (
unsigned int e = 0; e < n_entries; ++e)
938 value[e] *= (x[e] - lagrange_support_points[i]);
940 for (
unsigned int e = 0; e < n_entries; ++e)
941 values[0][e] = value[e];
947 std::array<Number2, n_entries> value, derivative = {};
948 for (
unsigned int e = 0; e < n_entries; ++e)
950 for (
unsigned int i = 0; i < n_supp; ++i)
951 for (
unsigned int e = 0; e < n_entries; ++e)
953 const Number2 v = x[e] - lagrange_support_points[i];
954 derivative[e] = derivative[e] * v + value[e];
958 for (
unsigned int e = 0; e < n_entries; ++e)
960 values[0][e] = value[e];
961 values[1][e] = derivative[e];
968 std::array<Number2, n_entries> value, derivative = {},
970 for (
unsigned int e = 0; e < n_entries; ++e)
972 for (
unsigned int i = 0; i < n_supp; ++i)
973 for (
unsigned int e = 0; e < n_entries; ++e)
975 const Number2 v = x[e] - lagrange_support_points[i];
977 derivative[e] = derivative[e] * v + value[e];
981 for (
unsigned int e = 0; e < n_entries; ++e)
983 values[0][e] = value[e];
984 values[1][e] = derivative[e];
985 values[2][e] =
static_cast<number
>(2) *
second[e];
997 const unsigned int m = coefficients.size();
998 std::vector<std::array<Number2, n_entries>> a(coefficients.size());
999 for (
unsigned int i = 0; i < coefficients.size(); ++i)
1000 for (
unsigned int e = 0; e < n_entries; ++e)
1001 a[i][e] = coefficients[i];
1003 unsigned int j_factorial = 1;
1008 const unsigned int min_valuessize_m =
std::min(n_derivatives + 1, m);
1009 for (
unsigned int j = 0; j < min_valuessize_m; ++j)
1011 for (
int k = m - 2; k >=
static_cast<int>(j); --k)
1012 for (
unsigned int e = 0; e < n_entries; ++e)
1013 a[k][e] += x[e] * a[k + 1][e];
1014 for (
unsigned int e = 0; e < n_entries; ++e)
1015 values[j][e] =
static_cast<number
>(j_factorial) * a[j][e];
1017 j_factorial *= j + 1;
1021 for (
unsigned int j = min_valuessize_m; j <= n_derivatives; ++j)
1022 for (
unsigned int e = 0; e < n_entries; ++e)
1028 template <
typename number>
1029 template <
class Archive>
1036 ar &in_lagrange_product_form;
1037 ar &lagrange_support_points;
1038 ar &lagrange_weight;
1043 template <
typename Number>
1050 Assert(alpha >= 0 && beta >= 0,
1057 const Number xeval = Number(-1) + 2. * x;
1063 p1 = ((alpha + beta + 2) * xeval + (alpha - beta)) / 2;
1067 for (
unsigned int i = 1; i < degree; ++i)
1069 const Number v = 2 * i + (alpha + beta);
1070 const Number a1 = 2 * (i + 1) * (i + (alpha + beta + 1)) * v;
1071 const Number a2 = (v + 1) * (alpha * alpha - beta * beta);
1072 const Number a3 = v * (v + 1) * (v + 2);
1073 const Number a4 = 2 * (i + alpha) * (i + beta) * (v + 2);
1075 const Number pn = ((a2 + a3 * xeval) * p1 - a4 * p0) / a1;
1084 template <
typename Number>
1090 std::vector<Number> x(degree, 0.5);
1101 const Number tolerance =
1102 4 *
std::max(
static_cast<Number
>(std::numeric_limits<double>::epsilon()),
1103 std::numeric_limits<Number>::epsilon());
1111 const unsigned int n_points = (alpha == beta ? degree / 2 : degree);
1112 for (
unsigned int k = 0; k < n_points; ++k)
1116 Number r = 0.5 - 0.5 *
std::cos(
static_cast<Number
>(2 * k + 1) /
1119 r = (r + x[k - 1]) / 2;
1122 for (
unsigned int it = 1; it < 1000; ++it)
1125 for (
unsigned int i = 0; i < k; ++i)
1126 s += 1. / (r - x[i]);
1130 (alpha + beta + degree + 1) *
1135 const Number delta = f / (f * s - J_x);
1143 if (it == converged + 1)
1148 ExcMessage(
"Newton iteration for zero of Jacobi polynomial "
1149 "did not converge."));
1155 for (
unsigned int k = n_points; k < degree; ++k)
1156 x[k] = 1.0 - x[degree - k - 1];
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)
void values_of_array(const std::array< Number2, n_entries > &points, const unsigned int n_derivatives, std::array< Number2, n_entries > *values) const
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
void serialize(Archive &ar, const unsigned int version)
static void multiply(std::vector< number > &coefficients, const number factor)
void value(const Number2 x, const unsigned int n_derivatives, Number2 *values) const
Polynomial< number > & operator*=(const double s)
virtual std::size_t memory_consumption() const
unsigned int degree() const
#define DEAL_II_ALWAYS_INLINE
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcNotImplemented()
static ::ExceptionBase & ExcEmptyObject()
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
Number jacobi_polynomial_value(const unsigned int degree, const int alpha, const int beta, const Number x)
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)
static constexpr double PI
static const unsigned int invalid_unsigned_int
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > cos(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)