numpy.ma.outerproduct#

ma.outerproduct(a, b)[source]#

計算兩個向量的外積。

給定兩個長度分別為 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 and C. F. Van Loan, Matrix Computations, 3rd ed., Baltimore, MD, Johns Hopkins University Press, 1996, pg. 8.

範例

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

>>> 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)