Developer documentation

binomials.hpp
Go to the documentation of this file.
1 #ifndef realroot_SOLVE_SBDSLV_BINOMIALS_H
2 #define realroot_SOLVE_SBDSLV_BINOMIALS_H
3 
4 #include <vector>
5 #include <assert.h>
6 //--------------------------------------------------------------------
7 namespace mmx {
8 
9 #ifndef WITH_POW
10 #define WITH_POW
11  template<class T>
12  T pow(const T& a, int i)
13  {
14  assert(i>=0);
15  if(i == 1) return a;
16  if(i > 0)
17  {
18  if (a == T(1)) return a;
19  if (a == T(-1)) return (i % 2 == 0) ? T(1) : T(-1);
20  T y(a);
21  T z(1);
22  unsigned int n = i;
23  unsigned int m;
24  while (n > 0) {
25  m = n / 2;
26  if (n %2 )
27  {
28  z *= y;
29  }
30  y = y * y;
31  n = m;
32  }
33  return z;
34  }
35  return T(1);
36  }
37 #endif //WITH_POW
38 //----------------------------------------------------------------------
40 template <typename T> T factorial(int n)
41 {
42  T r=1;
43  for (int i=2;i<=n;i++) r*=n;
44  return(r);
45 }
46 
47 
48 #ifndef WITH_BINOMIAL
49 #define WITH_BINOMIAL
50 
52 template <typename T> T binomial(int n, int p)
53 {
54  T n1=1;
55 
56  if(p!=0)
57  {
58  if((n-p)<p) p=n-p;
59  int n2=n;
60  for(int i=1;i<=p;i++,n2--) n1=n1*n2/T(i);
61  }
62 
63  return(n1);
64 }
65 #endif //WITH_BINOMIAL
66 //----------------------------------------------------------------------
67 template<typename C>
68 struct binomials {
70  std::vector<int> offs;
71  std::vector<C> data;
72  binomials(const binomials& b):offs(b.offs),data(b.data) {}
73  binomials();
74  // return the list [ binomial(d,i) ], for i = 0..d
75  const C* operator[]( int d );
76  const C& operator()( int i, int j ) { return (*this)[i][j]; };
77 };
78 
79 // IMPLEMENTATION
80 
81 template<class C>
82 binomials<C>::binomials() : offs(2), data(3)
83 {
84  offs[0] = 0; data[0] = 1;
85  offs[1] = 1; data[offs[1]] = 1; data[offs[1]+1] = 1;
86 };
87 
88 template<class C> binomials<C> binomials<C>::default_;
89 
90 template<class C>
91 const C* binomials<C>::operator[]( int d )
92 {
93  int s,j,x;
94  if ( (s=offs.size()) > d ) return &*(data.begin())+offs[d];
95  for ( x = 0, j = s; j <= d; x += j+1, j ++ ) {}
96  offs.resize(d+1);
97  data.resize(data.size()+x);
98  for ( j = s; j <= d; j ++ )
99  {
100  offs[j] = offs[j-1] + j;
101  for ( int i = 0; i < j-1; i ++ )
102  data[offs[j]+i+1] = data[offs[j-1]+i] + data[offs[j-1]+i+1];
103  data[offs[j]] = data[offs[j]+j] = 1;
104  };
105  return &*(data.begin())+offs[d];
106 };
107 
108 } //namespace mmx
109 
110 #endif
std::vector< C > data
Definition: binomials.hpp:71
const C & operator()(int i, int j)
Definition: binomials.hpp:76
T pow(const T &a, int i)
Definition: binomials.hpp:12
const C & b
Definition: Interval_glue.hpp:25
const C * operator[](int d)
Definition: binomials.hpp:91
binomials()
Definition: binomials.hpp:82
Definition: binomials.hpp:68
T factorial(int n)
Definition: binomials.hpp:40
double C
Definition: solver_mv_fatarcs.cpp:16
binomials(const binomials &b)
Definition: binomials.hpp:72
static binomials default_
Definition: binomials.hpp:69
Definition: array.hpp:12
std::vector< int > offs
Definition: binomials.hpp:70
T binomial(int n, int p)
Definition: binomials.hpp:52
#define assert(expr, msg)
Definition: shared_object.hpp:57
Home