aGrUM  0.21.0
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 47 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 38 of file gammaLog2.cpp.

References gum::Set< Key, Alloc >::emplace().

38  : _requires_precision_{requires_precision} {
39  GUM_CONSTRUCTOR(GammaLog2);
40  }
GammaLog2(bool requires_precision=false)
Default constructor.
Definition: gammaLog2.cpp:38
bool _requires_precision_
Indicates whether we need more precision for small values.
Definition: gammaLog2.h:113
+ Here is the call graph for this function:

◆ GammaLog2() [2/3]

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

Copy constructor.

Parameters
fromThe gum::GammaLog2 to copy.

Definition at line 43 of file gammaLog2.cpp.

References gum::Set< Key, Alloc >::emplace().

43  : _requires_precision_{from._requires_precision_} {
44  GUM_CONS_CPY(GammaLog2);
45  }
GammaLog2(bool requires_precision=false)
Default constructor.
Definition: gammaLog2.cpp:38
bool _requires_precision_
Indicates whether we need more precision for small values.
Definition: gammaLog2.h:113
+ Here is the call graph for this function:

◆ GammaLog2() [3/3]

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

Move constructor.

Parameters
fromThe gum::GammaLog2 to move.

Definition at line 48 of file gammaLog2.cpp.

References gum::Set< Key, Alloc >::emplace().

48  : _requires_precision_{from._requires_precision_} {
49  GUM_CONS_MOV(GammaLog2);
50  }
GammaLog2(bool requires_precision=false)
Default constructor.
Definition: gammaLog2.cpp:38
bool _requires_precision_
Indicates whether we need more precision for small values.
Definition: gammaLog2.h:113
+ Here is the call graph for this function:

◆ ~GammaLog2()

gum::GammaLog2::~GammaLog2 ( )

Class destructor.

Definition at line 53 of file gammaLog2.cpp.

References gum::Set< Key, Alloc >::emplace().

53  {
54  GUM_DESTRUCTOR(GammaLog2);
55  ;
56  }
GammaLog2(bool requires_precision=false)
Default constructor.
Definition: gammaLog2.cpp:38
+ Here is the call graph for this function:

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 31 of file gammaLog2_inl.h.

References gum::Set< Key, Alloc >::emplace().

31  {
32  if (x <= 0) GUM_ERROR(OutOfBounds, "log2(gamma()) should be called with a positive argument")
33 
34  // if x is small, use precomputed values
35  if (x < 50) {
36  if (x >= 0.01) {
38  const Idx index = int(x * 100);
39  return _small_values_[index]
40  + (_small_values_[index + 1] - _small_values_[index]) * double(x * 100 - index);
41  } else {
42  const Idx index = int(x * 100 + 0.5);
43  return _small_values_[index];
44  }
45  } else {
46  // for very small values of x, Gamma(x) is approximately equal to
47  // 1/x. Hence gammaLog2(x) is approximately equal to log2(1/x)
48  return std::log2(1.0 / x);
49  }
50  }
51 
52  // returns the approximation by the stirling formula
53  return (_log_sqrt_2pi_ + (x - 0.5f) * log(x) - x + log(1.0 + 1.0 / (12 * x))) * _inv_log2_;
54  }
static constexpr double _inv_log2_
The value of 1 / std::log(2).
Definition: gammaLog2.h:116
static const std::vector< double > _small_values_
The 5000 values from 0 to 50 by step of 1/100.
Definition: gammaLog2.h:122
bool _requires_precision_
Indicates whether we need more precision for small values.
Definition: gammaLog2.h:113
static constexpr double _log_sqrt_2pi_
The value of std::log ( std::sqrt(2pi) ).
Definition: gammaLog2.h:119
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
Potential< GUM_SCALAR > log2(const Potential< GUM_SCALAR > &arg)
Definition: potential.h:590
+ Here is the call 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 56 of file gammaLog2_inl.h.

References gum::Set< Key, Alloc >::emplace().

56 { return gammaLog2(x); }
double gammaLog2(double x) const
Returns log2 ( gamma (x) ) for x >= 0.
Definition: gammaLog2_inl.h:31
+ 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 58 of file gammaLog2_inl.h.

References gum::Set< Key, Alloc >::emplace().

58 { _requires_precision_ = prec; }
bool _requires_precision_
Indicates whether we need more precision for small values.
Definition: gammaLog2.h:113
+ Here is the call graph for this function:

Member Data Documentation

◆ _inv_log2_

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

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

Definition at line 116 of file gammaLog2.h.

◆ _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 119 of file gammaLog2.h.

◆ _requires_precision_

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

Indicates whether we need more precision for small values.

Definition at line 113 of file gammaLog2.h.

◆ _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 122 of file gammaLog2.h.


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