What's the difference between "numpy.add(a,b)" and "a+b"?
Asked Answered
R

2

9

Is there any difference between numpy.add(a,b) and a+b when adding two ndarrays a and b? The documentation says that numpy.add is the "Equivalent to x1 + x2 in terms of array broadcasting.". But I don't unserstand what this means, since numpy.add(numpy.array([1,2,3]),4) also works.

Receptor answered 3/8, 2016 at 15:19 Comment(0)
J
11

In Python syntax, a+b is translated to a.__add__(b). a.__add__ is a method that implements addition for objects of type a. A number has such a method, a list does as well ([1,3]+[4]) and so does a string ('abc'+'d').

numpy has implemented __add__ (and other standard __... methods) for its ndarray class (at least for the numeric dtypes).

That's all standard Python and numpy and been around for ever.

np.add is a ufunc. Look at its doc - see the out parameter, and the Binary ufuncs: section. It's a function, and has some methods like reduce, reduceat, etc that a.__add__ (and +) does not.

If you have two arrays or numbers and you want to sum them, the natural thing is to use +, a+b. np.add is useful in some special cases

Compare, for example, what happens to two lists:

In [16]: [1,2,3]+[4]
Out[16]: [1, 2, 3, 4]     # default list concatenation
In [17]: np.add([1,2,3],[4])
Out[17]: array([5, 6, 7])   # convert lists to arrays and sum

or an example using 2d broadcasting:

In [19]: np.add([[1],[2],[3]],[4,1])
Out[19]: 
array([[5, 2],
       [6, 3],
       [7, 4]])
In [20]: np.array([1,2,3])[:,None]+np.array([4,1])
Out[20]: 
array([[5, 2],
       [6, 3],
       [7, 4]])

And your example:

In [21]: numpy.add(numpy.array([1,2,3]),4)
Out[21]: array([5, 6, 7])
In [22]: numpy.array([1,2,3])+4
Out[22]: array([5, 6, 7])

"Equivalent to x1 + x2 in terms of array broadcasting." means, they both work and do the same thing.

broadcasting is another subject.

==================

The @ operator and np.matmul parallel isn't quite the same. The @ operator is a recent addition to the Python interpreter. It is translated to a call to the __matmul__ method - if defined. New numpy versions have such a definition. But the method is not defined for Python numbers or lists. There is also a function version, np.matmul, referencing the same code. There is also a x.dot and np.dot pairing, but no Python recognized operator or x.__dot__.

Jannelle answered 3/8, 2016 at 16:59 Comment(2)
Thank you very much for your detailed answer. If I got this right, numpy.add is faster (if it can be assured that a and b are ndarrays) than a+b since it is a ufunc?Receptor
The fact that one is a ufunc and the other a method shouldn't make a difference in speed. That's a user interface issue, not a speed one. I use + unless I need the added functionality of add (such as in add.reduceat).Jannelle
W
0

I believe that add() was first, the the library improved and gained normal mathematical operators.

BTW, we witness the same thing now with Python 3.5 and numpy, where matrix multiplication can be performed by the operator @. Prior to that, matrix mult of arrays could be done only by the dot() method of the array.

Broadcasting means the ability to perform operations between arrays of different sizes, like adding a number to a whole array. This works with operators on arrays as well.

Adding a number to an array is only a tiny example of the broadcasting ability. You can read more about it in Broadcasting It's a very cool feature which saves memory and coding, but not so easy to understand at first reading.

Weeper answered 3/8, 2016 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.