libDAI
util.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6  */
7 
8 
11 
12 
13 #ifndef __defined_libdai_util_h
14 #define __defined_libdai_util_h
15 
16 
17 #include <string>
18 #include <vector>
19 #include <set>
20 #include <map>
21 #include <iostream>
22 #include <boost/foreach.hpp>
23 #include <boost/functional/hash.hpp>
24 #include <boost/lexical_cast.hpp>
25 #include <algorithm>
26 #include <cerrno>
27 
28 #if defined(WINDOWS)
29 #include <cstdint> // only defined in C++11 and higher, but needed for Win64 builds in order to enable conditional code in MPIR library
30 #endif
31 #include <gmpxx.h>
32 
33 #include <dai/exceptions.h>
34 
35 
36 #if defined(WINDOWS)
37  #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
38 #elif defined(CYGWIN)
39  #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
40 #elif defined(MACOSX)
41  #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
42 #else
43  #include <tr1/unordered_map> // only present in modern GCC distributions
44 #endif
45 
46 
48 #define bforeach BOOST_FOREACH
49 
50 #ifdef DAI_DEBUG
51 
52 
57 #define DAI_PV(x) do {std::cerr << #x "= " << (x) << std::endl;} while(0)
58 
59 #define DAI_DMSG(str) do {std::cerr << str << std::endl;} while(0)
60 #else
61 #define DAI_PV(x) do {} while(0)
62 #define DAI_DMSG(str) do {} while(0)
63 #endif
64 
66 #define DAI_IFVERB(n, stmt) if(props.verbose>=n) { std::cerr << stmt; }
67 
68 
69 #ifdef WINDOWS
70 
71  double atanh( double x );
72 
74  double log1p( double x );
75 
77  #define INFINITY (std::numeric_limits<Real>::infinity())
78 
80  #define NAN (std::numeric_limits<Real>::quiet_NaN())
81 
82  #if defined(_MSC_VER)
83  // Disable unsafe warning (use of the function 'strcpy' instead of
84  // 'strcpy_s' for portability reasons;
85  #pragma warning( disable : 4996 )
86  // Workaround for the char16_t type defined in Matlab and MSVC 2010
87  #if (_MSC_VER >= 1600)
88  #define __STDC_UTF_16__
89  #endif
90  #endif
91 #endif
92 
93 
94 namespace dai {
95 
96 
98 typedef double Real;
99 
101 typedef mpz_class BigInt;
102 
104 inline size_t BigInt_size_t( const BigInt &N ) {
105  DAI_ASSERT( N <= (BigInt)std::numeric_limits<std::size_t>::max() );
106  return N.get_ui();
107 }
108 
110 bool isnan( Real x );
111 
113 inline Real log( Real x ) {
114  return std::log(x);
115 }
116 
118 inline Real log0( Real x ) {
119  return x ? std::log(x) : 0;
120 }
121 
123 inline Real exp( Real x ) {
124  return std::exp(x);
125 }
126 
128 inline Real pow( Real x, Real y ) {
129  errno = 0;
130  Real result = std::pow(x, y);
131  DAI_DEBASSERT( errno == 0 );
132  return result;
133 }
134 
135 
137 
139 template <typename T, typename U, typename H = boost::hash<T> >
140  class hash_map : public std::tr1::unordered_map<T,U,H> {};
141 
142 
144 double toc();
145 
146 
148 template<class T>
149 inline T abs( const T &t ) {
150  return (t < 0) ? (-t) : t;
151 }
152 
153 
155 void rnd_seed( size_t seed );
156 
158 Real rnd_uniform();
159 
162 
164 int rnd_int( int min, int max );
165 
167 inline int rnd( int n ) {
168  return rnd_int( 0, n-1 );
169 }
170 
171 
173 template<class T>
174 std::string toString( const T& x ) {
175  return boost::lexical_cast<std::string>(x);
176 }
177 
178 
180 template<class T>
181 T fromString( const std::string& x ) {
182  return boost::lexical_cast<T>(x);
183 }
184 
185 
187 template<class T>
188 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
189  os << "(";
190  for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
191  os << (it != x.begin() ? ", " : "") << *it;
192  os << ")";
193  return os;
194 }
195 
197 template<class T>
198 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
199  os << "{";
200  for( typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
201  os << (it != x.begin() ? ", " : "") << *it;
202  os << "}";
203  return os;
204 }
205 
207 template<class T1, class T2>
208 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
209  os << "{";
210  for( typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
211  os << (it != x.begin() ? ", " : "") << it->first << "->" << it->second;
212  os << "}";
213  return os;
214 }
215 
217 template<class T1, class T2>
218 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
219  os << "(" << x.first << ", " << x.second << ")";
220  return os;
221 }
222 
224 template<class T>
225 std::vector<T> concat( const std::vector<T>& u, const std::vector<T>& v ) {
226  std::vector<T> w;
227  w.reserve( u.size() + v.size() );
228  for( size_t i = 0; i < u.size(); i++ )
229  w.push_back( u[i] );
230  for( size_t i = 0; i < v.size(); i++ )
231  w.push_back( v[i] );
232  return w;
233 }
234 
236 
241 std::vector<std::string> tokenizeString( const std::string& s, bool singleDelim, const std::string& delim="\t\n" );
242 
243 
245 
249 typedef enum { NORMPROB, NORMLINF } ProbNormType;
250 
252 
259 typedef enum { DISTL1, DISTLINF, DISTTV, DISTKL, DISTHEL } ProbDistType;
260 
261 
262 } // end of namespace dai
263 
264 
265 #endif