numpy.ufunc.outer#
方法
- ufunc.outer(A, B, /, **kwargs)#
將 ufunc op 應用於 A 中的 a 和 B 中的 b 的所有配對 (a, b)。
令
M = A.ndim
,N = B.ndim
。則op.outer(A, B)
的結果 C 是一個維度為 M + N 的陣列,使得\[C[i_0, ..., i_{M-1}, j_0, ..., j_{N-1}] = op(A[i_0, ..., i_{M-1}], B[j_0, ..., j_{N-1}])\]對於一維的 A 和 B,這等同於
r = empty(len(A),len(B)) for i in range(len(A)): for j in range(len(B)): r[i,j] = op(A[i], B[j]) # op = ufunc in question
- 參數:
- 返回:
- rndarray
輸出陣列
另請參閱
numpy.outer
功能較弱版本的
np.multiply.outer
,它將所有輸入ravel
成 1D。這主要是為了與舊程式碼相容而存在。tensordot
np.tensordot(a, b, axes=((), ()))
和np.multiply.outer(a, b)
對於 a 和 b 的所有維度行為相同。
範例
>>> np.multiply.outer([1, 2, 3], [4, 5, 6]) array([[ 4, 5, 6], [ 8, 10, 12], [12, 15, 18]])
多維範例
>>> A = np.array([[1, 2, 3], [4, 5, 6]]) >>> A.shape (2, 3) >>> B = np.array([[1, 2, 3, 4]]) >>> B.shape (1, 4) >>> C = np.multiply.outer(A, B) >>> C.shape; C (2, 3, 1, 4) array([[[[ 1, 2, 3, 4]], [[ 2, 4, 6, 8]], [[ 3, 6, 9, 12]]], [[[ 4, 8, 12, 16]], [[ 5, 10, 15, 20]], [[ 6, 12, 18, 24]]]])