]> git.lizzy.rs Git - rust.git/blob - src/etc/ziggurat_tables.py
std::rand: correct an off-by-one in the Ziggurat code.
[rust.git] / src / etc / ziggurat_tables.py
1 #!/usr/bin/env python
2 # xfail-license
3
4 # This creates the tables used for distributions implemented using the
5 # ziggurat algorithm in `std::rand::distributions;`. They are
6 # (basically) the tables as used in the ZIGNOR variant (Doornik 2005).
7 # They are changed rarely, so the generated file should be checked in
8 # to git.
9 #
10 # It creates 3 tables: X as in the paper, F which is f(x_i), and
11 # F_DIFF which is f(x_i) - f(x_{i-1}). The latter two are just cached
12 # values which is not done in that paper (but is done in other
13 # variants). Note that the adZigR table is unnecessary because of
14 # algebra.
15 #
16 # It is designed to be compatible with Python 2 and 3.
17
18 from math import exp, sqrt, log, floor
19 import random
20
21 # The order should match the return value of `tables`
22 TABLE_NAMES = ['X', 'F']
23
24 # The actual length of the table is 1 more, to stop
25 # index-out-of-bounds errors. This should match the bitwise operation
26 # to find `i` in `zigurrat` in `libstd/rand/mod.rs`. Also the *_R and
27 # *_V constants below depend on this value.
28 TABLE_LEN = 256
29
30 # equivalent to `zigNorInit` in Doornik2005, but generalised to any
31 # distribution. r = dR, v = dV, f = probability density function,
32 # f_inv = inverse of f
33 def tables(r, v, f, f_inv):
34     # compute the x_i
35     xvec = [0]*(TABLE_LEN+1)
36
37     xvec[0] = v / f(r)
38     xvec[1] = r
39
40     for i in range(2, TABLE_LEN):
41         last = xvec[i-1]
42         xvec[i] = f_inv(v / last + f(last))
43
44     # cache the f's
45     fvec = [0]*(TABLE_LEN+1)
46     for i in range(TABLE_LEN+1):
47         fvec[i] = f(xvec[i])
48
49     return xvec, fvec
50
51 # Distributions
52 # N(0, 1)
53 def norm_f(x):
54     return exp(-x*x/2.0)
55 def norm_f_inv(y):
56     return sqrt(-2.0*log(y))
57
58 NORM_R = 3.6541528853610088
59 NORM_V = 0.00492867323399
60
61 NORM = tables(NORM_R, NORM_V,
62               norm_f, norm_f_inv)
63
64 # Exp(1)
65 def exp_f(x):
66     return exp(-x)
67 def exp_f_inv(y):
68     return -log(y)
69
70 EXP_R = 7.69711747013104972
71 EXP_V = 0.0039496598225815571993
72
73 EXP = tables(EXP_R, EXP_V,
74              exp_f, exp_f_inv)
75
76
77 # Output the tables/constants/types
78
79 def render_static(name, type, value):
80     # no space or
81     return 'pub static %s: %s =%s;\n' % (name, type, value)
82
83 # static `name`: [`type`, .. `len(values)`] =
84 #     [values[0], ..., values[3],
85 #      values[4], ..., values[7],
86 #      ... ];
87 def render_table(name, values):
88     rows = []
89     # 4 values on each row
90     for i in range(0, len(values), 4):
91         row = values[i:i+4]
92         rows.append(', '.join('%.18f' % f for f in row))
93
94     rendered = '\n    [%s]' % ',\n     '.join(rows)
95     return render_static(name, '[f64, .. %d]' % len(values), rendered)
96
97
98 with open('ziggurat_tables.rs', 'w') as f:
99     f.write('''// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
100 // file at the top-level directory of this distribution and at
101 // http://rust-lang.org/COPYRIGHT.
102 //
103 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
104 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
105 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
106 // option. This file may not be copied, modified, or distributed
107 // except according to those terms.
108
109 // Tables for distributions which are sampled using the ziggurat
110 // algorithm. Autogenerated by `ziggurat_tables.py`.
111
112 pub type ZigTable = &\'static [f64, .. %d];
113 '''  % (TABLE_LEN + 1))
114     for name, tables, r in [('NORM', NORM, NORM_R),
115                             ('EXP', EXP, EXP_R)]:
116         f.write(render_static('ZIG_%s_R' % name, 'f64', ' %.18f' % r))
117         for (tabname, table) in zip(TABLE_NAMES, tables):
118             f.write(render_table('ZIG_%s_%s' % (name, tabname), table))