aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
gammaLog2_inl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief The class for computing Log2 (Gamma(x)).
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 namespace gum {
30 
31  ALWAYS_INLINE double GammaLog2::gammaLog2(double x) const {
32  if (x <= 0)
34  "log2(gamma()) should be called with a positive argument");
35 
36  // if x is small, use precomputed values
37  if (x < 50) {
38  if (x >= 0.01) {
40  const Idx index = int(x * 100);
41  return small_values__[index]
43  * double(x * 100 - index);
44  } else {
45  const Idx index = int(x * 100 + 0.5);
46  return small_values__[index];
47  }
48  } else {
49  // for very small values of x, Gamma(x) is approximately equal to
50  // 1/x. Hence gammaLog2(x) is approximately equal to log2(1/x)
51  return std::log2(1.0 / x);
52  }
53  }
54 
55  // returns the approximation by the stirling formula
56  return (log_sqrt_2pi__ + (x - 0.5f) * log(x) - x + log(1.0 + 1.0 / (12 * x)))
57  * inv_log2__;
58  }
59 
60  ALWAYS_INLINE double GammaLog2::operator()(double x) const {
61  return gammaLog2(x);
62  }
63 
65 
66 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669