diff --git a/src/Makefile.am b/src/Makefile.am index 56b8f48e746ccca9e1122d779464da0059ab2cb8..7c886448aa2cfa8dc3a060f6da14865df00f7ec0 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 0000000000000000000000000000000000000000..24ea90e656a812aac2e2f8bfe7c64ace1af7e3b5 --- /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 79dde23d2df9955d246f08eaa880e7b2ee25b155..52e49417e2ba3eccf9015a9ca2b134f7d23e2767 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"