Austin Schuh | e6e7ea5 | 2019-01-13 17:26:36 -0800 | [diff] [blame] | 1 | #ifndef FRC971_CONTROL_LOOPS_BINOMIAL_H_ |
| 2 | #define FRC971_CONTROL_LOOPS_BINOMIAL_H_ |
| 3 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 4 | namespace frc971::control_loops { |
Austin Schuh | e6e7ea5 | 2019-01-13 17:26:36 -0800 | [diff] [blame] | 5 | |
| 6 | // Computes the factorial of n |
| 7 | constexpr double Factorial(int n) { |
| 8 | if (n <= 1) { |
| 9 | return 1.0; |
| 10 | } else { |
| 11 | return Factorial(n - 1) * n; |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | // Computes the binomial coefficients. n choose k. |
| 16 | constexpr double Binomial(int n, int k) { |
| 17 | return Factorial(n) / (Factorial(k) * Factorial(n - k)); |
| 18 | } |
| 19 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 20 | } // namespace frc971::control_loops |
Austin Schuh | e6e7ea5 | 2019-01-13 17:26:36 -0800 | [diff] [blame] | 21 | |
| 22 | #endif // FRC971_CONTROL_LOOPS_BINOMIAL_H_ |