8000 MAINT: Remove duplicate randint helpers code. by gfyoung · Pull Request #8088 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Remove duplicate randint helpers code. #8088

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 1 commit into from
Sep 23, 2016
Merged
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
277 changes: 0 additions & 277 deletions numpy/random/mtrand/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -573,283 +573,6 @@ def _shape_from_size(size, d):
shape = tuple(size) + (d,)
return shape


# Set up dictionary of integer types and relevant functions.
#
# The dictionary is keyed by dtype(...).name and the values
# are a tuple (low, high, function), where low and high are
# the bounds of the largest half open interval `[low, high)`
# and the function is the relevant function to call for
# that precision.
#
# The functions are all the same except for changed types in
# a few places. It would be easy to template them.

def _rand_bool(low, high, size, rngstate):
"""
_rand_bool(low, high, size, rngstate)

See `_rand_int32` for documentation, only the return type changes.

"""
cdef npy_bool off, rng, buf
cdef npy_bool *out
cdef ndarray array "arrayObject"
cdef npy_intp cnt
cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL)

rng = <npy_bool>(high - low)
off = <npy_bool>(low)
if size is None:
rk_random_bool(off, rng, 1, &buf, state)
return np.bool_(<npy_bool>buf)
else:
array = <ndarray>np.empty(size, np.bool_)
cnt = PyArray_SIZE(array)
out = <npy_bool *>PyArray_DATA(array)
with nogil:
rk_random_bool(off, rng, cnt, out, state)
return array


def _rand_int8(low, high, size, rngstate):
"""
_rand_int8(low, high, size, rngstate)

See `_rand_int32` for documentation, only the return type changes.

"""
cdef npy_uint8 off, rng, buf
cdef npy_uint8 *out
cdef ndarray array "arrayObject"
cdef npy_intp cnt
cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL)

rng = <npy_uint8>(high - low)
off = <npy_uint8>(<npy_int8>low)
if size is None:
rk_random_uint8(off, rng, 1, &buf, state)
return np.int8(<npy_int8>buf)
else:
array = <ndarray>np.empty(size, np.int8)
cnt = PyArray_SIZE(array)
out = <npy_uint8 *>PyArray_DATA(array)
with nogil:
rk_random_uint8(off, rng, cnt, out, state)
return array


def _rand_int16(low, high, size, rngstate):
"""
_rand_int16(low, high, size, rngstate)

See `_rand_int32` for documentation, only the return type changes.

"""
cdef npy_uint16 off, rng, buf
cdef npy_uint16 *out
cdef ndarray array "arrayObject"
cdef npy_intp cnt
cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL)

rng = <npy_uint16>(high - low)
off = <npy_uint16>(<npy_int16>low)
if size is None:
rk_random_uint16(off, rng, 1, &buf, state)
return np.int16(<npy_int16>buf)
else:
array = <ndarray>np.empty(size, np.int16)
cnt = PyArray_SIZE(array)
out = <npy_uint16 *>PyArray_DATA(array)
with nogil:
rk_random_uint16(off, rng, cnt, out, state)
return array


def _rand_int32(low, high, size, rngstate):
"""
_rand_int32(self, low, high, size, rngstate)

Return random np.int32 integers between `low` and `high`, inclusive.

Return random integers from the "discrete uniform" distribution in the
closed interval [`low`, `high`]. On entry the arguments are presumed
to have been validated for size and order for the np.int32 type.

Parameters
----------
low : int
Lowest (signed) integer to be drawn from the distribution.
high : int
Highest (signed) integer to be drawn from the distribution.
size : int or tuple of ints
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
``m * n * k`` samples are drawn. Default is None, in which case a
single value is returned.
rngstate : encapsulated pointer to rk_state
The specific type depends on the python version. In Python 2 it is
a PyCObject, in Python 3 a PyCapsule object.

Returns
-------
out : python scalar or ndarray of np.int32
`size`-shaped array of random integers from the appropriate
distribution, or a single such random int if `size` not provided.

"""
cdef npy_uint32 off, rng, buf
cdef npy_uint32 *out
cdef ndarray array "arrayObject"
cdef npy_intp cnt
cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL)

rng = <npy_uint32>(high - low)
off = <npy_uint32>(<npy_int32>low)
if size is None:
rk_random_uint32(off, rng, 1, &buf, state)
return np.int32(<npy_int32>buf)
else:
array = <ndarray>np.empty(size, np.int32)
cnt = PyArray_SIZE(array)
out = <npy_uint32 *>PyArray_DATA(array)
with nogil:
rk_random_uint32(off, rng, cnt, out, state)
return array


def _rand_int64(low, high, size, rngstate):
"""
_rand_int64(low, high, size, rngstate)

See `_rand_int32` for documentation, only the return type changes.

"""
cdef npy_uint64 off, rng, buf
cdef npy_uint64 *out
cdef ndarray array "arrayObject"
cdef npy_intp cnt
cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL)

rng = <npy_uint64>(high - low)
off = <npy_uint64>(<npy_int64>low)
if size is None:
rk_random_uint64(off, rng, 1, &buf, state)
return np.int64(<npy_int64>buf)
else:
array = <ndarray>np.empty(size, np.int64)
cnt = PyArray_SIZE(array)
out = <npy_uint64 *>PyArray_DATA(array)
with nogil:
rk_random_uint64(off, rng, cnt, out, state)
return array

def _rand_uint8(low, high, size, rngstate):
"""
_rand_uint8(low, high, size, rngstate)

See `_rand_int32` for documentation, only the return type changes.

"""
cdef npy_uint8 off, rng, buf
cdef npy_uint8 *out
cdef ndarray array "arrayObject"
cdef npy_intp cnt
cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL)

rng = <npy_uint8>(high - low)
off = <npy_uint8>(low)
if size is None:
rk_random_uint8(off, rng, 1, &buf, state)
return np.uint8(<npy_uint8>buf)
else:
array = <ndarray>np.empty(size, np.uint8)
cnt = PyArray_SIZE(array)
out = <npy_uint8 *>PyArray_DATA(array)
with nogil:
rk_random_uint8(off, rng, cnt, out, state)
return array


def _rand_uint16(low, high, size, rngstate):
"""
_rand_uint16(low, high, size, rngstate)

See `_rand_int32` for documentation, only the return type changes.

"""
cdef npy_uint16 off, rng, buf
cdef npy_uint16 *out
cdef ndarray array "arrayObject"
cdef npy_intp cnt
cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL)

rng = <npy_uint16>(high - low)
off = <npy_uint16>(low)
if size is None:
rk_random_uint16(off, rng, 1, &buf, state)
return np.uint16(<npy_uint16>buf)
else:
array = <ndarray>np.empty(size, np.uint16)
cnt = PyArray_SIZE(array)
out = <npy_uint16 *>PyArray_DATA(array)
with nogil:
rk_random_uint16(off, rng, cnt, out, state)
return array


def _rand_uint32(low, high, size, rngstate):
"""
_rand_uint32(self, low, high, size, rngstate)

See `_rand_int32` for documentation, only the return type changes.

"""
cdef npy_uint32 off, rng, buf
cdef npy_uint32 *out
cdef ndarray array "arrayObject"
cdef npy_intp cnt
cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL)

rng = <npy_uint32>(high - low)
off = <npy_uint32>(low)
if size is None:
rk_random_uint32(off, rng, 1, &buf, state)
return np.uint32(<npy_uint32>buf)
else:
array = <ndarray>np.empty(size, np.uint32)
cnt = PyArray_SIZE(array)
out = <npy_uint32 *>PyArray_DATA(array)
with nogil:
rk_random_uint32(off, rng, cnt, out, state)
return array


def _rand_uint64(low, high, size, rngstate):
"""
_rand_uint64(low, high, size, rngstate)

See `_rand_int32` for documentation, only the return type changes.

"""
cdef npy_uint64 off, rng, buf
cdef npy_uint64 *out
cdef ndarray array "arrayObject"
cdef npy_intp cnt
cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL)

rng = <npy_uint64>(high - low)
off = <npy_uint64>(low)
if size is None:
rk_random_uint64(off, rng, 1, &buf, state)
return np.uint64(<npy_uint64>buf)
else:
array = <ndarray>np.empty(size, np.uint64)
cnt = PyArray_SIZE(array)
out = <npy_uint64 *>PyArray_DATA(array)
with nogil:
rk_random_uint64(off, rng, cnt, out, state)
return array

# Look up table for randint functions keyed by type name. The stored data
# is a tuple (lbnd, ubnd, func), where lbnd is the smallest value for the
# type, ubnd is one greater than the largest value, and func is the
Expand Down
0