binomial.h 1.8 KB
 Matthieu Schaller committed Apr 21, 2020 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ``````/******************************************************************************* * This file is part of SWIFT. * Copyright (c) 2020 Matthieu Schaller (schaller@strw.leidenuniv.nl) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * ******************************************************************************/ #ifndef SWIFT_BINOMIAL_H #define SWIFT_BINOMIAL_H `````` Matthieu Schaller committed Apr 22, 2020 22 23 24 25 ``````/* Config parameters. */ #include "../config.h" /* Local headers */ `````` Matthieu Schaller committed Apr 21, 2020 26 27 28 29 30 31 ``````#include "error.h" #include "inline.h" /** * @brief Compute the binomial coefficient (n, k) * `````` Matthieu Schaller committed Apr 22, 2020 32 `````` * Only valid for values 0 <= n <= 8 and 0 <= k <= n. `````` Matthieu Schaller committed Apr 21, 2020 33 34 35 36 37 38 `````` */ __attribute__((const)) INLINE static int binomial(const int n, const int k) { #ifdef SWIFT_DEBUG_CHECKS assert(n >= 0); assert(k >= 0); `````` Matthieu Schaller committed Apr 22, 2020 39 `````` assert(n <= 8); `````` Matthieu Schaller committed Apr 21, 2020 40 41 42 `````` assert(k <= n); #endif `````` Matthieu Schaller committed Apr 22, 2020 43 44 45 46 47 48 49 50 51 `````` /* Hello Pascal! Nice to meet again */ static const int coeffs[9][9] = { {1, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 0, 0, 0, 0, 0, 0, 0}, {1, 2, 1, 0, 0, 0, 0, 0, 0}, {1, 3, 3, 1, 0, 0, 0, 0, 0}, {1, 4, 6, 4, 1, 0, 0, 0, 0}, {1, 5, 10, 10, 5, 1, 0, 0, 0}, {1, 6, 15, 20, 15, 6, 1, 0, 0}, {1, 7, 21, 35, 35, 21, 7, 1, 0}, {1, 8, 28, 56, 70, 56, 28, 8, 1} }; `````` Matthieu Schaller committed Apr 21, 2020 52 53 54 55 56 `````` return coeffs[n][k]; } #endif /* SWIFT_BINOMIAL_H */``````