add
Description
Add arguments element-wise.
Mandatory Input Parameters
Parameter |
Type |
Description |
|---|---|---|
x1, x2 |
array_like |
Arrays or scalars to be added. If x1.shape!=x2.shape, they must be broadcastable to a common shape. |
Optional Input Parameters
Return Value
Type |
Description |
|---|---|
ndarray/scalar |
Sum of x1 and x2, element-wise |
Examples
>>> import numpy as np
>>> np.add(1.1, 5.5)
6.6
>>> x1 = np.arange(9.0).reshape((3,3))
>>> x2 = np.arange(3.0)
>>> x1
array([[0., 1., 2.],
[3., 4., 5.],
[6., 7., 8.]])
>>> x2
array([0., 1., 2.])
>>>
>>> np.add(x1, x2)
array([[ 0., 2., 4.],
[ 3., 5., 7.],
[ 6., 8., 10.]])
>>
# If both x1 and x2 are of the ndarray type, you can use + to replace np.add.
>>> x1 = np.arange(0, 4).reshape((2,2))
>>> x1
array([[0, 1],
[2, 3]])
>>> x2 = np.arange(4, 8).reshape((2,2))
>>> x2
array([[4, 5],
[6, 7]])
>>>
>>> x1 + x2
array([[ 4, 6],
[ 8, 10]])
>>>
Parent topic: Basic Operation Functions