Merge branch 'master' of git@git.tuebingen.mpg.de:libdai
[libdai.git] / src / util.cpp
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 #include <dai/util.h>
23 #include <boost/random/linear_congruential.hpp>
24 #include <boost/random/uniform_real.hpp>
25 #include <boost/random/normal_distribution.hpp>
26 #include <boost/random/variate_generator.hpp>
27
28 #ifdef WINDOWS
29 #include <windows.h>
30 #include <boost/math/special_functions/atanh.hpp> // for atanh
31 #include <boost/math/special_functions/log1p.hpp> // for log1p
32 #include <float.h> // for _isnan
33 #else
34 // Assume POSIX compliant system. We need the following for querying the CPU time for this process
35 #include <sys/times.h>
36 #include <sys/param.h>
37 #endif
38
39
40 #ifdef WINDOWS
41 bool isnan( double x ) {
42 return _isnan( x );
43 }
44 double atanh( double x ) {
45 return boost::math::atanh( x );
46 }
47 double log1p( double x ) {
48 return boost::math::log1p( x );
49 }
50 #endif
51
52
53 namespace dai {
54
55
56 // Returns user+system time in seconds
57 double toc() {
58 #ifdef WINDOWS
59 SYSTEMTIME tbuf;
60 GetSystemTime(&tbuf);
61 return( (double)(tbuf.wSecond + (double)tbuf.wMilliseconds / 1000.0) );
62 #else
63 tms tbuf;
64 times(&tbuf);
65 return( (double)(tbuf.tms_utime + tbuf.tms_stime) / HZ );
66 #endif
67 }
68
69 // This is a typedef for a random number generator.
70 // Try boost::mt19937 or boost::ecuyer1988 instead of boost::minstd_rand
71 typedef boost::minstd_rand _rnd_gen_type;
72
73 _rnd_gen_type _rnd_gen(42U);
74
75 // Define a uniform random number distribution which produces
76 // values between 0 and 1 (0 inclusive, 1 exclusive).
77 boost::uniform_real<> _uni_dist(0,1);
78 boost::variate_generator<_rnd_gen_type&, boost::uniform_real<> > _uni_rnd(_rnd_gen, _uni_dist);
79
80 // Define a normal distribution with mean 0 and standard deviation 1.
81 boost::normal_distribution<> _normal_dist;
82 boost::variate_generator<_rnd_gen_type&, boost::normal_distribution<> > _normal_rnd(_rnd_gen, _normal_dist);
83
84
85 void rnd_seed( size_t seed ) {
86 _rnd_gen.seed(seed);
87 }
88
89 double rnd_uniform() {
90 return _uni_rnd();
91 }
92
93 double rnd_stdnormal() {
94 return _normal_rnd();
95 }
96
97 // Returns integer in interval [min, max]
98 int rnd_int( int min, int max ) {
99 return (int)floor(_uni_rnd() * (max + 1 - min) + min);
100 }
101
102
103 } // end of namespace dai