Skip to content

Commit

Permalink
Merge pull request #12089 from bashtage/permutation-in-place-bug
Browse files Browse the repository at this point in the history
BUG: Fix in-place permutation
  • Loading branch information
charris committed Oct 10, 2018
2 parents 8a560b9 + ec69d79 commit 072c90e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions numpy/random/mtrand/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4907,8 +4907,8 @@ cdef class RandomState:

# shuffle has fast-path for 1-d
if arr.ndim == 1:
# must return a copy
if arr is x:
# Return a copy if same memory
if np.may_share_memory(arr, x):
arr = np.array(arr)
self.shuffle(arr)
return arr
Expand Down
22 changes: 22 additions & 0 deletions numpy/random/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,25 @@ def test_shuffle_of_array_of_objects(self):
# Force Garbage Collection - should not segfault.
import gc
gc.collect()

def test_permutation_subclass(self):
class N(np.ndarray):
pass

np.random.seed(1)
orig = np.arange(3).view(N)
perm = np.random.permutation(orig)
assert_array_equal(perm, np.array([0, 2, 1]))
assert_array_equal(orig, np.arange(3).view(N))

class M(object):
a = np.arange(5)

def __array__(self):
return self.a

np.random.seed(1)
m = M()
perm = np.random.permutation(m)
assert_array_equal(perm, np.array([2, 1, 4, 0, 3]))
assert_array_equal(m.__array__(), np.arange(5))

0 comments on commit 072c90e

Please sign in to comment.