Merge branch 'pletscher'
[libdai.git] / src / util.cpp
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-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 #include <dai/util.h>
13 #include <boost/random/linear_congruential.hpp>
14 #include <boost/random/uniform_real.hpp>
15 #include <boost/random/normal_distribution.hpp>
16 #include <boost/random/variate_generator.hpp>
17
18 #ifdef WINDOWS
19 #include <windows.h>
20 #include <boost/math/special_functions/atanh.hpp> // for atanh
21 #include <boost/math/special_functions/log1p.hpp> // for log1p
22 #include <float.h> // for _isnan
23 #else
24 // Assume POSIX compliant system. We need the following for querying the system time
25 #include <sys/time.h>
26 #endif
27
28
29 #ifdef CYGWIN
30 bool isnan( double x ) {
31 return __isnand( x ); // isnan() is a macro in Cygwin (as required by C99)
32 }
33 #endif
34
35 #ifdef WINDOWS
36 bool isnan( double x ) {
37 return _isnan( x );
38 }
39 double atanh( double x ) {
40 return boost::math::atanh( x );
41 }
42 double log1p( double x ) {
43 return boost::math::log1p( x );
44 }
45 #endif
46
47
48 namespace dai {
49
50
51 // Returns user+system time in seconds
52 double toc() {
53 #ifdef WINDOWS
54 SYSTEMTIME tbuf;
55 GetSystemTime(&tbuf);
56 return( (double)(tbuf.wSecond + (double)tbuf.wMilliseconds / 1000.0) );
57 #else
58 struct timeval tv;
59 struct timezone tz;
60 gettimeofday( &tv, &tz );
61 return( (double)(tv.tv_sec + (double)tv.tv_usec / 1000000.0) );
62 #endif
63 }
64
65 /// Type of global random number generator
66 typedef boost::minstd_rand _rnd_gen_type; // Try boost::mt19937 or boost::ecuyer1988 instead of boost::minstd_rand
67
68 /// Global random number generator
69 _rnd_gen_type _rnd_gen(42U);
70
71 /// Uniform distribution with values between 0 and 1 (0 inclusive, 1 exclusive).
72 boost::uniform_real<> _uni_dist(0,1);
73
74 /// Global uniform random random number
75 boost::variate_generator<_rnd_gen_type&, boost::uniform_real<> > _uni_rnd(_rnd_gen, _uni_dist);
76
77 /// Normal distribution with mean 0 and standard deviation 1.
78 boost::normal_distribution<> _normal_dist;
79
80 /// Global random number generator with standard normal distribution
81 boost::variate_generator<_rnd_gen_type&, boost::normal_distribution<> > _normal_rnd(_rnd_gen, _normal_dist);
82
83
84 void rnd_seed( size_t seed ) {
85 _rnd_gen.seed(seed);
86 }
87
88 double rnd_uniform() {
89 return _uni_rnd();
90 }
91
92 double rnd_stdnormal() {
93 return _normal_rnd();
94 }
95
96 int rnd_int( int min, int max ) {
97 return (int)floor(_uni_rnd() * (max + 1 - min) + min);
98 }
99
100 void tokenizeString(const std::string& s, std::vector<std::string>& outTokens, const std::string& delim) {
101 size_t start = 0;
102 while (start < s.size()) {
103 size_t end = s.find_first_of(delim, start);
104 if (end > s.size())
105 end = s.size();
106 outTokens.push_back(s.substr(start, end - start));
107 start = end + 1;
108 }
109 }
110
111 } // end of namespace dai