-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
ENH: Allow size=0 in numpy.random.choice, regardless of array #8717
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,8 @@ | |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
include "Python.pxi" | ||
include "randint_helpers.pxi" | ||
include "numpy.pxd" | ||
include "randint_helpers.pxi" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be after |
||
include "cpython/pycapsule.pxd" | ||
|
||
from libc cimport string | ||
|
@@ -973,8 +973,8 @@ cdef class RandomState: | |
raise ValueError("low is out of bounds for %s" % (key,)) | ||
if high > highbnd: | ||
raise ValueError("high is out of bounds for %s" % (key,)) | ||
if low >= high: | ||
raise ValueError("low >= high") | ||
if low >= high and np.prod(size) != 0: | ||
raise ValueError("Range cannot be empty (low >= high) unless no samples are taken") | ||
|
||
with self.lock: | ||
ret = randfunc(low, high - 1, size, self.state_address) | ||
|
@@ -1100,14 +1100,14 @@ cdef class RandomState: | |
pop_size = operator.index(a.item()) | ||
except TypeError: | ||
raise ValueError("a must be 1-dimensional or an integer") | ||
if pop_size <= 0: | ||
raise ValueError("a must be greater than 0") | ||
if pop_size <= 0 and np.prod(size) != 0: | ||
raise ValueError("a must be greater than 0 unless no samples are taken") | ||
elif a.ndim != 1: | ||
raise ValueError("a must be 1-dimensional") | ||
else: | ||
pop_size = a.shape[0] | ||
if pop_size is 0: | ||
raise ValueError("a must be non-empty") | ||
if pop_size is 0 and np.prod(size) != 0: | ||
raise ValueError("a cannot be empty unless no samples are taken") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps |
||
|
||
if p is not None: | ||
d = len(p) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ def get_dispatch(dtypes): | |
|
||
{{for npy_dt, npy_udt, np_dt in get_dispatch(dtypes)}} | ||
|
||
def _rand_{{npy_dt}}(low, high, size, rngstate): | ||
def _rand_{{npy_dt}}(npy_{{npy_dt}} low, npy_{{npy_dt}} high, size, rngstate): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May as well enforce casting here |
||
""" | ||
_rand_{{npy_dt}}(low, high, size, rngstate) | ||
|
||
|
@@ -60,8 +60,8 @@ def _rand_{{npy_dt}}(low, high, size, rngstate): | |
cdef npy_intp cnt | ||
cdef rk_state *state = <rk_state *>PyCapsule_GetPointer(rngstate, NULL) | ||
|
||
rng = <npy_{{npy_udt}}>(high - low) | ||
off = <npy_{{npy_udt}}>(<npy_{{npy_dt}}>low) | ||
off = <npy_{{npy_udt}}>(low) | ||
rng = <npy_{{npy_udt}}>(high) - <npy_{{npy_udt}}>(low) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
if size is None: | ||
rk_random_{{npy_udt}}(off, rng, 1, &buf, state) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Newline needed here