Automatic Differentiation
 
Loading...
Searching...
No Matches
choose.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_CHOOSE_HPP
2#define STAN_MATH_PRIM_FUN_CHOOSE_HPP
3
7#include <boost/math/special_functions/binomial.hpp>
8#include <cmath>
9#include <limits>
10
11namespace stan {
12namespace math {
13
29inline 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
51template <typename T1, typename T2, require_any_container_t<T1, T2>* = nullptr>
52inline auto choose(const T1& a, const T2& b) {
54 a, b, [&](const auto& c, const auto& d) { return choose(c, d); });
55}
56
57} // namespace math
58} // namespace stan
59#endif
void check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high, Idxs... idxs)
Throw an exception if y is not less than high.
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
auto apply_scalar_binary(const T1 &x, const T2 &y, const F &f)
Base template function for vectorization of binary scalar functions defined by applying a functor to ...
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...