dask.array.outer

dask.array.outer

dask.array.outer(a, b)[源代码]

计算两个向量的外积。

此文档字符串是从 numpy.outer 复制的。

Dask 版本可能存在一些不一致性。

给定两个长度分别为 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,) 数组类

第一个输入向量。如果输入不是一维的,则会被展平。

b(N,) 数组类

第二个输入向量。如果尚未为一维,则输入将被展平。

(M, N) ndarray, 可选

存储结果的位置

1.9.0 新版功能.

返回
(M, N) ndarray

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

参见

inner
einsum

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

ufunc.outer

推广到除了一维以外的其他维度和操作。np.multiply.outer(a.ravel(), b.ravel()) 是等效的。

linalg.outer

一个与 Array API 兼容的 np.outer 变体,仅接受一维输入。

tensordot

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

参考文献

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)