# [Numpy-discussion] Multiple Boolean Operations

Andrea Gavana andrea.gavana@gmail....
Thu May 22 17:19:22 CDT 2008

```Hi Chris and All,

On Thu, May 22, 2008 at 8:40 PM, Christopher Barker wrote:
> Andrea Gavana wrote:
>> By the way, about the solution Francesc posted:
>>
>> xyzReq = (xCent >= xMin) & (xCent <= xMax) &  \
>>             (yCent >= yMin) & (yCent <= yMax) &  \
>>             (zCent >= zMin) & (zCent <= zMax)
>>
>> xyzReq = numpy.nonzero(xyzReq)[0]
>>
>> Do you think is there any chance that a C extension (or something
>> similar) could be faster?
>
> yep -- if I've be got this right, the above creates 7 temporary arrays.
> creating that many and pushing the data in and out of memory can be
> pretty slow for large arrays.
>
> In C, C++, Cython or Fortran, you can just do one loop, and one output
> array. It should be much faster for the big arrays.

Well, I have implemented it in 2 ways in Fortran, and actually the
Fortran solutions are slower than the numpy one (2 and 3 times slower
respectively). I attach the source code of the timing code and the 5
implementations I have at the moment (I have included Nathan's
implementation, which is as fast as Francesc's one but it has the

The timing I get on my home PC are:

Andrea's Solution: 0.42807561 Seconds/Trial
Francesc's Solution: 0.018297884 Seconds/Trial
Fortran Solution 1: 0.035862072 Seconds/Trial
Fortran Solution 2: 0.029822338 Seconds/Trial
Nathan's Solution: 0.018930507 Seconds/Trial

Maybe my fortran coding is sloppy but I don't really know fortran so
well to implement it better...

Thank you so much to everybody for your suggestions so far :-D

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: MultipleBoolean.py
Url: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080522/e0405ab5/attachment-0001.pl
-------------- next part --------------
A non-text attachment was scrubbed...
Name: MultipleBoolean3.f90
Type: application/octet-stream
Size: 569 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080522/e0405ab5/attachment-0002.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: MultipleBoolean4.f90
Type: application/octet-stream
Size: 630 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080522/e0405ab5/attachment-0003.obj
```