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