numpy.ma.outerproduct#
- ma.outerproduct(a, b)[源代码]#
计算两个向量的外积。
给定长度分别为 M 和 N 的两个向量 a 和 b,外积 [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, optional
存储结果的位置
- 返回:
- out(M, N) ndarray
out[i, j] = a[i] * b[j]
另请参阅
内积einsumeinsum('i,j->ij', a.ravel(), b.ravel())是等价的。ufunc.outer推广到非一维和执行其他操作。
np.multiply.outer(a.ravel(), b.ravel())是等价的。linalg.outernp.outer的一个符合 Array API 的变体,它只接受一维输入。tensordotnp.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.
示例
为计算曼德博集做一个(非常粗糙的)网格
>>> 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)