What is the corresponding C-api name of the following numpy function in Python
NickName:dongrixinyu Ask DateTime:2022-09-30T11:03:58

What is the corresponding C-api name of the following numpy function in Python

  • I wanna write a C extension lib for Python, aiming to replace Python code with C.
  • and the Python codes has several lines like below:
import numpy as np
a = np.array([1,3,12,0.43,234,-3,-4])
b = a[[1,3,5]]
print(b)

# array([ 3.  ,  0.43, -3.  ])

  • Different from an int as the index of a numpy array, this example treats an array as an index.

  • I am confused that getting the designated indexes of a given numpy array, what is the corresponding C-API name?

  • the NUMPY C-API files is in numpy c-api

  • Thanks very much.

Copyright Notice:Content Author:「dongrixinyu」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/73903608/what-is-the-corresponding-c-api-name-of-the-following-numpy-function-in-python

Answers
Jérôme Richard 2022-09-30T10:59:00

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.\nNumpy 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).\nWhile 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).",


More about “What is the corresponding C-api name of the following numpy function in Python” related questions

What is the corresponding C-api name of the following numpy function in Python

I wanna write a C extension lib for Python, aiming to replace Python code with C. and the Python codes has several lines like below: import numpy as np a = np.array([1,3,12,0.43,234,-3,-4]) b = a[...

Show Detail

Reversing axis in Numpy array using C-API

I am using the Python C-API to wrap some C++ code as a Python package. In the end, I have to reverse an axis in a numpy array, i.e. doing x = x[:, ::-1] Is there some way of doing this using the ...

Show Detail

How do I share the random number generator in numpy c-api?

I wrote a Python program for Monte Carlo simulations that calls numpy functions that makes use of the built-in random number generator (e.g., np.random.normal() or np.random.choice()) and also impo...

Show Detail

How to call a python function by name from the C-API?

From the c-api, I would like to call a python function by name. I would then be calling the function with a list of python objects as arguments. It is not clear to me in the Python documentation ...

Show Detail

Python C-API equivalent of "python -m module_name" (python 2.7)

Having looked the Python C-API documentation, I cannot figure out a way to execute a python script given just the name of the module (from c or c++ code). Is there an equivalent C-API function for

Show Detail

What is a PyObject in Python?

Short version I recently came across some Python code in which the return type for a function was specified as PyObject in the documentation. What is a PyObject? Detailed version I am not a C/C++

Show Detail

What is parameter name in PyArg_UnpackTuple (python c api) for?

This documentation http://docs.python.org/2/c-api/arg.html#PyArg_UnpackTuple describes all the parameters of function, except for const char *name. What is this parameter for? What should I put th...

Show Detail

Python+Numpy modules in free pascal

Developing a module (.pyd) for Python in free pascal is fairly easy, see Developing Python Modules with Pascal. But if I want to interface with numpy, this is not that easy. When using C to interface

Show Detail

Python C-API int128 support

In python one can handle very large integers (for instance uuid.uuid4().int.bit_length() gives 128), but the largest int datastructure the C-API documentation offers is long long, and it is a 64-bi...

Show Detail

numpy ufunc c-api ndarray(dtype=custom_dtype) operaration scalar of custom_dtype

I'm struggling with the situation when I need a code like this to work: from custom_lib import custom_type, custom_dtype import numpy as np a = custom_type(1) arr = np.array([a,a],dtype=custom_dty...

Show Detail