High-level Numpy functions are not meant to be used from C. In fact, not all of them are implemented in C (some are implemented in pure-Python calling other high-level function themselves implemented in C). In fact, there is not much benefit from doing apart from reusing some code that would not be very efficient compared to what can be implemented in C directly.
Numpy provides a quite-minimal interface (the one you provided) so modules can operate on Numpy array. In practice, low-level C modules often extract a pointer with PyArray_GetPtr
(or more specific macros like PyArray_GETPTR2
, while checking flags so to be safe) and directly operate on the array buffer based on strides (extracted with PyArray_STRIDES
). A Numpy array is just a big raw buffer with a fixed size and some meta-informations. Views add more information like the number of dimensions, the shape, the strides, etc. If possible, it is better to check the array is contiguous and write a code for contiguous arrays. Indeed, compilers tends not to generate a fast code when some strides are set to 1: it is your responsibility to optimize this in C (this is what Numpy does in its function but this part introduces some overheads). A code optimized to operate on contiguous array can be much faster (mainly due to the possible use of SIMD instruction and faster indexing instructions).
While you could use PyArray_GETITEM
, the resulting code will be significantly slower than a direct access due to possible checks, an inefficient generic indexing (breaking many compiler optimizations) and also because the function will certainly not be inlined by the compiler. It might be faster than a pure-Python code, but not by a large margin (certainly similar to a Cython code not using a direct indexing).