| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
/* randist/gauss.c |
|
2
|
|
|
|
|
|
|
* |
|
3
|
|
|
|
|
|
|
* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2006, 2007 James Theiler, Brian Gough |
|
4
|
|
|
|
|
|
|
* Copyright (C) 2006 Charles Karney |
|
5
|
|
|
|
|
|
|
* |
|
6
|
|
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
7
|
|
|
|
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or (at |
|
9
|
|
|
|
|
|
|
* your option) any later version. |
|
10
|
|
|
|
|
|
|
* |
|
11
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but |
|
12
|
|
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14
|
|
|
|
|
|
|
* General Public License for more details. |
|
15
|
|
|
|
|
|
|
* |
|
16
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License |
|
17
|
|
|
|
|
|
|
* along with this program; if not, write to the Free Software |
|
18
|
|
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
19
|
|
|
|
|
|
|
*/ |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
#include <math.h> |
|
22
|
|
|
|
|
|
|
#include "sandy_rng.h" |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
/* Of the two methods provided below, I think the Polar method is more |
|
25
|
|
|
|
|
|
|
* efficient, but only when you are actually producing two random |
|
26
|
|
|
|
|
|
|
* deviates. We don't produce two, because then we'd have to save one |
|
27
|
|
|
|
|
|
|
* in a static variable for the next call, and that would screws up |
|
28
|
|
|
|
|
|
|
* re-entrant or threaded code, so we only produce one. This makes |
|
29
|
|
|
|
|
|
|
* the Ratio method suddenly more appealing. |
|
30
|
|
|
|
|
|
|
* |
|
31
|
|
|
|
|
|
|
* [Added by Charles Karney] We use Leva's implementation of the Ratio |
|
32
|
|
|
|
|
|
|
* method which avoids calling log() nearly all the time and makes the |
|
33
|
|
|
|
|
|
|
* Ratio method faster than the Polar method (when it produces just one |
|
34
|
|
|
|
|
|
|
* result per call). Timing per call (gcc -O2 on 866MHz Pentium, |
|
35
|
|
|
|
|
|
|
* average over 10^8 calls) |
|
36
|
|
|
|
|
|
|
* |
|
37
|
|
|
|
|
|
|
* Polar method: 660 ns |
|
38
|
|
|
|
|
|
|
* Ratio method: 368 ns |
|
39
|
|
|
|
|
|
|
* |
|
40
|
|
|
|
|
|
|
*/ |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
/* Polar (Box-Mueller) method; See Knuth v2, 3rd ed, p122 */ |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
double |
|
45
|
3332
|
|
|
|
|
|
gsl_ran_gaussian (const gsl_rng * r, const double sigma) |
|
46
|
|
|
|
|
|
|
{ |
|
47
|
|
|
|
|
|
|
double x, y, r2; |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
do |
|
50
|
|
|
|
|
|
|
{ |
|
51
|
|
|
|
|
|
|
/* choose x,y in uniform square (-1,-1) to (+1,+1) */ |
|
52
|
4179
|
|
|
|
|
|
x = -1 + 2 * gsl_rng_uniform_pos (r); |
|
53
|
4179
|
|
|
|
|
|
y = -1 + 2 * gsl_rng_uniform_pos (r); |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
/* see if it is in the unit circle */ |
|
56
|
4179
|
|
|
|
|
|
r2 = x * x + y * y; |
|
57
|
|
|
|
|
|
|
} |
|
58
|
4179
|
100
|
|
|
|
|
while (r2 > 1.0 || r2 == 0); |
|
|
|
50
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
/* Box-Muller transform */ |
|
61
|
3332
|
|
|
|
|
|
return sigma * y * sqrt (-2.0 * log (r2) / r2); |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
/* Ratio method (Kinderman-Monahan); see Knuth v2, 3rd ed, p130. |
|
65
|
|
|
|
|
|
|
* K+M, ACM Trans Math Software 3 (1977) 257-260. |
|
66
|
|
|
|
|
|
|
* |
|
67
|
|
|
|
|
|
|
* [Added by Charles Karney] This is an implementation of Leva's |
|
68
|
|
|
|
|
|
|
* modifications to the original K+M method; see: |
|
69
|
|
|
|
|
|
|
* J. L. Leva, ACM Trans Math Software 18 (1992) 449-453 and 454-455. */ |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
double |
|
72
|
0
|
|
|
|
|
|
gsl_ran_gaussian_ratio_method (const gsl_rng * r, const double sigma) |
|
73
|
|
|
|
|
|
|
{ |
|
74
|
|
|
|
|
|
|
double u, v, x, y, Q; |
|
75
|
0
|
|
|
|
|
|
const double s = 0.449871; /* Constants from Leva */ |
|
76
|
0
|
|
|
|
|
|
const double t = -0.386595; |
|
77
|
0
|
|
|
|
|
|
const double a = 0.19600; |
|
78
|
0
|
|
|
|
|
|
const double b = 0.25472; |
|
79
|
0
|
|
|
|
|
|
const double r1 = 0.27597; |
|
80
|
0
|
|
|
|
|
|
const double r2 = 0.27846; |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
do /* This loop is executed 1.369 times on average */ |
|
83
|
|
|
|
|
|
|
{ |
|
84
|
|
|
|
|
|
|
/* Generate a point P = (u, v) uniform in a rectangle enclosing |
|
85
|
|
|
|
|
|
|
the K+M region v^2 <= - 4 u^2 log(u). */ |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
/* u in (0, 1] to avoid singularity at u = 0 */ |
|
88
|
0
|
|
|
|
|
|
u = 1 - gsl_rng_uniform (r); |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
/* v is in the asymmetric interval [-0.5, 0.5). However v = -0.5 |
|
91
|
|
|
|
|
|
|
is rejected in the last part of the while clause. The |
|
92
|
|
|
|
|
|
|
resulting normal deviate is strictly symmetric about 0 |
|
93
|
|
|
|
|
|
|
(provided that v is symmetric once v = -0.5 is excluded). */ |
|
94
|
0
|
|
|
|
|
|
v = gsl_rng_uniform (r) - 0.5; |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
/* Constant 1.7156 > sqrt(8/e) (for accuracy); but not by too |
|
97
|
|
|
|
|
|
|
much (for efficiency). */ |
|
98
|
0
|
|
|
|
|
|
v *= 1.7156; |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
/* Compute Leva's quadratic form Q */ |
|
101
|
0
|
|
|
|
|
|
x = u - s; |
|
102
|
0
|
|
|
|
|
|
y = fabs (v) - t; |
|
103
|
0
|
|
|
|
|
|
Q = x * x + y * (a * y - b * x); |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
/* Accept P if Q < r1 (Leva) */ |
|
106
|
|
|
|
|
|
|
/* Reject P if Q > r2 (Leva) */ |
|
107
|
|
|
|
|
|
|
/* Accept if v^2 <= -4 u^2 log(u) (K+M) */ |
|
108
|
|
|
|
|
|
|
/* This final test is executed 0.012 times on average. */ |
|
109
|
|
|
|
|
|
|
} |
|
110
|
0
|
0
|
|
|
|
|
while (Q >= r1 && (Q > r2 || v * v > -4 * u * u * log (u))); |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
return sigma * (v / u); /* Return slope */ |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
double |
|
116
|
0
|
|
|
|
|
|
gsl_ran_gaussian_pdf (const double x, const double sigma) |
|
117
|
|
|
|
|
|
|
{ |
|
118
|
0
|
|
|
|
|
|
double u = x / fabs (sigma); |
|
119
|
0
|
|
|
|
|
|
double p = (1 / (sqrt (2 * M_PI) * fabs (sigma))) * exp (-u * u / 2); |
|
120
|
0
|
|
|
|
|
|
return p; |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
double |
|
124
|
0
|
|
|
|
|
|
gsl_ran_ugaussian (const gsl_rng * r) |
|
125
|
|
|
|
|
|
|
{ |
|
126
|
0
|
|
|
|
|
|
return gsl_ran_gaussian (r, 1.0); |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
double |
|
130
|
0
|
|
|
|
|
|
gsl_ran_ugaussian_ratio_method (const gsl_rng * r) |
|
131
|
|
|
|
|
|
|
{ |
|
132
|
0
|
|
|
|
|
|
return gsl_ran_gaussian_ratio_method (r, 1.0); |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
double |
|
136
|
0
|
|
|
|
|
|
gsl_ran_ugaussian_pdf (const double x) |
|
137
|
|
|
|
|
|
|
{ |
|
138
|
0
|
|
|
|
|
|
return gsl_ran_gaussian_pdf (x, 1.0); |
|
139
|
|
|
|
|
|
|
} |
|
140
|
|
|
|
|
|
|
|