[Numpy-discussion] Slicing API?

Todd Miller jmiller at stsci.edu
Thu Jan 2 12:36:53 EST 2003

Edward C. Jones wrote:

> Todd Miller wrote:
>> Edward C. Jones wrote:
>>> Both in Numeric and now in numarray I have found a need for API 
>>> functions for slicing. Has anyone thought about this?
>> Speaking for myself and the numarray C-API, the answer is no.   What 
>> API do you want?   Can you suggest function prototypes? 
> An API version of  arrout[slices] = arrin[slices]:
> static int
> NA_CopySlice(PyArrayObject* arrin, PyArrayObject* arrout,
>     int* startin, int* stepin, int* stopin, int* startout, int* stepout);
I would suggest something more like the following then:

typedef struct {
    int start, stop, step;
} NumSlice;

static int
NA_CopySlice(PyArrayObject* arrin, int indim, NumSlice *slicein,
    PyArrayObject* arrout,  int outdim, NumSlice *sliceout);

The differences are:

1.  A slice dimension count is added for both input and output arrays. 
 This enables use of partial indices.

2.  Slice values are expressed using the NumSlice typedef/struct rather 
than 3 independent int arrays.

3. The parameter order is shuffled so that input array parameters are 
kept together, and output array parameters are kept together.

But,  I still have these comments:

1.  It looks like it will be cumbersome to use.

2.  We should probably implement it as a callback to Python to avoid 
introducing another set of assignment semantics.  Thus, the 
implementation would really just be building up and executing the calls 
for:  outarr.__setitem__(outslices, inarr.__getitem__(inslices)).

3. The slicing implementation for numarray objects should be optimized 
to C this quarter, if not this month.  So in terms of efficiency, not to 
mention comment 2, this won't buy much.

4. Since Numeric doesn't have this already,  we're probably missing 
something obvious.  

Comments?  Still interested?


More information about the NumPy-Discussion mailing list