numpy.add#

numpy.add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'add'>#

逐元素相加引數。

參數:
x1, x2array_like

要相加的陣列。如果 x1.shape != x2.shape,它們必須可廣播到一個共同的形狀(這會變成輸出的形狀)。

outndarray, None, 或 ndarray 和 None 的 tuple, 可選

儲存結果的位置。如果提供,則其形狀必須與輸入廣播到的形狀一致。如果未提供或為 None,則會傳回新分配的陣列。tuple(僅能作為關鍵字引數)的長度必須等於輸出的數量。

wherearray_like, 可選

此條件會廣播到輸入之上。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他位置,out 陣列將保留其原始值。請注意,如果透過預設 out=None 建立未初始化的 out 陣列,則其中條件為 False 的位置將保持未初始化。

**kwargs

對於其他僅限關鍵字的引數,請參閱 ufunc 文件

傳回:
addndarray 或 scalar

x1x2 的元素級總和。如果 x1x2 都是純量,則這是一個純量。

註解

在陣列廣播方面,等效於 x1 + x2

範例

>>> import numpy as np
>>> np.add(1.0, 4.0)
5.0
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.add(x1, x2)
array([[  0.,   2.,   4.],
       [  3.,   5.,   7.],
       [  6.,   8.,  10.]])

+ 運算子可以用作 np.add 在 ndarray 上的簡寫。

>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> x1 + x2
array([[ 0.,  2.,  4.],
       [ 3.,  5.,  7.],
       [ 6.,  8., 10.]])