File: polynomial.hh

package info (click to toggle)
dune-functions 2.6~20180228-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,188 kB
  • sloc: cpp: 8,599; makefile: 3
file content (109 lines) | stat: -rw-r--r-- 2,603 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_FUNCTIONS_ANALYTICFUNCTIONS_POLYNOMIAL_HH
#define DUNE_FUNCTIONS_ANALYTICFUNCTIONS_POLYNOMIAL_HH


namespace Dune {
namespace Functions {


/**
 * \brief A scalar polynomial implementation
 *
 * \ingroup FunctionImplementations
 *
 * \tparam K Scalar type. The polynomial will map K to K
 *
 * This class exists mainly to demonstrate how to implement
 * the \ref Concept::DifferentiableFunction<Range(Domain), DerivativeTraits> concept.
 */
template<class K>
class Polynomial
{
public:

  //! Default constructor
  Polynomial() = default;

  //! Copy constructor
  Polynomial(const Polynomial& other) = default;

  //! Move constructor
  Polynomial(Polynomial&& other) = default;

  /**
   * \brief Create from list of coefficients
   *
   * Coefficients are ordered in accordance with
   * the corresponding monomial order
   */
  Polynomial(std::initializer_list<double> coefficients) :
    coefficients_(coefficients)
  {}

  /**
   * \brief Create from list of coefficients
   *
   * Coefficients are ordered in accordance with
   * the corresponding monomial order. This will
   * move the coefficients from givven vector.
   */
  Polynomial(std::vector<K>&& coefficients) :
      coefficients_(std::move(coefficients))
  {}

  /**
   * \brief Create from list of coefficients
   *
   * Coefficients are ordered in accordance with
   * the corresponding monomial order. This will
   * copy coefficients from given vector.
   */
  Polynomial(const std::vector<K>& coefficients) :
    coefficients_(coefficients)
  {}

  //! Evaluate polynomial
  K operator() (const K& x) const
  {
    auto y = K(0);
    for (size_t i=0; i<coefficients_.size(); ++i)
      y += coefficients_[i] * std::pow(x, i);
    return y;
  }

  /**
   * \brief Obtain derivative of Polynomial function
   *
   * \ingroup FunctionImplementations
   *
   * The derivative contains its own coefficient
   * list and is not updated if the original function
   * is changed.
   */
  friend Polynomial derivative(const Polynomial& p)
  {
    std::vector<K> dpCoefficients(p.coefficients().size()-1);
    for (size_t i=1; i<p.coefficients_.size(); ++i)
      dpCoefficients[i-1] = p.coefficients()[i]*i;
    return Polynomial(std::move(dpCoefficients));
  }

  //! Obtain reference to coefficient vector
  const std::vector<K>& coefficients() const
  {
    return coefficients_;
  }

private:
  std::vector<K> coefficients_;
};



}} // namespace Dune::Functions



#endif // DUNE_FUNCTIONS_ANALYTICFUNCTIONS_POLYNOMIAL_HH