bernstein.h
Go to the documentation of this file.
1 
9 #ifndef _CLASS_BERNSTEIN
10 #define _CLASS_BERNSTEIN
11 
12 #include "curve_abc.h"
13 
14 #include "MathDefs.h"
15 
16 #include <math.h>
17 #include <vector>
18 #include <stdexcept>
19 
20 namespace curves {
26 inline unsigned int bin(const unsigned int n, const unsigned int k) {
27  if (k > n) throw std::runtime_error("binomial coefficient higher than degree");
28  if (k == 0) return 1;
29  if (k > n / 2) return bin(n, n - k);
30  return n * bin(n - 1, k - 1) / k;
31 }
32 
36 template <typename Numeric = double>
37 struct Bern {
38  Bern() {}
39  Bern(const unsigned int m, const unsigned int i) : m_minus_i(m - i), i_(i), bin_m_i_(bin(m, i)) {}
40 
41  ~Bern() {}
42 
46  Numeric operator()(const Numeric u) const {
47  if (!(u >= 0. && u <= 1.)) {
48  throw std::invalid_argument("u needs to be betwen 0 and 1.");
49  }
50  return bin_m_i_ * (pow(u, i_)) * pow((1 - u), m_minus_i);
51  }
52 
56  virtual bool operator==(const Bern& other) const {
57  return curves::isApprox<Numeric>(m_minus_i, other.m_minus_i) && curves::isApprox<Numeric>(i_, other.i_) &&
58  curves::isApprox<Numeric>(bin_m_i_, other.bin_m_i_);
59  }
60 
64  virtual bool operator!=(const Bern& other) const { return !(*this == other); }
65 
66  /* Attributes */
67  Numeric m_minus_i;
68  Numeric i_;
69  Numeric bin_m_i_;
70  /* Attributes */
71 
72  // Serialization of the class
73  friend class boost::serialization::access;
74  template <class Archive>
75  void serialize(Archive& ar, const unsigned int version) {
76  if (version) {
77  // Do something depending on version ?
78  }
79  ar& boost::serialization::make_nvp("m_minus_i", m_minus_i);
80  ar& boost::serialization::make_nvp("i", i_);
81  ar& boost::serialization::make_nvp("bin_m_i", bin_m_i_);
82  }
83 }; // End struct Bern
84 
87 template <typename Numeric>
88 std::vector<Bern<Numeric> > makeBernstein(const unsigned int n) {
89  std::vector<Bern<Numeric> > res;
90  for (unsigned int i = 0; i <= n; ++i) {
91  res.push_back(Bern<Numeric>(n, i));
92  }
93  return res;
94 }
95 } // namespace curves
96 
97 DEFINE_CLASS_TEMPLATE_VERSION(typename Numeric, curves::Bern<Numeric>)
98 
99 #endif //_CLASS_BERNSTEIN
double Numeric
Definition: effector_spline.h:26
interface for a Curve of arbitrary dimension.
Definition: bernstein.h:20
brief Computes all Bernstein polynomes for a certain degree std::vector< Bern< Numeric > > makeBernstein(const unsigned int n)
Definition: bernstein.h:88
Definition: fwd.h:49