From aa972e1b86aff7a6341bf8411370c7018f2592e7 Mon Sep 17 00:00:00 2001 From: stevet Date: Wed, 1 May 2019 16:19:21 +0200 Subject: [PATCH] more efficient and stable binomial coefficients computation --- include/curves/bernstein.h | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/include/curves/bernstein.h b/include/curves/bernstein.h index 5066f83..31c8aed 100644 --- a/include/curves/bernstein.h +++ b/include/curves/bernstein.h @@ -20,18 +20,6 @@ namespace curves { -/// \brief Computes factorial of a number. -/// \param n : an unsigned integer. -/// \return \f\$n!\f\$ -/// -inline unsigned int fact(const unsigned int n) -{ - unsigned int res = 1; - for (unsigned int i=2 ; i <= n ; ++i) - res *= i; - return res; -} - /// \brief Computes a binomial coefficient. /// \param n : an unsigned integer. /// \param k : an unsigned integer. @@ -39,7 +27,13 @@ inline unsigned int fact(const unsigned int n) /// inline unsigned int bin(const unsigned int n, const unsigned int k) { - return fact(n) / (fact(k) * fact(n - k)); + if(k > n) + throw std::runtime_error("binomial coefficient higher than degree"); + if(k == 0) + return 1; + if(k > n/2) + return bin(n,n-k); + return n * bin(n-1,k-1) / k; } /// \class Bernstein. -- GitLab