WPILibC++ 2023.4.3-108-ge5452e3
binomial_coef.hpp
Go to the documentation of this file.
1/*################################################################################
2 ##
3 ## Copyright (C) 2016-2022 Keith O'Hara
4 ##
5 ## This file is part of the GCE-Math C++ library.
6 ##
7 ## Licensed under the Apache License, Version 2.0 (the "License");
8 ## you may not use this file except in compliance with the License.
9 ## You may obtain a copy of the License at
10 ##
11 ## http://www.apache.org/licenses/LICENSE-2.0
12 ##
13 ## Unless required by applicable law or agreed to in writing, software
14 ## distributed under the License is distributed on an "AS IS" BASIS,
15 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ## See the License for the specific language governing permissions and
17 ## limitations under the License.
18 ##
19 ################################################################################*/
20
21#ifndef _gcem_binomial_coef_HPP
22#define _gcem_binomial_coef_HPP
23
24namespace internal
25{
26
27template<typename T>
28constexpr
29T
30binomial_coef_recur(const T n, const T k)
31noexcept
32{
33 return( // edge cases
34 (k == T(0) || n == k) ? T(1) : // deals with 0 choose 0 case
35 n == T(0) ? T(0) :
36 // else
37 binomial_coef_recur(n-1,k-1) + binomial_coef_recur(n-1,k) );
38}
39
40template<typename T, typename std::enable_if<std::is_integral<T>::value>::type* = nullptr>
41constexpr
42T
43binomial_coef_check(const T n, const T k)
44noexcept
45{
46 return binomial_coef_recur(n,k);
47}
48
49template<typename T, typename std::enable_if<!std::is_integral<T>::value>::type* = nullptr>
50constexpr
51T
52binomial_coef_check(const T n, const T k)
53noexcept
54{
55 return( // NaN check; removed due to MSVC problems; template not being ignored in <int> cases
56 // (is_nan(n) || is_nan(k)) ? GCLIM<T>::quiet_NaN() :
57 //
58 static_cast<T>(binomial_coef_recur(static_cast<ullint_t>(n),static_cast<ullint_t>(k))) );
59}
60
61template<typename T1, typename T2, typename TC = common_t<T1,T2>>
62constexpr
63TC
64binomial_coef_type_check(const T1 n, const T2 k)
65noexcept
66{
67 return binomial_coef_check(static_cast<TC>(n),static_cast<TC>(k));
68}
69
70}
71
72/**
73 * Compile-time binomial coefficient
74 *
75 * @param n integral-valued input.
76 * @param k integral-valued input.
77 * @return computes the Binomial coefficient
78 * \f[ \binom{n}{k} = \frac{n!}{k!(n-k)!} \f]
79 * also known as '\c n choose \c k '.
80 */
81
82template<typename T1, typename T2>
83constexpr
84common_t<T1,T2>
85binomial_coef(const T1 n, const T2 k)
86noexcept
87{
89}
90
91#endif
constexpr common_t< T1, T2 > binomial_coef(const T1 n, const T2 k) noexcept
Compile-time binomial coefficient.
Definition: binomial_coef.hpp:85
type
Definition: core.h:575
unsigned long long int ullint_t
Definition: gcem_options.hpp:69
Definition: Eigen_Colamd.h:50
constexpr TC binomial_coef_type_check(const T1 n, const T2 k) noexcept
Definition: binomial_coef.hpp:64
constexpr T binomial_coef_check(const T n, const T k) noexcept
Definition: binomial_coef.hpp:43
constexpr T binomial_coef_recur(const T n, const T k) noexcept
Definition: binomial_coef.hpp:30