Skip to content

Commit

Permalink
Merge pull request #2800 from seberg/issue2755
Browse files Browse the repository at this point in the history
BUG: Fix regression for in1d with non-array input
  • Loading branch information
njsmith committed Dec 21, 2012
2 parents c932553 + b0ac985 commit 3abd869
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions numpy/lib/arraysetops.py
Expand Up @@ -324,6 +324,10 @@ def in1d(ar1, ar2, assume_unique=False):
array([0, 2, 0])
"""
# Ravel both arrays, behavior for the first array could be different
ar1 = np.asarray(ar1).ravel()
ar2 = np.asarray(ar2).ravel()

# This code is significantly faster when the condition is satisfied.
if len(ar2) < 10 * len(ar1) ** 0.145:
mask = np.zeros(len(ar1), dtype=np.bool)
Expand Down
18 changes: 16 additions & 2 deletions numpy/lib/tests/test_arraysetops.py
Expand Up @@ -124,8 +124,9 @@ def test_in1d(self):
# we use two different sizes for the b array here to test the
# two different paths in in1d().
for mult in (1, 10):
a = np.array([5, 7, 1, 2])
b = np.array([2, 4, 3, 1, 5] * mult)
# One check without np.array, to make sure lists are handled correct
a = [5, 7, 1, 2]
b = [2, 4, 3, 1, 5] * mult
ec = np.array([True, False, True, True])
c = in1d(a, b, assume_unique=True)
assert_array_equal(c, ec)
Expand Down Expand Up @@ -188,6 +189,19 @@ def test_in1d_char_array( self ):

assert_array_equal(c, ec)

def test_in1d_ravel(self):
# Test that in1d ravels its input arrays. This is not documented
# behavior however. The test is to ensure consistentency.
a = np.arange(6).reshape(2,3)
b = np.arange(3,9).reshape(3,2)
long_b = np.arange(3, 63).reshape(30,2)
ec = np.array([False, False, False, True, True, True])

assert_array_equal(in1d(a, b, assume_unique=True), ec)
assert_array_equal(in1d(a, b, assume_unique=False), ec)
assert_array_equal(in1d(a, long_b, assume_unique=True), ec)
assert_array_equal(in1d(a, long_b, assume_unique=False), ec)

def test_union1d( self ):
a = np.array( [5, 4, 7, 1, 2] )
b = np.array( [2, 4, 3, 3, 2, 1, 5] )
Expand Down

0 comments on commit 3abd869

Please sign in to comment.