8000 ENH: random: Add the method `permuted` to Generator. by WarrenWeckesser · Pull Request #15121 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: random: Add the method permuted to Generator. #15121

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 14 commits into from
Sep 2, 2020
Merged
Changes from 1 commit
Commits
8000
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
Prev Previous commit
Next Next commit
Handle axis=None more efficiently.
  • Loading branch information
WarrenWeckesser committed Aug 14, 2020
commit 023114fa656df68de15470261649bae695bb96b8
33 changes: 31 additions & 2 deletions numpy/random/_generator.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ from ._common cimport (POISSON_LAM_MAX, CONS_POSITIVE, CONS_NONE,
validate_output_shape
)

cdef extern from "numpy/arrayobject.h":
#int PyArray_SetWritebackIfCopyBase(np.ndarray, np.ndarray)
int PyArray_ResolveWritebackIfCopy(np.ndarray)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int PyArray_ResolveWritebackIfCopy(np.ndarray)
int PyArray_ResolveWritebackIfCopy(np.ndarray) except -1

object PyArray_FromArray(object, object, int)

enum:
NPY_ARRAY_WRITEBACKIFCOPY

np.import_array()

cdef int64_t _safe_sum_nonneg_int64(size_t num_colors, int64_t *colors):
Expand Down Expand Up @@ -4184,7 +4192,7 @@ cdef class Generator:

Parameters
----------
x : array_like
x : array_like, at least one-dimensional
Array to be shuffled.
axis : int, optional
Slices of `x` in this axis are shuffled. Each slice
Expand Down Expand Up @@ -4255,6 +4263,9 @@ cdef class Generator:
cdef np.npy_intp axlen, axstride, itemsize
cdef void *buf
cdef np.flatiter it
cdef np.ndarray to_shuffle
cdef int status
cdef int flags

x = np.asarray(x)

Expand All @@ -4268,7 +4279,25 @@ cdef class Generator:
out[...] = x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC this uses force-casting, maybe we should use np.copyto(out, x, casting="safe"). This also supports a few types of broadcasting of input to output. That is fine as such, but I feel it may be confusing with respect to the axis argument if the number of dimensions do not match. Maybe we should just enforce identical shapes?


if axis is None:
self.shuffle(out.flat)
if x.ndim > 1:
if not (np.PyArray_FLAGS(out) & (np.NPY_ARRAY_C_CONTIGUOUS |
np.NPY_ARRAY_F_CONTIGUOUS)):
flags = (np.NPY_ARRAY_C_CONTIGUOUS |
NPY_ARRAY_WRITEBACKIFCOPY)
to_shuffle = PyArray_FromArray(out, <object>NULL, flags)
self.shuffle(to_shuffle.ravel(order='K'))
# Because we only execute this block if out is not
# contiguous, we know this call will always result in a
# copy of to_shuffle back to out. I.e. status will be 1.
status = PyArray_ResolveWritebackIfCopy(to_shuffle)
assert status == 1
else:
# out is n-d with n > 1, but is either C- or F-contiguous,
# so we know out.ravel(order='A') is a view.
self.shuffle(out.ravel(order='A'))
else:
# out is 1-d
self.shuffle(out)
return out

ax = normalize_axis_index(axis, np.ndim(out))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ax = normalize_axis_index(axis, np.ndim(out))
ax = normalize_axis_index(axis, out.ndim)

Expand Down
0