codekingpro/portable-devtools
114k
1#ifndef NUMPY_CORE_INCLUDE_NUMPY_RANDOM_DISTRIBUTIONS_H_
2#define NUMPY_CORE_INCLUDE_NUMPY_RANDOM_DISTRIBUTIONS_H_
3
4#ifdef __cplusplus
5extern "C" {
6#endif
7
8#include <Python.h>
9#include "numpy/npy_common.h"
10#include <stddef.h>
11#include <stdbool.h>
12#include <stdint.h>
13
14#include "numpy/npy_math.h"
15#include "numpy/random/bitgen.h"
16
17/*
18 * RAND_INT_TYPE is used to share integer generators with RandomState which
19 * used long in place of int64_t. If changing a distribution that uses
20 * RAND_INT_TYPE, then the original unmodified copy must be retained for
21 * use in RandomState by copying to the legacy distributions source file.
22 */
23#ifdef NP_RANDOM_LEGACY
24#define RAND_INT_TYPE long
25#define RAND_INT_MAX LONG_MAX
26#else
27#define RAND_INT_TYPE int64_t
28#define RAND_INT_MAX INT64_MAX
29#endif
30
31#ifdef _MSC_VER
32#define DECLDIR __declspec(dllexport)
33#else
34#define DECLDIR extern
35#endif
36
37#ifndef MIN
38#define MIN(x, y) (((x) < (y)) ? x : y)
39#define MAX(x, y) (((x) > (y)) ? x : y)
40#endif
41
42#ifndef M_PI
43#define M_PI 3.14159265358979323846264338328
44#endif
45
46typedef struct s_binomial_t {
47 int has_binomial; /* !=0: following parameters initialized for binomial */
48 double psave;
49 RAND_INT_TYPE nsave;
50 double r;
51 double q;
52 double fm;
53 RAND_INT_TYPE m;
54 double p1;
55 double xm;
56 double xl;
57 double xr;
58 double c;
59 double laml;
60 double lamr;
61 double p2;
62 double p3;
63 double p4;
64} binomial_t;
65
66DECLDIR float random_standard_uniform_f(bitgen_t *bitgen_state);
67DECLDIR double random_standard_uniform(bitgen_t *bitgen_state);
68DECLDIR void random_standard_uniform_fill(bitgen_t *, npy_intp, double *);
69DECLDIR void random_standard_uniform_fill_f(bitgen_t *, npy_intp, float *);
70
71DECLDIR int64_t random_positive_int64(bitgen_t *bitgen_state);
72DECLDIR int32_t random_positive_int32(bitgen_t *bitgen_state);
73DECLDIR int64_t random_positive_int(bitgen_t *bitgen_state);
74DECLDIR uint64_t random_uint(bitgen_t *bitgen_state);
75
76DECLDIR double random_standard_exponential(bitgen_t *bitgen_state);
77DECLDIR float random_standard_exponential_f(bitgen_t *bitgen_state);
78DECLDIR void random_standard_exponential_fill(bitgen_t *, npy_intp, double *);
79DECLDIR void random_standard_exponential_fill_f(bitgen_t *, npy_intp, float *);
80DECLDIR void random_standard_exponential_inv_fill(bitgen_t *, npy_intp, double *);
81DECLDIR void random_standard_exponential_inv_fill_f(bitgen_t *, npy_intp, float *);
82
83DECLDIR double random_standard_normal(bitgen_t *bitgen_state);
84DECLDIR float random_standard_normal_f(bitgen_t *bitgen_state);
85DECLDIR void random_standard_normal_fill(bitgen_t *, npy_intp, double *);
86DECLDIR void random_standard_normal_fill_f(bitgen_t *, npy_intp, float *);
87DECLDIR double random_standard_gamma(bitgen_t *bitgen_state, double shape);
88DECLDIR float random_standard_gamma_f(bitgen_t *bitgen_state, float shape);
89
90DECLDIR double random_normal(bitgen_t *bitgen_state, double loc, double scale);
91
92DECLDIR double random_gamma(bitgen_t *bitgen_state, double shape, double scale);
93DECLDIR float random_gamma_f(bitgen_t *bitgen_state, float shape, float scale);
94
95DECLDIR double random_exponential(bitgen_t *bitgen_state, double scale);
96DECLDIR double random_uniform(bitgen_t *bitgen_state, double lower, double range);
97DECLDIR double random_beta(bitgen_t *bitgen_state, double a, double b);
98DECLDIR double random_chisquare(bitgen_t *bitgen_state, double df);
99DECLDIR double random_f(bitgen_t *bitgen_state, double dfnum, double dfden);
100DECLDIR double random_standard_cauchy(bitgen_t *bitgen_state);
101DECLDIR double random_pareto(bitgen_t *bitgen_state, double a);
102DECLDIR double random_weibull(bitgen_t *bitgen_state, double a);
103DECLDIR double random_power(bitgen_t *bitgen_state, double a);
104DECLDIR double random_laplace(bitgen_t *bitgen_state, double loc, double scale);
105DECLDIR double random_gumbel(bitgen_t *bitgen_state, double loc, double scale);
106DECLDIR double random_logistic(bitgen_t *bitgen_state, double loc, double scale);
107DECLDIR double random_lognormal(bitgen_t *bitgen_state, double mean, double sigma);
108DECLDIR double random_rayleigh(bitgen_t *bitgen_state, double mode);
109DECLDIR double random_standard_t(bitgen_t *bitgen_state, double df);
110DECLDIR double random_noncentral_chisquare(bitgen_t *bitgen_state, double df,
111 double nonc);
112DECLDIR double random_noncentral_f(bitgen_t *bitgen_state, double dfnum,
113 double dfden, double nonc);
114DECLDIR double random_wald(bitgen_t *bitgen_state, double mean, double scale);
115DECLDIR double random_vonmises(bitgen_t *bitgen_state, double mu, double kappa);
116DECLDIR double random_triangular(bitgen_t *bitgen_state, double left, double mode,
117 double right);
118
119DECLDIR RAND_INT_TYPE random_poisson(bitgen_t *bitgen_state, double lam);
120DECLDIR RAND_INT_TYPE random_negative_binomial(bitgen_t *bitgen_state, double n,
121 double p);
122
123DECLDIR int64_t random_binomial(bitgen_t *bitgen_state, double p,
124 int64_t n, binomial_t *binomial);
125
126DECLDIR int64_t random_logseries(bitgen_t *bitgen_state, double p);
127DECLDIR int64_t random_geometric(bitgen_t *bitgen_state, double p);
128DECLDIR RAND_INT_TYPE random_geometric_search(bitgen_t *bitgen_state, double p);
129DECLDIR RAND_INT_TYPE random_zipf(bitgen_t *bitgen_state, double a);
130DECLDIR int64_t random_hypergeometric(bitgen_t *bitgen_state,
131 int64_t good, int64_t bad, int64_t sample);
132DECLDIR uint64_t random_interval(bitgen_t *bitgen_state, uint64_t max);
133
134/* Generate random uint64 numbers in closed interval [off, off + rng]. */
135DECLDIR uint64_t random_bounded_uint64(bitgen_t *bitgen_state, uint64_t off,
136 uint64_t rng, uint64_t mask,
137 bool use_masked);
138
139/* Generate random uint32 numbers in closed interval [off, off + rng]. */
140DECLDIR uint32_t random_buffered_bounded_uint32(bitgen_t *bitgen_state,
141 uint32_t off, uint32_t rng,
142 uint32_t mask, bool use_masked,
143 int *bcnt, uint32_t *buf);
144DECLDIR uint16_t random_buffered_bounded_uint16(bitgen_t *bitgen_state,
145 uint16_t off, uint16_t rng,
146 uint16_t mask, bool use_masked,
147 int *bcnt, uint32_t *buf);
148DECLDIR uint8_t random_buffered_bounded_uint8(bitgen_t *bitgen_state, uint8_t off,
149 uint8_t rng, uint8_t mask,
150 bool use_masked, int *bcnt,
151 uint32_t *buf);
152DECLDIR npy_bool random_buffered_bounded_bool(bitgen_t *bitgen_state, npy_bool off,
153 npy_bool rng, npy_bool mask,
154 bool use_masked, int *bcnt,
155 uint32_t *buf);
156
157DECLDIR void random_bounded_uint64_fill(bitgen_t *bitgen_state, uint64_t off,
158 uint64_t rng, npy_intp cnt,
159 bool use_masked, uint64_t *out);
160DECLDIR void random_bounded_uint32_fill(bitgen_t *bitgen_state, uint32_t off,
161 uint32_t rng, npy_intp cnt,
162 bool use_masked, uint32_t *out);
163DECLDIR void random_bounded_uint16_fill(bitgen_t *bitgen_state, uint16_t off,
164 uint16_t rng, npy_intp cnt,
165 bool use_masked, uint16_t *out);
166DECLDIR void random_bounded_uint8_fill(bitgen_t *bitgen_state, uint8_t off,
167 uint8_t rng, npy_intp cnt,
168 bool use_masked, uint8_t *out);
169DECLDIR void random_bounded_bool_fill(bitgen_t *bitgen_state, npy_bool off,
170 npy_bool rng, npy_intp cnt,
171 bool use_masked, npy_bool *out);
172
173DECLDIR void random_multinomial(bitgen_t *bitgen_state, RAND_INT_TYPE n, RAND_INT_TYPE *mnix,
174 double *pix, npy_intp d, binomial_t *binomial);
175
176/* multivariate hypergeometric, "count" method */
177DECLDIR int random_multivariate_hypergeometric_count(bitgen_t *bitgen_state,
178 int64_t total,
179 size_t num_colors, int64_t *colors,
180 int64_t nsample,
181 size_t num_variates, int64_t *variates);
182
183/* multivariate hypergeometric, "marginals" method */
184DECLDIR void random_multivariate_hypergeometric_marginals(bitgen_t *bitgen_state,
185 int64_t total,
186 size_t num_colors, int64_t *colors,
187 int64_t nsample,
188 size_t num_variates, int64_t *variates);
189
190/* Common to legacy-distributions.c and distributions.c but not exported */
191
192RAND_INT_TYPE random_binomial_btpe(bitgen_t *bitgen_state,
193 RAND_INT_TYPE n,
194 double p,
195 binomial_t *binomial);
196RAND_INT_TYPE random_binomial_inversion(bitgen_t *bitgen_state,
197 RAND_INT_TYPE n,
198 double p,
199 binomial_t *binomial);
200double random_loggam(double x);
201static inline double next_double(bitgen_t *bitgen_state) {
202 return bitgen_state->next_double(bitgen_state->state);
203}
204
205#ifdef __cplusplus
206}
207#endif
208
209#endif /* NUMPY_CORE_INCLUDE_NUMPY_RANDOM_DISTRIBUTIONS_H_ */
210 