Significant improvement of documentation
[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 CPU time for this process
36 #include <sys/times.h>
37 #include <sys/param.h>
38 #endif
39
40
41 #ifdef WINDOWS
42 bool isnan( double x ) {
43 return _isnan( x );
44 }
45 double atanh( double x ) {
46 return boost::math::atanh( x );
47 }
48 double log1p( double x ) {
49 return boost::math::log1p( x );
50 }
51 #endif
52
53
54 namespace dai {
55
56
57 // Returns user+system time in seconds
58 double toc() {
59 #ifdef WINDOWS
60 SYSTEMTIME tbuf;
61 GetSystemTime(&tbuf);
62 return( (double)(tbuf.wSecond + (double)tbuf.wMilliseconds / 1000.0) );
63 #else
64 tms tbuf;
65 times(&tbuf);
66 return( (double)(tbuf.tms_utime + tbuf.tms_stime) / HZ );
67 #endif
68 }
69
70 // This is a typedef for a random number generator.
71 // Try boost::mt19937 or boost::ecuyer1988 instead of boost::minstd_rand
72 typedef boost::minstd_rand _rnd_gen_type;
73
74 _rnd_gen_type _rnd_gen(42U);
75
76 // Define a uniform random number distribution which produces
77 // values between 0 and 1 (0 inclusive, 1 exclusive).
78 boost::uniform_real<> _uni_dist(0,1);
79 boost::variate_generator<_rnd_gen_type&, boost::uniform_real<> > _uni_rnd(_rnd_gen, _uni_dist);
80
81 // Define a normal distribution with mean 0 and standard deviation 1.
82 boost::normal_distribution<> _normal_dist;
83 boost::variate_generator<_rnd_gen_type&, boost::normal_distribution<> > _normal_rnd(_rnd_gen, _normal_dist);
84
85
86 void rnd_seed( size_t seed ) {
87 _rnd_gen.seed(seed);
88 }
89
90 double rnd_uniform() {
91 return _uni_rnd();
92 }
93
94 double rnd_stdnormal() {
95 return _normal_rnd();
96 }
97
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