1 #ifndef STAN_MATH_PRIM_FUN_CHOOSE_HPP
2 #define STAN_MATH_PRIM_FUN_CHOOSE_HPP
3
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/functor/apply_scalar_binary.hpp>
7 #include <boost/math/special_functions/binomial.hpp>
8 #include <cmath>
9 #include <limits>
10
11 namespace stan {
12 namespace math {
13
14 /**
15 * Return the binomial coefficient for the specified integer
16 * arguments.
17 *
18 * The binomial coefficient, \f${n \choose k}\f$, read "n choose k", is
19 * defined for \f$0 \leq k \leq n\f$ (otherwise return 0) by
20 *
21 * \f${n \choose k} = \frac{n!}{k! (n-k)!}\f$.
22 *
23 * @param n total number of objects
24 * @param k number of objects chosen
25 * @return n choose k or 0 iff k > n
26 * @throw std::domain_error if either argument is negative or the
27 * result will not fit in an int type
28 */
choose(int n,int k)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 }
35 const double choices = boost::math::binomial_coefficient<double>(n, k);
36 check_less_or_equal("choose", "n choose k", choices,
37 std::numeric_limits<int>::max());
38 return static_cast<int>(std::round(choices));
39 }
40
41 /**
42 * Enables the vectorised application of the binomial coefficient function,
43 * when the first and/or second arguments are containers.
44 *
45 * @tparam T1 type of first input
46 * @tparam T2 type of second input
47 * @param a First input
48 * @param b Second input
49 * @return Binomial coefficient function applied to the two inputs.
50 */
51 template <typename T1, typename T2, require_any_container_t<T1, T2>* = nullptr>
choose(const T1 & a,const T2 & b)52 inline auto choose(const T1& a, const T2& b) {
53 return apply_scalar_binary(
54 a, b, [&](const auto& c, const auto& d) { return choose(c, d); });
55 }
56
57 } // namespace math
58 } // namespace stan
59 #endif
60