| 1 | /*************************************************************************** | 
|---|
| 2 | * blitz/rand-normal.h    Random Gaussian (Normal) generator | 
|---|
| 3 | * | 
|---|
| 4 | * $Id: rand-normal.h,v 1.1.1.1 1999-11-26 16:37:04 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.1.1.1  1999/04/09  17:58:59  ansari | 
|---|
| 40 | * Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99 | 
|---|
| 41 | * | 
|---|
| 42 | * Revision 1.4  1998/03/14 00:04:47  tveldhui | 
|---|
| 43 | * 0.2-alpha-05 | 
|---|
| 44 | * | 
|---|
| 45 | * Revision 1.3  1997/07/16 14:51:20  tveldhui | 
|---|
| 46 | * Update: Alpha release 0.2 (Arrays) | 
|---|
| 47 | * | 
|---|
| 48 | * Revision 1.2  1997/01/24 14:42:00  tveldhui | 
|---|
| 49 | * Periodic RCS update | 
|---|
| 50 | * | 
|---|
| 51 | */ | 
|---|
| 52 |  | 
|---|
| 53 | #ifndef BZ_RAND_NORMAL_H | 
|---|
| 54 | #define BZ_RAND_NORMAL_H | 
|---|
| 55 |  | 
|---|
| 56 | #ifndef BZ_RANDOM_H | 
|---|
| 57 | #include <blitz/random.h> | 
|---|
| 58 | #endif | 
|---|
| 59 |  | 
|---|
| 60 | #ifndef BZ_RAND_UNIFORM_H | 
|---|
| 61 | #include <blitz/rand-uniform.h> | 
|---|
| 62 | #endif | 
|---|
| 63 |  | 
|---|
| 64 | #include <math.h> | 
|---|
| 65 |  | 
|---|
| 66 | BZ_NAMESPACE(blitz) | 
|---|
| 67 |  | 
|---|
| 68 | template<class P_uniform BZ_TEMPLATE_DEFAULT(Uniform)> | 
|---|
| 69 | class Normal { | 
|---|
| 70 |  | 
|---|
| 71 | public: | 
|---|
| 72 | typedef double T_numtype; | 
|---|
| 73 |  | 
|---|
| 74 | Normal(double mean = 0.0, double variance = 1.0, double = 0.0) | 
|---|
| 75 | : mean_(mean), sigma_(::sqrt(variance)) | 
|---|
| 76 | { | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | void randomize() | 
|---|
| 80 | { | 
|---|
| 81 | uniform_.randomize(); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | double random() | 
|---|
| 85 | { | 
|---|
| 86 | double u, v; | 
|---|
| 87 |  | 
|---|
| 88 | do { | 
|---|
| 89 | u = uniform_.random(); | 
|---|
| 90 | v = uniform_.random(); | 
|---|
| 91 | } while (v == 0); | 
|---|
| 92 |  | 
|---|
| 93 | return mean_ + sigma_ * ::sqrt(-2*::log(v)) * ::cos(M_PI * (2*u - 1)); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | private: | 
|---|
| 97 | double mean_, sigma_; | 
|---|
| 98 | P_uniform uniform_; | 
|---|
| 99 | }; | 
|---|
| 100 |  | 
|---|
| 101 | BZ_NAMESPACE_END | 
|---|
| 102 |  | 
|---|
| 103 | #endif // BZ_RAND_NORMAL_H | 
|---|
| 104 |  | 
|---|