a45d27433bd28d21111976353745e6168952ad5c
[libdai.git] / include / dai / util.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 /// \file
24 /// \brief Defines general utility functions and adds an abstraction layer for platform-dependent functionality
25 /// \todo Improve documentation
26
27
28 #ifndef __defined_libdai_util_h
29 #define __defined_libdai_util_h
30
31
32 #include <vector>
33 #include <set>
34 #include <map>
35 #include <iostream>
36 #include <cstdio>
37 #include <boost/foreach.hpp>
38 #include <boost/functional/hash.hpp>
39 #include <algorithm>
40
41
42 #if defined(WINDOWS)
43 #include <map>
44 #elif defined(CYGWIN)
45 #include <boost/tr1/unordered_map.hpp>
46 #else
47 #include <tr1/unordered_map>
48 #endif
49
50
51 /// An alias to the BOOST_FOREACH macro from the boost::foreach library
52 #define foreach BOOST_FOREACH
53
54 #ifdef DAI_DEBUG
55 /// \brief "Print variable". Prints the text of an expression, followed by its value (only if DAI_DEBUG is defined)
56 /**
57 * Useful debugging macro to see what your code is doing.
58 * Example: \code DAI_PV(3+4) \endcode
59 */ Output: \code 3+4= 7 \endcode
60 #define DAI_PV(x) do {std::cerr << #x "= " << (x) << std::endl;} while(0)
61 /// "Debugging message": Prints a message (only if DAI_DEBUG is defined)
62 #define DAI_DMSG(str) do {std::cerr << str << std::endl;} while(0)
63 #else
64 #define DAI_PV(x) do {} while(0)
65 #define DAI_DMSG(str) do {} while(0)
66 #endif
67
68 /// Produces accessor and mutator methods according to the common pattern.
69 /** Example:
70 * \code DAI_ACCMUT(size_t& maxIter(), { return props.maxiter; }); \endcode
71 * \todo At the moment, only the mutator appears in doxygen documentation.
72 #define DAI_ACCMUT(x,y) \
73 x y; \
74 const x const y;
75
76 /// Macro to give error message \a stmt if props.verbose>=\a n
77 #define DAI_IFVERB(n, stmt) if(props.verbose>=n) { cerr << stmt; }
78
79
80 /// Real number (alias for double, which could be changed to long double if necessary)
81 typedef double Real;
82
83
84 #ifdef WINDOWS
85 /// Returns true if argument is NAN (Not A Number)
86 bool isnan( double x );
87
88 /// Returns inverse hyperbolic tangent of argument
89 double atanh( double x );
90
91 /// Returns log(1+x)
92 double log1p( double x );
93
94 /// Define INFINITY
95 #define INFINITY (std::numeric_limits<double>::infinity())
96 #endif
97
98
99 namespace dai {
100
101
102 #ifdef WINDOWS
103 /// hash_map is an alias for std::map.
104 /** Since there is no TR1 unordered_map implementation available yet, we fall back on std::map.
105 */
106 template <typename T, typename U, typename H = boost::hash<T> >
107 class hash_map : public std::map<T,U> {};
108 #else
109 /// hash_map is an alias for std::tr1::unordered_map.
110 /** We use the (experimental) TR1 unordered_map implementation included in modern GCC distributions.
111 */
112 template <typename T, typename U, typename H = boost::hash<T> >
113 class hash_map : public std::tr1::unordered_map<T,U,H> {};
114 #endif
115
116
117 /// Returns wall clock time in seconds
118 double toc();
119
120
121 /// Sets the random seed
122 void rnd_seed( size_t seed );
123
124 /// Returns a real number, distributed uniformly on [0,1)
125 double rnd_uniform();
126
127 /// Returns a real number from a standard-normal distribution
128 double rnd_stdnormal();
129
130 /// Returns a random integer in interval [min, max]
131 int rnd_int( int min, int max );
132
133
134 /// Writes a std::vector to a std::ostream
135 template<class T>
136 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
137 os << "(";
138 for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
139 os << (it != x.begin() ? ", " : "") << *it;
140 os << ")";
141 return os;
142 }
143
144 /// Writes a std::set to a std::ostream
145 template<class T>
146 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
147 os << "{";
148 for( typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
149 os << (it != x.begin() ? ", " : "") << *it;
150 os << "}";
151 return os;
152 }
153
154 /// Writes a std::map to a std::ostream
155 template<class T1, class T2>
156 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
157 os << "{";
158 for( typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
159 os << (it != x.begin() ? ", " : "") << it->first << "->" << it->second;
160 os << "}";
161 return os;
162 }
163
164 /// Writes a std::pair to a std::ostream
165 template<class T1, class T2>
166 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
167 os << "(" << x.first << ", " << x.second << ")";
168 return os;
169 }
170
171 /// Concatenate two vectors
172 template<class T>
173 std::vector<T> concata (const std::vector<T>& u, const std::vector<T>& v ) {
174 std::vector<T> w;
175 w.reserve( u.size() + v.size() );
176 for( size_t i = 0; i < u.size(); i++ )
177 w.push_back( u[i] );
178 for( size_t i = 0; i < v.size(); i++ )
179 w.push_back( v[i] );
180 return w;
181 }
182
183 /// Used to keep track of the progress made by iterative algorithms
184 class Diffs : public std::vector<double> {
185 private:
186 size_t _maxsize;
187 double _def;
188 std::vector<double>::iterator _pos;
189 std::vector<double>::iterator _maxpos;
190 public:
191 /// Constructor
192 Diffs(long maxsize, double def) : std::vector<double>(), _maxsize(maxsize), _def(def) {
193 this->reserve(_maxsize);
194 _pos = begin();
195 _maxpos = begin();
196 }
197 /// Returns maximum difference encountered
198 double maxDiff() {
199 if( size() < _maxsize )
200 return _def;
201 else
202 return( *_maxpos );
203 }
204 /// Register new difference x
205 void push(double x) {
206 if( size() < _maxsize ) {
207 push_back(x);
208 _pos = end();
209 if( size() > 1 ) {
210 if( *_maxpos < back() ) {
211 _maxpos = end();
212 _maxpos--;
213 }
214 } else {
215 _maxpos = begin();
216 }
217 }
218 else {
219 if( _pos == end() )
220 _pos = begin();
221 if( _maxpos == _pos ) {
222 *_pos++ = x;
223 _maxpos = max_element(begin(),end());
224 } else {
225 if( x > *_maxpos )
226 _maxpos = _pos;
227 *_pos++ = x;
228 }
229 }
230 }
231 /// Return maximum number of differences stored
232 size_t maxSize() { return _maxsize; }
233 };
234
235
236 } // end of namespace dai
237
238
239 #endif