aGrUM  0.20.2
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  :
39  requires_precision__{requires_precision} {
40  GUM_CONSTRUCTOR(GammaLog2);
41  }
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 44 of file gammaLog2.cpp.

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

44  :
45  requires_precision__{from.requires_precision__} {
46  GUM_CONS_CPY(GammaLog2);
47  }
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 50 of file gammaLog2.cpp.

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

50  :
51  requires_precision__{from.requires_precision__} {
52  GUM_CONS_MOV(GammaLog2);
53  }
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 56 of file gammaLog2.cpp.

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

56 { GUM_DESTRUCTOR(GammaLog2); }
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)
33  GUM_ERROR(OutOfBounds,
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]
42  + (small_values__[index + 1] - 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  }
static constexpr double log_sqrt_2pi__
The value of std::log ( std::sqrt(2pi) ).
Definition: gammaLog2.h:119
static constexpr double inv_log2__
The value of 1 / std::log(2).
Definition: gammaLog2.h:116
bool requires_precision__
Indicates whether we need more precision for small values.
Definition: gammaLog2.h:113
static const std::vector< double > small_values__
The 5000 values from 0 to 50 by step of 1/100.
Definition: gammaLog2.h:122
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54
Potential< GUM_SCALAR > log2(const Potential< GUM_SCALAR > &arg)
Definition: potential.h:612
+ 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 60 of file gammaLog2_inl.h.

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

60  {
61  return gammaLog2(x);
62  }
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 64 of file gammaLog2_inl.h.

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

64 { 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: