gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Some usefull math constants lemon/math.h also includes the standard cmath, so one should prefer using just lemon/math.h instead of cmath.
0 2 1
default
3 files changed with 67 insertions and 2 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
1
/* -*- C++ -*-
2
 *
3
 * This file is a part of LEMON, a generic C++ optimization library
4
 *
5
 * Copyright (C) 2003-2008
6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8
 *
9
 * Permission to use, modify and distribute this software is granted
10
 * provided that this copyright notice appears in all copies. For
11
 * precise terms see the accompanying LICENSE file.
12
 *
13
 * This software is provided "AS IS" with no warranty of any kind,
14
 * express or implied, and with no claim as to its suitability for any
15
 * purpose.
16
 *
17
 */
18

	
19
#ifndef LEMON_MATH_H
20
#define LEMON_MATH_H
21

	
22
///\ingroup misc
23
///\file
24
///\brief Some extensions to the standard \c cmath library.
25
///
26
///Some extensions to the standard \c cmath library.
27
///
28
///This file includes the standard math library (cmath).
29

	
30
#include<cmath>
31

	
32
namespace lemon {
33

	
34
  /// \addtogroup misc
35
  /// @{
36
  
37
  /// The Euler constant
38
  const long double E       = 2.7182818284590452353602874713526625L;
39
  /// log_2(e)
40
  const long double LOG2E   = 1.4426950408889634073599246810018921L;
41
  /// log_10(e)
42
  const long double LOG10E  = 0.4342944819032518276511289189166051L;
43
  /// ln(2)
44
  const long double LN2     = 0.6931471805599453094172321214581766L;
45
  /// ln(10)
46
  const long double LN10    = 2.3025850929940456840179914546843642L;
47
  /// pi
48
  const long double PI      = 3.1415926535897932384626433832795029L;
49
  /// pi/2
50
  const long double PI_2    = 1.5707963267948966192313216916397514L;
51
  /// pi/4
52
  const long double PI_4    = 0.7853981633974483096156608458198757L;
53
  /// sqrt(2)
54
  const long double SQRT2   = 1.4142135623730950488016887242096981L;
55
  /// 1/sqrt(2)
56
  const long double SQRT1_2 = 0.7071067811865475244008443621048490L;
57
  
58

	
59
  /// @}
60

	
61
} //namespace lemon
62

	
63
#endif //LEMON_TOLERANCE_H
Ignore white space 512 line context
1 1
EXTRA_DIST += \
2 2
	lemon/Makefile \
3 3
	lemon/lemon.pc.in
4 4

	
5 5
pkgconfig_DATA += lemon/lemon.pc
6 6

	
7 7
lib_LTLIBRARIES += lemon/libemon.la
8 8

	
9 9
lemon_libemon_la_SOURCES = \
10 10
        lemon/base.cc \
11 11
        lemon/random.cc
12 12

	
13 13

	
14 14
lemon_libemon_la_CXXFLAGS = $(GLPK_CFLAGS) $(CPLEX_CFLAGS) $(SOPLEX_CXXFLAGS)
15 15
lemon_libemon_la_LDFLAGS = $(GLPK_LIBS) $(CPLEX_LIBS) $(SOPLEX_LIBS)
16 16

	
17 17
lemon_HEADERS += \
18 18
        lemon/dim2.h \
19 19
	lemon/maps.h \
20
	lemon/math.h \
20 21
        lemon/random.h \
21 22
	lemon/list_graph.h \
22 23
        lemon/tolerance.h
23 24

	
24 25
bits_HEADERS += \
25 26
	lemon/bits/alteration_notifier.h \
26 27
	lemon/bits/array_map.h \
27 28
	lemon/bits/base_extender.h \
28 29
	lemon/bits/default_map.h \
29 30
	lemon/bits/graph_extender.h \
30 31
        lemon/bits/invalid.h \
31 32
	lemon/bits/map_extender.h \
32 33
	lemon/bits/traits.h \
33 34
        lemon/bits/utility.h \
34 35
	lemon/bits/vector_map.h
35 36

	
36 37
concept_HEADERS += \
37 38
	lemon/concept_check.h \
38 39
	lemon/concepts/digraph.h \
39 40
	lemon/concepts/graph.h \
40 41
	lemon/concepts/maps.h \
41 42
	lemon/concepts/graph_components.h
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
/*
20 20
 * This file contains the reimplemented version of the Mersenne Twister
21 21
 * Generator of Matsumoto and Nishimura.
22 22
 *
23 23
 * See the appropriate copyright notice below.
24 24
 * 
25 25
 * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
26 26
 * All rights reserved.                          
27 27
 *
28 28
 * Redistribution and use in source and binary forms, with or without
29 29
 * modification, are permitted provided that the following conditions
30 30
 * are met:
31 31
 *
32 32
 * 1. Redistributions of source code must retain the above copyright
33 33
 *    notice, this list of conditions and the following disclaimer.
34 34
 *
35 35
 * 2. Redistributions in binary form must reproduce the above copyright
36 36
 *    notice, this list of conditions and the following disclaimer in the
37 37
 *    documentation and/or other materials provided with the distribution.
38 38
 *
39 39
 * 3. The names of its contributors may not be used to endorse or promote 
40 40
 *    products derived from this software without specific prior written 
41 41
 *    permission.
42 42
 *
43 43
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 44
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 45
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
46 46
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
47 47
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
48 48
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49 49
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50 50
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 51
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52 52
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 53
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54 54
 * OF THE POSSIBILITY OF SUCH DAMAGE.
55 55
 *
56 56
 *
57 57
 * Any feedback is very welcome.
58 58
 * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
59 59
 * email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
60 60
 */
61 61

	
62 62
#ifndef LEMON_RANDOM_H
63 63
#define LEMON_RANDOM_H
64 64

	
65 65
#include <algorithm>
66 66
#include <iterator>
67 67
#include <vector>
68 68

	
69 69
#include <ctime>
70
#include <cmath>
71 70

	
71
#include <lemon/math.h>
72 72
#include <lemon/dim2.h>
73

	
73 74
///\ingroup misc
74 75
///\file
75 76
///\brief Mersenne Twister random number generator
76 77

	
77 78
namespace lemon {
78 79

	
79 80
  namespace _random_bits {
80 81
    
81 82
    template <typename _Word, int _bits = std::numeric_limits<_Word>::digits>
82 83
    struct RandomTraits {};
83 84

	
84 85
    template <typename _Word>
85 86
    struct RandomTraits<_Word, 32> {
86 87

	
87 88
      typedef _Word Word;
88 89
      static const int bits = 32;
89 90

	
90 91
      static const int length = 624;
91 92
      static const int shift = 397;
92 93
      
93 94
      static const Word mul = 0x6c078965u;
94 95
      static const Word arrayInit = 0x012BD6AAu;
95 96
      static const Word arrayMul1 = 0x0019660Du;
96 97
      static const Word arrayMul2 = 0x5D588B65u;
97 98

	
98 99
      static const Word mask = 0x9908B0DFu;
99 100
      static const Word loMask = (1u << 31) - 1;
100 101
      static const Word hiMask = ~loMask;
101 102

	
102 103

	
103 104
      static Word tempering(Word rnd) {
104 105
        rnd ^= (rnd >> 11);
105 106
        rnd ^= (rnd << 7) & 0x9D2C5680u;
106 107
        rnd ^= (rnd << 15) & 0xEFC60000u;
107 108
        rnd ^= (rnd >> 18);
108 109
        return rnd;
109 110
      }
110 111

	
111 112
    };
112 113

	
113 114
    template <typename _Word>
114 115
    struct RandomTraits<_Word, 64> {
115 116

	
116 117
      typedef _Word Word;
117 118
      static const int bits = 64;
118 119

	
119 120
      static const int length = 312;
120 121
      static const int shift = 156;
121 122

	
122 123
      static const Word mul = Word(0x5851F42Du) << 32 | Word(0x4C957F2Du);
123 124
      static const Word arrayInit = Word(0x00000000u) << 32 |Word(0x012BD6AAu);
124 125
      static const Word arrayMul1 = Word(0x369DEA0Fu) << 32 |Word(0x31A53F85u);
125 126
      static const Word arrayMul2 = Word(0x27BB2EE6u) << 32 |Word(0x87B0B0FDu);
126 127

	
127 128
      static const Word mask = Word(0xB5026F5Au) << 32 | Word(0xA96619E9u);
128 129
      static const Word loMask = (Word(1u) << 31) - 1;
129 130
      static const Word hiMask = ~loMask;
130 131

	
131 132
      static Word tempering(Word rnd) {
132 133
        rnd ^= (rnd >> 29) & (Word(0x55555555u) << 32 | Word(0x55555555u));
133 134
        rnd ^= (rnd << 17) & (Word(0x71D67FFFu) << 32 | Word(0xEDA60000u));
134 135
        rnd ^= (rnd << 37) & (Word(0xFFF7EEE0u) << 32 | Word(0x00000000u));
135 136
        rnd ^= (rnd >> 43);
136 137
        return rnd;
137 138
      }
138 139

	
139 140
    };
140 141

	
141 142
    template <typename _Word>
142 143
    class RandomCore {
143 144
    public:
144 145

	
145 146
      typedef _Word Word;
146 147

	
147 148
    private:
148 149

	
149 150
      static const int bits = RandomTraits<Word>::bits;
150 151

	
151 152
      static const int length = RandomTraits<Word>::length;
152 153
      static const int shift = RandomTraits<Word>::shift;
153 154

	
154 155
    public:
155 156

	
156 157
      void initState() {
157 158
        static const Word seedArray[4] = {
158 159
          0x12345u, 0x23456u, 0x34567u, 0x45678u
159 160
        };
160 161
    
161 162
        initState(seedArray, seedArray + 4);
162 163
      }
163 164

	
164 165
      void initState(Word seed) {
165 166

	
166 167
        static const Word mul = RandomTraits<Word>::mul;
167 168

	
168 169
        current = state; 
169 170

	
170 171
        Word *curr = state + length - 1;
171 172
        curr[0] = seed; --curr;
172 173
        for (int i = 1; i < length; ++i) {
173 174
          curr[0] = (mul * ( curr[1] ^ (curr[1] >> (bits - 2)) ) + i);
174 175
          --curr;
175 176
        }
176 177
      }
177 178

	
178 179
      template <typename Iterator>
179 180
      void initState(Iterator begin, Iterator end) {
180 181

	
181 182
        static const Word init = RandomTraits<Word>::arrayInit;
182 183
        static const Word mul1 = RandomTraits<Word>::arrayMul1;
183 184
        static const Word mul2 = RandomTraits<Word>::arrayMul2;
184 185

	
185 186

	
186 187
        Word *curr = state + length - 1; --curr;
187 188
        Iterator it = begin; int cnt = 0;
188 189
        int num;
189 190

	
190 191
        initState(init);
191 192

	
192 193
        num = length > end - begin ? length : end - begin;
193 194
        while (num--) {
194 195
          curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul1)) 
195 196
            + *it + cnt;
196 197
          ++it; ++cnt;
197 198
          if (it == end) {
198 199
            it = begin; cnt = 0;
199 200
          }
200 201
          if (curr == state) {
201 202
            curr = state + length - 1; curr[0] = state[0];
202 203
          }
203 204
          --curr;
204 205
        }
205 206

	
206 207
        num = length - 1; cnt = length - (curr - state) - 1;
207 208
        while (num--) {
208 209
          curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul2))
209 210
            - cnt;
210 211
          --curr; ++cnt;
211 212
          if (curr == state) {
212 213
            curr = state + length - 1; curr[0] = state[0]; --curr;
213 214
            cnt = 1;
214 215
          }
215 216
        }
216 217
        
217 218
        state[length - 1] = Word(1) << (bits - 1);
218 219
      }
219 220
      
220 221
      void copyState(const RandomCore& other) {
221 222
        std::copy(other.state, other.state + length, state);
222 223
        current = state + (other.current - other.state);
223 224
      }
224 225

	
225 226
      Word operator()() {
226 227
        if (current == state) fillState();
227 228
        --current;
228 229
        Word rnd = *current;
229 230
        return RandomTraits<Word>::tempering(rnd);
230 231
      }
231 232

	
232 233
    private:
233 234

	
234 235
  
235 236
      void fillState() {
236 237
        static const Word mask[2] = { 0x0ul, RandomTraits<Word>::mask };
237 238
        static const Word loMask = RandomTraits<Word>::loMask;
238 239
        static const Word hiMask = RandomTraits<Word>::hiMask;
239 240

	
240 241
        current = state + length; 
241 242

	
242 243
        register Word *curr = state + length - 1;
243 244
        register long num;
244 245
      
245 246
        num = length - shift;
246 247
        while (num--) {
247 248
          curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
248 249
            curr[- shift] ^ mask[curr[-1] & 1ul];
249 250
          --curr;
250 251
        }
251 252
        num = shift - 1;
252 253
        while (num--) {
253 254
          curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
254 255
            curr[length - shift] ^ mask[curr[-1] & 1ul];
255 256
          --curr;
256 257
        }
257 258
        state[0] = (((state[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^
258 259
          curr[length - shift] ^ mask[curr[length - 1] & 1ul];
259 260

	
260 261
      }
261 262

	
262 263
  
263 264
      Word *current;
264 265
      Word state[length];
265 266
      
266 267
    };
267 268

	
268 269

	
269 270
    template <typename Result, 
270 271
              int shift = (std::numeric_limits<Result>::digits + 1) / 2>
271 272
    struct Masker {
272 273
      static Result mask(const Result& result) {
273 274
        return Masker<Result, (shift + 1) / 2>::
274 275
          mask(static_cast<Result>(result | (result >> shift)));
275 276
      }
276 277
    };
277 278
    
278 279
    template <typename Result>
279 280
    struct Masker<Result, 1> {
280 281
      static Result mask(const Result& result) {
281 282
        return static_cast<Result>(result | (result >> 1));
282 283
      }
283 284
    };
284 285

	
285 286
    template <typename Result, typename Word, 
286 287
              int rest = std::numeric_limits<Result>::digits, int shift = 0, 
287 288
              bool last = rest <= std::numeric_limits<Word>::digits>
288 289
    struct IntConversion {
289 290
      static const int bits = std::numeric_limits<Word>::digits;
290 291
    
291 292
      static Result convert(RandomCore<Word>& rnd) {
292 293
        return static_cast<Result>(rnd() >> (bits - rest)) << shift;
293 294
      }
294 295
      
295 296
    }; 
296 297

	
297 298
    template <typename Result, typename Word, int rest, int shift> 
298 299
    struct IntConversion<Result, Word, rest, shift, false> {
299 300
      static const int bits = std::numeric_limits<Word>::digits;
300 301

	
301 302
      static Result convert(RandomCore<Word>& rnd) {
302 303
        return (static_cast<Result>(rnd()) << shift) | 
303 304
          IntConversion<Result, Word, rest - bits, shift + bits>::convert(rnd);
304 305
      }
305 306
    };
306 307

	
307 308

	
308 309
    template <typename Result, typename Word,
309 310
              bool one_word = (std::numeric_limits<Word>::digits < 
310 311
			       std::numeric_limits<Result>::digits) >
311 312
    struct Mapping {
312 313
      static Result map(RandomCore<Word>& rnd, const Result& bound) {
313 314
        Word max = Word(bound - 1);
314 315
        Result mask = Masker<Result>::mask(bound - 1);
315 316
        Result num;
316 317
        do {
317 318
          num = IntConversion<Result, Word>::convert(rnd) & mask; 
318 319
        } while (num > max);
319 320
        return num;
320 321
      }
321 322
    };
322 323

	
323 324
    template <typename Result, typename Word>
324 325
    struct Mapping<Result, Word, false> {
325 326
      static Result map(RandomCore<Word>& rnd, const Result& bound) {
326 327
        Word max = Word(bound - 1);
327 328
        Word mask = Masker<Word, (std::numeric_limits<Result>::digits + 1) / 2>
328 329
          ::mask(max);
... ...
@@ -506,367 +507,367 @@
506 507
  /// // int e = rnd.integer(1, 1 + 6);     // 1..6
507 508
  /// int b = rnd.uinteger<int>();          // 0 .. 2^31 - 1
508 509
  /// int c = rnd.integer<int>();           // - 2^31 .. 2^31 - 1
509 510
  /// bool g = rnd.boolean();               // P(g = true) = 0.5
510 511
  /// bool h = rnd.boolean(0.8);            // P(h = true) = 0.8
511 512
  ///\endcode
512 513
  ///
513 514
  /// LEMON provides a global instance of the random number
514 515
  /// generator which name is \ref lemon::rnd "rnd". Usually it is a
515 516
  /// good programming convenience to use this global generator to get
516 517
  /// random numbers.
517 518
  class Random {
518 519
  private:
519 520

	
520 521
    // Architecture word
521 522
    typedef unsigned long Word;
522 523
    
523 524
    _random_bits::RandomCore<Word> core;
524 525
    _random_bits::BoolProducer<Word> bool_producer;
525 526
    
526 527

	
527 528
  public:
528 529

	
529 530
    /// \brief Default constructor
530 531
    ///
531 532
    /// Constructor with constant seeding.
532 533
    Random() { core.initState(); }
533 534

	
534 535
    /// \brief Constructor with seed
535 536
    ///
536 537
    /// Constructor with seed. The current number type will be converted
537 538
    /// to the architecture word type.
538 539
    template <typename Number>
539 540
    Random(Number seed) { 
540 541
      _random_bits::Initializer<Number, Word>::init(core, seed);
541 542
    }
542 543

	
543 544
    /// \brief Constructor with array seeding
544 545
    ///
545 546
    /// Constructor with array seeding. The given range should contain
546 547
    /// any number type and the numbers will be converted to the
547 548
    /// architecture word type.
548 549
    template <typename Iterator>
549 550
    Random(Iterator begin, Iterator end) { 
550 551
      typedef typename std::iterator_traits<Iterator>::value_type Number;
551 552
      _random_bits::Initializer<Number, Word>::init(core, begin, end);
552 553
    }
553 554

	
554 555
    /// \brief Copy constructor
555 556
    ///
556 557
    /// Copy constructor. The generated sequence will be identical to
557 558
    /// the other sequence. It can be used to save the current state
558 559
    /// of the generator and later use it to generate the same
559 560
    /// sequence.
560 561
    Random(const Random& other) {
561 562
      core.copyState(other.core);
562 563
    }
563 564

	
564 565
    /// \brief Assign operator
565 566
    ///
566 567
    /// Assign operator. The generated sequence will be identical to
567 568
    /// the other sequence. It can be used to save the current state
568 569
    /// of the generator and later use it to generate the same
569 570
    /// sequence.
570 571
    Random& operator=(const Random& other) {
571 572
      if (&other != this) {
572 573
        core.copyState(other.core);
573 574
      }
574 575
      return *this;
575 576
    }
576 577

	
577 578
    /// \brief Returns a random real number from the range [0, 1)
578 579
    ///
579 580
    /// It returns a random real number from the range [0, 1). The
580 581
    /// default Number type is \c double.
581 582
    template <typename Number>
582 583
    Number real() {
583 584
      return _random_bits::RealConversion<Number, Word>::convert(core);
584 585
    }
585 586

	
586 587
    double real() {
587 588
      return real<double>();
588 589
    }
589 590

	
590 591
    /// \brief Returns a random real number the range [0, b)
591 592
    ///
592 593
    /// It returns a random real number from the range [0, b).
593 594
    template <typename Number>
594 595
    Number real(Number b) { 
595 596
      return real<Number>() * b; 
596 597
    }
597 598

	
598 599
    /// \brief Returns a random real number from the range [a, b)
599 600
    ///
600 601
    /// It returns a random real number from the range [a, b).
601 602
    template <typename Number>
602 603
    Number real(Number a, Number b) { 
603 604
      return real<Number>() * (b - a) + a; 
604 605
    }
605 606

	
606 607
    /// \brief Returns a random real number from the range [0, 1)
607 608
    ///
608 609
    /// It returns a random double from the range [0, 1).
609 610
    double operator()() {
610 611
      return real<double>();
611 612
    }
612 613

	
613 614
    /// \brief Returns a random real number from the range [0, b)
614 615
    ///
615 616
    /// It returns a random real number from the range [0, b).
616 617
    template <typename Number>
617 618
    Number operator()(Number b) { 
618 619
      return real<Number>() * b; 
619 620
    }
620 621

	
621 622
    /// \brief Returns a random real number from the range [a, b)
622 623
    ///
623 624
    /// It returns a random real number from the range [a, b).
624 625
    template <typename Number>
625 626
    Number operator()(Number a, Number b) { 
626 627
      return real<Number>() * (b - a) + a; 
627 628
    }
628 629

	
629 630
    /// \brief Returns a random integer from a range
630 631
    ///
631 632
    /// It returns a random integer from the range {0, 1, ..., b - 1}.
632 633
    template <typename Number>
633 634
    Number integer(Number b) {
634 635
      return _random_bits::Mapping<Number, Word>::map(core, b);
635 636
    }
636 637

	
637 638
    /// \brief Returns a random integer from a range
638 639
    ///
639 640
    /// It returns a random integer from the range {a, a + 1, ..., b - 1}.
640 641
    template <typename Number>
641 642
    Number integer(Number a, Number b) {
642 643
      return _random_bits::Mapping<Number, Word>::map(core, b - a) + a;
643 644
    }
644 645

	
645 646
    /// \brief Returns a random integer from a range
646 647
    ///
647 648
    /// It returns a random integer from the range {0, 1, ..., b - 1}.
648 649
    template <typename Number>
649 650
    Number operator[](Number b) {
650 651
      return _random_bits::Mapping<Number, Word>::map(core, b);
651 652
    }
652 653

	
653 654
    /// \brief Returns a random non-negative integer
654 655
    ///
655 656
    /// It returns a random non-negative integer uniformly from the
656 657
    /// whole range of the current \c Number type. The default result
657 658
    /// type of this function is <tt>unsigned int</tt>.
658 659
    template <typename Number>
659 660
    Number uinteger() {
660 661
      return _random_bits::IntConversion<Number, Word>::convert(core);
661 662
    }
662 663

	
663 664
    unsigned int uinteger() {
664 665
      return uinteger<unsigned int>();
665 666
    }
666 667

	
667 668
    /// \brief Returns a random integer
668 669
    ///
669 670
    /// It returns a random integer uniformly from the whole range of
670 671
    /// the current \c Number type. The default result type of this
671 672
    /// function is \c int.
672 673
    template <typename Number>
673 674
    Number integer() {
674 675
      static const int nb = std::numeric_limits<Number>::digits + 
675 676
        (std::numeric_limits<Number>::is_signed ? 1 : 0);
676 677
      return _random_bits::IntConversion<Number, Word, nb>::convert(core);
677 678
    }
678 679

	
679 680
    int integer() {
680 681
      return integer<int>();
681 682
    }
682 683
    
683 684
    /// \brief Returns a random bool
684 685
    ///
685 686
    /// It returns a random bool. The generator holds a buffer for
686 687
    /// random bits. Every time when it become empty the generator makes
687 688
    /// a new random word and fill the buffer up.
688 689
    bool boolean() {
689 690
      return bool_producer.convert(core);
690 691
    }
691 692

	
692 693
    ///\name Non-uniform distributions
693 694
    ///
694 695
    
695 696
    ///@{
696 697
    
697 698
    /// \brief Returns a random bool
698 699
    ///
699 700
    /// It returns a random bool with given probability of true result.
700 701
    bool boolean(double p) {
701 702
      return operator()() < p;
702 703
    }
703 704

	
704 705
    /// Standard Gauss distribution
705 706

	
706 707
    /// Standard Gauss distribution.
707 708
    /// \note The Cartesian form of the Box-Muller
708 709
    /// transformation is used to generate a random normal distribution.
709 710
    /// \todo Consider using the "ziggurat" method instead.
710 711
    double gauss() 
711 712
    {
712 713
      double V1,V2,S;
713 714
      do {
714 715
	V1=2*real<double>()-1;
715 716
	V2=2*real<double>()-1;
716 717
	S=V1*V1+V2*V2;
717 718
      } while(S>=1);
718 719
      return std::sqrt(-2*std::log(S)/S)*V1;
719 720
    }
720 721
    /// Gauss distribution with given mean and standard deviation
721 722

	
722 723
    /// Gauss distribution with given mean and standard deviation.
723 724
    /// \sa gauss()
724 725
    double gauss(double mean,double std_dev)
725 726
    {
726 727
      return gauss()*std_dev+mean;
727 728
    }
728 729

	
729 730
    /// Exponential distribution with given mean
730 731

	
731 732
    /// This function generates an exponential distribution random number
732 733
    /// with mean <tt>1/lambda</tt>.
733 734
    ///
734 735
    double exponential(double lambda=1.0)
735 736
    {
736 737
      return -std::log(1.0-real<double>())/lambda;
737 738
    }
738 739

	
739 740
    /// Gamma distribution with given integer shape
740 741

	
741 742
    /// This function generates a gamma distribution random number.
742 743
    /// 
743 744
    ///\param k shape parameter (<tt>k>0</tt> integer)
744 745
    double gamma(int k) 
745 746
    {
746 747
      double s = 0;
747 748
      for(int i=0;i<k;i++) s-=std::log(1.0-real<double>());
748 749
      return s;
749 750
    }
750 751
    
751 752
    /// Gamma distribution with given shape and scale parameter
752 753

	
753 754
    /// This function generates a gamma distribution random number.
754 755
    /// 
755 756
    ///\param k shape parameter (<tt>k>0</tt>)
756 757
    ///\param theta scale parameter
757 758
    ///
758 759
    double gamma(double k,double theta=1.0)
759 760
    {
760 761
      double xi,nu;
761 762
      const double delta = k-std::floor(k);
762
      const double v0=M_E/(M_E-delta);
763
      const double v0=E/(E-delta);
763 764
      do {
764 765
	double V0=1.0-real<double>();
765 766
	double V1=1.0-real<double>();
766 767
	double V2=1.0-real<double>();
767 768
	if(V2<=v0) 
768 769
	  {
769 770
	    xi=std::pow(V1,1.0/delta);
770 771
	    nu=V0*std::pow(xi,delta-1.0);
771 772
	  }
772 773
	else 
773 774
	  {
774 775
	    xi=1.0-std::log(V1);
775 776
	    nu=V0*std::exp(-xi);
776 777
	  }
777 778
      } while(nu>std::pow(xi,delta-1.0)*std::exp(-xi));
778 779
      return theta*(xi-gamma(int(std::floor(k))));
779 780
    }
780 781
    
781 782
    /// Weibull distribution
782 783

	
783 784
    /// This function generates a Weibull distribution random number.
784 785
    /// 
785 786
    ///\param k shape parameter (<tt>k>0</tt>)
786 787
    ///\param lambda scale parameter (<tt>lambda>0</tt>)
787 788
    ///
788 789
    double weibull(double k,double lambda)
789 790
    {
790 791
      return lambda*pow(-std::log(1.0-real<double>()),1.0/k);
791 792
    }  
792 793
      
793 794
    /// Pareto distribution
794 795

	
795 796
    /// This function generates a Pareto distribution random number.
796 797
    /// 
797 798
    ///\param k shape parameter (<tt>k>0</tt>)
798 799
    ///\param x_min location parameter (<tt>x_min>0</tt>)
799 800
    ///
800 801
    double pareto(double k,double x_min)
801 802
    {
802 803
      return exponential(gamma(k,1.0/x_min));
803 804
    }  
804 805
      
805 806
    ///@}
806 807
    
807 808
    ///\name Two dimensional distributions
808 809
    ///
809 810

	
810 811
    ///@{
811 812
    
812 813
    /// Uniform distribution on the full unit circle
813 814

	
814 815
    /// Uniform distribution on the full unit circle.
815 816
    ///
816 817
    dim2::Point<double> disc() 
817 818
    {
818 819
      double V1,V2;
819 820
      do {
820 821
	V1=2*real<double>()-1;
821 822
	V2=2*real<double>()-1;
822 823
	
823 824
      } while(V1*V1+V2*V2>=1);
824 825
      return dim2::Point<double>(V1,V2);
825 826
    }
826 827
    /// A kind of two dimensional Gauss distribution
827 828

	
828 829
    /// This function provides a turning symmetric two-dimensional distribution.
829 830
    /// Both coordinates are of standard normal distribution, but they are not
830 831
    /// independent.
831 832
    ///
832 833
    /// \note The coordinates are the two random variables provided by
833 834
    /// the Box-Muller method.
834 835
    dim2::Point<double> gauss2()
835 836
    {
836 837
      double V1,V2,S;
837 838
      do {
838 839
	V1=2*real<double>()-1;
839 840
	V2=2*real<double>()-1;
840 841
	S=V1*V1+V2*V2;
841 842
      } while(S>=1);
842 843
      double W=std::sqrt(-2*std::log(S)/S);
843 844
      return dim2::Point<double>(W*V1,W*V2);
844 845
    }
845 846
    /// A kind of two dimensional exponential distribution
846 847

	
847 848
    /// This function provides a turning symmetric two-dimensional distribution.
848 849
    /// The x-coordinate is of conditionally exponential distribution
849 850
    /// with the condition that x is positive and y=0. If x is negative and 
850 851
    /// y=0 then, -x is of exponential distribution. The same is true for the
851 852
    /// y-coordinate.
852 853
    dim2::Point<double> exponential2() 
853 854
    {
854 855
      double V1,V2,S;
855 856
      do {
856 857
	V1=2*real<double>()-1;
857 858
	V2=2*real<double>()-1;
858 859
	S=V1*V1+V2*V2;
859 860
      } while(S>=1);
860 861
      double W=-std::log(S)/S;
861 862
      return dim2::Point<double>(W*V1,W*V2);
862 863
    }
863 864

	
864 865
    ///@}    
865 866
  };
866 867

	
867 868

	
868 869
  extern Random rnd;
869 870

	
870 871
}
871 872

	
872 873
#endif
0 comments (0 inline)