Merge branch 'vaske'
[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 <algorithm>
39
40
41 #if defined(WINDOWS)
42 #include <map>
43 #elif defined(CYGWIN)
44 #include <boost/tr1/unordered_map.hpp>
45 #else
46 #include <tr1/unordered_map>
47 #endif
48
49
50 /// An alias to the BOOST_FOREACH macro from the boost::foreach library
51 #define foreach BOOST_FOREACH
52
53
54 /// Real number (alias for double, which could be changed to long double if necessary)
55 typedef double Real;
56
57
58 #ifdef WINDOWS
59 /// Returns true if argument is NAN (Not A Number)
60 bool isnan( double x );
61
62 /// Returns inverse hyperbolic tangent of argument
63 double atanh( double x );
64
65 /// Returns log(1+x)
66 double log1p( double x );
67
68 /// Define INFINITY
69 #define INFINITY (std::numeric_limits<double>::infinity())
70 #endif
71
72
73 namespace dai {
74
75
76 #ifdef WINDOWS
77 /// hash_map is an alias for std::map.
78 /** Since there is no TR1 unordered_map implementation available yet, we fall back on std::map.
79 */
80 template <typename T, typename U>
81 class hash_map : public std::map<T,U> {};
82 #else
83 /// hash_map is an alias for std::tr1::unordered_map.
84 /** We use the (experimental) TR1 unordered_map implementation included in modern GCC distributions.
85 */
86 template <typename T, typename U>
87 class hash_map : public std::tr1::unordered_map<T,U> {};
88 #endif
89
90
91 /// Returns wall clock time in seconds
92 double toc();
93
94
95 /// Sets the random seed
96 void rnd_seed( size_t seed );
97
98 /// Returns a real number, distributed uniformly on [0,1)
99 double rnd_uniform();
100
101 /// Returns a real number from a standard-normal distribution
102 double rnd_stdnormal();
103
104 /// Returns a random integer in interval [min, max]
105 int rnd_int( int min, int max );
106
107
108 /// Writes a std::vector to a std::ostream
109 template<class T>
110 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
111 os << "(";
112 for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
113 os << (it != x.begin() ? ", " : "") << *it;
114 os << ")";
115 return os;
116 }
117
118 /// Writes a std::set to a std::ostream
119 template<class T>
120 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
121 os << "{";
122 for( typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
123 os << (it != x.begin() ? ", " : "") << *it;
124 os << "}";
125 return os;
126 }
127
128 /// Writes a std::map to a std::ostream
129 template<class T1, class T2>
130 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
131 os << "{";
132 for( typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
133 os << (it != x.begin() ? ", " : "") << it->first << "->" << it->second;
134 os << "}";
135 return os;
136 }
137
138 /// Writes a std::pair to a std::ostream
139 template<class T1, class T2>
140 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
141 os << "(" << x.first << ", " << x.second << ")";
142 return os;
143 }
144
145
146 /// Used to keep track of the progress made by iterative algorithms
147 class Diffs : public std::vector<double> {
148 private:
149 size_t _maxsize;
150 double _def;
151 std::vector<double>::iterator _pos;
152 std::vector<double>::iterator _maxpos;
153 public:
154 /// Constructor
155 Diffs(long maxsize, double def) : std::vector<double>(), _maxsize(maxsize), _def(def) {
156 this->reserve(_maxsize);
157 _pos = begin();
158 _maxpos = begin();
159 }
160 /// Returns maximum difference encountered
161 double maxDiff() {
162 if( size() < _maxsize )
163 return _def;
164 else
165 return( *_maxpos );
166 }
167 /// Register new difference x
168 void push(double x) {
169 if( size() < _maxsize ) {
170 push_back(x);
171 _pos = end();
172 if( size() > 1 ) {
173 if( *_maxpos < back() ) {
174 _maxpos = end();
175 _maxpos--;
176 }
177 } else {
178 _maxpos = begin();
179 }
180 }
181 else {
182 if( _pos == end() )
183 _pos = begin();
184 if( _maxpos == _pos ) {
185 *_pos++ = x;
186 _maxpos = max_element(begin(),end());
187 } else {
188 if( x > *_maxpos )
189 _maxpos = _pos;
190 *_pos++ = x;
191 }
192 }
193 }
194 /// Return maximum number of differences stored
195 size_t maxSize() { return _maxsize; }
196 };
197
198
199 /// Split a string into tokens
200 void tokenizeString( const std::string& s,
201 std::vector<std::string>& outTokens,
202 const std::string& delim="\t\n" );
203
204
205 } // end of namespace dai
206
207
208 #endif