30 #ifndef DOXYGEN_SHOULD_SKIP_THIS 42 template <
template <
typename >
class ALLOC >
50 template <
template <
typename >
class ALLOC >
52 const allocator_type& alloc) :
53 VariableLog2ParamComplexity< ALLOC >::allocator_type(alloc),
60 template <
template <
typename >
class ALLOC >
62 const VariableLog2ParamComplexity< ALLOC >& from,
71 template <
template <
typename >
class ALLOC >
73 const VariableLog2ParamComplexity< ALLOC >& from) :
78 template <
template <
typename >
class ALLOC >
80 VariableLog2ParamComplexity< ALLOC >&& from,
89 template <
template <
typename >
class ALLOC >
91 VariableLog2ParamComplexity< ALLOC >&& from) :
96 template <
template <
typename >
class ALLOC >
97 VariableLog2ParamComplexity< ALLOC >*
101 ALLOC< VariableLog2ParamComplexity< ALLOC > > allocator(alloc);
102 VariableLog2ParamComplexity< ALLOC >* table = allocator.allocate(1);
104 allocator.construct(table, *
this);
106 allocator.deallocate(table, 1);
114 template <
template <
typename >
class ALLOC >
115 VariableLog2ParamComplexity< ALLOC >*
122 template <
template <
typename >
class ALLOC >
129 template <
template <
typename >
class ALLOC >
130 INLINE VariableLog2ParamComplexity< ALLOC >&
132 operator=(
const VariableLog2ParamComplexity< ALLOC >& from) {
138 template <
template <
typename >
class ALLOC >
139 INLINE VariableLog2ParamComplexity< ALLOC >&
141 operator=(VariableLog2ParamComplexity< ALLOC >&& from) {
147 template <
template <
typename >
class ALLOC >
154 template <
template <
typename >
class ALLOC >
161 template <
template <
typename >
class ALLOC >
167 if (r == std::size_t(1))
return 0.0;
168 if (n == 0.0)
return 0.0;
169 if (n == 1.0)
return std::log2((
double)r);
173 "In the penalty of the fNML score, n must be greater " 174 <<
"than or equal to 0. But, here, n = " << n);
180 std::size_t xn = (std::size_t)n;
187 return __cache[std::pair< std::size_t, double >{r, n}];
188 }
catch (NotFound&) {}
210 double k_r = std::exp((log2Cnr2 - log2Cnr1) *
M_LN2);
211 double q_r = 1.0 + k_r * n / (6.0 - 2.0);
212 for (std::size_t i = std::size_t(6); i <= r; ++i) {
213 log2Cnr = log2Cnr1 + std::log2(q_r);
216 q_r = 1.0 + k_r * (n / (i - 1.0));
230 return __cache[std::pair< std::size_t, double >{r, n}];
231 }
catch (NotFound&) {}
238 if (r == std::size_t(2))
return log2Cnr1;
241 double log2Cnr2 = 0.0;
245 double k_r = std::exp((log2Cnr2 - log2Cnr1) *
M_LN2);
246 double q_r = 1.0 + k_r * n / (3.0 - 2.0);
247 double log2Cnr = 0.0;
248 for (std::size_t i = std::size_t(3); i <= r; ++i) {
249 log2Cnr = log2Cnr1 + std::log2(q_r);
252 q_r = 1.0 + k_r * (n / (i - 1.0));
266 template <
template <
typename >
class ALLOC >
276 GammaLog2 gamma_log2;
285 for (
double h = 1; h < n; ++h) {
287 (gamma_log2(n + 1) - gamma_log2(h + 1) - gamma_log2((n - h) + 1))
289 + h * std::log(h / n) + (n - h) * std::log((n - h) / n);
290 cn2 += std::exp(elt);
298 std::ofstream outfile(filename);
299 if (!outfile.is_open()) {
300 GUM_ERROR(IOError,
"It is impossible to open file " << filename);
302 outfile.precision(20);
303 outfile <<
"namespace gum {\n\n";
312 outfile <<
" // the CTable cache for log2(Cnr), n < " 313 << VariableLog2ParamComplexityCTableNSize <<
" and r in {2,3,4,5}\n";
314 outfile <<
" const double VariableLog2ParamComplexityCTable[4][" 315 << VariableLog2ParamComplexityCTableNSize <<
"] = {\n";
320 for (
const auto cn2 : cn2_table) {
325 const double logCn2 = (
double)std::log2(cn2);
332 for (std::size_t i = std::size_t(0);
335 if (i > std::size_t(0)) outfile <<
",\n ";
336 const double logCn3 = (
double)std::log2(cn2_table[i] + i);
343 for (std::size_t i = std::size_t(0);
346 if (i > std::size_t(0)) outfile <<
",\n ";
347 const double logCn4 = (
double)std::log2(cn2_table[i] * (1.0 + i / 2.0) + i);
354 for (std::size_t i = std::size_t(0);
357 if (i > std::size_t(0)) outfile <<
",\n ";
358 const double logCn5 =
359 (
double)std::log2(cn2_table[i] * (1.0 + 5.0 * i / 6.0) + i + i * i / 3.0);
365 outfile <<
" };\n\n";
366 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
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
constexpr std::size_t VariableLog2ParamComplexityCTableRSize
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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)