1 | /***************************************************************************
|
---|
2 | * blitz/rand-normal.h Random Gaussian (Normal) generator
|
---|
3 | *
|
---|
4 | * $Id: rand-normal.h,v 1.1.1.1 1999-04-09 17:58:59 ansari Exp $
|
---|
5 | *
|
---|
6 | * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
|
---|
7 | *
|
---|
8 | * This program is free software; you can redistribute it and/or
|
---|
9 | * modify it under the terms of the GNU General Public License
|
---|
10 | * as published by the Free Software Foundation; either version 2
|
---|
11 | * of the License, or (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This program is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * Suggestions: blitz-suggest@cybervision.com
|
---|
19 | * Bugs: blitz-bugs@cybervision.com
|
---|
20 | *
|
---|
21 | * For more information, please see the Blitz++ Home Page:
|
---|
22 | * http://seurat.uwaterloo.ca/blitz/
|
---|
23 | *
|
---|
24 | ***************************************************************************
|
---|
25 | *
|
---|
26 | * This generator transforms a (0,1] uniform distribution into
|
---|
27 | * a Normal distribution. Let u,v be (0,1] random variables. Then
|
---|
28 | *
|
---|
29 | * x = sqrt(-2 ln v) cos(pi (2u-1))
|
---|
30 | *
|
---|
31 | * is N(0,1) distributed.
|
---|
32 | *
|
---|
33 | * Reference: Athanasios Papoulis, "Probability, random variables,
|
---|
34 | * and stochastic processes," McGraw-Hill : Toronto, 1991.
|
---|
35 | *
|
---|
36 | ***************************************************************************
|
---|
37 | *
|
---|
38 | * $Log: not supported by cvs2svn $
|
---|
39 | * Revision 1.4 1998/03/14 00:04:47 tveldhui
|
---|
40 | * 0.2-alpha-05
|
---|
41 | *
|
---|
42 | * Revision 1.3 1997/07/16 14:51:20 tveldhui
|
---|
43 | * Update: Alpha release 0.2 (Arrays)
|
---|
44 | *
|
---|
45 | * Revision 1.2 1997/01/24 14:42:00 tveldhui
|
---|
46 | * Periodic RCS update
|
---|
47 | *
|
---|
48 | */
|
---|
49 |
|
---|
50 | #ifndef BZ_RAND_NORMAL_H
|
---|
51 | #define BZ_RAND_NORMAL_H
|
---|
52 |
|
---|
53 | #ifndef BZ_RANDOM_H
|
---|
54 | #include <blitz/random.h>
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | #ifndef BZ_RAND_UNIFORM_H
|
---|
58 | #include <blitz/rand-uniform.h>
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | #include <math.h>
|
---|
62 |
|
---|
63 | BZ_NAMESPACE(blitz)
|
---|
64 |
|
---|
65 | template<class P_uniform BZ_TEMPLATE_DEFAULT(Uniform)>
|
---|
66 | class Normal {
|
---|
67 |
|
---|
68 | public:
|
---|
69 | typedef double T_numtype;
|
---|
70 |
|
---|
71 | Normal(double mean = 0.0, double variance = 1.0, double = 0.0)
|
---|
72 | : mean_(mean), sigma_(::sqrt(variance))
|
---|
73 | {
|
---|
74 | }
|
---|
75 |
|
---|
76 | void randomize()
|
---|
77 | {
|
---|
78 | uniform_.randomize();
|
---|
79 | }
|
---|
80 |
|
---|
81 | double random()
|
---|
82 | {
|
---|
83 | double u, v;
|
---|
84 |
|
---|
85 | do {
|
---|
86 | u = uniform_.random();
|
---|
87 | v = uniform_.random();
|
---|
88 | } while (v == 0);
|
---|
89 |
|
---|
90 | return mean_ + sigma_ * ::sqrt(-2*::log(v)) * ::cos(M_PI * (2*u - 1));
|
---|
91 | }
|
---|
92 |
|
---|
93 | private:
|
---|
94 | double mean_, sigma_;
|
---|
95 | P_uniform uniform_;
|
---|
96 | };
|
---|
97 |
|
---|
98 | BZ_NAMESPACE_END
|
---|
99 |
|
---|
100 | #endif // BZ_RAND_NORMAL_H
|
---|
101 |
|
---|