Miscellaneous changes:
[libdai.git] / include / dai / util.h
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 #ifndef __defined_libdai_util_h
23 #define __defined_libdai_util_h
24
25
26 #include <vector>
27 #include <set>
28 #include <map>
29 #include <iostream>
30 #include <cstdio>
31 #include <boost/foreach.hpp>
32
33 #ifdef WINDOWS
34 #include <float.h> // for _isnan (?)
35 #include <boost/math/special_functions/atanh.hpp> // for atanh
36 #include <boost/math/special_functions/log1p.hpp> // for log1p
37 // #include <xmath.h>
38 #endif
39
40
41 #define foreach BOOST_FOREACH
42
43
44 namespace dai {
45
46
47 #ifdef WINDOWS
48 bool isnan( double x ) inline { return _isnan( x ); }
49 using boost::math:atanh;
50 using boost::math::log1p;
51 #endif
52
53
54 double toc();
55 void rnd_seed( size_t seed );
56 double rnd_uniform();
57 double rnd_stdnormal();
58 int rnd_int( int min, int max );
59
60
61 // Output a std::vector
62 template<class T>
63 std::ostream& operator << (std::ostream& os, const std::vector<T> & x) {
64 os << "(";
65 for( typename std::vector<T>::const_iterator it = x.begin(); it != x.end(); it++ )
66 os << (it != x.begin() ? ", " : "") << *it;
67 os << ")";
68 return os;
69 }
70
71
72 // Output a std::set
73 template<class T>
74 std::ostream& operator << (std::ostream& os, const std::set<T> & x) {
75 os << "{";
76 for( typename std::set<T>::const_iterator it = x.begin(); it != x.end(); it++ )
77 os << (it != x.begin() ? ", " : "") << *it;
78 os << "}";
79 return os;
80 }
81
82
83 /// Output a std::map
84 template<class T1, class T2>
85 std::ostream& operator << (std::ostream& os, const std::map<T1,T2> & x) {
86 os << "{";
87 for( typename std::map<T1,T2>::const_iterator it = x.begin(); it != x.end(); it++ )
88 os << (it != x.begin() ? ", " : "") << it->first << "->" << it->second;
89 os << "}";
90 return os;
91 }
92
93
94 /// Output a std::pair
95 template<class T1, class T2>
96 std::ostream& operator << (std::ostream& os, const std::pair<T1,T2> & x) {
97 os << "(" << x.first << ", " << x.second << ")";
98 return os;
99 }
100
101
102 } // end of namespace dai
103
104
105 #endif