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