7#ifndef DUNE_FUNCTIONS_ANALYTICFUNCTIONS_POLYNOMIAL_HH
8#define DUNE_FUNCTIONS_ANALYTICFUNCTIONS_POLYNOMIAL_HH
11#include <initializer_list>
15#include <dune/common/hybridutilities.hh>
24 template<
class K,
class Allocator>
25 auto polynomialDerivativeCoefficients(
const std::vector<K, Allocator>& coefficients) {
26 if (coefficients.size()==0)
27 return std::vector<K, Allocator>();
28 std::vector<K, Allocator> dpCoefficients(coefficients.size()-1);
29 for (
size_t i=1; i<coefficients.size(); ++i)
30 dpCoefficients[i-1] = coefficients[i]*K(i);
31 return dpCoefficients;
36 template<
class K,
unsigned long n>
37 auto polynomialDerivativeCoefficients(
const std::array<K, n>& coefficients) {
42 std::array<K, n-1> dpCoefficients;
43 for (
size_t i=1; i<coefficients.size(); ++i)
44 dpCoefficients[i-1] = coefficients[i]*K(i);
45 return dpCoefficients;
54 template<
class I, I i0, I... i,
class J, J j0, J... j>
55 auto polynomialDerivativeCoefficientsHelper(std::integer_sequence<I, i0, i...>, std::integer_sequence<J, j0, j...>) {
56 return std::integer_sequence<I, i*I(j)...>();
61 template<
class I, I... i>
62 auto polynomialDerivativeCoefficients(std::integer_sequence<I, i...> coefficients) {
63 if constexpr (
sizeof...(i)==0)
66 return polynomialDerivativeCoefficientsHelper(coefficients, std::make_index_sequence<
sizeof...(i)>());
72 auto polynomialDerivativeCoefficients(
const std::tuple<T...>& coefficients) {
73 if constexpr (
sizeof...(T)==0)
83 auto mult = Dune::Hybrid::hybridFunctor(std::multiplies());
84 return Dune::unpackIntegerSequence([&](
auto... i) {
85 return std::tuple(mult(std::get<i+1>(coefficients), std::integral_constant<long signed int, i+1>()) ...);
86 }, std::make_index_sequence<
sizeof...(T)-1>());
120template<
class K,
class C=std::vector<K>>
124 struct IsIntegerSequence :
public std::false_type {};
126 template<
class I, I... i>
127 struct IsIntegerSequence<std::integer_sequence<I, i...>> :
public std::true_type {};
153 auto n = Dune::Hybrid::size(coefficients_);
154 Dune::Hybrid::forEach(Dune::range(n), [&](
auto i) {
155 y += Dune::Hybrid::elementAt(coefficients_, i) * std::pow(x,
int(i));
163 if constexpr (IsIntegerSequence<Coefficients>::value)
180 auto derivativeCoefficients = Impl::polynomialDerivativeCoefficients(p.
coefficients());
181 using DerivativeCoefficients =
decltype(derivativeCoefficients);
188 return coefficients_;
198Polynomial(std::vector<K>) -> Polynomial<K, std::vector<K>>;
200template<
class K,
unsigned long n>
201Polynomial(std::array<K,n>) -> Polynomial<K, std::array<K,n>>;
203template<
class K, K... ci>
204Polynomial(std::integer_sequence<K, ci...>) -> Polynomial<K, std::integer_sequence<K,ci...>>;
207Polynomial(std::initializer_list<K>) -> Polynomial<K, std::vector<K>>;
223template<
class K,
class Coefficients>
224auto makePolynomial(Coefficients coefficients)
226 return Polynomial<K, Coefficients>(std::move(coefficients));
238template<
class K,
class C>
239auto makePolynomial(std::initializer_list<C> coefficients)
241 return Polynomial<K>(std::move(coefficients));
A univariate polynomial implementation.
Definition: polynomial.hh:122
Polynomial()=default
Default constructor.
K operator()(const K &x) const
Evaluate polynomial.
Definition: polynomial.hh:150
C Coefficients
The type of the stored coefficient container.
Definition: polynomial.hh:132
const Coefficients & coefficients() const
Obtain reference to coefficient vector.
Definition: polynomial.hh:186
Polynomial(Coefficients coefficients)
Create from container of coefficients.
Definition: polynomial.hh:145
bool operator==(const Polynomial &other) const
Comparison of coefficients.
Definition: polynomial.hh:161
friend auto derivative(const Polynomial &p)
Obtain derivative of Polynomial function.
Definition: polynomial.hh:178
Definition: polynomial.hh:17