numpy.ma.outer#

ma.outer(a, b)[原始碼]#

計算兩個向量的外積。

給定兩個長度分別為 MN 的向量 ab,外積 [1]

[[a_0*b_0  a_0*b_1 ... a_0*b_{N-1} ]
 [a_1*b_0    .
 [ ...          .
 [a_{M-1}*b_0            a_{M-1}*b_{N-1} ]]
參數:
a(M,) 類陣列 (array_like)

第一個輸入向量。如果輸入還不是一維的,則會被展平。

b(N,) 類陣列 (array_like)

第二個輸入向量。如果輸入還不是一維的,則會被展平。

out(M, N) ndarray,選用

儲存結果的位置

傳回:
out(M, N) ndarray

out[i, j] = a[i] * b[j]

參見

inner
einsum

einsum('i,j->ij', a.ravel(), b.ravel()) 是等效的。

ufunc.outer

推廣到 1D 以外的維度和其他運算的泛化。np.multiply.outer(a.ravel(), b.ravel()) 是等效的。

linalg.outer

與 Array API 相容的 np.outer 變體,僅接受一維輸入。

tensordot

np.tensordot(a.ravel(), b.ravel(), axes=((), ())) 是等效的。

註解

遮罩值會被替換為 0。

參考文獻

[1]

G. H. Golub 和 C. F. Van Loan, 《矩陣計算》(Matrix Computations),第 3 版,巴爾的摩,馬里蘭州,約翰·霍普金斯大學出版社,1996 年,第 8 頁。

範例

建立一個(非常粗略的)網格以計算曼德博集合

>>> import numpy as np
>>> rl = np.outer(np.ones((5,)), np.linspace(-2, 2, 5))
>>> rl
array([[-2., -1.,  0.,  1.,  2.],
       [-2., -1.,  0.,  1.,  2.],
       [-2., -1.,  0.,  1.,  2.],
       [-2., -1.,  0.,  1.,  2.],
       [-2., -1.,  0.,  1.,  2.]])
>>> im = np.outer(1j*np.linspace(2, -2, 5), np.ones((5,)))
>>> im
array([[0.+2.j, 0.+2.j, 0.+2.j, 0.+2.j, 0.+2.j],
       [0.+1.j, 0.+1.j, 0.+1.j, 0.+1.j, 0.+1.j],
       [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
       [0.-1.j, 0.-1.j, 0.-1.j, 0.-1.j, 0.-1.j],
       [0.-2.j, 0.-2.j, 0.-2.j, 0.-2.j, 0.-2.j]])
>>> grid = rl + im
>>> grid
array([[-2.+2.j, -1.+2.j,  0.+2.j,  1.+2.j,  2.+2.j],
       [-2.+1.j, -1.+1.j,  0.+1.j,  1.+1.j,  2.+1.j],
       [-2.+0.j, -1.+0.j,  0.+0.j,  1.+0.j,  2.+0.j],
       [-2.-1.j, -1.-1.j,  0.-1.j,  1.-1.j,  2.-1.j],
       [-2.-2.j, -1.-2.j,  0.-2.j,  1.-2.j,  2.-2.j]])

使用字母「向量」的範例

>>> x = np.array(['a', 'b', 'c'], dtype=object)
>>> np.outer(x, [1, 2, 3])
array([['a', 'aa', 'aaa'],
       ['b', 'bb', 'bbb'],
       ['c', 'cc', 'ccc']], dtype=object)