From 2abec55cc4c2414fa90dde6d108932989b91f7c3 Mon Sep 17 00:00:00 2001 From: Thomas Fetzer Date: Mon, 25 Jun 2018 15:20:52 +0200 Subject: [PATCH] [math] Implement weighted means --- dumux/common/math.hh | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/dumux/common/math.hh b/dumux/common/math.hh index 0c2dd6e89d..37c2baa440 100644 --- a/dumux/common/math.hh +++ b/dumux/common/math.hh @@ -36,19 +36,40 @@ namespace Dumux { /*! * \ingroup Common - * \brief Calculate the harmonic mean of two scalar values. + * \brief Calculate the (weighted) arithmetic mean of two scalar values. * * \param x The first input value * \param y The second input value + * \param wx The first weight + * \param wy The second weight */ template -constexpr Scalar harmonicMean(Scalar x, Scalar y) noexcept +constexpr Scalar arithmeticMean(Scalar x, Scalar y, Scalar wx = 1.0, Scalar wy = 1.0) noexcept { - static_assert(Dune::IsNumber::value, "The arguments x and y have to be numbers!"); + static_assert(Dune::IsNumber::value, "The arguments x, y, wx, and wy have to be numbers!"); + + if (x*y <= 0) + return 0; + return (x * wx + y * wy)/(wx + wy); +} + +/*! + * \ingroup Common + * \brief Calculate the (weighted) harmonic mean of two scalar values. + * + * \param x The first input value + * \param y The second input value + * \param wx The first weight + * \param wy The second weight + */ +template +constexpr Scalar harmonicMean(Scalar x, Scalar y, Scalar wx = 1.0, Scalar wy = 1.0) noexcept +{ + static_assert(Dune::IsNumber::value, "The arguments x, y, wx, and wy have to be numbers!"); if (x*y <= 0) return 0; - return (2*x*y)/(x + y); + return (wx + wy) / (wx / x + wy / y); } /*! -- GitLab