Documentation improvement, interface changes and new example:
[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-2009 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 /// \todo Improve documentation
15
16
17 #ifndef __defined_libdai_util_h
18 #define __defined_libdai_util_h
19
20
21 #include <string>
22 #include <vector>
23 #include <set>
24 #include <map>
25 #include <iostream>
26 #include <boost/foreach.hpp>
27 #include <boost/functional/hash.hpp>
28 #include <algorithm>
29
30
31 #if defined(WINDOWS)
32 #include <map>
33 #elif defined(CYGWIN)
34 #include <boost/tr1/unordered_map.hpp>
35 #else
36 #include <tr1/unordered_map>
37 #endif
38
39
40 /// An alias to the BOOST_FOREACH macro from the boost::foreach library
41 #define foreach BOOST_FOREACH
42
43 #ifdef DAI_DEBUG
44 /// \brief "Print variable". Prints the text of an expression, followed by its value (only if DAI_DEBUG is defined)
45 /**
46 * Useful debugging macro to see what your code is doing.
47 * Example: \code DAI_PV(3+4) \endcode
48 * Output: \code 3+4= 7 \endcode
49 */
50 #define DAI_PV(x) do {std::cerr << #x "= " << (x) << std::endl;} while(0)
51 /// "Debugging message": Prints a message (only if DAI_DEBUG is defined)
52 #define DAI_DMSG(str) do {std::cerr << str << std::endl;} while(0)
53 #else
54 #define DAI_PV(x) do {} while(0)
55 #define DAI_DMSG(str) do {} while(0)
56 #endif
57
58 /// Produces accessor and mutator methods according to the common pattern.
59 /** Example:
60 * \code DAI_ACCMUT(size_t& maxIter(), { return props.maxiter; }); \endcode
61 * \todo At the moment, only the mutator appears in doxygen documentation.
62 */
63 #define DAI_ACCMUT(x,y) \
64 x y; \
65 const x const y;
66
67 /// Macro to give error message \a stmt if props.verbose>=\a n
68 #define DAI_IFVERB(n, stmt) if(props.verbose>=n) { cerr << stmt; }
69
70
71 #ifdef WINDOWS
72 /// Returns true if argument is NAN (Not A Number)
73 bool isnan( double x );
74
75 /// Returns inverse hyperbolic tangent of argument
76 double atanh( double x );
77
78 /// Returns log(1+x)
79 double log1p( double x );
80
81 /// Define INFINITY
82 #define INFINITY (std::numeric_limits<double>::infinity())
83 #endif
84
85
86 namespace dai {
87
88
89 /// Real number (alias for double, which could be changed to long double if necessary)
90 typedef double Real;
91
92 /// Returns logarithm of \a x
93 inline Real log( Real x ) {
94 return std::log(x);
95 }
96
97 /// Returns exponent of \a x
98 inline Real exp( Real x ) {
99 return std::exp(x);
100 }
101
102
103 #ifdef WINDOWS
104 /// hash_map is an alias for std::map.
105 /** Since there is no TR1 unordered_map implementation available yet, we fall back on std::map.
106 */
107 template <typename T, typename U, typename H = boost::hash<T> >
108 class hash_map : public std::map<T,U> {};
109 #else
110 /// hash_map is an alias for std::tr1::unordered_map.
111 /** We use the (experimental) TR1 unordered_map implementation included in modern GCC distributions.
112 */
113 template <typename T, typename U, typename H = boost::hash<T> >
114 class hash_map : public std::tr1::unordered_map<T,U,H> {};
115 #endif
116
117
118 /// Returns wall clock time in seconds
119 double toc();
120
121
122 /// Returns absolute value of \a t
123 template<class T>
124 inline T abs( const T &t ) {
125 return (t < 0) ? (-t) : t;
126 }
127
128
129 /// Sets the random seed
130 void rnd_seed( size_t seed );
131
132 /// Returns a real number, distributed uniformly on [0,1)
133 Real rnd_uniform();
134
135 /// Returns a real number from a standard-normal distribution
136 Real rnd_stdnormal();
137
138 /// Returns a random integer in interval [min, max]
139 int rnd_int( int min, int max );
140
141 /// Returns a random integer in the half-open interval \f$[0,n)\f$
142 inline int rnd( int n) {
143 return rnd_int( 0, n-1 );
144 }
145
146
147 /// Writes a std::vector to a std::ostream
148 template<class T>
149 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
150 os << "(";
151 for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
152 os << (it != x.begin() ? ", " : "") << *it;
153 os << ")";
154 return os;
155 }
156
157 /// Writes a std::set to a std::ostream
158 template<class T>
159 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
160 os << "{";
161 for( typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
162 os << (it != x.begin() ? ", " : "") << *it;
163 os << "}";
164 return os;
165 }
166
167 /// Writes a std::map to a std::ostream
168 template<class T1, class T2>
169 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
170 os << "{";
171 for( typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
172 os << (it != x.begin() ? ", " : "") << it->first << "->" << it->second;
173 os << "}";
174 return os;
175 }
176
177 /// Writes a std::pair to a std::ostream
178 template<class T1, class T2>
179 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
180 os << "(" << x.first << ", " << x.second << ")";
181 return os;
182 }
183
184 /// Concatenate two vectors
185 template<class T>
186 std::vector<T> concat( const std::vector<T>& u, const std::vector<T>& v ) {
187 std::vector<T> w;
188 w.reserve( u.size() + v.size() );
189 for( size_t i = 0; i < u.size(); i++ )
190 w.push_back( u[i] );
191 for( size_t i = 0; i < v.size(); i++ )
192 w.push_back( v[i] );
193 return w;
194 }
195
196 /// Split a string into tokens
197 void tokenizeString( const std::string& s, std::vector<std::string>& outTokens, const std::string& delim="\t\n" );
198
199 /// Used to keep track of the progress made by iterative algorithms
200 class Diffs : public std::vector<double> {
201 private:
202 size_t _maxsize;
203 double _def;
204 std::vector<double>::iterator _pos;
205 std::vector<double>::iterator _maxpos;
206 public:
207 /// Constructor
208 Diffs(long maxsize, double def) : std::vector<double>(), _maxsize(maxsize), _def(def) {
209 this->reserve(_maxsize);
210 _pos = begin();
211 _maxpos = begin();
212 }
213 /// Returns maximum difference encountered
214 double maxDiff() {
215 if( size() < _maxsize )
216 return _def;
217 else
218 return( *_maxpos );
219 }
220 /// Register new difference x
221 void push(double x) {
222 if( size() < _maxsize ) {
223 push_back(x);
224 _pos = end();
225 if( size() > 1 ) {
226 if( *_maxpos < back() ) {
227 _maxpos = end();
228 _maxpos--;
229 }
230 } else {
231 _maxpos = begin();
232 }
233 } else {
234 if( _pos == end() )
235 _pos = begin();
236 if( _maxpos == _pos ) {
237 *_pos++ = x;
238 _maxpos = max_element(begin(),end());
239 } else {
240 if( x > *_maxpos )
241 _maxpos = _pos;
242 *_pos++ = x;
243 }
244 }
245 }
246 /// Return maximum number of differences stored
247 size_t maxSize() { return _maxsize; }
248 };
249
250
251 } // end of namespace dai
252
253
254 #endif