File: SalmonMath.hpp

package info (click to toggle)
salmon 0.7.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,352 kB
  • ctags: 5,243
  • sloc: cpp: 42,341; ansic: 6,252; python: 228; makefile: 207; sh: 190
file content (79 lines) | stat: -rw-r--r-- 2,163 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef SALMON_MATH_HPP
#define SALMON_MATH_HPP

// If we have built-ins, do as Boost does

#ifndef BOOST_LIKELY
#if defined(__has_builtin)
#if __has_builtin(__builtin_expect)
#define BOOST_LIKELY(x) __builtin_expect(x, 1)
#endif
#endif
#endif

#ifndef BOOST_UNLIKELY
#if defined(__has_builtin)
#if __has_builtin(__builtin_expect)
#define BOOST_UNLIKELY(x) __builtin_expect(x, 0)
#endif
#endif
#endif

// If we didn't have those built-ins fall back to this

#ifndef BOOST_LIKELY
#define BOOST_LIKELY(x) (x)
#endif

#ifndef BOOST_UNLIKELY
#define BOOST_UNLIKELY(x) (x)
#endif

#include <cmath>
#include <cassert>

namespace salmon {

    namespace math {
        constexpr double LOG_0 = HUGE_VAL;
        constexpr double LOG_1 = 0;
        constexpr double LOG_ONEHALF = -0.69314718055994530941;
        constexpr double LOG_ORPHAN_PROB = -2.30258509299404568401;
        constexpr double EPSILON = 0.375e-10;
        const double LOG_EPSILON = log(EPSILON);

        inline double log(double v) { return (v > 0) ? std::log(v) : LOG_0; }
        inline double exp(double v) { return std::exp(v); }
        inline bool isLog0(double v) { return v == LOG_0; }
        // Taken from https://github.com/adarob/eXpress/blob/master/src/main.h
        inline bool approxEqual(double a, double b, double eps=EPSILON) {
            return std::abs(a-b) <= eps;
        }

        // Taken from https://github.com/adarob/eXpress/blob/master/src/main.h
        inline double logAdd(double x, double y) {
            if (std::abs(x) == LOG_0) { return y; }
            if (std::abs(y) == LOG_0) { return x; }
            if (y > x) { std::swap(x,y); }
            double sum = x + std::log(1 + std::exp(y-x));
            return sum;
        }

        // Taken from https://github.com/adarob/eXpress/blob/master/src/main.h
        inline double logSub(double x, double y) {
            if (std::abs(y) == LOG_0) { return x; }
            if (x <= y) {
                assert(std::fabs(x-y) < 1e-5);
                return LOG_0;
            }
            double diff = x + std::log(1-std::exp(y-x));
            return diff;
        }


    }

}


#endif //SALMON_MATH_HPP