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
5 This file is part of libDAI.
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.
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.
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
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>
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
35 // Assume POSIX compliant system. We need the following for querying the system time
41 bool isnan( double x
) {
42 return __isnand( x
); // isnan() is a macro in Cygwin (as required by C99)
47 bool isnan( double x
) {
50 double atanh( double x
) {
51 return boost::math::atanh( x
);
53 double log1p( double x
) {
54 return boost::math::log1p( x
);
62 // Returns user+system time in seconds
67 return( (double)(tbuf
.wSecond
+ (double)tbuf
.wMilliseconds
/ 1000.0) );
71 gettimeofday( &tv
, &tz
);
72 return( (double)(tv
.tv_sec
+ (double)tv
.tv_usec
/ 1000000.0) );
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
;
80 _rnd_gen_type
_rnd_gen(42U);
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
);
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
);
92 void rnd_seed( size_t seed
) {
96 double rnd_uniform() {
100 double rnd_stdnormal() {
101 return _normal_rnd();
104 int rnd_int( int min
, int max
) {
105 return (int)floor(_uni_rnd() * (max
+ 1 - min
) + min
);
108 void tokenizeString(const std::string
& s
,
109 std::vector
<std::string
>& outTokens
,
110 const std::string
& delim
)
113 while (start
< s
.size()) {
114 size_t end
= s
.find_first_of(delim
, start
);
115 if (end
> s
.size()) {
118 outTokens
.push_back(s
.substr(start
, end
- start
));
123 } // end of namespace dai