summaryrefslogtreecommitdiff
path: root/numpy/lib/tests/test_arraysetops.py
blob: ccdcc75566bb0585f4737cb8581f556e63f85379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
""" Test functions for 1D array set operations.

"""

from numpy.testing import *
set_package_path()
import numpy
from numpy.lib.arraysetops import *
from numpy.lib.arraysetops import ediff1d
restore_path()

##################################################

class test_aso(NumpyTestCase):
    ##
    # 03.11.2005, c
    def check_unique1d( self ):

        a = numpy.array( [5, 7, 1, 2, 1, 5, 7] )

        ec = numpy.array( [1, 2, 5, 7] )
        c = unique1d( a )
        assert_array_equal( c, ec )

        assert_array_equal([], unique1d([]))

    ##
    # 03.11.2005, c
    def check_intersect1d( self ):

        a = numpy.array( [5, 7, 1, 2] )
        b = numpy.array( [2, 4, 3, 1, 5] )

        ec = numpy.array( [1, 2, 5] )
        c = intersect1d( a, b )
        assert_array_equal( c, ec )

        assert_array_equal([], intersect1d([],[]))

    ##
    # 03.11.2005, c
    def check_intersect1d_nu( self ):

        a = numpy.array( [5, 5, 7, 1, 2] )
        b = numpy.array( [2, 1, 4, 3, 3, 1, 5] )

        ec = numpy.array( [1, 2, 5] )
        c = intersect1d_nu( a, b )
        assert_array_equal( c, ec )

        assert_array_equal([], intersect1d_nu([],[]))

    ##
    # 03.11.2005, c
    def check_setxor1d( self ):

        a = numpy.array( [5, 7, 1, 2] )
        b = numpy.array( [2, 4, 3, 1, 5] )

        ec = numpy.array( [3, 4, 7] )
        c = setxor1d( a, b )
        assert_array_equal( c, ec )

        a = numpy.array( [1, 2, 3] )
        b = numpy.array( [6, 5, 4] )

        ec = numpy.array( [1, 2, 3, 4, 5, 6] )
        c = setxor1d( a, b )
        assert_array_equal( c, ec )

        a = numpy.array( [1, 8, 2, 3] )
        b = numpy.array( [6, 5, 4, 8] )

        ec = numpy.array( [1, 2, 3, 4, 5, 6] )
        c = setxor1d( a, b )
        assert_array_equal( c, ec )

        assert_array_equal([], setxor1d([],[]))

    def check_ediff1d(self):
        zero_elem = numpy.array([])
        one_elem = numpy.array([1])
        two_elem = numpy.array([1,2])

        assert_array_equal([],ediff1d(zero_elem))
        assert_array_equal([0],ediff1d(zero_elem,to_begin=0))
        assert_array_equal([0],ediff1d(zero_elem,to_end=0))
        assert_array_equal([-1,0],ediff1d(zero_elem,to_begin=-1,to_end=0))
        assert_array_equal([],ediff1d(one_elem))
        assert_array_equal([1],ediff1d(two_elem))

    ##
    # 03.11.2005, c
    def check_setmember1d( self ):

        a = numpy.array( [5, 7, 1, 2] )
        b = numpy.array( [2, 4, 3, 1, 5] )

        ec = numpy.array( [True, False, True, True] )
        c = setmember1d( a, b )
        assert_array_equal( c, ec )

        a[0] = 8
        ec = numpy.array( [False, False, True, True] )
        c = setmember1d( a, b )
        assert_array_equal( c, ec )

        a[0], a[3] = 4, 8
        ec = numpy.array( [True, False, True, False] )
        c = setmember1d( a, b )
        assert_array_equal( c, ec )

        assert_array_equal([], setmember1d([],[]))

    ##
    # 03.11.2005, c
    def check_union1d( self ):

        a = numpy.array( [5, 4, 7, 1, 2] )
        b = numpy.array( [2, 4, 3, 3, 2, 1, 5] )

        ec = numpy.array( [1, 2, 3, 4, 5, 7] )
        c = union1d( a, b )
        assert_array_equal( c, ec )

        assert_array_equal([], union1d([],[]))

    ##
    # 03.11.2005, c
    # 09.01.2006
    def check_setdiff1d( self ):

        a = numpy.array( [6, 5, 4, 7, 1, 2] )
        b = numpy.array( [2, 4, 3, 3, 2, 1, 5] )

        ec = numpy.array( [6, 7] )
        c = setdiff1d( a, b )
        assert_array_equal( c, ec )

        a = numpy.arange( 21 )
        b = numpy.arange( 19 )
        ec = numpy.array( [19, 20] )
        c = setdiff1d( a, b )
        assert_array_equal( c, ec )

        assert_array_equal([], setdiff1d([],[]))


    ##
    # 03.11.2005, c
    def check_manyways( self ):

        nItem = 100
        a = numpy.fix( nItem / 10 * numpy.random.random( nItem ) )
        b = numpy.fix( nItem / 10 * numpy.random.random( nItem ) )

        c1 = intersect1d_nu( a, b )
        c2 = unique1d( intersect1d( a, b ) )
        assert_array_equal( c1, c2 )

        a = numpy.array( [5, 7, 1, 2, 8] )
        b = numpy.array( [9, 8, 2, 4, 3, 1, 5] )

        c1 = setxor1d( a, b )
        aux1 = intersect1d( a, b )
        aux2 = union1d( a, b )
        c2 = setdiff1d( aux2, aux1 )
        assert_array_equal( c1, c2 )

if __name__ == "__main__":
    NumpyTest().run()