3

I am trying to locate the NumPy .c file that contains basic vectorized array operations. For instance, I'd like to know what piece of code runs when you do something simple like a scalar addition to an array like a + 5 or perform an aggregation like a.sum(). I believe the ndarray object gets declared here.

I'd also like to know if any of the linear algebra libraries like BLAS or LAPACK are involved with these basic arithmetic operations? Is the code as simple as a for-loop iterating over a C array or is there some magical way that computers do basic operations on contiguous arrays without for-loops?

1 Answer 1

3

Most of that stuff is in numpy/core/src/umath/loops.c.src. This is a template file NumPy uses to generate lots and lots of very similar C functions. No BLAS or LAPACK calls are involved.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for pointing me to this. Just to be 100% clear - numpy uses a 'regular' for-loop to do things like add scalars to arrays and sum them up?
I think I am close to tying it all together. The sum array method is defined in calculation.h which calls the PyArray_GenericReduceFunction. This uses a NumericOps variable. This is where I am lost. It appears that many methods are set with PyArray_SetNumericOps but I don't see where that function is called and I don't know how it links to the for-loops.
@TedPetrou: It's not quite as regular as the for loops you're probably used to, and it's not always one for loop, but most of the iteration is done with for loops. As for PyArray_SetNumericOps, that happens here, in the initializer for the numpy.core.umath module.
(While hunting that down, I found there's actually a Python interface numpy.core.multiarray.set_numeric_ops that permits overriding the defaults set by the umath initializer, so if you want to get crazy, you can redefine how basic arithmetic operations behave for ndarrays.)
Thanks for the help. I'm still not sure of the sequence of PyArray_SetNumericOps getting called to the 'reduce' operation getting appended to n_ops.add and finally the for-loops completing the action in loops.c.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.