~jtaylor/ubuntu/precise/python-numpy/multiarch-fix-818867

« back to all changes in this revision

Viewing changes to numpy/lib/arraysetops.py

  • Committer: Bazaar Package Importer
  • Author(s): Ondrej Certik, Riku Voipio, Tiziano Zito, Carlos Galisteo, Ondrej Certik
  • Date: 2008-07-08 15:08:16 UTC
  • mfrom: (0.1.21 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080708150816-ekf992jcp2k1eua3
Tags: 1:1.1.0-3
[ Riku Voipio ]
* debian/control: atlas is not available on armel, and after a quick look
  neither on alpha. I'd also suggest dropping
  libatlas-sse-dev|libatlas-sse2-dev|libatlas-3dnow-dev alternative combo
  away, these are potentially dangerous on buildd's. Ondrej: dropped.
  (Closes: #489568)

[ Tiziano Zito ]
* patch: build _dotblas.c when ATLAS is not installed, build-conflict with
  atlas, build-depend on blas+lapack only, as it used to be (Closes: #489726)

[ Carlos Galisteo ]
* debian/control
  - Added Homepage field.

[ Ondrej Certik ]
* Checked the package on i386 and amd64, both with and without atlas, all
  tests run and the numpy package is faster if atlas is around. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""
2
 
Set operations for 1D numeric arrays based on sort() function.
 
2
Set operations for 1D numeric arrays based on sorting.
3
3
 
4
 
Contains:
 
4
:Contains:
5
5
  ediff1d,
6
6
  unique1d,
7
7
  intersect1d,
11
11
  union1d,
12
12
  setdiff1d
13
13
 
14
 
All functions work best with integer numerical arrays on input
15
 
(e.g. indices). For floating point arrays, innacurate results may appear due to
16
 
usual round-off and floating point comparison issues.
 
14
:Notes:
 
15
 
 
16
All functions work best with integer numerical arrays on input (e.g. indices).
 
17
For floating point arrays, innacurate results may appear due to usual round-off
 
18
and floating point comparison issues.
17
19
 
18
20
Except unique1d, union1d and intersect1d_nu, all functions expect inputs with
19
 
unique elements. Speed could be gained in some operations by an implementaion
20
 
of sort(), that can provide directly the permutation vectors, avoiding thus
21
 
calls to argsort().
 
21
unique elements. Speed could be gained in some operations by an implementaion of
 
22
sort(), that can provide directly the permutation vectors, avoiding thus calls
 
23
to argsort().
22
24
 
23
 
Run test_unique1d_speed() to compare performance of numpy.unique1d() and
 
25
Run _test_unique1d_speed() to compare performance of numpy.unique1d() and
24
26
numpy.unique() - it should be the same.
25
27
 
26
28
To do: Optionally return indices analogously to unique1d for all functions.
27
29
 
28
 
Author: Robert Cimrman
29
 
 
30
30
created:       01.11.2005
31
 
last revision: 12.10.2006
 
31
last revision: 07.01.2007
 
32
 
 
33
:Author: Robert Cimrman
32
34
"""
33
35
__all__ = ['ediff1d', 'unique1d', 'intersect1d', 'intersect1d_nu', 'setxor1d',
34
36
           'setmember1d', 'union1d', 'setdiff1d']
36
38
import time
37
39
import numpy as nm
38
40
 
39
 
def ediff1d(ary, to_end = None, to_begin = None):
40
 
    """Array difference with prefixed and/or appended value.
41
 
 
42
 
    See also: unique1d, intersect1d, intersect1d_nu, setxor1d,
43
 
    setmember1d, union1d, setdiff1d
 
41
def ediff1d(ary, to_end=None, to_begin=None):
 
42
    """The differences between consecutive elements of an array, possibly with
 
43
    prefixed and/or appended values.
 
44
 
 
45
    Parameters
 
46
    ----------
 
47
    ary : array
 
48
        This array will be flattened before the difference is taken.
 
49
    to_end : number, optional
 
50
        If provided, this number will be tacked onto the end of the returned
 
51
        differences.
 
52
    to_begin : number, optional
 
53
        If provided, this number will be taked onto the beginning of the
 
54
        returned differences.
 
55
 
 
56
    Returns
 
57
    -------
 
58
    ed : array
 
59
        The differences. Loosely, this will be (ary[1:] - ary[:-1]).
 
60
 
44
61
    """
45
62
    ary = nm.asarray(ary).flat
46
63
    ed = ary[1:] - ary[:-1]
 
64
    arrays = [ed]
47
65
    if to_begin is not None:
48
 
        if to_end is not None:
49
 
            ed = nm.r_[to_begin, ed, to_end]
50
 
        else:
51
 
            ed = nm.insert(ed, 0, to_begin)
52
 
    elif to_end is not None:
53
 
        ed = nm.append(ed, to_end)
54
 
        
 
66
        arrays.insert(0, to_begin)
 
67
    if to_end is not None:
 
68
        arrays.append(to_end)
 
69
 
 
70
    if len(arrays) != 1:
 
71
        # We'll save ourselves a copy of a potentially large array in the common
 
72
        # case where neither to_begin or to_end was given.
 
73
        ed = nm.hstack(arrays)
 
74
 
55
75
    return ed
56
76
 
57
77
def unique1d(ar1, return_index=False):
58
 
    """Unique elements of 1D array. When return_index is True, return
59
 
    also the indices indx such that ar1.flat[indx] is the resulting
60
 
    array of unique elements.
61
 
    
62
 
    See also: ediff1d, intersect1d, intersect1d_nu, setxor1d,
63
 
    setmember1d, union1d, setdiff1d
 
78
    """Find the unique elements of 1D array.
 
79
 
 
80
    Most of the other array set operations operate on the unique arrays
 
81
    generated by this function.
 
82
 
 
83
    Parameters
 
84
    ----------
 
85
    ar1 : array
 
86
        This array will be flattened if it is not already 1D.
 
87
    return_index : bool, optional
 
88
        If True, also return the indices against ar1 that result in the unique
 
89
        array.
 
90
 
 
91
    Returns
 
92
    -------
 
93
    unique : array
 
94
        The unique values.
 
95
    unique_indices : int array, optional
 
96
        The indices of the unique values. Only provided if return_index is True.
 
97
 
 
98
    See Also
 
99
    --------
 
100
      numpy.lib.arraysetops : Module with a number of other functions
 
101
                              for performing set operations on arrays.
 
102
 
64
103
    """
65
104
    ar = nm.asarray(ar1).flatten()
66
105
    if ar.size == 0:
67
106
        if return_index: return nm.empty(0, nm.bool), ar
68
107
        else: return ar
69
 
    
 
108
 
70
109
    if return_index:
71
110
        perm = ar.argsort()
72
111
        aux = ar[perm]
73
112
        flag = nm.concatenate( ([True], aux[1:] != aux[:-1]) )
74
113
        return perm[flag], aux[flag]
75
 
    
 
114
 
76
115
    else:
77
116
        ar.sort()
78
117
        flag = nm.concatenate( ([True], ar[1:] != ar[:-1]) )
79
118
        return ar[flag]
80
119
 
81
 
def intersect1d( ar1, ar2 ):
 
120
def intersect1d(ar1, ar2):
82
121
    """Intersection of 1D arrays with unique elements.
83
122
 
84
 
    See also: ediff1d, unique1d, intersect1d_nu, setxor1d,
85
 
    setmember1d, union1d, setdiff1d
 
123
    Use unique1d() to generate arrays with only unique elements to use as inputs
 
124
    to this function. Alternatively, use intersect1d_nu() which will find the
 
125
    unique values for you.
 
126
 
 
127
    Parameters
 
128
    ----------
 
129
    ar1 : array
 
130
    ar2 : array
 
131
 
 
132
    Returns
 
133
    -------
 
134
    intersection : array
 
135
 
 
136
    See Also
 
137
    --------
 
138
    numpy.lib.arraysetops : Module with a number of other functions for
 
139
                            performing set operations on arrays.
 
140
 
86
141
    """
87
142
    aux = nm.concatenate((ar1,ar2))
88
143
    aux.sort()
89
144
    return aux[aux[1:] == aux[:-1]]
90
145
 
91
 
def intersect1d_nu( ar1, ar2 ):
 
146
def intersect1d_nu(ar1, ar2):
92
147
    """Intersection of 1D arrays with any elements.
93
148
 
94
 
    See also: ediff1d, unique1d, intersect1d, setxor1d,
95
 
    setmember1d, union1d, setdiff1d
 
149
    The input arrays do not have unique elements like intersect1d() requires.
 
150
 
 
151
    Parameters
 
152
    ----------
 
153
    ar1 : array
 
154
    ar2 : array
 
155
 
 
156
    Returns
 
157
    -------
 
158
    intersection : array
 
159
 
 
160
    See Also
 
161
    --------
 
162
    numpy.lib.arraysetops : Module with a number of other functions for
 
163
                            performing set operations on arrays.
 
164
 
96
165
    """
97
 
    # Might be faster then unique1d( intersect1d( ar1, ar2 ) )?
 
166
    # Might be faster than unique1d( intersect1d( ar1, ar2 ) )?
98
167
    aux = nm.concatenate((unique1d(ar1), unique1d(ar2)))
99
168
    aux.sort()
100
169
    return aux[aux[1:] == aux[:-1]]
101
170
 
102
 
def setxor1d( ar1, ar2 ):
 
171
def setxor1d(ar1, ar2):
103
172
    """Set exclusive-or of 1D arrays with unique elements.
104
173
 
105
 
    See also: ediff1d, unique1d, intersect1d, intersect1d_nu,
106
 
    setmember1d, union1d, setdiff1d
 
174
    Use unique1d() to generate arrays with only unique elements to use as inputs
 
175
    to this function.
 
176
 
 
177
    Parameters
 
178
    ----------
 
179
    ar1 : array
 
180
    ar2 : array
 
181
 
 
182
    Returns
 
183
    -------
 
184
    xor : array
 
185
        The values that are only in one, but not both, of the input arrays.
 
186
 
 
187
    See Also
 
188
    --------
 
189
    numpy.lib.arraysetops : Module with a number of other functions for
 
190
                            performing set operations on arrays.
 
191
 
107
192
    """
108
193
    aux = nm.concatenate((ar1, ar2))
109
194
    if aux.size == 0:
110
195
        return aux
111
 
    
 
196
 
112
197
    aux.sort()
113
198
#    flag = ediff1d( aux, to_end = 1, to_begin = 1 ) == 0
114
199
    flag = nm.concatenate( ([True], aux[1:] != aux[:-1], [True] ) )
116
201
    flag2 = flag[1:] == flag[:-1]
117
202
    return aux[flag2]
118
203
 
119
 
def setmember1d( ar1, ar2 ):
120
 
    """Return an array of shape of ar1 containing 1 where the elements of
121
 
    ar1 are in ar2 and 0 otherwise.
122
 
 
123
 
    See also: ediff1d, unique1d, intersect1d, intersect1d_nu, setxor1d,
124
 
    union1d, setdiff1d
 
204
def setmember1d(ar1, ar2):
 
205
    """Return a boolean array of shape of ar1 containing True where the elements
 
206
    of ar1 are in ar2 and False otherwise.
 
207
 
 
208
    Use unique1d() to generate arrays with only unique elements to use as inputs
 
209
    to this function.
 
210
 
 
211
    Parameters
 
212
    ----------
 
213
    ar1 : array
 
214
    ar2 : array
 
215
 
 
216
    Returns
 
217
    -------
 
218
    mask : bool array
 
219
        The values ar1[mask] are in ar2.
 
220
 
 
221
    See Also
 
222
    --------
 
223
    numpy.lib.arraysetops : Module with a number of other functions for
 
224
                            performing set operations on arrays.
 
225
 
125
226
    """
126
 
    zlike = nm.zeros_like
 
227
    ar1 = nm.asarray( ar1 )
 
228
    ar2 = nm.asarray( ar2 )
127
229
    ar = nm.concatenate( (ar1, ar2 ) )
128
 
    tt = nm.concatenate( (zlike( ar1 ), zlike( ar2 ) + 1) )
129
 
    perm = ar.argsort()
 
230
    b1 = nm.zeros( ar1.shape, dtype = nm.int8 )
 
231
    b2 = nm.ones( ar2.shape, dtype = nm.int8 )
 
232
    tt = nm.concatenate( (b1, b2) )
 
233
 
 
234
    # We need this to be a stable sort, so always use 'mergesort' here. The
 
235
    # values from the first array should always come before the values from the
 
236
    # second array.
 
237
    perm = ar.argsort(kind='mergesort')
130
238
    aux = ar[perm]
131
239
    aux2 = tt[perm]
132
240
#    flag = ediff1d( aux, 1 ) == 0
133
241
    flag = nm.concatenate( (aux[1:] == aux[:-1], [False] ) )
134
 
 
135
242
    ii = nm.where( flag * aux2 )[0]
136
243
    aux = perm[ii+1]
137
244
    perm[ii+1] = perm[ii]
138
245
    perm[ii] = aux
139
246
 
140
 
    indx = perm.argsort()[:len( ar1 )]
 
247
    indx = perm.argsort(kind='mergesort')[:len( ar1 )]
141
248
 
142
249
    return flag[indx]
143
250
 
144
 
def union1d( ar1, ar2 ):
145
 
    """Union of 1D arrays with unique elements.
146
 
 
147
 
    See also: ediff1d, unique1d, intersect1d, intersect1d_nu, setxor1d,
148
 
    setmember1d, setdiff1d
 
251
def union1d(ar1, ar2):
 
252
    """
 
253
    Union of 1D arrays with unique elements.
 
254
 
 
255
    Use unique1d() to generate arrays with only unique elements to use as inputs
 
256
    to this function.
 
257
 
 
258
    Parameters
 
259
    ----------
 
260
    ar1 : array
 
261
    ar2 : array
 
262
 
 
263
    Returns
 
264
    -------
 
265
    union : array
 
266
 
 
267
    See also
 
268
    --------
 
269
    numpy.lib.arraysetops : Module with a number of other functions for
 
270
                            performing set operations on arrays.
 
271
 
149
272
    """
150
273
    return unique1d( nm.concatenate( (ar1, ar2) ) )
151
274
 
152
 
def setdiff1d( ar1, ar2 ):
 
275
def setdiff1d(ar1, ar2):
153
276
    """Set difference of 1D arrays with unique elements.
154
277
 
155
 
    See also: ediff1d, unique1d, intersect1d, intersect1d_nu, setxor1d,
156
 
    setmember1d, union1d
 
278
    Use unique1d() to generate arrays with only unique elements to use as inputs
 
279
    to this function.
 
280
 
 
281
    Parameters
 
282
    ----------
 
283
    ar1 : array
 
284
    ar2 : array
 
285
 
 
286
    Returns
 
287
    -------
 
288
    difference : array
 
289
        The values in ar1 that are not in ar2.
 
290
 
 
291
    See Also
 
292
    --------
 
293
    numpy.lib.arraysetops : Module with a number of other functions for
 
294
                            performing set operations on arrays.
 
295
 
157
296
    """
158
297
    aux = setmember1d(ar1,ar2)
159
298
    if aux.size == 0:
161
300
    else:
162
301
        return nm.asarray(ar1)[aux == 0]
163
302
 
164
 
def test_unique1d_speed( plot_results = False ):
 
303
def _test_unique1d_speed( plot_results = False ):
165
304
#    exponents = nm.linspace( 2, 7, 9 )
166
305
    exponents = nm.linspace( 2, 7, 9 )
167
306
    ratios = []
222
361
        pylab.show()
223
362
 
224
363
if (__name__ == '__main__'):
225
 
    test_unique1d_speed( plot_results = True )
 
364
    _test_unique1d_speed( plot_results = True )