Raptor 4.0.0-rc.1
A fast and space-efficient pre-filter
Loading...
Searching...
No Matches
logspace.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2026 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2026 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.693147180559945};
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_x, log_y), logs...);
35}
36
38// log(x - y) = log_x + log(1 - std::exp(difference))
39// expm1 is more accurate than using exp if the difference is close to 0.
40// std::log1p(-std::exp(difference)) = std::log(1 + (-std::exp(difference)))
41// = std::log(1 - std::exp(difference))
42// std::log(-std::expm1(difference)) = std::log(-(std::exp(difference) - 1))
43// = std::log(1 - std::exp(difference))
44[[nodiscard]] inline double substract(double const log_x, double const log_y) noexcept
45{
46 double const difference{log_y - log_x};
47 return log_x + (difference > -ln_2 ? std::log(-std::expm1(difference)) : std::log1p(-std::exp(difference)));
48}
49
50struct add_fn
51{
52 [[nodiscard]] double operator()(double const log_x, double const log_y) const noexcept
53 {
54 return add(log_x, log_y);
55 };
56};
57
58} // 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:44
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:51
Hide me