# [Numpy-discussion] Multiple Boolean Operations

Stéfan van der Walt stefan@sun.ac...
Thu May 22 16:23:18 CDT 2008

```Hi Andrea

2008/5/22 Andrea Gavana <andrea.gavana@gmail.com>:
> 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? Or something else using weave? I understand
> that this solution is already highly optimized as it uses the power of
> numpy with the logic operations in Python, but I was wondering if I
> can make it any faster: on my PC, the algorithm runs in 0.01 seconds,
> more or less, for 150,000 cells, but today I encountered a case in
> which I had 10800 sub-grids... 10800*0.01 is close to 2 minutes :-(
> Otherwise, I will try and implement it in Fortran and wrap it with
> f2py, assuming I am able to do it correctly and the overhead of
> calling an external extension is not killing the execution time.

I wrote a quick proof of concept (no guarantees).  You can find it

1. Install Cython if you haven't already
2. Run "python setup.py build_ext -i" to build the C extension
3. Use the code, e.g.,

import xyz
out = xyz.filter(array([1.0, 2.0, 3.0]), 2, 5,
array([2.0, 4.0, 6.0]), 2, 4,
array([-1.0, -2.0, -4.0]), -3, -2)

In the above case, out is [False, True, False].

Regards
Stéfan
```