Merge branch 'master' of git.tuebingen.mpg.de:libdai
[libdai.git] / include / dai / util.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2010 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 /// \file
13 /// \brief Defines general utility functions and adds an abstraction layer for platform-dependent functionality
14
15
16 #ifndef __defined_libdai_util_h
17 #define __defined_libdai_util_h
18
19
20 #include <string>
21 #include <vector>
22 #include <set>
23 #include <map>
24 #include <iostream>
25 #include <boost/foreach.hpp>
26 #include <boost/functional/hash.hpp>
27 #include <algorithm>
28 #include <cerrno>
29
30 #include <dai/exceptions.h>
31
32
33 #if defined(WINDOWS)
34 #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
35 #elif defined(CYGWIN)
36 #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
37 #elif defined(MACOSX)
38 #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
39 #else
40 #include <tr1/unordered_map> // only present in modern GCC distributions
41 #endif
42
43
44 /// An alias to the BOOST_FOREACH macro from the boost::foreach library
45 #define foreach BOOST_FOREACH
46
47 #ifdef DAI_DEBUG
48 /// \brief "Print variable". Prints the text of an expression, followed by its value (only if DAI_DEBUG is defined)
49 /**
50 * Useful debugging macro to see what your code is doing.
51 * Example: \code DAI_PV(3+4) \endcode
52 * Output: \code 3+4= 7 \endcode
53 */
54 #define DAI_PV(x) do {std::cerr << #x "= " << (x) << std::endl;} while(0)
55 /// "Debugging message": Prints a message (only if DAI_DEBUG is defined)
56 #define DAI_DMSG(str) do {std::cerr << str << std::endl;} while(0)
57 #else
58 #define DAI_PV(x) do {} while(0)
59 #define DAI_DMSG(str) do {} while(0)
60 #endif
61
62 /// Macro to write message \a stmt to \c std::cerr if \a props.verbose >= \a n
63 #define DAI_IFVERB(n, stmt) if(props.verbose>=n) { std::cerr << stmt; }
64
65
66 #ifdef CYGWIN
67 /// Returns true if argument is NAN (Not A Number)
68 bool isnan( double x );
69 #endif
70
71
72 #ifdef WINDOWS
73 /// Returns true if argument is NAN (Not A Number)
74 bool isnan( double x );
75
76 /// Returns inverse hyperbolic tangent of argument
77 double atanh( double x );
78
79 /// Returns log(1+x)
80 double log1p( double x );
81
82 /// Define INFINITY
83 #define INFINITY (std::numeric_limits<Real>::infinity())
84 #endif
85
86
87 namespace dai {
88
89
90 /// Real number (alias for \c double, which could be changed to <tt>long double</tt> if necessary)
91 typedef double Real;
92
93 /// Returns logarithm of \a x
94 inline Real log( Real x ) {
95 return std::log(x);
96 }
97
98 /// Returns logarithm of \a x, or 0 if \a x == 0
99 inline Real log0( Real x ) {
100 return x ? std::log(x) : 0;
101 }
102
103 /// Returns exponent of \a x
104 inline Real exp( Real x ) {
105 return std::exp(x);
106 }
107
108 /// Returns \a to the power \a y
109 inline Real pow( Real x, Real y ) {
110 errno = 0;
111 Real result = std::pow(x, y);
112 DAI_DEBASSERT( errno == 0 );
113 return result;
114 }
115
116
117 /// hash_map is an alias for \c std::tr1::unordered_map.
118 /** We use the (experimental) TR1 unordered_map implementation included in modern GCC distributions or in boost versions 1.37 and higher.
119 */
120 template <typename T, typename U, typename H = boost::hash<T> >
121 class hash_map : public std::tr1::unordered_map<T,U,H> {};
122
123
124 /// Returns wall clock time in seconds
125 double toc();
126
127
128 /// Returns absolute value of \a t
129 template<class T>
130 inline T abs( const T &t ) {
131 return (t < 0) ? (-t) : t;
132 }
133
134
135 /// Sets the random seed
136 void rnd_seed( size_t seed );
137
138 /// Returns a real number, distributed uniformly on [0,1)
139 Real rnd_uniform();
140
141 /// Returns a real number from a standard-normal distribution
142 Real rnd_stdnormal();
143
144 /// Returns a random integer in interval [\a min, \a max]
145 int rnd_int( int min, int max );
146
147 /// Returns a random integer in the half-open interval [0, \a n)
148 inline int rnd( int n ) {
149 return rnd_int( 0, n-1 );
150 }
151
152
153 /// Writes a \c std::vector<> to a \c std::ostream
154 template<class T>
155 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
156 os << "(";
157 for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
158 os << (it != x.begin() ? ", " : "") << *it;
159 os << ")";
160 return os;
161 }
162
163 /// Writes a \c std::set<> to a \c std::ostream
164 template<class T>
165 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
166 os << "{";
167 for( typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
168 os << (it != x.begin() ? ", " : "") << *it;
169 os << "}";
170 return os;
171 }
172
173 /// Writes a \c std::map<> to a \c std::ostream
174 template<class T1, class T2>
175 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
176 os << "{";
177 for( typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
178 os << (it != x.begin() ? ", " : "") << it->first << "->" << it->second;
179 os << "}";
180 return os;
181 }
182
183 /// Writes a \c std::pair<> to a \c std::ostream
184 template<class T1, class T2>
185 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
186 os << "(" << x.first << ", " << x.second << ")";
187 return os;
188 }
189
190 /// Concatenates two vectors
191 template<class T>
192 std::vector<T> concat( const std::vector<T>& u, const std::vector<T>& v ) {
193 std::vector<T> w;
194 w.reserve( u.size() + v.size() );
195 for( size_t i = 0; i < u.size(); i++ )
196 w.push_back( u[i] );
197 for( size_t i = 0; i < v.size(); i++ )
198 w.push_back( v[i] );
199 return w;
200 }
201
202 /// Split a string into tokens delimited by one of the characters in \a delim
203 void tokenizeString( const std::string& s, std::vector<std::string>& outTokens, const std::string& delim="\t\n" );
204
205
206 /// Enumerates different ways of normalizing a probability measure.
207 /**
208 * - NORMPROB means that the sum of all entries should be 1;
209 * - NORMLINF means that the maximum absolute value of all entries should be 1.
210 */
211 typedef enum { NORMPROB, NORMLINF } ProbNormType;
212
213 /// Enumerates different distance measures between probability measures.
214 /**
215 * - DISTL1 is the \f$\ell_1\f$ distance (sum of absolute values of pointwise difference);
216 * - DISTLINF is the \f$\ell_\infty\f$ distance (maximum absolute value of pointwise difference);
217 * - DISTTV is the total variation distance (half of the \f$\ell_1\f$ distance);
218 * - DISTKL is the Kullback-Leibler distance (\f$\sum_i p_i (\log p_i - \log q_i)\f$).
219 * - DISTHEL is the Hellinger distance (\f$\frac{1}{2}\sum_i (\sqrt{p_i}-\sqrt{q_i})^2\f$).
220 */
221 typedef enum { DISTL1, DISTLINF, DISTTV, DISTKL, DISTHEL } ProbDistType;
222
223
224 } // end of namespace dai
225
226
227 #endif