[Numpy-discussion] Need help for implementing a fast clip in numpy (was slow clip)

Charles R Harris charlesr.harris at gmail.com
Sat Jan 13 01:32:43 EST 2007


On 1/12/07, David Cournapeau <cournape at gmail.com> wrote:
>
> On 1/13/07, Christopher Barker <Chris.Barker at noaa.gov> wrote:
> > I think it may have all been cleared up now, but just in case:
> >
>
> > but how do you get single strided? this is what always made it hard for
> > me to know how to write this kind of code.


<snip>

Make a copy of the array. New copies are C_CONTIGUOUS by default. There is
also the ascontiguousarray function:

ascontiguousarray(a, dtype=None)
    Return 'a' as an array contiguous in memory (C order).

Which makes a copy only when required. Since clip returns a new array
anyway, this shouldn't be a problem except when clipping against another
array, which you would also want to be contiguous. In practice, I don't
think using ascontiguousarray for the clipping array would add much overhead
as the array would likely be contiguous in the first place. You would
probably want to match data types also. I think the needed ops are already
implemented for the types at the c-level, __gt__ for instance, and in that
case you can simply use the function pointer with some loss of speed. If you
can determine the corresponding c-type, and I think you can, then it
shouldn't be too much trouble to implement type specific clipping, but it
might not be worth the effort. There are lots of code snippets in other
functions similar to what you need that could be a good starting point. You
just need to find them 8^)

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070112/d81764e4/attachment.html>


More information about the NumPy-Discussion mailing list