I found a template to calculate the binomial coefficient, which I happily used for function generation. The advantage is that I use this template for compile time Bernstein polynomial generation instead of using the derived polynomials (just 5 very simple ones).
I initially thought the code would become easier by doing so because the generation of the five random functions now obvious. Unfortunately, the code below is hard to read for someone not used to templates. Is there a way to get rid of at least some of the template specializations?
// Template functions to estimate the binominal coefficient
template<uint8_t n, uint8_t k>
struct binomial {
static constexpr int value = (binomial<n - 1, k - 1>::value + binomial<n - 1, k>::value);
};
template<>
struct binomial<0, 0> {
static constexpr int value = 1;
};
template<uint8_t n>
struct binomial<n, 0> {
static constexpr int value = 1;
};
template<uint8_t n>
struct binomial<n, n> {
static constexpr int value = 1;
};
You might probably use constexpr functions. Here is C++11-friendly version:
constexpr int factorial(int n)
{
return (n == 1) ? 1 : n * factorial(n - 1);
}
constexpr int bin_coeff(int n, int k)
{
return factorial(n) / factorial(n - k) / factorial(k);
}
EXAMPLE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With