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

David Cournapeau david at ar.media.kyoto-u.ac.jp
Tue Jan 9 08:19:22 CST 2007


Hi,

    I am finally implementing a C function to replace the current slow 
implementation of clip in python as promised a few weeks ago. The idea 
is to implement it as the following:

def clip(input, min, max):
    a   = input.copy()
    putmask(a, a <= min, min)
    putmask(a, a >= max, max)
    return a

I don't have that much experience in writing general numpy functions, so 
I was wondering of other people could advise me on the following points.

    - should I care only about numeric types, eg something where 
PyArray_ISNUMBER returns true ?
    - If input is an array of type T1, min a scalar of type T2 and max a 
scalar of type T3, which type should be the reference, assuming they are 
all basic numeric types (float, int, etc...). Eg, if T1 is integer, T2 
and T3 are float, what am I supposed to do ? Should I use the basic rule 
as told in numpy doc, and in that case, and do something like this to 
determine common datatype (pseudo code):

    int typenum;
    for each arg in arglist:
       typenum = PyArray_ObjectType(arg, typenum)

    - PyArray_PutMask does not assume anything on the datatype of the 
input array nor this alignement, and treat its binary content as a 
buffer to char*. Do I need to do the same, or can I just use "normal" 
pointer arithmetic on arrays which elements can be casted to basic C types ?

    cheers,

    David


More information about the Numpy-discussion mailing list