Stan Math Library  2.20.0
reverse mode automatic differentiation
choose.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_FUN_CHOOSE_HPP
2 #define STAN_MATH_PRIM_SCAL_FUN_CHOOSE_HPP
3 
7 #include <boost/math/special_functions/binomial.hpp>
8 #include <limits>
9 #include <cmath>
10 
11 namespace stan {
12 namespace math {
13 
29 inline int choose(int n, int k) {
30  check_nonnegative("choose", "n", n);
31  check_nonnegative("choose", "k", k);
32  if (k > n)
33  return 0;
34  const double choices = boost::math::binomial_coefficient<double>(n, k);
35  check_less_or_equal("choose", "n choose k", choices,
37  return static_cast<int>(std::round(choices));
38 }
39 
40 } // namespace math
41 } // namespace stan
42 #endif
void check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high)
Check if y is less or equal to high.
fvar< T > round(const fvar< T > &x)
Return the closest integer to the specified argument, with halfway cases rounded away from zero...
Definition: round.hpp:24
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
int choose(int n, int k)
Return the binomial coefficient for the specified integer arguments.
Definition: choose.hpp:29
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Definition: max.hpp:21

     [ Stan Home Page ] © 2011–2018, Stan Development Team.