1 /* This file is part of libDAI - http://www.libdai.org/
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.
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
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>
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
24 // Assume POSIX compliant system. We need the following for querying the system time
30 bool isnan( double x
) {
31 return __isnand( x
); // isnan() is a macro in Cygwin (as required by C99)
36 bool isnan( double x
) {
39 double atanh( double x
) {
40 return boost::math::atanh( x
);
42 double log1p( double x
) {
43 return boost::math::log1p( x
);
51 Real
max( const std::vector
<Real
> &v
) {
55 return *std::max_element( v
.begin(), v
.end() );
58 // Returns user+system time in seconds
63 return( (double)(tbuf
.wSecond
+ (double)tbuf
.wMilliseconds
/ 1000.0) );
67 gettimeofday( &tv
, &tz
);
68 return( (double)(tv
.tv_sec
+ (double)tv
.tv_usec
/ 1000000.0) );
72 /// Type of global random number generator
73 typedef boost::minstd_rand _rnd_gen_type
; // Try boost::mt19937 or boost::ecuyer1988 instead of boost::minstd_rand
75 /// Global random number generator
76 _rnd_gen_type
_rnd_gen(42U);
78 /// Uniform distribution with values between 0 and 1 (0 inclusive, 1 exclusive).
79 boost::uniform_real
<Real
> _uni_dist(0,1);
81 /// Global uniform random random number
82 boost::variate_generator
<_rnd_gen_type
&, boost::uniform_real
<Real
> > _uni_rnd(_rnd_gen
, _uni_dist
);
84 /// Normal distribution with mean 0 and standard deviation 1.
85 boost::normal_distribution
<Real
> _normal_dist
;
87 /// Global random number generator with standard normal distribution
88 boost::variate_generator
<_rnd_gen_type
&, boost::normal_distribution
<Real
> > _normal_rnd(_rnd_gen
, _normal_dist
);
91 void rnd_seed( size_t seed
) {
99 Real
rnd_stdnormal() {
100 return _normal_rnd();
103 int rnd_int( int min
, int max
) {
104 return (int)floor(_uni_rnd() * (max
+ 1 - min
) + min
);
107 void tokenizeString(const std::string
& s
, std::vector
<std::string
>& outTokens
, const std::string
& delim
) {
109 while (start
< s
.size()) {
110 size_t end
= s
.find_first_of(delim
, start
);
113 outTokens
.push_back(s
.substr(start
, end
- start
));
118 } // end of namespace dai