8000 API: rearrange the cython files in numpy.random by mattip · Pull Request #14608 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

API: rearrange the cython files in numpy.random #14608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 17, 2019
Prev Previous commit
Next Next commit
API: refactor function names in distribution.{h,c}, refactor float_fill
  • Loading branch information
mattip committed Oct 11, 2019
commit fbda4849525316223d984b27f9f48bdd4ad477bb
1 change: 1 addition & 0 deletions numpy/random/_common.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ ctypedef double (*random_double_1)(void *state, double a) nogil
ctypedef double (*random_double_2)(void *state, double a, double b) nogil
ctypedef double (*random_double_3)(void *state, double a, double b, double c) nogil

ctypedef double (*random_float_fill)(bitgen_t *state, np.npy_intp count, float* out) nogil
ctypedef float (*random_float_0)(bitgen_t *state) nogil
ctypedef float (*random_float_1)(bitgen_t *state, float a) nogil

Expand Down
9 changes: 5 additions & 4 deletions numpy/random/_common.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,16 @@ cdef object double_fill(void *func, bitgen_t *state, object size, object lock, o
return out_array

cdef object float_fill(void *func, bitgen_t *state, object size, object lock, object out):
cdef random_float_0 random_func = (<random_float_0>func)
cdef random_float_fill random_func = (<random_float_fill>func)
cdef float out_val
cdef float *out_array_data
cdef np.ndarray out_array
cdef np.npy_intp i, n

if size is None and out is None:
with lock:
return random_func(state)
random_func(state, 1, &out_val)
return out_val

if out is not None:
check_output(out, np.float32, size)
Expand All @@ -280,8 +282,7 @@ cdef object float_fill(void *func, bitgen_t *state, object size, object lock, ob
n = np.PyArray_SIZE(out_array)
out_array_data = <float *>np.PyArray_DATA(out_array)
with lock, nogil:
for i in range(n):
out_array_data[i] = random_func(state)
random_func(state, n, out_array_data)
return out_array

cdef object float_fill_from_double(void *func, bitgen_t *state, object size, object lock, object out):
Expand Down
43 changes: 23 additions & 20 deletions numpy/random/generator.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,35 @@ cdef extern from "include/distributions.h":

ctypedef s_binomial_t binomial_t

double random_double(bitgen_t *bitgen_state) nogil
void random_double_fill(bitgen_t* bitgen_state, np.npy_intp cnt, double *out) nogil
double random_standard_uniform(bitgen_t *bitgen_state) nogil
void random_standard_uniform_fill(bitgen_t* bitgen_state, np.npy_intp cnt, double *out) nogil
double random_standard_exponential(bitgen_t *bitgen_state) nogil
void random_standard_exponential_fill(bitgen_t *bitgen_state, np.npy_intp cnt, double *out) nogil
double random_standard_exponential_zig(bitgen_t *bitgen_state) nogil
void random_standard_exponential_zig_fill(bitgen_t *bitgen_state, np.npy_intp cnt, double *out) nogil
double random_gauss_zig(bitgen_t* bitgen_state) nogil
void random_gauss_zig_fill(bitgen_t *bitgen_state, np.npy_intp count, double *out) nogil
double random_standard_gamma_zig(bitgen_t *bitgen_state, double shape) nogil
double random_standard_normal(bitgen_t* bitgen_state) nogil
void random_standard_normal_fill(bitgen_t *bitgen_state, np.npy_intp count, double *out) nogil
void random_standard_normal_fill_f(bitgen_t *bitgen_state, np.npy_intp count, float *out) nogil
double random_standard_gamma(bitgen_t *bitgen_state, double shape) nogil

float random_float(bitgen_t *bitgen_state) nogil
float random_standard_uniform_f(bitgen_t *bitgen_state) nogil
void random_standard_uniform_fill_f(bitgen_t* bitgen_state, np.npy_intp cnt, float *out) nogil
float random_standard_exponential_f(bitgen_t *bitgen_state) nogil
float random_standard_exponential_zig_f(bitgen_t *bitgen_state) nogil
float random_gauss_zig_f(bitgen_t* bitgen_state) nogil
void random_standard_exponential_fill_f(bitgen_t *bitgen_state, np.npy_intp cnt, float *out) nogil
void random_standard_exponential_zig_fill_f(bitgen_t *bitgen_state, np.npy_intp cnt, float *out) nogil
float random_standard_normal_f(bitgen_t* bitgen_state) nogil
float random_standard_gamma_f(bitgen_t *bitgen_state, float shape) nogil
float random_standard_gamma_zig_f(bitgen_t *bitgen_state, float shape) nogil

int64_t random_positive_int64(bitgen_t *bitgen_state) nogil
int32_t random_positive_int32(bitgen_t *bitgen_state) nogil
int64_t random_positive_int(bitgen_t *bitgen_state) nogil
uint64_t random_uint(bitgen_t *bitgen_state) nogil

double random_normal_zig(bitgen_t *bitgen_state, double loc, double scale) nogil
double random_normal(bitgen_t *bitgen_state, double loc, double scale) nogil

double random_gamma(bitgen_t *bitgen_state, double shape, double scale) nogil
float random_gamma_float(bitgen_t *bitgen_state, float shape, float scale) nogil
float random_gamma_f(bitgen_t *bitgen_state, float shape, float scale) nogil

double random_exponential(bitgen_t *bitgen_state, double scale) nogil
double random_uniform(bitgen_t *bitgen_state, double lower, double range) nogil
Expand Down Expand Up @@ -284,9 +287,9 @@ cdef class Generator:
cdef double temp
key = np.dtype(dtype).name
if key == 'float64':
return double_fill(&random_double_fill, &self._bitgen, size, self.lock, out)
return double_fill(&random_standard_uniform_fill, &self._bitgen, size, self.lock, out)
elif key == 'float32':
return float_fill(&random_float, &self._bitgen, size, self.lock, out)
return float_fill(&random_standard_uniform_fill_f, &self._bitgen, size, self.lock, out)
else:
raise TypeError('Unsupported dtype "%s" for random' % key)

Expand Down Expand Up @@ -432,9 +435,9 @@ cdef class Generator:
return double_fill(&random_standard_exponential_fill, &self._bitgen, size, self.lock, out)
elif key == 'float32':
if method == u'zig':
return float_fill(&random_standard_exponential_zig_f, &self._bitgen, size, self.lock, out)
return float_fill(&random_standard_exponential_zig_fill_f, &self._bitgen, size, self.lock, out)
else:
return float_fill(&random_standard_exponential_f, &self._bitgen, size, self.lock, out)
return float_fill(&random_standard_exponential_fill_f, &self._bitgen, size, self.lock, out)
else:
raise TypeError('Unsupported dtype "%s" for standard_exponential'
% key)
Expand Down Expand Up @@ -1011,9 +1014,9 @@ cdef class Generator:
"""
key = np.dtype(dtype).name
if key == 'float64':
return double_fill(&random_gauss_zig_fill, &self._bitgen, size, self.lock, out)
return double_fill(&random_standard_normal_fill, &self._bitgen, size, self.lock, out)
elif key == 'float32':
10000 return float_fill(&random_gauss_zig_f, &self._bitgen, size, self.lock, out)
return float_fill(&random_standard_normal_fill_f, &self._bitgen, size, self.lock, out)

else:
raise TypeError('Unsupported dtype "%s" for standard_normal' % key)
Expand Down Expand Up @@ -1114,7 +1117,7 @@ cdef class Generator:
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random

"""
return cont(&random_normal_zig, &self._bitgen, size, self.lock, 2,
return cont(&random_normal, &self._bitgen, size, self.lock, 2,
loc, '', CONS_NONE,
scale, 'scale', CONS_NON_NEGATIVE,
0.0, '', CONS_NONE,
Expand Down Expand Up @@ -1200,13 +1203,13 @@ cdef class Generator:
cdef void *func
key = np.dtype(dtype).name
if key == 'float64':
return cont(&random_standard_gamma_zig, &self._bitgen, size, self.lock, 1,
return cont(&random_standard_gamma, &self._bitgen, size, self.lock, 1,
shape, 'shape', CONS_NON_NEGATIVE,
0.0, '', CONS_NONE,
0.0, '', CONS_NONE,
out)
if key == 'float32':
return cont_f(&random_standard_gamma_zig_f, &self._bitgen, size, self.lock,
return cont_f(&random_standard_gamma_f, &self._bitgen, size, self.lock,
shape, 'shape', CONS_NON_NEGATIVE,
out)
else:
Expand Down Expand Up @@ -3864,7 +3867,7 @@ cdef class Generator:
while i < totsize:
acc = 0.0
for j in range(k):
val_data[i+j] = random_standard_gamma_zig(&self._bitgen,
val_data[i+j] = random_standard_gamma(&self._bitgen,
alpha_data[j])
acc = acc + val_data[i + j]
invacc = 1/acc
Expand Down
39 changes: 14 additions & 25 deletions numpy/random/include/distributions.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,47 +78,36 @@ static NPY_INLINE double next_double(bitgen_t *bitgen_state) {

DECLDIR double loggam(double x);

DECLDIR float random_float(bitgen_t *bitgen_state);
DECLDIR double random_double(bitgen_t *bitgen_state);
DECLDIR void random_double_fill(bitgen_t *bitgen_state, npy_intp cnt, double *out);
DECLDIR float random_standard_uniform_f(bitgen_t *bitgen_state);
DECLDIR double random_standard_uniform(bitgen_t *bitgen_state);
DECLDIR void random_standard_uniform_fill(bitgen_t *, npy_intp, double *);
DECLDIR void random_standard_uniform_fill_f(bitgen_t *, npy_intp, float *);

DECLDIR int64_t random_positive_int64(bitgen_t *bitgen_state);
DECLDIR int32_t random_positive_int32(bitgen_t *bitgen_state);
DECLDIR int64_t random_positive_int(bitgen_t *bitgen_state);
DECLDIR uint64_t random_uint(bitgen_t *bitgen_state);

DECLDIR double random_standard_exponential(bitgen_t *bitgen_state);
DECLDIR void random_standard_exponential_fill(bitgen_t *bitgen_state, npy_intp cnt,
double *out);
DECLDIR float random_standard_exponential_f(bitgen_t *bitgen_state);
DECLDIR double random_standard_exponential_zig(bitgen_t *bitgen_state);
DECLDIR void random_standard_exponential_zig_fill(bitgen_t *bitgen_state,
npy_intp cnt, double *out);
DECLDIR float random_standard_exponential_zig_f(bitgen_t *bitgen_state);

/*
DECLDIR double random_gauss(bitgen_t *bitgen_state);
DECLDIR float random_gauss_f(bitgen_t *bitgen_state);
*/
DECLDIR double random_gauss_zig(bitgen_t *bitgen_state);
DECLDIR float random_gauss_zig_f(bitgen_t *bitgen_state);
DECLDIR void random_gauss_zig_fill(bitgen_t *bitgen_state, npy_intp cnt,
double *out);

/*
DECLDIR void random_standard_exponential_fill(bitgen_t *, npy_intp, double *);
DECLDIR void random_standard_exponential_fill_f(bitgen_t *, npy_intp, float *);
DECLDIR void random_standard_exponential_zig_fill(bitgen_t *, npy_intp, double *);
DECLDIR void random_standard_exponential_zig_fill_f(bitgen_t *, npy_intp, float *);

DECLDIR double random_standard_normal(bitgen_t *bitgen_state);
DECLDIR float random_standard_normal_f(bitgen_t *bitgen_state);
DECLDIR void random_standard_normal_fill(bitgen_t *, npy_intp, double *);
DECLDIR void random_standard_normal_fill_f(bitgen_t *, npy_intp, float *);
DECLDIR double random_standard_gamma(bitgen_t *bitgen_state, double shape);
DECLDIR float random_standard_gamma_f(bitgen_t *bitgen_state, float shape);
*/
DECLDIR double random_standard_gamma_zig(bitgen_t *bitgen_state, double shape);
DECLDIR float random_standard_gamma_zig_f(bitgen_t *bitgen_state, float shape);

/*
DECLDIR double random_normal(bitgen_t *bitgen_state, double loc, double scale);
*/
DECLDIR double random_normal_zig(bitgen_t *bitgen_state, double loc, double scale);

DECLDIR double random_gamma(bitgen_t *bitgen_state, double shape, double scale);
DECLDIR float random_gamma_float(bitgen_t *bitgen_state, float shape, float scale);
DECLDIR float random_gamma_f(bitgen_t *bitgen_state, float shape, float scale);

DECLDIR double random_exponential(bitgen_t *bitgen_state, double scale);
DECLDIR double random_uniform(bitgen_t *bitgen_state, double lower, double range);
Expand Down
4 changes: 2 additions & 2 deletions numpy/random/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ cdef extern from "include/distributions.h":

ctypedef s_binomial_t binomial_t

void random_double_fill(bitgen_t* bitgen_state, np.npy_intp cnt, double *out) nogil
void random_standard_uniform_fill(bitgen_t* bitgen_state, np.npy_intp cnt, double *out) nogil
int64_t random_positive_int(bitgen_t *bitgen_state) nogil
double random_uniform(bitgen_t *bitgen_state, double lower, double range) nogil
double random_vonmises(bitgen_t *bitgen_state, double mu, double kappa) nogil
Expand Down Expand Up @@ -410,7 +410,7 @@ cdef class RandomState:

"""
cdef double temp
return double_fill(&random_double_fill, &self._bitgen, size, self.lock, None)
return double_fill(&random_standard_uniform_fill, &self._bitgen, size, self.lock, None)

def random(self, size=None):
"""
Expand Down
Loading
0