fb34f6bd81ff0fa8b3089906669fe690e3fde276
1 /* This file is part of libDAI - http://www.libdai.org/
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.
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
13 /// \brief Defines general utility functions and adds an abstraction layer for platform-dependent functionality
16 #ifndef __defined_libdai_util_h
17 #define __defined_libdai_util_h
25 #include <boost/foreach.hpp>
26 #include <boost/functional/hash.hpp>
31 #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
33 #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
35 #include <boost/tr1/unordered_map.hpp> // only present in boost 1.37 and higher
37 #include <tr1/unordered_map> // only present in modern GCC distributions
41 /// An alias to the BOOST_FOREACH macro from the boost::foreach library
42 #define foreach BOOST_FOREACH
45 /// \brief "Print variable". Prints the text of an expression, followed by its value (only if DAI_DEBUG is defined)
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
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)
55 #define DAI_PV(x) do {} while(0)
56 #define DAI_DMSG(str) do {} while(0)
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; }
64 /// Returns true if argument is NAN (Not A Number)
65 bool isnan( double x
);
67 /// Returns inverse hyperbolic tangent of argument
68 double atanh( double x
);
71 double log1p( double x
);
74 #define INFINITY (std::numeric_limits<Real>::infinity())
81 /// Real number (alias for \c double, which could be changed to <tt>long double</tt> if necessary)
84 /// Returns logarithm of \a x
85 inline Real
log( Real x
) {
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;
94 /// Returns exponent of \a x
95 inline Real
exp( Real x
) {
99 /// Returns maximum value of a std::vector<Real>
100 Real
max( const std::vector
<Real
> &v
);
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.
106 template <typename T
, typename U
, typename H
= boost::hash
<T
> >
107 class hash_map
: public std::tr1::unordered_map
<T
,U
,H
> {};
110 /// Returns wall clock time in seconds
114 /// Returns absolute value of \a t
116 inline T
abs( const T
&t
) {
117 return (t
< 0) ? (-t
) : t
;
121 /// Sets the random seed
122 void rnd_seed( size_t seed
);
124 /// Returns a real number, distributed uniformly on [0,1)
127 /// Returns a real number from a standard-normal distribution
128 Real
rnd_stdnormal();
130 /// Returns a random integer in interval [\a min, \a max]
131 int rnd_int( int min
, int max
);
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 );
139 /// Writes a \c std::vector<> to a \c std::ostream
141 std::ostream
& operator << (std::ostream
& os
, const std::vector
<T
> & x
) {
143 for( typename
std::vector
<T
>::const_iterator it
= x
.begin(); it
!= x
.end(); it
++ )
144 os
<< (it
!= x
.begin() ? ", " : "") << *it
;
149 /// Writes a \c std::set<> to a \c std::ostream
151 std::ostream
& operator << (std::ostream
& os
, const std::set
<T
> & x
) {
153 for( typename
std::set
<T
>::const_iterator it
= x
.begin(); it
!= x
.end(); it
++ )
154 os
<< (it
!= x
.begin() ? ", " : "") << *it
;
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
) {
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
;
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
<< ")";
176 /// Concatenates two vectors
178 std::vector
<T
> concat( const std::vector
<T
>& u
, const std::vector
<T
>& v
) {
180 w
.reserve( u
.size() + v
.size() );
181 for( size_t i
= 0; i
< u
.size(); i
++ )
183 for( size_t i
= 0; i
< v
.size(); i
++ )
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" );
192 } // end of namespace dai