0
3
0
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-2007 |
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 |
///\file |
20 | 20 |
///\brief Instantiation of the Random class. |
21 | 21 |
|
22 | 22 |
#include <lemon/random.h> |
23 | 23 |
|
24 | 24 |
namespace lemon { |
25 | 25 |
/// \brief Global random number generator instance |
26 | 26 |
/// |
27 |
/// A global |
|
27 |
/// A global Mersenne Twister random number generator instance. |
|
28 | 28 |
Random rnd; |
29 | 29 |
} |
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-2007 |
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 | 70 |
#include <cmath> |
71 | 71 |
|
72 | 72 |
#include <lemon/dim2.h> |
73 | 73 |
///\ingroup misc |
74 | 74 |
///\file |
75 | 75 |
///\brief Mersenne Twister random number generator |
76 |
/// |
|
77 |
///\author Balazs Dezso |
|
78 | 76 |
|
79 | 77 |
namespace lemon { |
80 | 78 |
|
81 | 79 |
namespace _random_bits { |
82 | 80 |
|
83 | 81 |
template <typename _Word, int _bits = std::numeric_limits<_Word>::digits> |
84 | 82 |
struct RandomTraits {}; |
85 | 83 |
|
86 | 84 |
template <typename _Word> |
87 | 85 |
struct RandomTraits<_Word, 32> { |
88 | 86 |
|
89 | 87 |
typedef _Word Word; |
90 | 88 |
static const int bits = 32; |
91 | 89 |
|
92 | 90 |
static const int length = 624; |
93 | 91 |
static const int shift = 397; |
94 | 92 |
|
95 | 93 |
static const Word mul = 0x6c078965u; |
96 | 94 |
static const Word arrayInit = 0x012BD6AAu; |
97 | 95 |
static const Word arrayMul1 = 0x0019660Du; |
98 | 96 |
static const Word arrayMul2 = 0x5D588B65u; |
99 | 97 |
|
100 | 98 |
static const Word mask = 0x9908B0DFu; |
101 | 99 |
static const Word loMask = (1u << 31) - 1; |
102 | 100 |
static const Word hiMask = ~loMask; |
103 | 101 |
|
104 | 102 |
|
105 | 103 |
static Word tempering(Word rnd) { |
106 | 104 |
rnd ^= (rnd >> 11); |
107 | 105 |
rnd ^= (rnd << 7) & 0x9D2C5680u; |
108 | 106 |
rnd ^= (rnd << 15) & 0xEFC60000u; |
109 | 107 |
rnd ^= (rnd >> 18); |
110 | 108 |
return rnd; |
111 | 109 |
} |
112 | 110 |
|
113 | 111 |
}; |
114 | 112 |
|
115 | 113 |
template <typename _Word> |
116 | 114 |
struct RandomTraits<_Word, 64> { |
117 | 115 |
|
118 | 116 |
typedef _Word Word; |
119 | 117 |
static const int bits = 64; |
120 | 118 |
|
121 | 119 |
static const int length = 312; |
122 | 120 |
static const int shift = 156; |
123 | 121 |
|
124 | 122 |
static const Word mul = Word(0x5851F42Du) << 32 | Word(0x4C957F2Du); |
125 | 123 |
static const Word arrayInit = Word(0x00000000u) << 32 |Word(0x012BD6AAu); |
126 | 124 |
static const Word arrayMul1 = Word(0x369DEA0Fu) << 32 |Word(0x31A53F85u); |
127 | 125 |
static const Word arrayMul2 = Word(0x27BB2EE6u) << 32 |Word(0x87B0B0FDu); |
128 | 126 |
|
129 | 127 |
static const Word mask = Word(0xB5026F5Au) << 32 | Word(0xA96619E9u); |
130 | 128 |
static const Word loMask = (Word(1u) << 31) - 1; |
131 | 129 |
static const Word hiMask = ~loMask; |
132 | 130 |
|
133 | 131 |
static Word tempering(Word rnd) { |
134 | 132 |
rnd ^= (rnd >> 29) & (Word(0x55555555u) << 32 | Word(0x55555555u)); |
135 | 133 |
rnd ^= (rnd << 17) & (Word(0x71D67FFFu) << 32 | Word(0xEDA60000u)); |
136 | 134 |
rnd ^= (rnd << 37) & (Word(0xFFF7EEE0u) << 32 | Word(0x00000000u)); |
137 | 135 |
rnd ^= (rnd >> 43); |
138 | 136 |
return rnd; |
139 | 137 |
} |
140 | 138 |
|
141 | 139 |
}; |
142 | 140 |
|
143 | 141 |
template <typename _Word> |
144 | 142 |
class RandomCore { |
145 | 143 |
public: |
146 | 144 |
|
147 | 145 |
typedef _Word Word; |
148 | 146 |
|
149 | 147 |
private: |
150 | 148 |
|
151 | 149 |
static const int bits = RandomTraits<Word>::bits; |
152 | 150 |
|
153 | 151 |
static const int length = RandomTraits<Word>::length; |
154 | 152 |
static const int shift = RandomTraits<Word>::shift; |
155 | 153 |
|
156 | 154 |
public: |
157 | 155 |
|
158 | 156 |
void initState() { |
159 | 157 |
static const Word seedArray[4] = { |
160 | 158 |
0x12345u, 0x23456u, 0x34567u, 0x45678u |
161 | 159 |
}; |
162 | 160 |
|
163 | 161 |
initState(seedArray, seedArray + 4); |
164 | 162 |
} |
165 | 163 |
|
166 | 164 |
void initState(Word seed) { |
167 | 165 |
|
168 | 166 |
static const Word mul = RandomTraits<Word>::mul; |
169 | 167 |
|
170 | 168 |
current = state; |
171 | 169 |
|
172 | 170 |
Word *curr = state + length - 1; |
173 | 171 |
curr[0] = seed; --curr; |
... | ... |
@@ -423,198 +421,196 @@ |
423 | 421 |
}; |
424 | 422 |
|
425 | 423 |
template <typename Result, typename Word> |
426 | 424 |
struct Initializer { |
427 | 425 |
|
428 | 426 |
template <typename Iterator> |
429 | 427 |
static void init(RandomCore<Word>& rnd, Iterator begin, Iterator end) { |
430 | 428 |
std::vector<Word> ws; |
431 | 429 |
for (Iterator it = begin; it != end; ++it) { |
432 | 430 |
ws.push_back(Word(*it)); |
433 | 431 |
} |
434 | 432 |
rnd.initState(ws.begin(), ws.end()); |
435 | 433 |
} |
436 | 434 |
|
437 | 435 |
static void init(RandomCore<Word>& rnd, Result seed) { |
438 | 436 |
rnd.initState(seed); |
439 | 437 |
} |
440 | 438 |
}; |
441 | 439 |
|
442 | 440 |
template <typename Word> |
443 | 441 |
struct BoolConversion { |
444 | 442 |
static bool convert(RandomCore<Word>& rnd) { |
445 | 443 |
return (rnd() & 1) == 1; |
446 | 444 |
} |
447 | 445 |
}; |
448 | 446 |
|
449 | 447 |
template <typename Word> |
450 | 448 |
struct BoolProducer { |
451 | 449 |
Word buffer; |
452 | 450 |
int num; |
453 | 451 |
|
454 | 452 |
BoolProducer() : num(0) {} |
455 | 453 |
|
456 | 454 |
bool convert(RandomCore<Word>& rnd) { |
457 | 455 |
if (num == 0) { |
458 | 456 |
buffer = rnd(); |
459 | 457 |
num = RandomTraits<Word>::bits; |
460 | 458 |
} |
461 | 459 |
bool r = (buffer & 1); |
462 | 460 |
buffer >>= 1; |
463 | 461 |
--num; |
464 | 462 |
return r; |
465 | 463 |
} |
466 | 464 |
}; |
467 | 465 |
|
468 | 466 |
} |
469 | 467 |
|
470 | 468 |
/// \ingroup misc |
471 | 469 |
/// |
472 | 470 |
/// \brief Mersenne Twister random number generator |
473 | 471 |
/// |
474 | 472 |
/// The Mersenne Twister is a twisted generalized feedback |
475 | 473 |
/// shift-register generator of Matsumoto and Nishimura. The period |
476 | 474 |
/// of this generator is \f$ 2^{19937} - 1 \f$ and it is |
477 | 475 |
/// equi-distributed in 623 dimensions for 32-bit numbers. The time |
478 | 476 |
/// performance of this generator is comparable to the commonly used |
479 | 477 |
/// generators. |
480 | 478 |
/// |
481 | 479 |
/// This implementation is specialized for both 32-bit and 64-bit |
482 | 480 |
/// architectures. The generators differ sligthly in the |
483 | 481 |
/// initialization and generation phase so they produce two |
484 | 482 |
/// completly different sequences. |
485 | 483 |
/// |
486 | 484 |
/// The generator gives back random numbers of serveral types. To |
487 | 485 |
/// get a random number from a range of a floating point type you |
488 | 486 |
/// can use one form of the \c operator() or the \c real() member |
489 | 487 |
/// function. If you want to get random number from the {0, 1, ..., |
490 | 488 |
/// n-1} integer range use the \c operator[] or the \c integer() |
491 | 489 |
/// method. And to get random number from the whole range of an |
492 | 490 |
/// integer type you can use the argumentless \c integer() or \c |
493 | 491 |
/// uinteger() functions. After all you can get random bool with |
494 | 492 |
/// equal chance of true and false or given probability of true |
495 | 493 |
/// result with the \c boolean() member functions. |
496 | 494 |
/// |
497 | 495 |
///\code |
498 | 496 |
/// // The commented code is identical to the other |
499 | 497 |
/// double a = rnd(); // [0.0, 1.0) |
500 | 498 |
/// // double a = rnd.real(); // [0.0, 1.0) |
501 | 499 |
/// double b = rnd(100.0); // [0.0, 100.0) |
502 | 500 |
/// // double b = rnd.real(100.0); // [0.0, 100.0) |
503 | 501 |
/// double c = rnd(1.0, 2.0); // [1.0, 2.0) |
504 | 502 |
/// // double c = rnd.real(1.0, 2.0); // [1.0, 2.0) |
505 | 503 |
/// int d = rnd[100000]; // 0..99999 |
506 | 504 |
/// // int d = rnd.integer(100000); // 0..99999 |
507 | 505 |
/// int e = rnd[6] + 1; // 1..6 |
508 | 506 |
/// // int e = rnd.integer(1, 1 + 6); // 1..6 |
509 | 507 |
/// int b = rnd.uinteger<int>(); // 0 .. 2^31 - 1 |
510 | 508 |
/// int c = rnd.integer<int>(); // - 2^31 .. 2^31 - 1 |
511 | 509 |
/// bool g = rnd.boolean(); // P(g = true) = 0.5 |
512 | 510 |
/// bool h = rnd.boolean(0.8); // P(h = true) = 0.8 |
513 | 511 |
///\endcode |
514 | 512 |
/// |
515 | 513 |
/// The lemon provides a global instance of the random number |
516 | 514 |
/// generator which name is \ref lemon::rnd "rnd". Usually it is a |
517 | 515 |
/// good programming convenience to use this global generator to get |
518 | 516 |
/// random numbers. |
519 |
/// |
|
520 |
/// \author Balazs Dezso |
|
521 | 517 |
class Random { |
522 | 518 |
private: |
523 | 519 |
|
524 |
// |
|
520 |
// Architecture word |
|
525 | 521 |
typedef unsigned long Word; |
526 | 522 |
|
527 | 523 |
_random_bits::RandomCore<Word> core; |
528 | 524 |
_random_bits::BoolProducer<Word> bool_producer; |
529 | 525 |
|
530 | 526 |
|
531 | 527 |
public: |
532 | 528 |
|
533 | 529 |
/// \brief Constructor |
534 | 530 |
/// |
535 | 531 |
/// Constructor with constant seeding. |
536 | 532 |
Random() { core.initState(); } |
537 | 533 |
|
538 | 534 |
/// \brief Constructor |
539 | 535 |
/// |
540 | 536 |
/// Constructor with seed. The current number type will be converted |
541 | 537 |
/// to the architecture word type. |
542 | 538 |
template <typename Number> |
543 | 539 |
Random(Number seed) { |
544 | 540 |
_random_bits::Initializer<Number, Word>::init(core, seed); |
545 | 541 |
} |
546 | 542 |
|
547 | 543 |
/// \brief Constructor |
548 | 544 |
/// |
549 | 545 |
/// Constructor with array seeding. The given range should contain |
550 | 546 |
/// any number type and the numbers will be converted to the |
551 | 547 |
/// architecture word type. |
552 | 548 |
template <typename Iterator> |
553 | 549 |
Random(Iterator begin, Iterator end) { |
554 | 550 |
typedef typename std::iterator_traits<Iterator>::value_type Number; |
555 | 551 |
_random_bits::Initializer<Number, Word>::init(core, begin, end); |
556 | 552 |
} |
557 | 553 |
|
558 | 554 |
/// \brief Copy constructor |
559 | 555 |
/// |
560 | 556 |
/// Copy constructor. The generated sequence will be identical to |
561 | 557 |
/// the other sequence. It can be used to save the current state |
562 | 558 |
/// of the generator and later use it to generate the same |
563 | 559 |
/// sequence. |
564 | 560 |
Random(const Random& other) { |
565 | 561 |
core.copyState(other.core); |
566 | 562 |
} |
567 | 563 |
|
568 | 564 |
/// \brief Assign operator |
569 | 565 |
/// |
570 | 566 |
/// Assign operator. The generated sequence will be identical to |
571 | 567 |
/// the other sequence. It can be used to save the current state |
572 | 568 |
/// of the generator and later use it to generate the same |
573 | 569 |
/// sequence. |
574 | 570 |
Random& operator=(const Random& other) { |
575 | 571 |
if (&other != this) { |
576 | 572 |
core.copyState(other.core); |
577 | 573 |
} |
578 | 574 |
return *this; |
579 | 575 |
} |
580 | 576 |
|
581 | 577 |
/// \brief Returns a random real number from the range [0, 1) |
582 | 578 |
/// |
583 | 579 |
/// It returns a random real number from the range [0, 1). The |
584 | 580 |
/// default Number type is double. |
585 | 581 |
template <typename Number> |
586 | 582 |
Number real() { |
587 | 583 |
return _random_bits::RealConversion<Number, Word>::convert(core); |
588 | 584 |
} |
589 | 585 |
|
590 | 586 |
double real() { |
591 | 587 |
return real<double>(); |
592 | 588 |
} |
593 | 589 |
|
594 | 590 |
/// \brief Returns a random real number the range [0, b) |
595 | 591 |
/// |
596 | 592 |
/// It returns a random real number from the range [0, b). |
597 | 593 |
template <typename Number> |
598 | 594 |
Number real(Number b) { |
599 | 595 |
return real<Number>() * b; |
600 | 596 |
} |
601 | 597 |
|
602 | 598 |
/// \brief Returns a random real number from the range [a, b) |
603 | 599 |
/// |
604 | 600 |
/// It returns a random real number from the range [a, b). |
605 | 601 |
template <typename Number> |
606 | 602 |
Number real(Number a, Number b) { |
607 | 603 |
return real<Number>() * (b - a) + a; |
608 | 604 |
} |
609 | 605 |
|
610 | 606 |
/// \brief Returns a random real number from the range [0, 1) |
611 | 607 |
/// |
612 | 608 |
/// It returns a random double from the range [0, 1). |
613 | 609 |
double operator()() { |
614 | 610 |
return real<double>(); |
615 | 611 |
} |
616 | 612 |
|
617 | 613 |
/// \brief Returns a random real number from the range [0, b) |
618 | 614 |
/// |
619 | 615 |
/// It returns a random real number from the range [0, b). |
620 | 616 |
template <typename Number> |
... | ... |
@@ -630,244 +626,247 @@ |
630 | 626 |
return real<Number>() * (b - a) + a; |
631 | 627 |
} |
632 | 628 |
|
633 | 629 |
/// \brief Returns a random integer from a range |
634 | 630 |
/// |
635 | 631 |
/// It returns a random integer from the range {0, 1, ..., b - 1}. |
636 | 632 |
template <typename Number> |
637 | 633 |
Number integer(Number b) { |
638 | 634 |
return _random_bits::Mapping<Number, Word>::map(core, b); |
639 | 635 |
} |
640 | 636 |
|
641 | 637 |
/// \brief Returns a random integer from a range |
642 | 638 |
/// |
643 | 639 |
/// It returns a random integer from the range {a, a + 1, ..., b - 1}. |
644 | 640 |
template <typename Number> |
645 | 641 |
Number integer(Number a, Number b) { |
646 | 642 |
return _random_bits::Mapping<Number, Word>::map(core, b - a) + a; |
647 | 643 |
} |
648 | 644 |
|
649 | 645 |
/// \brief Returns a random integer from a range |
650 | 646 |
/// |
651 | 647 |
/// It returns a random integer from the range {0, 1, ..., b - 1}. |
652 | 648 |
template <typename Number> |
653 | 649 |
Number operator[](Number b) { |
654 | 650 |
return _random_bits::Mapping<Number, Word>::map(core, b); |
655 | 651 |
} |
656 | 652 |
|
657 | 653 |
/// \brief Returns a random non-negative integer |
658 | 654 |
/// |
659 | 655 |
/// It returns a random non-negative integer uniformly from the |
660 | 656 |
/// whole range of the current \c Number type. The default result |
661 | 657 |
/// type of this function is unsigned int. |
662 | 658 |
template <typename Number> |
663 | 659 |
Number uinteger() { |
664 | 660 |
return _random_bits::IntConversion<Number, Word>::convert(core); |
665 | 661 |
} |
666 | 662 |
|
667 | 663 |
unsigned int uinteger() { |
668 | 664 |
return uinteger<unsigned int>(); |
669 | 665 |
} |
670 | 666 |
|
671 | 667 |
/// \brief Returns a random integer |
672 | 668 |
/// |
673 | 669 |
/// It returns a random integer uniformly from the whole range of |
674 | 670 |
/// the current \c Number type. The default result type of this |
675 | 671 |
/// function is int. |
676 | 672 |
template <typename Number> |
677 | 673 |
Number integer() { |
678 | 674 |
static const int nb = std::numeric_limits<Number>::digits + |
679 | 675 |
(std::numeric_limits<Number>::is_signed ? 1 : 0); |
680 | 676 |
return _random_bits::IntConversion<Number, Word, nb>::convert(core); |
681 | 677 |
} |
682 | 678 |
|
683 | 679 |
int integer() { |
684 | 680 |
return integer<int>(); |
685 | 681 |
} |
686 | 682 |
|
687 | 683 |
/// \brief Returns a random bool |
688 | 684 |
/// |
689 | 685 |
/// It returns a random bool. The generator holds a buffer for |
690 | 686 |
/// random bits. Every time when it become empty the generator makes |
691 | 687 |
/// a new random word and fill the buffer up. |
692 | 688 |
bool boolean() { |
693 | 689 |
return bool_producer.convert(core); |
694 | 690 |
} |
695 | 691 |
|
696 | 692 |
///\name Nonuniform distributions |
697 | 693 |
/// |
698 | 694 |
|
699 | 695 |
///@{ |
700 | 696 |
|
701 | 697 |
/// \brief Returns a random bool |
702 | 698 |
/// |
703 | 699 |
/// It returns a random bool with given probability of true result |
704 | 700 |
bool boolean(double p) { |
705 | 701 |
return operator()() < p; |
706 | 702 |
} |
707 | 703 |
|
708 | 704 |
/// Standard Gauss distribution |
709 | 705 |
|
710 | 706 |
/// Standard Gauss distribution. |
711 | 707 |
/// \note The Cartesian form of the Box-Muller |
712 | 708 |
/// transformation is used to generate a random normal distribution. |
713 | 709 |
/// \todo Consider using the "ziggurat" method instead. |
714 | 710 |
double gauss() |
715 | 711 |
{ |
716 | 712 |
double V1,V2,S; |
717 | 713 |
do { |
718 | 714 |
V1=2*real<double>()-1; |
719 | 715 |
V2=2*real<double>()-1; |
720 | 716 |
S=V1*V1+V2*V2; |
721 | 717 |
} while(S>=1); |
722 | 718 |
return std::sqrt(-2*std::log(S)/S)*V1; |
723 | 719 |
} |
724 | 720 |
/// Gauss distribution with given mean and standard deviation |
725 | 721 |
|
722 |
/// Gauss distribution with given mean and standard deviation |
|
726 | 723 |
/// \sa gauss() |
727 |
/// |
|
728 | 724 |
double gauss(double mean,double std_dev) |
729 | 725 |
{ |
730 | 726 |
return gauss()*std_dev+mean; |
731 | 727 |
} |
732 | 728 |
|
733 | 729 |
/// Exponential distribution with given mean |
734 | 730 |
|
735 | 731 |
/// This function generates an exponential distribution random number |
736 | 732 |
/// with mean <tt>1/lambda</tt>. |
737 | 733 |
/// |
738 | 734 |
double exponential(double lambda=1.0) |
739 | 735 |
{ |
740 | 736 |
return -std::log(1.0-real<double>())/lambda; |
741 | 737 |
} |
742 | 738 |
|
743 | 739 |
/// Gamma distribution with given integer shape |
744 | 740 |
|
745 | 741 |
/// This function generates a gamma distribution random number. |
746 | 742 |
/// |
747 | 743 |
///\param k shape parameter (<tt>k>0</tt> integer) |
748 | 744 |
double gamma(int k) |
749 | 745 |
{ |
750 | 746 |
double s = 0; |
751 | 747 |
for(int i=0;i<k;i++) s-=std::log(1.0-real<double>()); |
752 | 748 |
return s; |
753 | 749 |
} |
754 | 750 |
|
755 | 751 |
/// Gamma distribution with given shape and scale parameter |
756 | 752 |
|
757 | 753 |
/// This function generates a gamma distribution random number. |
758 | 754 |
/// |
759 | 755 |
///\param k shape parameter (<tt>k>0</tt>) |
760 | 756 |
///\param theta scale parameter |
761 | 757 |
/// |
762 | 758 |
double gamma(double k,double theta=1.0) |
763 | 759 |
{ |
764 | 760 |
double xi,nu; |
765 | 761 |
const double delta = k-std::floor(k); |
766 | 762 |
const double v0=M_E/(M_E-delta); |
767 | 763 |
do { |
768 | 764 |
double V0=1.0-real<double>(); |
769 | 765 |
double V1=1.0-real<double>(); |
770 | 766 |
double V2=1.0-real<double>(); |
771 | 767 |
if(V2<=v0) |
772 | 768 |
{ |
773 | 769 |
xi=std::pow(V1,1.0/delta); |
774 | 770 |
nu=V0*std::pow(xi,delta-1.0); |
775 | 771 |
} |
776 | 772 |
else |
777 | 773 |
{ |
778 | 774 |
xi=1.0-std::log(V1); |
779 | 775 |
nu=V0*std::exp(-xi); |
780 | 776 |
} |
781 | 777 |
} while(nu>std::pow(xi,delta-1.0)*std::exp(-xi)); |
782 | 778 |
return theta*(xi-gamma(int(std::floor(k)))); |
783 | 779 |
} |
784 | 780 |
|
785 | 781 |
/// Weibull distribution |
786 | 782 |
|
787 | 783 |
/// This function generates a Weibull distribution random number. |
788 | 784 |
/// |
789 | 785 |
///\param k shape parameter (<tt>k>0</tt>) |
790 | 786 |
///\param lambda scale parameter (<tt>lambda>0</tt>) |
791 | 787 |
/// |
792 | 788 |
double weibull(double k,double lambda) |
793 | 789 |
{ |
794 | 790 |
return lambda*pow(-std::log(1.0-real<double>()),1.0/k); |
795 | 791 |
} |
796 | 792 |
|
797 | 793 |
/// Pareto distribution |
798 | 794 |
|
799 | 795 |
/// This function generates a Pareto distribution random number. |
800 | 796 |
/// |
801 | 797 |
///\param k shape parameter (<tt>k>0</tt>) |
802 | 798 |
///\param x_min location parameter (<tt>x_min>0</tt>) |
803 | 799 |
/// |
804 | 800 |
double pareto(double k,double x_min) |
805 | 801 |
{ |
806 | 802 |
return exponential(gamma(k,1.0/x_min)); |
807 | 803 |
} |
808 | 804 |
|
809 | 805 |
///@} |
810 | 806 |
|
811 | 807 |
///\name Two dimensional distributions |
812 | 808 |
/// |
813 | 809 |
|
814 | 810 |
///@{ |
815 | 811 |
|
816 | 812 |
/// Uniform distribution on the full unit circle. |
813 |
|
|
814 |
/// Uniform distribution on the full unit circle. |
|
815 |
/// |
|
817 | 816 |
dim2::Point<double> disc() |
818 | 817 |
{ |
819 | 818 |
double V1,V2; |
820 | 819 |
do { |
821 | 820 |
V1=2*real<double>()-1; |
822 | 821 |
V2=2*real<double>()-1; |
823 | 822 |
|
824 | 823 |
} while(V1*V1+V2*V2>=1); |
825 | 824 |
return dim2::Point<double>(V1,V2); |
826 | 825 |
} |
827 | 826 |
/// A kind of two dimensional Gauss distribution |
828 | 827 |
|
829 | 828 |
/// This function provides a turning symmetric two-dimensional distribution. |
830 | 829 |
/// Both coordinates are of standard normal distribution, but they are not |
831 | 830 |
/// independent. |
832 | 831 |
/// |
833 | 832 |
/// \note The coordinates are the two random variables provided by |
834 | 833 |
/// the Box-Muller method. |
835 | 834 |
dim2::Point<double> gauss2() |
836 | 835 |
{ |
837 | 836 |
double V1,V2,S; |
838 | 837 |
do { |
839 | 838 |
V1=2*real<double>()-1; |
840 | 839 |
V2=2*real<double>()-1; |
841 | 840 |
S=V1*V1+V2*V2; |
842 | 841 |
} while(S>=1); |
843 | 842 |
double W=std::sqrt(-2*std::log(S)/S); |
844 | 843 |
return dim2::Point<double>(W*V1,W*V2); |
845 | 844 |
} |
846 | 845 |
/// A kind of two dimensional exponential distribution |
847 | 846 |
|
848 | 847 |
/// This function provides a turning symmetric two-dimensional distribution. |
849 | 848 |
/// The x-coordinate is of conditionally exponential distribution |
850 | 849 |
/// with the condition that x is positive and y=0. If x is negative and |
851 | 850 |
/// y=0 then, -x is of exponential distribution. The same is true for the |
852 | 851 |
/// y-coordinate. |
853 | 852 |
dim2::Point<double> exponential2() |
854 | 853 |
{ |
855 | 854 |
double V1,V2,S; |
856 | 855 |
do { |
857 | 856 |
V1=2*real<double>()-1; |
858 | 857 |
V2=2*real<double>()-1; |
859 | 858 |
S=V1*V1+V2*V2; |
860 | 859 |
} while(S>=1); |
861 | 860 |
double W=-std::log(S)/S; |
862 | 861 |
return dim2::Point<double>(W*V1,W*V2); |
863 | 862 |
} |
864 | 863 |
|
865 | 864 |
///@} |
866 | 865 |
}; |
867 | 866 |
|
868 | 867 |
|
869 | 868 |
extern Random rnd; |
870 | 869 |
|
871 | 870 |
} |
872 | 871 |
|
873 | 872 |
#endif |
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-2007 |
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 |
#ifndef LEMON_TOLERANCE_H |
20 | 20 |
#define LEMON_TOLERANCE_H |
21 | 21 |
|
22 | 22 |
///\ingroup misc |
23 | 23 |
///\file |
24 | 24 |
///\brief A basic tool to handle the anomalies of calculation with |
25 | 25 |
///floating point numbers. |
26 | 26 |
/// |
27 | 27 |
///\todo It should be in a module like "Basic tools" |
28 | 28 |
|
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
/// \addtogroup misc |
33 | 33 |
/// @{ |
34 | 34 |
|
35 | 35 |
///\brief A class to provide a basic way to |
36 | 36 |
///handle the comparison of numbers that are obtained |
37 | 37 |
///as a result of a probably inexact computation. |
38 | 38 |
/// |
39 | 39 |
///Tolerance is a class to provide a basic way to |
40 | 40 |
///handle the comparison of numbers that are obtained |
41 | 41 |
///as a result of a probably inexact computation. |
42 | 42 |
/// |
43 | 43 |
///This is an abstract class, it should be specialized for all numerical |
44 | 44 |
///data types. These specialized classes like \ref Tolerance\<double\> |
45 | 45 |
///may offer additional tuning parameters. |
46 | 46 |
/// |
47 | 47 |
///\sa Tolerance<float> |
48 | 48 |
///\sa Tolerance<double> |
49 | 49 |
///\sa Tolerance<long double> |
50 | 50 |
///\sa Tolerance<int> |
51 |
#if defined __GNUC__ && !defined __STRICT_ANSI__ |
|
51 | 52 |
///\sa Tolerance<long long int> |
53 |
#endif |
|
52 | 54 |
///\sa Tolerance<unsigned int> |
55 |
#if defined __GNUC__ && !defined __STRICT_ANSI__ |
|
53 | 56 |
///\sa Tolerance<unsigned long long int> |
57 |
#endif |
|
54 | 58 |
|
55 | 59 |
template<class T> |
56 | 60 |
class Tolerance |
57 | 61 |
{ |
58 | 62 |
public: |
59 | 63 |
typedef T Value; |
60 | 64 |
|
61 | 65 |
///\name Comparisons |
62 | 66 |
///The concept is that these bool functions return with \c true only if |
63 | 67 |
///the related comparisons hold even if some numerical error appeared |
64 | 68 |
///during the computations. |
65 | 69 |
|
66 | 70 |
///@{ |
67 | 71 |
|
68 | 72 |
///Returns \c true if \c a is \e surely strictly less than \c b |
69 | 73 |
static bool less(Value a,Value b) {return false;} |
70 | 74 |
///Returns \c true if \c a is \e surely different from \c b |
71 | 75 |
static bool different(Value a,Value b) {return false;} |
72 | 76 |
///Returns \c true if \c a is \e surely positive |
73 | 77 |
static bool positive(Value a) {return false;} |
74 | 78 |
///Returns \c true if \c a is \e surely negative |
75 | 79 |
static bool negative(Value a) {return false;} |
76 | 80 |
///Returns \c true if \c a is \e surely non-zero |
77 | 81 |
static bool nonZero(Value a) {return false;} |
78 | 82 |
|
79 | 83 |
///@} |
80 | 84 |
|
81 | 85 |
///Returns the zero value. |
82 | 86 |
static Value zero() {return T();} |
83 | 87 |
|
84 | 88 |
// static bool finite(Value a) {} |
85 | 89 |
// static Value big() {} |
86 | 90 |
// static Value negativeBig() {} |
87 | 91 |
}; |
88 | 92 |
|
89 | 93 |
|
90 | 94 |
///Float specialization of \ref Tolerance. |
91 | 95 |
|
92 | 96 |
///Float specialization of \ref Tolerance. |
93 | 97 |
///\sa Tolerance |
94 | 98 |
///\relates Tolerance |
95 | 99 |
template<> |
96 | 100 |
class Tolerance<float> |
97 | 101 |
{ |
98 | 102 |
static float def_epsilon; |
99 | 103 |
float _epsilon; |
100 | 104 |
public: |
101 | 105 |
///\e |
102 | 106 |
typedef float Value; |
103 | 107 |
|
104 | 108 |
///Constructor setting the epsilon tolerance to the default value. |
105 | 109 |
Tolerance() : _epsilon(def_epsilon) {} |
106 | 110 |
///Constructor setting the epsilon tolerance. |
107 | 111 |
Tolerance(float e) : _epsilon(e) {} |
108 | 112 |
|
109 | 113 |
///Return the epsilon value. |
110 | 114 |
Value epsilon() const {return _epsilon;} |
111 | 115 |
///Set the epsilon value. |
112 | 116 |
void epsilon(Value e) {_epsilon=e;} |
113 | 117 |
|
114 | 118 |
///Return the default epsilon value. |
115 | 119 |
static Value defaultEpsilon() {return def_epsilon;} |
116 | 120 |
///Set the default epsilon value. |
117 | 121 |
static void defaultEpsilon(Value e) {def_epsilon=e;} |
118 | 122 |
|
119 | 123 |
///\name Comparisons |
120 | 124 |
///See class Tolerance for more details. |
121 | 125 |
|
122 | 126 |
///@{ |
123 | 127 |
|
124 | 128 |
///Returns \c true if \c a is \e surely strictly less than \c b |
125 | 129 |
bool less(Value a,Value b) const {return a+_epsilon<b;} |
126 | 130 |
///Returns \c true if \c a is \e surely different from \c b |
127 | 131 |
bool different(Value a,Value b) const { return less(a,b)||less(b,a); } |
128 | 132 |
///Returns \c true if \c a is \e surely positive |
129 | 133 |
bool positive(Value a) const { return _epsilon<a; } |
130 | 134 |
///Returns \c true if \c a is \e surely negative |
131 | 135 |
bool negative(Value a) const { return -_epsilon>a; } |
132 | 136 |
///Returns \c true if \c a is \e surely non-zero |
133 |
bool nonZero(Value a) const { return positive(a)||negative(a); } |
|
137 |
bool nonZero(Value a) const { return positive(a)||negative(a); } |
|
134 | 138 |
|
135 | 139 |
///@} |
136 | 140 |
|
137 | 141 |
///Returns zero |
138 | 142 |
static Value zero() {return 0;} |
139 | 143 |
}; |
140 | 144 |
|
141 | 145 |
///Double specialization of \ref Tolerance. |
142 | 146 |
|
143 | 147 |
///Double specialization of \ref Tolerance. |
144 | 148 |
///\sa Tolerance |
145 | 149 |
///\relates Tolerance |
146 | 150 |
template<> |
147 | 151 |
class Tolerance<double> |
148 | 152 |
{ |
149 | 153 |
static double def_epsilon; |
150 | 154 |
double _epsilon; |
151 | 155 |
public: |
152 | 156 |
///\e |
153 | 157 |
typedef double Value; |
154 | 158 |
|
155 | 159 |
///Constructor setting the epsilon tolerance to the default value. |
156 | 160 |
Tolerance() : _epsilon(def_epsilon) {} |
157 | 161 |
///Constructor setting the epsilon tolerance. |
158 | 162 |
Tolerance(double e) : _epsilon(e) {} |
159 | 163 |
|
160 | 164 |
///Return the epsilon value. |
161 | 165 |
Value epsilon() const {return _epsilon;} |
162 | 166 |
///Set the epsilon value. |
163 | 167 |
void epsilon(Value e) {_epsilon=e;} |
164 | 168 |
|
165 | 169 |
///Return the default epsilon value. |
166 | 170 |
static Value defaultEpsilon() {return def_epsilon;} |
167 | 171 |
///Set the default epsilon value. |
168 | 172 |
static void defaultEpsilon(Value e) {def_epsilon=e;} |
169 | 173 |
|
170 | 174 |
///\name Comparisons |
171 | 175 |
///See class Tolerance for more details. |
172 | 176 |
|
173 | 177 |
///@{ |
174 | 178 |
|
175 | 179 |
///Returns \c true if \c a is \e surely strictly less than \c b |
176 | 180 |
bool less(Value a,Value b) const {return a+_epsilon<b;} |
177 | 181 |
///Returns \c true if \c a is \e surely different from \c b |
178 | 182 |
bool different(Value a,Value b) const { return less(a,b)||less(b,a); } |
179 | 183 |
///Returns \c true if \c a is \e surely positive |
180 | 184 |
bool positive(Value a) const { return _epsilon<a; } |
181 | 185 |
///Returns \c true if \c a is \e surely negative |
182 | 186 |
bool negative(Value a) const { return -_epsilon>a; } |
183 | 187 |
///Returns \c true if \c a is \e surely non-zero |
184 |
bool nonZero(Value a) const { return positive(a)||negative(a); } |
|
188 |
bool nonZero(Value a) const { return positive(a)||negative(a); } |
|
185 | 189 |
|
186 | 190 |
///@} |
187 | 191 |
|
188 | 192 |
///Returns zero |
189 | 193 |
static Value zero() {return 0;} |
190 | 194 |
}; |
191 | 195 |
|
192 | 196 |
///Long double specialization of \ref Tolerance. |
193 | 197 |
|
194 | 198 |
///Long double specialization of \ref Tolerance. |
195 | 199 |
///\sa Tolerance |
196 | 200 |
///\relates Tolerance |
197 | 201 |
template<> |
198 | 202 |
class Tolerance<long double> |
199 | 203 |
{ |
200 | 204 |
static long double def_epsilon; |
201 | 205 |
long double _epsilon; |
202 | 206 |
public: |
203 | 207 |
///\e |
204 | 208 |
typedef long double Value; |
205 | 209 |
|
206 | 210 |
///Constructor setting the epsilon tolerance to the default value. |
207 | 211 |
Tolerance() : _epsilon(def_epsilon) {} |
208 | 212 |
///Constructor setting the epsilon tolerance. |
209 | 213 |
Tolerance(long double e) : _epsilon(e) {} |
210 | 214 |
|
211 | 215 |
///Return the epsilon value. |
212 | 216 |
Value epsilon() const {return _epsilon;} |
213 | 217 |
///Set the epsilon value. |
214 | 218 |
void epsilon(Value e) {_epsilon=e;} |
215 | 219 |
|
216 | 220 |
///Return the default epsilon value. |
217 | 221 |
static Value defaultEpsilon() {return def_epsilon;} |
218 | 222 |
///Set the default epsilon value. |
219 | 223 |
static void defaultEpsilon(Value e) {def_epsilon=e;} |
220 | 224 |
|
221 | 225 |
///\name Comparisons |
222 | 226 |
///See class Tolerance for more details. |
223 | 227 |
|
224 | 228 |
///@{ |
225 | 229 |
|
226 | 230 |
///Returns \c true if \c a is \e surely strictly less than \c b |
227 | 231 |
bool less(Value a,Value b) const {return a+_epsilon<b;} |
228 | 232 |
///Returns \c true if \c a is \e surely different from \c b |
229 | 233 |
bool different(Value a,Value b) const { return less(a,b)||less(b,a); } |
230 | 234 |
///Returns \c true if \c a is \e surely positive |
231 | 235 |
bool positive(Value a) const { return _epsilon<a; } |
232 | 236 |
///Returns \c true if \c a is \e surely negative |
233 | 237 |
bool negative(Value a) const { return -_epsilon>a; } |
234 | 238 |
///Returns \c true if \c a is \e surely non-zero |
235 |
bool nonZero(Value a) const { return positive(a)||negative(a); } |
|
239 |
bool nonZero(Value a) const { return positive(a)||negative(a); } |
|
236 | 240 |
|
237 | 241 |
///@} |
238 | 242 |
|
239 | 243 |
///Returns zero |
240 | 244 |
static Value zero() {return 0;} |
241 | 245 |
}; |
242 | 246 |
|
243 | 247 |
///Integer specialization of \ref Tolerance. |
244 | 248 |
|
245 | 249 |
///Integer specialization of \ref Tolerance. |
246 | 250 |
///\sa Tolerance |
247 | 251 |
template<> |
248 | 252 |
class Tolerance<int> |
249 | 253 |
{ |
250 | 254 |
public: |
251 | 255 |
///\e |
252 | 256 |
typedef int Value; |
253 | 257 |
|
254 | 258 |
///\name Comparisons |
255 | 259 |
///See \ref Tolerance for more details. |
256 | 260 |
|
257 | 261 |
///@{ |
258 | 262 |
|
259 | 263 |
///Returns \c true if \c a is \e surely strictly less than \c b |
260 | 264 |
static bool less(Value a,Value b) { return a<b;} |
261 | 265 |
///Returns \c true if \c a is \e surely different from \c b |
262 | 266 |
static bool different(Value a,Value b) { return a!=b; } |
263 | 267 |
///Returns \c true if \c a is \e surely positive |
264 | 268 |
static bool positive(Value a) { return 0<a; } |
265 | 269 |
///Returns \c true if \c a is \e surely negative |
266 | 270 |
static bool negative(Value a) { return 0>a; } |
267 | 271 |
///Returns \c true if \c a is \e surely non-zero |
268 |
static bool nonZero(Value a) { return a!=0; } |
|
272 |
static bool nonZero(Value a) { return a!=0; } |
|
269 | 273 |
|
270 | 274 |
///@} |
271 | 275 |
|
272 | 276 |
///Returns zero |
273 | 277 |
static Value zero() {return 0;} |
274 | 278 |
}; |
275 | 279 |
|
276 | 280 |
///Unsigned integer specialization of \ref Tolerance. |
277 | 281 |
|
278 | 282 |
///Unsigned integer specialization of \ref Tolerance. |
279 | 283 |
///\sa Tolerance |
280 | 284 |
template<> |
281 | 285 |
class Tolerance<unsigned int> |
282 | 286 |
{ |
283 | 287 |
public: |
284 | 288 |
///\e |
285 | 289 |
typedef unsigned int Value; |
286 | 290 |
|
287 | 291 |
///\name Comparisons |
288 | 292 |
///See \ref Tolerance for more details. |
289 | 293 |
|
290 | 294 |
///@{ |
291 | 295 |
|
292 | 296 |
///Returns \c true if \c a is \e surely strictly less than \c b |
293 | 297 |
static bool less(Value a,Value b) { return a<b;} |
294 | 298 |
///Returns \c true if \c a is \e surely different from \c b |
295 | 299 |
static bool different(Value a,Value b) { return a!=b; } |
296 | 300 |
///Returns \c true if \c a is \e surely positive |
297 | 301 |
static bool positive(Value a) { return 0<a; } |
298 | 302 |
///Returns \c true if \c a is \e surely negative |
299 | 303 |
static bool negative(Value) { return false; } |
300 | 304 |
///Returns \c true if \c a is \e surely non-zero |
301 |
static bool nonZero(Value a) { return a!=0; } |
|
305 |
static bool nonZero(Value a) { return a!=0; } |
|
302 | 306 |
|
303 | 307 |
///@} |
304 | 308 |
|
305 | 309 |
///Returns zero |
306 | 310 |
static Value zero() {return 0;} |
307 | 311 |
}; |
308 | 312 |
|
309 | 313 |
|
310 | 314 |
///Long integer specialization of \ref Tolerance. |
311 | 315 |
|
312 | 316 |
///Long integer specialization of \ref Tolerance. |
313 | 317 |
///\sa Tolerance |
314 | 318 |
template<> |
315 | 319 |
class Tolerance<long int> |
316 | 320 |
{ |
317 | 321 |
public: |
318 | 322 |
///\e |
319 | 323 |
typedef long int Value; |
320 | 324 |
|
321 | 325 |
///\name Comparisons |
322 | 326 |
///See \ref Tolerance for more details. |
323 | 327 |
|
324 | 328 |
///@{ |
325 | 329 |
|
326 | 330 |
///Returns \c true if \c a is \e surely strictly less than \c b |
327 | 331 |
static bool less(Value a,Value b) { return a<b;} |
328 | 332 |
///Returns \c true if \c a is \e surely different from \c b |
329 | 333 |
static bool different(Value a,Value b) { return a!=b; } |
330 | 334 |
///Returns \c true if \c a is \e surely positive |
331 | 335 |
static bool positive(Value a) { return 0<a; } |
332 | 336 |
///Returns \c true if \c a is \e surely negative |
333 | 337 |
static bool negative(Value a) { return 0>a; } |
334 | 338 |
///Returns \c true if \c a is \e surely non-zero |
335 |
static bool nonZero(Value a) { return a!=0;} |
|
339 |
static bool nonZero(Value a) { return a!=0;} |
|
336 | 340 |
|
337 | 341 |
///@} |
338 | 342 |
|
339 | 343 |
///Returns zero |
340 | 344 |
static Value zero() {return 0;} |
341 | 345 |
}; |
342 | 346 |
|
343 | 347 |
///Unsigned long integer specialization of \ref Tolerance. |
344 | 348 |
|
345 | 349 |
///Unsigned long integer specialization of \ref Tolerance. |
346 | 350 |
///\sa Tolerance |
347 | 351 |
template<> |
348 | 352 |
class Tolerance<unsigned long int> |
349 | 353 |
{ |
350 | 354 |
public: |
351 | 355 |
///\e |
352 | 356 |
typedef unsigned long int Value; |
353 | 357 |
|
354 | 358 |
///\name Comparisons |
355 | 359 |
///See \ref Tolerance for more details. |
356 | 360 |
|
357 | 361 |
///@{ |
358 | 362 |
|
359 | 363 |
///Returns \c true if \c a is \e surely strictly less than \c b |
360 | 364 |
static bool less(Value a,Value b) { return a<b;} |
361 | 365 |
///Returns \c true if \c a is \e surely different from \c b |
362 | 366 |
static bool different(Value a,Value b) { return a!=b; } |
363 | 367 |
///Returns \c true if \c a is \e surely positive |
364 | 368 |
static bool positive(Value a) { return 0<a; } |
365 | 369 |
///Returns \c true if \c a is \e surely negative |
366 | 370 |
static bool negative(Value) { return false; } |
367 | 371 |
///Returns \c true if \c a is \e surely non-zero |
368 |
static bool nonZero(Value a) { return a!=0;} |
|
372 |
static bool nonZero(Value a) { return a!=0;} |
|
369 | 373 |
|
370 | 374 |
///@} |
371 | 375 |
|
372 | 376 |
///Returns zero |
373 | 377 |
static Value zero() {return 0;} |
374 | 378 |
}; |
375 | 379 |
|
376 | 380 |
#if defined __GNUC__ && !defined __STRICT_ANSI__ |
377 | 381 |
|
378 | 382 |
///Long long integer specialization of \ref Tolerance. |
379 | 383 |
|
380 | 384 |
///Long long integer specialization of \ref Tolerance. |
381 | 385 |
///\warning This class (more exactly, type <tt>long long</tt>) |
382 | 386 |
///is not ansi compatible. |
383 | 387 |
///\sa Tolerance |
384 | 388 |
template<> |
385 | 389 |
class Tolerance<long long int> |
386 | 390 |
{ |
387 | 391 |
public: |
388 | 392 |
///\e |
389 | 393 |
typedef long long int Value; |
390 | 394 |
|
391 | 395 |
///\name Comparisons |
392 | 396 |
///See \ref Tolerance for more details. |
393 | 397 |
|
394 | 398 |
///@{ |
395 | 399 |
|
396 | 400 |
///Returns \c true if \c a is \e surely strictly less than \c b |
397 | 401 |
static bool less(Value a,Value b) { return a<b;} |
398 | 402 |
///Returns \c true if \c a is \e surely different from \c b |
399 | 403 |
static bool different(Value a,Value b) { return a!=b; } |
400 | 404 |
///Returns \c true if \c a is \e surely positive |
401 | 405 |
static bool positive(Value a) { return 0<a; } |
402 | 406 |
///Returns \c true if \c a is \e surely negative |
403 | 407 |
static bool negative(Value a) { return 0>a; } |
404 | 408 |
///Returns \c true if \c a is \e surely non-zero |
405 |
static bool nonZero(Value a) { return a!=0;} |
|
409 |
static bool nonZero(Value a) { return a!=0;} |
|
406 | 410 |
|
407 | 411 |
///@} |
408 | 412 |
|
409 | 413 |
///Returns zero |
410 | 414 |
static Value zero() {return 0;} |
411 | 415 |
}; |
412 | 416 |
|
413 | 417 |
///Unsigned long long integer specialization of \ref Tolerance. |
414 | 418 |
|
415 | 419 |
///Unsigned long long integer specialization of \ref Tolerance. |
416 | 420 |
///\warning This class (more exactly, type <tt>unsigned long long</tt>) |
417 | 421 |
///is not ansi compatible. |
418 | 422 |
///\sa Tolerance |
419 | 423 |
template<> |
420 | 424 |
class Tolerance<unsigned long long int> |
421 | 425 |
{ |
422 | 426 |
public: |
423 | 427 |
///\e |
424 | 428 |
typedef unsigned long long int Value; |
425 | 429 |
|
426 | 430 |
///\name Comparisons |
427 | 431 |
///See \ref Tolerance for more details. |
428 | 432 |
|
429 | 433 |
///@{ |
430 | 434 |
|
431 | 435 |
///Returns \c true if \c a is \e surely strictly less than \c b |
432 | 436 |
static bool less(Value a,Value b) { return a<b;} |
433 | 437 |
///Returns \c true if \c a is \e surely different from \c b |
434 | 438 |
static bool different(Value a,Value b) { return a!=b; } |
435 | 439 |
///Returns \c true if \c a is \e surely positive |
436 | 440 |
static bool positive(Value a) { return 0<a; } |
437 | 441 |
///Returns \c true if \c a is \e surely negative |
438 | 442 |
static bool negative(Value) { return false; } |
439 | 443 |
///Returns \c true if \c a is \e surely non-zero |
440 |
static bool nonZero(Value a) { return a!=0;} |
|
444 |
static bool nonZero(Value a) { return a!=0;} |
|
441 | 445 |
|
442 | 446 |
///@} |
443 | 447 |
|
444 | 448 |
///Returns zero |
445 | 449 |
static Value zero() {return 0;} |
446 | 450 |
}; |
447 | 451 |
|
448 | 452 |
#endif |
449 | 453 |
|
450 | 454 |
/// @} |
451 | 455 |
|
452 | 456 |
} //namespace lemon |
453 | 457 |
|
454 | 458 |
#endif //LEMON_TOLERANCE_H |
0 comments (0 inline)