8000 Merge pull request #2 from bashtage/randomgen-update · mattip/numpy@ee3593d · GitHub
[go: up one dir, main page]

Skip to content

Commit ee3593d

Browse files
authored
Merge pull request #2 from bashtage/randomgen-update
MAINT: Sync with randomgen changes
2 parents ba96ca5 + ac66298 commit ee3593d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+7851
-3951
lines changed

numpy/random/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@
137137
]
138138

139139
# from .mtrand import *
140-
from .randomgen.legacy import LegacyGenerator as RandomState
140+
from .randomgen.mtrand import RandomState
141141
mtrand = RandomState()
142142
for _x in dir(mtrand):
143143
if _x[0] != '_' and _x not in ('poisson_lam_max', 'state'):
144144
locals()[_x] = getattr(mtrand, _x)
145145
del _x
146146

147147
# Some aliases:
148-
ranf = random = sample = random_sample
148+
ranf = random = sample = mtrand.random_sample
149149
__all__.extend(['ranf', 'random', 'sample'])
150150

151151
def __RandomState_ctor():

numpy/random/randomgen/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
from .xorshift1024 import Xorshift1024
1111
from .xoshiro256starstar import Xoshiro256StarStar
1212
from .xoshiro512starstar import Xoshiro512StarStar
13-
13+
from .mtrand import RandomState
1414
__all__ = ['RandomGenerator', 'DSFMT', 'MT19937', 'PCG64', 'PCG32', 'Philox',
1515
'ThreeFry', 'ThreeFry32', 'Xoroshiro128', 'Xorshift1024',
16-
'Xoshiro256StarStar', 'Xoshiro512StarStar',
17-
'hypergeometric', 'multinomial', 'random_sample']
16+
'Xoshiro256StarStar', 'Xoshiro512StarStar', 'RandomState']
1817

1918
#from ._version import get_versions
2019

numpy/random/randomgen/_pickle.py

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .xorshift1024 import Xorshift1024
1111
from .xoshiro256starstar import Xoshiro256StarStar
1212
from .xoshiro512starstar import Xoshiro512StarStar
13-
from .legacy import LegacyGenerator
13+
from .mtrand import RandomState
1414

1515
BasicRNGS = {'MT19937': MT19937,
1616
'DSFMT': DSFMT,
@@ -78,9 +78,9 @@ def __brng_ctor(brng_name='mt19937'):
7878
return brng()
7979

8080

81-
def __legacy_ctor(brng_name='mt19937'):
81+
def __randomstate_ctor(brng_name='mt19937'):
8282
"""
83-
Pickling helper function that returns a LegacyGenerator object
83+
Pickling helper function that returns a legacy RandomState-like object
8484
8585
Parameters
8686
----------
@@ -89,8 +89,8 @@ def __legacy_ctor(brng_name='mt19937'):
8989
9090
Returns
9191
-------
92-
lg: LegacyGenerator
93-
LegacyGenerator using the named core BasicRNG
92+
rs: RandomState
93+
Legacy RandomState using the named core BasicRNG
9494
"""
9595
try:
9696
brng_name = brng_name.decode('ascii')
@@ -101,30 +101,4 @@ def __legacy_ctor(brng_name='mt19937'):
101101
else:
102102
raise ValueError(str(brng_name) + ' is not a known BasicRNG module.')
103103

104-
return LegacyGenerator(brng())
105-
106-
107-
def _experiment_ctor(brng_name='mt19937'):
108-
"""
109-
Pickling helper function that returns a LegacyGenerator object
110-
111-
Parameters
112-
----------
113-
brng_name: str
114-
String containing the name of the Basic RNG
115-
116-
Returns
117-
-------
118-
brng: BasicRNG
119-
Basic RNG instance
120-
"""
121-
try:
122-
brng_name = brng_name.decode('ascii')
123-
except AttributeError:
124-
pass
125-
if brng_name in BasicRNGS:
126-
brng = BasicRNGS[brng_name]
127-
else:
128-
raise ValueError(str(brng_name) + ' is not a known BasicRNG module.')
129-
130-
return LegacyGenerator(brng())
104+
return RandomState(brng())

numpy/random/randomgen/bounded_integers.pxd.in

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,11 @@ from __future__ import absolute_import
33
from libc.stdint cimport (uint8_t, uint16_t, uint32_t, uint64_t,
44
int8_t, int16_t, int32_t, int64_t, intptr_t)
55
import numpy as np
6-
cimport numpy as np
6+
cimport numpy as np
77
ctypedef np.npy_bool bool_t
88

99
from .common cimport brng_t
1010

11-
_randint_types = {'bool': (0, 2),
12-
'int8': (-2**7, 2**7),
13-
'int16': (-2**15, 2**15),
14-
'int32': (-2**31, 2**31),
15-
'int64': (-2**63, 2**63),
16-
'uint8': (0, 2**8),
17-
'uint16': (0, 2**16),
18-
'uint32': (0, 2**32),
19-
'uint64': (0, 2**64)
20-
}
21-
2211
cdef inline uint64_t _gen_mask(uint64_t max_val) nogil:
2312
"""Mask generator for use in bounded random numbers"""
2413
# Smallest bit mask >= max

numpy/random/randomgen/bounded_integers.pyx.in

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ from .distributions cimport *
99

1010
np.import_array()
1111

12+
_randint_types = {'bool': (0, 2),
13+
'int8': (-2**7, 2**7),
14+
'int16': (-2**15, 2**15),
15+
'int32': (-2**31, 2**31),
16+
'int64': (-2**63, 2**63),
17+
'uint8': (0, 2**8),
18+
'uint16': (0, 2**16),
19+
'uint32': (0, 2**32),
20+
'uint64': (0, 2**64)}
1221
{{
1322
py:
1423
type_info = (('uint32', 'uint32', 'uint64', 'NPY_UINT64', 0, 0, 0, '0X100000000ULL'),
@@ -21,7 +30,6 @@ type_info = (('uint32', 'uint32', 'uint64', 'NPY_UINT64', 0, 0, 0, '0X100000000U
2130
)}}
2231
{{for nptype, utype, nptype_up, npctype, remaining, bitshift, lb, ub in type_info}}
2332
{{ py: otype = nptype + '_' if nptype == 'bool' else nptype }}
24-
2533
cdef object _rand_{{nptype}}_broadcast(np.ndarray low, np.ndarray high, object size,
2634
bint use_masked,
2735
brng_t *state, object lock):
@@ -42,7 +50,6 @@ cdef object _rand_{{nptype}}_broadcast(np.ndarray low, np.ndarray high, object s
4250
cdef np.broadcast it
4351
cdef int buf_rem = 0
4452

45-
4653
# Array path
4754
low_arr = <np.ndarray>low
4855
high_arr = <np.ndarray>high
@@ -83,7 +90,6 @@ cdef object _rand_{{nptype}}_broadcast(np.ndarray low, np.ndarray high, object s
8390
np.PyArray_MultiIter_NEXT(it)
8491
return out_arr
8592
{{endfor}}
86-
8793
{{
8894
py:
8995
big_type_info = (('uint64', 'uint64', 'NPY_UINT64', '0x0ULL', '0xFFFFFFFFFFFFFFFFULL'),
@@ -166,7 +172,6 @@ cdef object _rand_{{nptype}}_broadcast(object low, object high, object size,
166172

167173
return out_arr
168174
{{endfor}}
169-
170175
{{
171176
py:
172177
type_info = (('uint64', 'uint64', '0x0ULL', '0xFFFFFFFFFFFFFFFFULL'),
@@ -241,8 +246,8 @@ cdef object _rand_{{nptype}}(object low, object high, object size,
241246
high_arr = <np.ndarray>np.array(high, copy=False)
242247
low_ndim = np.PyArray_NDIM(low_arr)
243248
high_ndim = np.PyArray_NDIM(high_arr)
244-
if ((low_ndim == 0 or (low_ndim==1 and low_arr.size==1 and size is not None)) and
245-
(high_ndim == 0 or (high_ndim==1 and high_arr.size==1 and size is not None))):
249+
if ((low_ndim == 0 or (low_ndim == 1 and low_arr.size == 1 and size is not None)) and
250+
(high_ndim == 0 or (high_ndim == 1 and high_arr.size == 1 and size is not None))):
246251
low = int(low_arr)
247252
high = int(high_arr)
248253
# Subtract 1 since internal generator produces on closed interval [low, high]

numpy/random/randomgen/common.pxd

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ cdef enum ConstraintType:
2727

2828
ctypedef ConstraintType constraint_type
2929

30+
cdef object benchmark(brng_t *brng, object lock, Py_ssize_t cnt, object method)
31+
cdef object random_raw(brng_t *brng, object lock, object size, object output)
32+
cdef object prepare_cffi(brng_t *brng)
33+
cdef object prepare_ctypes(brng_t *brng)
3034
cdef int check_constraint(double val, object name, constraint_type cons) except -1
3135
cdef int check_array_constraint(np.ndarray val, object name, constraint_type cons) except -1
3236

3337
cdef extern from "src/aligned_malloc/aligned_malloc.h":
34-
cdef void *PyArray_realloc_aligned(void *p, size_t n);
35-
cdef void *PyArray_malloc_aligned(size_t n);
36-
cdef void *PyArray_calloc_aligned(size_t n, size_t s);
37-
cdef void PyArray_free_aligned(void *p);
38+
cdef void *PyArray_realloc_aligned(void *p, size_t n)
39+
cdef void *PyArray_malloc_aligned(size_t n)
40+
cdef void *PyArray_calloc_aligned(size_t n, size_t s)
41+
cdef void PyArray_free_aligned(void *p)
3842

3943
ctypedef double (*random_double_fill)(brng_t *state, np.npy_intp count, double* out) nogil
4044
ctypedef double (*random_double_0)(void *state) nogil
@@ -105,5 +109,5 @@ cdef inline void compute_complex(double *rv_r, double *rv_i, double loc_r,
105109
scale_r = sqrt(var_r)
106110
scale_i = sqrt(var_i)
107111

108-
rv_i[0] = loc_i + scale_i * (rho * rv_r[0] + scale_c * rv_i[0])
112+
rv_i[0] = loc_i + scale_i * (rho * rv_r[0] + scale_c * rv_i[0])
109113
rv_r[0] = loc_r + scale_r * rv_r[0]

0 commit comments

Comments
 (0)
0