Dune Core Modules (2.8.0)

polynomial.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_FUNCTIONS_ANALYTICFUNCTIONS_POLYNOMIAL_HH
4#define DUNE_FUNCTIONS_ANALYTICFUNCTIONS_POLYNOMIAL_HH
5
6#include <cmath>
7#include <initializer_list>
8#include <vector>
9
10namespace Dune {
11namespace Functions {
12
13
24template<class K>
26{
27public:
28
30 Polynomial() = default;
31
33 Polynomial(const Polynomial& other) = default;
34
36 Polynomial(Polynomial&& other) = default;
37
39 Polynomial& operator=(const Polynomial& other) = default;
40
42 Polynomial& operator=(Polynomial&& other) = default;
43
50 Polynomial(std::initializer_list<K> coefficients) :
51 coefficients_(coefficients)
52 {}
53
61 Polynomial(std::vector<K>&& coefficients) :
62 coefficients_(std::move(coefficients))
63 {}
64
72 Polynomial(const std::vector<K>& coefficients) :
73 coefficients_(coefficients)
74 {}
75
77 K operator() (const K& x) const
78 {
79 auto y = K(0);
80 for (size_t i=0; i<coefficients_.size(); ++i)
81 y += coefficients_[i] * std::pow(x, i);
82 return y;
83 }
84
95 {
96 std::vector<K> dpCoefficients(p.coefficients().size()-1);
97 for (size_t i=1; i<p.coefficients_.size(); ++i)
98 dpCoefficients[i-1] = p.coefficients()[i]*i;
99 return Polynomial(std::move(dpCoefficients));
100 }
101
103 const std::vector<K>& coefficients() const
104 {
105 return coefficients_;
106 }
107
108private:
109 std::vector<K> coefficients_;
110};
111
112
113
114}} // namespace Dune::Functions
115
116
117
118#endif // DUNE_FUNCTIONS_ANALYTICFUNCTIONS_POLYNOMIAL_HH
A scalar polynomial implementation.
Definition: polynomial.hh:26
Polynomial()=default
Default constructor.
const std::vector< K > & coefficients() const
Obtain reference to coefficient vector.
Definition: polynomial.hh:103
Polynomial & operator=(const Polynomial &other)=default
Copy-assignment operator.
Polynomial & operator=(Polynomial &&other)=default
Move-assignment operator.
Polynomial(std::initializer_list< K > coefficients)
Create from list of coefficients.
Definition: polynomial.hh:50
K operator()(const K &x) const
Evaluate polynomial.
Definition: polynomial.hh:77
Polynomial(const Polynomial &other)=default
Copy constructor.
Polynomial(Polynomial &&other)=default
Move constructor.
Polynomial(const std::vector< K > &coefficients)
Create from list of coefficients.
Definition: polynomial.hh:72
Polynomial(std::vector< K > &&coefficients)
Create from list of coefficients.
Definition: polynomial.hh:61
friend Polynomial derivative(const Polynomial &p)
Obtain derivative of Polynomial function.
Definition: polynomial.hh:94
Dune namespace.
Definition: alignedallocator.hh:11
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 22, 23:33, 2024)