fastq_to_fasta
A template for creation of SeqAn3 apps, with a FASTQ to FASTA example app.
logspace.hpp
Go to the documentation of this file.
1 // --------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2022, 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 
8 #pragma once
9 
10 #include <cmath>
11 
13 {
14 
15 constexpr double ln_2{0.693147180559945309417232121458176568L};
16 constexpr double negative_inf{-std::numeric_limits<double>::infinity()};
17 
19 // Produces correct result if either term is -inf.
20 // Needs a check for the case where both terms are -inf.
21 [[nodiscard]] inline double add(double const log_x, double const log_y) noexcept
22 {
23  double const max{std::max(log_x, log_y)};
24  return max == negative_inf ? negative_inf : max + std::log1p(std::exp(-std::abs(log_x - log_y)));
25 }
26 
28 template <typename... types>
29 [[nodiscard]] double add(double const log_x, double const log_y, types... logs) noexcept
30 {
31  return add(add(log_y, log_x), logs...);
32 }
33 
35 // expm1 is more accurate than using exp if the difference is close to 0.
36 [[nodiscard]] inline double substract(double const log_x, double const log_y) noexcept
37 {
38  double const difference{log_y - log_x};
39  return log_x + difference > -ln_2 ? std::log(std::expm1(difference)) : std::log1p(-std::exp(difference));
40 }
41 
42 struct add_fn
43 {
44  [[nodiscard]] double operator()(double const log_x, double const log_y) const noexcept
45  {
46  return add(log_x, log_y);
47  };
48 };
49 
50 } // namespace raptor::logspace
Definition: logspace.hpp:13
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:36
double add(double const log_x, double const log_y) noexcept
The log of a sum of two log terms.
Definition: logspace.hpp:21
constexpr double negative_inf
Definition: logspace.hpp:16
constexpr double ln_2
Definition: logspace.hpp:15
Definition: logspace.hpp:43
double operator()(double const log_x, double const log_y) const noexcept
Definition: logspace.hpp:44