summaryrefslogtreecommitdiff
path: root/numpy/ma
diff options
context:
space:
mode:
authorJulien Phalip <jphalip@gmail.com>2013-02-20 06:44:50 +0000
committerJulien Phalip <jphalip@gmail.com>2013-04-08 22:21:31 -0700
commita3f2e0461eeec2077e7ed1f71bf1e0756e893257 (patch)
tree89c1fb09c34519d907249293f3a7ef8ed587da5e /numpy/ma
parentd1b195d943da80cafd42f935fa9ec920eb18c7e5 (diff)
downloadnumpy-a3f2e0461eeec2077e7ed1f71bf1e0756e893257.tar.gz
ENH: add `invert` parameter to numpy.in1d().
Diffstat (limited to 'numpy/ma')
-rw-r--r--numpy/ma/extras.py9
-rw-r--r--numpy/ma/tests/test_extras.py13
2 files changed, 19 insertions, 3 deletions
diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py
index a809db53c..861fefad0 100644
--- a/numpy/ma/extras.py
+++ b/numpy/ma/extras.py
@@ -1121,7 +1121,7 @@ def setxor1d(ar1, ar2, assume_unique=False):
flag2 = (flag[1:] == flag[:-1])
return aux[flag2]
-def in1d(ar1, ar2, assume_unique=False):
+def in1d(ar1, ar2, assume_unique=False, invert=False):
"""
Test whether each element of an array is also present in a second
array.
@@ -1147,8 +1147,11 @@ def in1d(ar1, ar2, assume_unique=False):
# the values from the second array.
order = ar.argsort(kind='mergesort')
sar = ar[order]
- equal_adj = (sar[1:] == sar[:-1])
- flag = ma.concatenate((equal_adj, [False]))
+ if invert:
+ bool_ar = (sar[1:] != sar[:-1])
+ else:
+ bool_ar = (sar[1:] == sar[:-1])
+ flag = ma.concatenate((bool_ar, [invert]))
indx = order.argsort(kind='mergesort')[:len(ar1)]
if assume_unique:
diff --git a/numpy/ma/tests/test_extras.py b/numpy/ma/tests/test_extras.py
index b6643fc4d..d9f94a01f 100644
--- a/numpy/ma/tests/test_extras.py
+++ b/numpy/ma/tests/test_extras.py
@@ -810,6 +810,19 @@ class TestArraySetOps(TestCase):
assert_array_equal([], in1d([], []))
+ def test_in1d_invert(self):
+ "Test in1d's invert parameter"
+ a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
+ b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
+ assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))
+
+ a = array([5, 5, 2, 1, -1], mask=[0, 0, 0, 0, 1])
+ b = array([1, 5, -1], mask=[0, 0, 1])
+ assert_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))
+
+ assert_array_equal([], in1d([], [], invert=True))
+
+
def test_union1d(self):
"Test union1d"
a = array([1, 2, 5, 7, 5, -1], mask=[0, 0, 0, 0, 0, 1])