8000 MAINT: Sync with randomgen changes by bashtage · Pull Request #2 · mattip/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Sync with randomgen changes #2

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 3 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions numpy/random/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@
]

# from .mtrand import *
from .randomgen.legacy import LegacyGenerator as RandomState
from .randomgen.mtrand import RandomState
mtrand = RandomState()
for _x in dir(mtrand):
if _x[0] != '_' and _x not in ('poisson_lam_max', 'state'):
locals()[_x] = getattr(mtrand, _x)
del _x

# Some aliases:
ranf = random = sample = random_sample
ranf = random = sample = mtrand.random_sample
__all__.extend(['ranf', 'random', 'sample'])

def __RandomState_ctor():
Expand Down
5 changes: 2 additions & 3 deletions numpy/random/randomgen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
from .xorshift1024 import Xorshift1024
from .xoshiro256starstar import Xoshiro256StarStar
from .xoshiro512starstar import Xoshiro512StarStar

from .mtrand import RandomState
__all__ = ['RandomGenerator', 'DSFMT', 'MT19937', 'PCG64', 'PCG32', 'Philox',
'ThreeFry', 'ThreeFry32', 'Xoroshiro128', 'Xorshift1024',
'Xoshiro256StarStar', 'Xoshiro512StarStar',
'hypergeometric', 'multinomial', 'random_sample']
'Xoshiro256StarStar', 'Xoshiro512StarStar', 'RandomState']

#from ._version import get_versions

Expand Down
38 changes: 6 additions & 32 deletions numpy/random/randomgen/_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .xorshift1024 import Xorshift1024
from .xoshiro256starstar import Xoshiro256StarStar
from .xoshiro512starstar import Xoshiro512StarStar
from .legacy import LegacyGenerator
from .mtrand import RandomState

BasicRNGS = {'MT19937': MT19937,
'DSFMT': DSFMT,
Expand Down Expand Up @@ -78,9 +78,9 @@ def __brng_ctor(brng_name='mt19937'):
return brng()


def __legacy_ctor(brng_name='mt19937'):
def __randomstate_ctor(brng_name='mt19937'):
"""
Pickling helper function that returns a LegacyGenerator object
Pickling helper function that returns a legacy RandomState-like object

Parameters
----------
Expand All @@ -89,8 +89,8 @@ def __legacy_ctor(brng_name='mt19937'):

Returns
-------
lg: LegacyGenerator
LegacyGenerator using the named core BasicRNG
rs: RandomState
Legacy RandomState using the named core BasicRNG
"""
try:
brng_name = brng_name.decode('ascii')
Expand All @@ -101,30 +101,4 @@ def __legacy_ctor(brng_name='mt19937'):
else:
raise ValueError(str(brng_name) + ' is not a known BasicRNG module.')

return LegacyGenerator(brng())


def _experiment_ctor(brng_name='mt19937'):
"""
Pickling helper function that returns a LegacyGenerator object

Parameters
----------
brng_name: str
String containing the name of the Basic RNG

Returns
-------
brng: BasicRNG
Basic RNG instance
"""
try:
brng_name = brng_name.decode('ascii')
except AttributeError:
pass
if brng_name in BasicRNGS:
brng = BasicRNGS[brng_name]
else:
raise ValueError(str(brng_name) + ' is not a known BasicRNG module.')

return LegacyGenerator(brng())
return RandomState(brng())
13 changes: 1 addition & 12 deletions numpy/random/randomgen/bounded_integers.pxd.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,11 @@ from __future__ import absolute_import
from libc.stdint cimport (uint8_t, uint16_t, uint32_t, uint64_t,
int8_t, int16_t, int32_t, int64_t, intptr_t)
import numpy as np
cimport numpy as np
cimport numpy as np
ctypedef np.npy_bool bool_t

from .common cimport brng_t

_randint_types = {'bool': (0, 2),
'int8': (-2**7, 2**7),
'int16': (-2**15, 2**15),
'int32': (-2**31, 2**31),
'int64': (-2**63, 2**63),
'uint8': (0, 2**8),
'uint16': (0, 2**16),
'uint32': (0, 2**32),
'uint64': (0, 2**64)
}

cdef inline uint64_t _gen_mask(uint64_t max_val) nogil:
"""Mask generator for use in bounded random numbers"""
# Smallest bit mask >= max
Expand Down
17 changes: 11 additions & 6 deletions numpy/random/randomgen/bounded_integers.pyx.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ from .distributions cimport *

np.import_array()

_randint_types = {'bool': (0, 2),
'int8': (-2**7, 2**7),
'int16': (-2**15, 2**15),
'int32': (-2**31, 2**31),
'int64': (-2**63, 2**63),
'uint8': (0, 2**8),
'uint16': (0, 2**16),
'uint32': (0, 2**32),
'uint64': (0, 2**64)}
{{
py:
type_info = (('uint32', 'uint32', 'uint64', 'NPY_UINT64', 0, 0, 0, '0X100000000ULL'),
Expand All @@ -21,7 +30,6 @@ type_info = (('uint32', 'uint32', 'uint64', 'NPY_UINT64', 0, 0, 0, '0X100000000U
)}}
{{for nptype, utype, nptype_up, npctype, remaining, bitshift, lb, ub in type_info}}
{{ py: otype = nptype + '_' if nptype == 'bool' else nptype }}

cdef object _rand_{{nptype}}_broadcast(np.ndarray low, np.ndarray high, object size,
bint use_masked,
brng_t *state, object lock):
Expand All @@ -42,7 +50,6 @@ cdef object _rand_{{nptype}}_broadcast(np.ndarray low, np.ndarray high, object s
cdef np.broadcast it
cdef int buf_rem = 0


# Array path
low_arr = <np.ndarray>low
high_arr = <np.ndarray>high
Expand Down Expand Up @@ -83,7 +90,6 @@ cdef object _rand_{{nptype}}_broadcast(np.ndarray low, np.ndarray high, object s
np.PyArray_MultiIter_NEXT(it)
return out_arr
{{endfor}}

{{
py:
big_type_info = (('uint64', 'uint64', 'NPY_UINT64', '0x0ULL', '0xFFFFFFFFFFFFFFFFULL'),
Expand Down Expand Up @@ -166,7 +172,6 @@ cdef object _rand_{{nptype}}_broadcast(object low, object high, object size,

return out_arr
{{endfor}}

{{
py:
type_info = (('uint64', 'uint64', '0x0ULL', '0xFFFFFFFFFFFFFFFFULL'),
Expand Down Expand Up @@ -241,8 +246,8 @@ cdef object _rand_{{nptype}}(object low, object high, object size,
high_arr = <np.ndarray>np.array(high, copy=False)
low_ndim = np.PyArray_NDIM(low_arr)
high_ndim = np.PyArray_NDIM(high_arr)
if ((low_ndim == 0 or (low_ndim==1 and low_arr.size==1 and size is not None)) and
(high_ndim == 0 or (high_ndim==1 and high_arr.size==1 and size is not None))):
if ((low_ndim == 0 or (low_ndim == 1 and low_arr.size == 1 and size is not None)) and
(high_ndim == 0 or (high_ndim == 1 and high_arr.size == 1 and size is not None))):
low = int(low_arr)
high = int(high_arr)
# Subtract 1 since internal generator produces on closed interval [low, high]
Expand Down
14 changes: 9 additions & 5 deletions numpy/random/randomgen/common.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ cdef enum ConstraintType:

ctypedef ConstraintType constraint_type

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

cdef extern from "src/aligned_malloc/aligned_malloc.h":
cdef void *PyArray_realloc_aligned(void *p, size_t n);
cdef void *PyArray_malloc_aligned(size_t n);
cdef void *PyArray_calloc_aligned(size_t n, size_t s);
cdef void PyArray_free_aligned(void *p);
cdef void *PyArray_realloc_aligned(void *p, size_t n)
cdef void *PyArray_malloc_aligned(size_t n)
cdef void *PyArray_calloc_aligned(size_t n, size_t s)
cdef void PyArray_free_aligned(void *p)

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

rv_i[0] = loc_i + scale_i * (rho * rv_r[0] + scale_c * rv_i[0])
rv_i[0] = loc_i + scale_i * (rho * rv_r[0] + scale_c * rv_i[0])
rv_r[0] = loc_r + scale_r * rv_r[0]
Loading
0