[Numpy-discussion] Questions about C API

Todd Miller jmiller at stsci.edu
Fri Apr 9 11:33:06 CDT 2004

On Fri, 2004-04-09 at 13:25, Russell E Owen wrote:
> I'm trying to write a median function (surely one is there already, 
> but if so I've not found it). The fastest technique seems to involve 
> rearranging the data in place. 

You might want to try out numarray.image.median (or
numarray.image.combine.median for versions < 0.9).

> So I'd like to offer two calls:
> - A version that rearranges the input array if no local copy needed 
> to be made (e.g. the input data was already contiguous and of the 
> expected type). I know how to write that one.
> - A safer, slower version that always makes a copy of the input data. 
> I'm stuck on that. The high-level interface has flags for when to 
> make a local copy, but none of them seems to mean "always make a 
> local copy". Surely I am missing something there? I guess I could get 
> the data, see if a local copy was made and if not, make a copy of my 
> own, but that seems messy.

The high level API is set up to do the least amount of work to guarantee
a set of constraints.  You can independently constrain numarray
properties (contiguousness, byteswap, alignment) and the array type to
be what you know your code can process.  The high level API will create
or convert arrays as necessary, and then release them when the function
is done processing (provided you manage your ref counts correctly as
shown in the manual).  So, the gist of the high level API is that it
takes some mysterious object (a poorly behaved array, an array of the
wrong type, nested lists, etc.) and converts it into a nicely formed
proxy array on which you do your computing.  In the case where the
mysterious object actually was a well behaved properly typed array,
almost no work has to be done and the proxy is the original array.

If you need a copy because your code is going to destroy the input
array,  use NA_copy() or just do a .copy() method call yourself.  Making
this kind of copy could indeed be folded into the existing API and it
would result in simpler application code, but it isn't there now.

> Also, is there any simple sample code running around for handling 
> arrays of any type (e.g. with C++ templates)? 

I think Peter Verveer did some code like this in the nd_image package. 
The general idea is to constrain the arrays with type tAny in the high
level API and then switch off of a->descr->type_num to select type
specific code.


> -- 
> Todd Miller <jmiller at stsci.edu>

More information about the Numpy-discussion mailing list