From 502cbee5b21841b09a04996a70c3ec1f89f11ce7 Mon Sep 17 00:00:00 2001 From: Matthieu Schaller <schaller@strw.leidenuniv.nl> Date: Tue, 21 Apr 2020 23:55:40 +0200 Subject: [PATCH] Added a function to compute the binomial coefficient of a pair of integers --- src/Makefile.am | 2 +- src/binomial.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/multipole.h | 1 + 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/binomial.h diff --git a/src/Makefile.am b/src/Makefile.am index 56b8f48e74..7c886448aa 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -52,7 +52,7 @@ include_HEADERS = space.h runner.h queue.h task.h lock.h cell.h part.h const.h \ chemistry.h chemistry_io.h chemistry_struct.h cosmology.h restart.h space_getsid.h utilities.h \ mesh_gravity.h cbrt.h exp10.h velociraptor_interface.h swift_velociraptor_part.h outputlist.h \ logger_io.h tracers_io.h tracers.h tracers_struct.h star_formation_io.h fof.h fof_struct.h fof_io.h \ - multipole.h multipole_struct.h \ + multipole.h multipole_struct.h binomial.h \ star_formation_struct.h star_formation.h star_formation_iact.h \ star_formation_logger.h star_formation_logger_struct.h \ pressure_floor.h pressure_floor_struct.h pressure_floor_iact.h \ diff --git a/src/binomial.h b/src/binomial.h new file mode 100644 index 0000000000..24ea90e656 --- /dev/null +++ b/src/binomial.h @@ -0,0 +1,46 @@ +/******************************************************************************* + * 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 <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ +#ifndef SWIFT_BINOMIAL_H +#define SWIFT_BINOMIAL_H + +#include "error.h" +#include "inline.h" + +/** + * @brief Compute the binomial coefficient (n, k) + * + * Only valid for values up to n = 5. + */ +__attribute__((const)) INLINE static int binomial(const int n, const int k) { + +#ifdef SWIFT_DEBUG_CHECKS + assert(n >= 0); + assert(k >= 0); + assert(n <= 5); + assert(k <= n); +#endif + + static const int coeffs[6][6] = {{1, 0, 0, 0, 0, 0}, {1, 1, 0, 0, 0, 0}, + {1, 2, 1, 0, 0, 0}, {1, 3, 3, 1, 0, 0}, + {1, 4, 6, 4, 1, 0}, {1, 5, 10, 10, 5, 1}}; + + return coeffs[n][k]; +} + +#endif /* SWIFT_BINOMIAL_H */ diff --git a/src/multipole.h b/src/multipole.h index 79dde23d2d..52e49417e2 100644 --- a/src/multipole.h +++ b/src/multipole.h @@ -30,6 +30,7 @@ /* Includes. */ #include "accumulate.h" #include "align.h" +#include "binomial.h" #include "const.h" #include "error.h" #include "gravity.h" -- GitLab