Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NumPy: Where in the source code are `arange` and `array` functions defined?

Tags:

python

numpy

I was looking for the source code for the arange and array functions in NumPy, but I couldn't find it: https://github.com/numpy/numpy/search?utf8=%E2%9C%93&q=%22def+arange%22+path%3Anumpy%2Fcore&type=

Could anyone enlighten me?

like image 381
FreshAir Avatar asked May 08 '18 04:05

FreshAir


2 Answers

numpy.array and numpy.arange are written in C. You can tell because they say "built-in" when you look at them:

>>> numpy.array
<built-in function array>
>>> numpy.arange
<built-in function arange>

That means there's no def statement. Instead, we look at what module they come from:

>>> numpy.array.__module__
'numpy.core.multiarray'
>>> numpy.arange.__module__
'numpy.core.multiarray'

navigate to the corresponding source file, and take a look at the array controlling the module's exported functions:

{"array",
    (PyCFunction)_array_fromobject,
    METH_VARARGS|METH_KEYWORDS, NULL},
...
{"arange",
    (PyCFunction)array_arange,
    METH_VARARGS|METH_KEYWORDS, NULL},

numpy.array and numpy.arange correspond to _array_fromobject and array_arange in that file. That's not where all the work happens, though. You'll need to keep digging to find all the relevant code.

like image 55
user2357112 supports Monica Avatar answered Oct 08 '22 04:10

user2357112 supports Monica


These are defined in multiarraymodule.c:

https://github.com/numpy/numpy/blob/820765d762513510a8e46f108e8bc8b366127f8f/numpy/core/src/multiarray/multiarraymodule.c#L4279

array function in Python is _array_fromobject() in C, and arange function in Python is array_arange() in C.

like image 20
HYRY Avatar answered Oct 08 '22 06:10

HYRY