Raptor
A fast and space-efficient pre-filter
All Classes Namespaces Files Functions Variables Macros Pages Concepts
logspace.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <algorithm>
13#include <cmath>
14
15namespace raptor::logspace
16{
17
18constexpr double ln_2{0.693147180559945309417232121458176568L};
19constexpr double negative_inf{-std::numeric_limits<double>::infinity()};
20
22// Produces correct result if either term is -inf.
23// Needs a check for the case where both terms are -inf.
24[[nodiscard]] inline double add(double const log_x, double const log_y) noexcept
25{
26 double const max{std::max(log_x, log_y)};
27 return max == negative_inf ? negative_inf : max + std::log1p(std::exp(-std::abs(log_x - log_y)));
28}
29
31template <typename... types>
32[[nodiscard]] double add(double const log_x, double const log_y, types... logs) noexcept
33{
34 return add(add(log_y, log_x), logs...);
35}
36
38// expm1 is more accurate than using exp if the difference is close to 0.
39// std::log1p(-std::exp(difference)) = std::log(1 + (-std::exp(difference)))
40// = std::log(1 - std::exp(difference))
41// std::log(-std::expm1(difference)) = std::log(-(std::exp(difference) - 1))
42// = std::log(1 - std::exp(difference))
43[[nodiscard]] inline double substract(double const log_x, double const log_y) noexcept
44{
45 double const difference{log_y - log_x};
46 return log_x + difference > -ln_2 ? std::log(-std::expm1(difference)) : std::log1p(-std::exp(difference));
47}
48
49struct add_fn
50{
51 [[nodiscard]] double operator()(double const log_x, double const log_y) const noexcept
52 {
53 return add(log_x, log_y);
54 };
55};
56
57} // namespace raptor::logspace
T exp(T... args)
T expm1(T... args)
T infinity(T... args)
T log1p(T... args)
T log(T... args)
double substract(double const log_x, double const log_y) noexcept
The log of a difference of two log terms. (log_x - log_y)
Definition logspace.hpp:43
double add(double const log_x, double const log_y) noexcept
The log of a sum of two log terms.
Definition logspace.hpp:24
T max(T... args)
T min(T... args)
Definition logspace.hpp:50
Hide me