[Numpy-discussion] Multiple Boolean Operations

Andrea Gavana andrea.gavana at gmail.com
Fri May 23 05:36:07 EDT 2008


Hi Stefan & All,

On Fri, May 23, 2008 at 1:02 AM, Stéfan van der Walt wrote:
> Hi Andrea
>
> 2008/5/23 Andrea Gavana <andrea.gavana at gmail.com>:
>> Thank you very much for this! I am going to try it and time it,
>> comparing it with the other implementations. I think I need to study a
>> bit your code as I know almost nothing about Cython :-D
>
> That won't be necessary -- the Fortran-implementation is guaranteed to win!
>
> Just to make sure, I timed it anyway (on somewhat larger arrays):
>
> Francesc's Solution: 0.062797403 Seconds/Trial
> Fortran Solution 1: 0.050316906 Seconds/Trial
> Fortran Solution 2: 0.052595496 Seconds/Trial
> Nathan's Solution: 0.055562282 Seconds/Trial
> Cython Solution: 0.06250751 Seconds/Trial
>
> Nathan's version runs over the data 6 times, and still does better
> than the Pyrex version.  I don't know why!
>
> But, hey, this algorithm is parallelisable!  Wait, no, it's bedtime.

Thank you so much for testing, and thanks to the list for the kind
help and suggestions. In any case, after all these different
implementations, I think the only way to make it faster is to
progressively reduce the size of the vectors on which I make the
inequality tests, while somehow keeping the original vectors indices.
Let me explain with a example:

# step1 will be a vector with nCells elements, filled with True
# or False values
step1 = xCent >= xMin

# step2 will be a vector with nonzero(step1) cells, filled
# with True or False values
step2 = xCent[step1] <= xMax

# step3 will be a vector with nonzero(step2) cells, filled
# with True or False values
step3 = yCent[step2] >= yMin

And so on. The probelm with this approach is that I lose the original
indices for which I want all the inequality tests to succeed: for
example, consider the following:

>>> xMin, xMax = 3, 7
>>> xCent = numpy.arange(10)
>>> step1 = xCent >= xMin
>>> numpy.nonzero(step1)[0]
array([3, 4, 5, 6, 7, 8, 9])

>>> step2 = xCent[step1] <= xMax
>>> numpy.nonzero(step2)[0]
array([0, 1, 2, 3, 4])

Which are no more the indices of the original vector as I shrunk down
xCent to xCent[step1], and the real indices should be:

>>> realStep = (xCent >= xMin)  & (xCent <= xMax)
>>> numpy.nonzero(realStep)[0]
array([0, 1, 2, 3, 4, 5, 6, 7])

So, now the question is. If I iteratively shrink down the vectors as I
did before, is there any way to get back the original indices for
which all the conditions are satisfied? Sorry if this looks like a
dummy/noob question, is just I am not sure on how to implement it.

Thank you very much for your help.

 Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/



More information about the NumPy-Discussion mailing list