0

I was coding in numpy and had a question that made me think about the structure of the package and how methods are implemented. Here I give basic methods to illustrate my point:

import numpy as np
a = np.array([1, 2, 3])
b = np.ones(3)

It is possible to do either a.dot(b) or np.dot(a, b). However, it is only possible to do np.multiply(a, b) since multiply is not an attribute of numpy.ndarray.

I was wondering, if the dot is twice defined as a static method and non-static one under the numpy.ndarray class, and np.multiply as a pure static method? Then, it is part of the class and instance, and the use of both ways is justified.

Then, I figured out that the dot method is part of the numpy.core.multiarray.dot and there is no such decorator as static. However, it is wrapped by some other C function. Is there any explanation on how things are internally implemented that justify this case? How is the big picture package structure of numpy?

1 Answer 1

1

The compiled internals of numpy, whether we are talking about the functions or the ndarray methods, are complicated, and don't readily fit the textbook python class layout.

Often when there are functions and methods of the same name, the function ensures that its argument(s) is an array and then delegates the action to the method. If the function is written python, you might see it ending with (e.g. for np.sum):

return _wrapreduction(....)

np.dot with 2 arguments doesn't quite if this pattern, but it's close enough. It's also compiled so we can't readily see how it works together with the dot method.

np.multiply is a ufunc, a large group of compiled functions with a shared calling structure. The function docs elaborate on that.

It has a note:

Equivalent to `x1` * `x2` in terms of array broadcasting

The python interpreter converts operators like * into method calls like x.__mul__. Each class implements that in its own way. Often the numpy operators use one of the ufunc (but with a c-level call).

np.matmul is a ufunc that works a lot like np.dot. The @ operator calls x.__matmul__.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.