27 #ifndef DOXYGEN_SHOULD_SKIP_THIS 39 template <
template <
typename >
class ALLOC >
47 template <
template <
typename >
class ALLOC >
49 const allocator_type& alloc) :
50 VariableLog2ParamComplexity< ALLOC >::allocator_type(alloc),
57 template <
template <
typename >
class ALLOC >
59 const VariableLog2ParamComplexity< ALLOC >& from,
68 template <
template <
typename >
class ALLOC >
70 const VariableLog2ParamComplexity< ALLOC >& from) :
75 template <
template <
typename >
class ALLOC >
77 VariableLog2ParamComplexity< ALLOC >&& from,
86 template <
template <
typename >
class ALLOC >
88 VariableLog2ParamComplexity< ALLOC >&& from) :
93 template <
template <
typename >
class ALLOC >
94 VariableLog2ParamComplexity< ALLOC >*
98 ALLOC< VariableLog2ParamComplexity< ALLOC > > allocator(alloc);
99 VariableLog2ParamComplexity< ALLOC >* table = allocator.allocate(1);
101 allocator.construct(table, *
this);
103 allocator.deallocate(table, 1);
111 template <
template <
typename >
class ALLOC >
112 VariableLog2ParamComplexity< ALLOC >*
119 template <
template <
typename >
class ALLOC >
126 template <
template <
typename >
class ALLOC >
127 INLINE VariableLog2ParamComplexity< ALLOC >&
129 operator=(
const VariableLog2ParamComplexity< ALLOC >& from) {
135 template <
template <
typename >
class ALLOC >
136 INLINE VariableLog2ParamComplexity< ALLOC >&
138 operator=(VariableLog2ParamComplexity< ALLOC >&& from) {
144 template <
template <
typename >
class ALLOC >
151 template <
template <
typename >
class ALLOC >
158 template <
template <
typename >
class ALLOC >
164 if (r == std::size_t(1))
return 0.0;
165 if (n == 0.0)
return 0.0;
166 if (n == 1.0)
return std::log2((
double)r);
170 "In the penalty of the fNML score, n must be greater " 171 <<
"than or equal to 0. But, here, n = " << n);
177 std::size_t xn = (std::size_t)n;
184 return __cache[std::pair< std::size_t, double >{r, n}];
185 }
catch (NotFound&) {}
207 double k_r = std::exp((log2Cnr2 - log2Cnr1) *
M_LN2);
208 double q_r = 1.0 + k_r * n / (6.0 - 2.0);
209 for (std::size_t i = std::size_t(6); i <= r; ++i) {
210 log2Cnr = log2Cnr1 + std::log2(q_r);
213 q_r = 1.0 + k_r * (n / (i - 1.0));
227 return __cache[std::pair< std::size_t, double >{r, n}];
228 }
catch (NotFound&) {}
235 if (r == std::size_t(2))
return log2Cnr1;
238 double log2Cnr2 = 0.0;
242 double k_r = std::exp((log2Cnr2 - log2Cnr1) *
M_LN2);
243 double q_r = 1.0 + k_r * n / (3.0 - 2.0);
244 double log2Cnr = 0.0;
245 for (std::size_t i = std::size_t(3); i <= r; ++i) {
246 log2Cnr = log2Cnr1 + std::log2(q_r);
249 q_r = 1.0 + k_r * (n / (i - 1.0));
263 template <
template <
typename >
class ALLOC >
273 GammaLog2 gamma_log2;
282 for (
double h = 1; h < n; ++h) {
284 (gamma_log2(n + 1) - gamma_log2(h + 1) - gamma_log2((n - h) + 1))
286 + h * std::log(h / n) + (n - h) * std::log((n - h) / n);
287 cn2 += std::exp(elt);
295 std::ofstream outfile(filename);
296 if (!outfile.is_open()) {
297 GUM_ERROR(IOError,
"It is impossible to open file " << filename);
299 outfile.precision(20);
300 outfile <<
"namespace gum {\n\n";
309 outfile <<
" // the CTable cache for log2(Cnr), n < " 310 << VariableLog2ParamComplexityCTableNSize <<
" and r in {2,3,4,5}\n";
311 outfile <<
" const double VariableLog2ParamComplexityCTable[4][" 312 << VariableLog2ParamComplexityCTableNSize <<
"] = {\n";
317 for (
const auto cn2 : cn2_table) {
322 const double logCn2 = (
double)std::log2(cn2);
329 for (std::size_t i = std::size_t(0);
332 if (i > std::size_t(0)) outfile <<
",\n ";
333 const double logCn3 = (
double)std::log2(cn2_table[i] + i);
340 for (std::size_t i = std::size_t(0);
343 if (i > std::size_t(0)) outfile <<
",\n ";
344 const double logCn4 = (
double)std::log2(cn2_table[i] * (1.0 + i / 2.0) + i);
351 for (std::size_t i = std::size_t(0);
354 if (i > std::size_t(0)) outfile <<
",\n ";
355 const double logCn5 =
356 (
double)std::log2(cn2_table[i] * (1.0 + 5.0 * i / 6.0) + i + i * i / 3.0);
362 outfile <<
" };\n\n";
363 outfile <<
"} /* namespace gum */\n";
VariableLog2ParamComplexity(const allocator_type &alloc=allocator_type())
default constructor
virtual VariableLog2ParamComplexity * clone() const
virtual copy constructor
allocator_type getAllocator() const
returns the allocator used by the parameterized complexity class
const double VariableLog2ParamComplexityCTable[4][1000]
HashTable< std::pair< std::size_t, double >, double > __cache
The class for computing Log2 (Gamma(x)).
constexpr std::size_t VariableLog2ParamComplexityCTableRSize
gum is the global namespace for all aGrUM entities
the class for computing the log2 of the parametric complexity of an r-ary multinomial variable ...
void useCache(const bool on_off)
indicates whether we wish to use a cache for the Cnr
void CnrToFile(const std::string &filename)
the function used to write the cpp file with the values of log2(Cnr)
constexpr std::size_t VariableLog2ParamComplexityCTableNSize
ALLOC< double > allocator_type
type for the allocators passed in arguments of methods
void clear()
Removes all the elements in the hash table.
virtual ~VariableLog2ParamComplexity()
destructor
VariableLog2ParamComplexity & operator=(const VariableLog2ParamComplexity &from)
copy operator
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
void clearCache()
clears the current cache
double log2Cnr(const std::size_t r, const double n)
returns the value of the log in base 2 of Cnr
#define GUM_ERROR(type, msg)