aGrUM  0.18.1
a C++ library for (probabilistic) graphical models
gum::GammaLog2 Class Reference

The class for computing Log2 (Gamma(x)) More...

#include <agrum/tools/core/math/gammaLog2.h>

+ Collaboration diagram for gum::GammaLog2:

Public Member Functions

Constructors / Destructors
 GammaLog2 (bool requires_precision=false)
 Default constructor. More...
 
 GammaLog2 (const GammaLog2 &from)
 Copy constructor. More...
 
 GammaLog2 (GammaLog2 &&from)
 Move constructor. More...
 
 ~GammaLog2 ()
 Class destructor. More...
 
Operators
double operator() (double x) const
 Returns log2 ( gamma (x) ) for x > 0. More...
 
void setPrecision (bool p)
 Sets whether we need more precision for small values. More...
 
double gammaLog2 (double x) const
 Returns log2 ( gamma (x) ) for x >= 0. More...
 

Detailed Description

The class for computing Log2 (Gamma(x))

Definition at line 48 of file gammaLog2.h.

Constructor & Destructor Documentation

◆ GammaLog2() [1/3]

gum::GammaLog2::GammaLog2 ( bool  requires_precision = false)

Default constructor.

Parameters
requires_precisionSet if precision is required or not.

Definition at line 39 of file gammaLog2.cpp.

39  :
40  requires_precision__{requires_precision} {
41  GUM_CONSTRUCTOR(GammaLog2);
42  }
GammaLog2(bool requires_precision=false)
Default constructor.
Definition: gammaLog2.cpp:39
bool requires_precision__
Indicates whether we need more precision for small values.
Definition: gammaLog2.h:114

◆ GammaLog2() [2/3]

gum::GammaLog2::GammaLog2 ( const GammaLog2 from)

Copy constructor.

Parameters
fromThe gum::GammaLog2 to copy.

Definition at line 45 of file gammaLog2.cpp.

References requires_precision__.

45  :
46  requires_precision__{from.requires_precision__} {
47  GUM_CONS_CPY(GammaLog2);
48  }
GammaLog2(bool requires_precision=false)
Default constructor.
Definition: gammaLog2.cpp:39
bool requires_precision__
Indicates whether we need more precision for small values.
Definition: gammaLog2.h:114

◆ GammaLog2() [3/3]

gum::GammaLog2::GammaLog2 ( GammaLog2 &&  from)

Move constructor.

Parameters
fromThe gum::GammaLog2 to move.

Definition at line 51 of file gammaLog2.cpp.

51  :
52  requires_precision__{from.requires_precision__} {
53  GUM_CONS_MOV(GammaLog2);
54  }
GammaLog2(bool requires_precision=false)
Default constructor.
Definition: gammaLog2.cpp:39
bool requires_precision__
Indicates whether we need more precision for small values.
Definition: gammaLog2.h:114

◆ ~GammaLog2()

gum::GammaLog2::~GammaLog2 ( )

Class destructor.

Definition at line 57 of file gammaLog2.cpp.

References small_values__.

57 { GUM_DESTRUCTOR(GammaLog2); }
GammaLog2(bool requires_precision=false)
Default constructor.
Definition: gammaLog2.cpp:39

Member Function Documentation

◆ gammaLog2()

ALWAYS_INLINE double gum::GammaLog2::gammaLog2 ( double  x) const

Returns log2 ( gamma (x) ) for x >= 0.

Parameters
xA positive double.
Returns
Returns log2 ( gamma (x) ) for x >= 0.
Exceptions
OutOfBoundsRaised if x <= 0.

Definition at line 32 of file gammaLog2_inl.h.

References GUM_ERROR, inv_log2__, log_sqrt_2pi__, requires_precision__, and small_values__.

Referenced by operator()().

32  {
33  if (x <= 0)
34  GUM_ERROR(OutOfBounds,
35  "log2(gamma()) should be called with a positive argument");
36 
37  // if x is small, use precomputed values
38  if (x < 50) {
39  if (x >= 0.01) {
41  const Idx index = int(x * 100);
42  return small_values__[index]
43  + (small_values__[index + 1] - small_values__[index])
44  * double(x * 100 - index);
45  } else {
46  const Idx index = int(x * 100 + 0.5);
47  return small_values__[index];
48  }
49  } else {
50  // for very small values of x, Gamma(x) is approximately equal to
51  // 1/x. Hence gammaLog2(x) is approximately equal to log2(1/x)
52  return std::log2(1.0 / x);
53  }
54  }
55 
56  // returns the approximation by the stirling formula
57  return (log_sqrt_2pi__ + (x - 0.5f) * log(x) - x + log(1.0 + 1.0 / (12 * x)))
58  * inv_log2__;
59  }
static constexpr double log_sqrt_2pi__
The value of std::log ( std::sqrt(2pi) ).
Definition: gammaLog2.h:120
static constexpr double inv_log2__
The value of 1 / std::log(2).
Definition: gammaLog2.h:117
bool requires_precision__
Indicates whether we need more precision for small values.
Definition: gammaLog2.h:114
static const std::vector< double > small_values__
The 5000 values from 0 to 50 by step of 1/100.
Definition: gammaLog2.h:123
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the caller graph for this function:

◆ operator()()

ALWAYS_INLINE double gum::GammaLog2::operator() ( double  x) const

Returns log2 ( gamma (x) ) for x > 0.

Exceptions
OutOfBoundsRaised if raised if x <= 0.

Definition at line 61 of file gammaLog2_inl.h.

References gammaLog2().

61  {
62  return gammaLog2(x);
63  }
double gammaLog2(double x) const
Returns log2 ( gamma (x) ) for x >= 0.
Definition: gammaLog2_inl.h:32
+ Here is the call graph for this function:

◆ setPrecision()

INLINE void gum::GammaLog2::setPrecision ( bool  p)

Sets whether we need more precision for small values.

Parameters
pIf true, precision is enable.

Definition at line 65 of file gammaLog2_inl.h.

References requires_precision__.

65 { requires_precision__ = prec; }
bool requires_precision__
Indicates whether we need more precision for small values.
Definition: gammaLog2.h:114

Member Data Documentation

◆ inv_log2__

constexpr double gum::GammaLog2::inv_log2__ {M_LOG2E}
staticprivate

The value of 1 / std::log(2).

Definition at line 117 of file gammaLog2.h.

Referenced by gammaLog2().

◆ log_sqrt_2pi__

constexpr double gum::GammaLog2::log_sqrt_2pi__ {GUM_LOG_SQRT_2PI}
staticprivate

The value of std::log ( std::sqrt(2pi) ).

Definition at line 120 of file gammaLog2.h.

Referenced by gammaLog2().

◆ requires_precision__

bool gum::GammaLog2::requires_precision__ {false}
private

Indicates whether we need more precision for small values.

Definition at line 114 of file gammaLog2.h.

Referenced by GammaLog2(), gammaLog2(), and setPrecision().

◆ small_values__

const std::vector< double > gum::GammaLog2::small_values__
staticprivate

The 5000 values from 0 to 50 by step of 1/100.

Definition at line 123 of file gammaLog2.h.

Referenced by gammaLog2(), and ~GammaLog2().


The documentation for this class was generated from the following files: