dask.array.outer
dask.array.outer¶
- dask.array.outer(a, b)[源代码]¶
计算两个向量的外积。
此文档字符串是从 numpy.outer 复制的。
Dask 版本可能存在一些不一致性。
给定两个长度分别为
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,) 数组类
第一个输入向量。如果输入不是一维的,则会被展平。
- b(N,) 数组类
第二个输入向量。如果尚未为一维,则输入将被展平。
- 出(M, N) ndarray, 可选
存储结果的位置
1.9.0 新版功能.
- 返回
- 出(M, N) ndarray
out[i, j] = a[i] * b[j]
参见
参考文献
- 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)