Raptor 3.0.0-rc.1
A fast and space-efficient pre-filter for querying very large collections of nucleotide sequences
 
logspace.hpp
Go to the documentation of this file.
1// --------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2023, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2023, Knut Reinert & MPI für molekulare Genetik
4// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5// shipped with this file and also available at: https://github.com/seqan/raptor/blob/main/LICENSE.md
6// --------------------------------------------------------------------------------------------------
7
13#pragma once
14
15#include <cmath>
16
17namespace raptor::logspace
18{
19
20constexpr double ln_2{0.693147180559945309417232121458176568L};
21constexpr double negative_inf{-std::numeric_limits<double>::infinity()};
22
24// Produces correct result if either term is -inf.
25// Needs a check for the case where both terms are -inf.
26[[nodiscard]] inline double add(double const log_x, double const log_y) noexcept
27{
28 double const max{std::max(log_x, log_y)};
29 return max == negative_inf ? negative_inf : max + std::log1p(std::exp(-std::abs(log_x - log_y)));
30}
31
33template <typename... types>
34[[nodiscard]] double add(double const log_x, double const log_y, types... logs) noexcept
35{
36 return add(add(log_y, log_x), logs...);
37}
38
40// expm1 is more accurate than using exp if the difference is close to 0.
41[[nodiscard]] inline double substract(double const log_x, double const log_y) noexcept
42{
43 double const difference{log_y - log_x};
44 return log_x + difference > -ln_2 ? std::log(std::expm1(difference)) : std::log1p(-std::exp(difference));
45}
46
47struct add_fn
48{
49 [[nodiscard]] double operator()(double const log_x, double const log_y) const noexcept
50 {
51 return add(log_x, log_y);
52 };
53};
54
55} // 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:41
double add(double const log_x, double const log_y) noexcept
The log of a sum of two log terms.
Definition: logspace.hpp:26
T max(T... args)
Definition: logspace.hpp:48