13#ifndef COMBINATORICS_HH
14#define COMBINATORICS_HH
41 for (
int i=2; i<=k; ++i)
64 size_t n = std::accumulate(begin(ks),end(ks),0);
68 for (
size_t i=1; i<=k; ++i)
92 constexpr int numberOfMultiindices(
int d,
int m)
100 return (m+2)*(m+1)/2;
102 for (
int i=0; i<=m; ++i)
103 n += numberOfMultiindices(d-1,i);
125 std::array<size_t,d> multiIndex(
size_t m,
size_t n)
127 static_assert(d>=1,
"multiindices only defined for d>0");
128 assert(n < numberOfMultiindices(d,m));
130 std::array<size_t,d> ks;
140 while (n >= l+numberOfMultiindices(d-1,m-i))
142 l += numberOfMultiindices(d-1,m-i);
147 auto hs = multiIndex<d-1>(m-i,n-l);
148 std::copy(begin(hs),end(hs),begin(ks));
167 void next_multiindex(It first, It last,
int const m)
170 if (std::accumulate(first,last,0)==m)
173 first = std::find_if(first,last,[](
int k) {
return k!=0; });
size_t multinomial(std::array< size_t, m > const &ks)
Computes the multinomial coefficient , where .
constexpr int binomial(int n, int k)
Computes the binomial coefficient .