Moved platform independent build options into Makefile.ALL and documented tests/testdai
[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
29
30 #if defined(WINDOWS)
31 #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
32 #elif defined(CYGWIN)
33 #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
34 #elif defined(MACOSX)
35 #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
36 #else
37 #include <tr1/unordered_map> // only present in modern GCC distributions
38 #endif
39
40
41 /// An alias to the BOOST_FOREACH macro from the boost::foreach library
42 #define foreach BOOST_FOREACH
43
44 #ifdef DAI_DEBUG
45 /// \brief "Print variable". Prints the text of an expression, followed by its value (only if DAI_DEBUG is defined)
46 /**
47 * Useful debugging macro to see what your code is doing.
48 * Example: \code DAI_PV(3+4) \endcode
49 * Output: \code 3+4= 7 \endcode
50 */
51 #define DAI_PV(x) do {std::cerr << #x "= " << (x) << std::endl;} while(0)
52 /// "Debugging message": Prints a message (only if DAI_DEBUG is defined)
53 #define DAI_DMSG(str) do {std::cerr << str << std::endl;} while(0)
54 #else
55 #define DAI_PV(x) do {} while(0)
56 #define DAI_DMSG(str) do {} while(0)
57 #endif
58
59 /// Macro to write message \a stmt to \c std::cerr if \a props.verbose >= \a n
60 #define DAI_IFVERB(n, stmt) if(props.verbose>=n) { std::cerr << stmt; }
61
62
63 #ifdef WINDOWS
64 /// Returns true if argument is NAN (Not A Number)
65 bool isnan( double x );
66
67 /// Returns inverse hyperbolic tangent of argument
68 double atanh( double x );
69
70 /// Returns log(1+x)
71 double log1p( double x );
72
73 /// Define INFINITY
74 #define INFINITY (std::numeric_limits<Real>::infinity())
75 #endif
76
77
78 namespace dai {
79
80
81 /// Real number (alias for \c double, which could be changed to <tt>long double</tt> if necessary)
82 typedef double Real;
83
84 /// Returns logarithm of \a x
85 inline Real log( Real x ) {
86 return std::log(x);
87 }
88
89 /// Returns logarithm of \a x, or 0 if \a x == 0
90 inline Real log0( Real x ) {
91 return x ? std::log(x) : 0;
92 }
93
94 /// Returns exponent of \a x
95 inline Real exp( Real x ) {
96 return std::exp(x);
97 }
98
99 /// Returns maximum value of a std::vector<Real>
100 Real max( const std::vector<Real> &v );
101
102
103 /// hash_map is an alias for \c std::tr1::unordered_map.
104 /** We use the (experimental) TR1 unordered_map implementation included in modern GCC distributions or in boost versions 1.37 and higher.
105 */
106 template <typename T, typename U, typename H = boost::hash<T> >
107 class hash_map : public std::tr1::unordered_map<T,U,H> {};
108
109
110 /// Returns wall clock time in seconds
111 double toc();
112
113
114 /// Returns absolute value of \a t
115 template<class T>
116 inline T abs( const T &t ) {
117 return (t < 0) ? (-t) : t;
118 }
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 Real rnd_uniform();
126
127 /// Returns a real number from a standard-normal distribution
128 Real rnd_stdnormal();
129
130 /// Returns a random integer in interval [\a min, \a max]
131 int rnd_int( int min, int max );
132
133 /// Returns a random integer in the half-open interval [0, \a n)
134 inline int rnd( int n ) {
135 return rnd_int( 0, n-1 );
136 }
137
138
139 /// Writes a \c std::vector<> to a \c std::ostream
140 template<class T>
141 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
142 os << "(";
143 for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
144 os << (it != x.begin() ? ", " : "") << *it;
145 os << ")";
146 return os;
147 }
148
149 /// Writes a \c std::set<> to a \c std::ostream
150 template<class T>
151 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
152 os << "{";
153 for( typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
154 os << (it != x.begin() ? ", " : "") << *it;
155 os << "}";
156 return os;
157 }
158
159 /// Writes a \c std::map<> to a \c std::ostream
160 template<class T1, class T2>
161 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
162 os << "{";
163 for( typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
164 os << (it != x.begin() ? ", " : "") << it->first << "->" << it->second;
165 os << "}";
166 return os;
167 }
168
169 /// Writes a \c std::pair<> to a \c std::ostream
170 template<class T1, class T2>
171 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
172 os << "(" << x.first << ", " << x.second << ")";
173 return os;
174 }
175
176 /// Concatenates two vectors
177 template<class T>
178 std::vector<T> concat( const std::vector<T>& u, const std::vector<T>& v ) {
179 std::vector<T> w;
180 w.reserve( u.size() + v.size() );
181 for( size_t i = 0; i < u.size(); i++ )
182 w.push_back( u[i] );
183 for( size_t i = 0; i < v.size(); i++ )
184 w.push_back( v[i] );
185 return w;
186 }
187
188 /// Split a string into tokens delimited by one of the characters in \a delim
189 void tokenizeString( const std::string& s, std::vector<std::string>& outTokens, const std::string& delim="\t\n" );
190
191
192 } // end of namespace dai
193
194
195 #endif