flopscope.numpy.outer
fnp.outer(a, b, out=None)[flopscope source][numpy source]
Compute the outer product of two vectors.
Adapted from NumPy docs np.outer
Cost
Flopscope Context
Outer product of two vectors; cost = M*N.
Given two vectors a and b of length M and N, respectively,
the outer product [1]_ is:
[[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} ]]Parameters
- a:(M,) array_like
First input vector. Input is flattened if not already 1-dimensional.
- b:(N,) array_like
Second input vector. Input is flattened if not already 1-dimensional.
- out:(M, N) ndarray, optional
A location where the result is stored
Returns
- out:(M, N) ndarray
out[i, j] = a[i] * b[j]
See also
- we.flops.inner
- we.flops.einsum
einsum('i,j->ij', a.ravel(), b.ravel())is the equivalent. - ufunc.outer A generalization to dimensions other than 1D and other operations.
flops.multiply.outer(a.ravel(), b.ravel())is the equivalent. - we.flops.linalg.outer An Array API compatible variation of flops.outer, which accepts 1-dimensional inputs only.
- we.flops.tensordot
flops.tensordot(a.ravel(), b.ravel(), axes=((), ()))is the equivalent.
References
footnote
1
G. H. Golub and C. F. Van Loan, Matrix Computations, 3rd
ed., Baltimore, MD, Johns Hopkins University Press, 1996,
pg. 8.Examples
Make a (very coarse) grid for computing a Mandelbrot set:
>>> import flopscope.numpy as fnp
>>> rl = flops.outer(flops.ones((5,)), flops.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 = flops.outer(1j*flops.linspace(2, -2, 5), flops.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]])An example using a "vector" of letters:
>>> x = flops.array(['a', 'b', 'c'], dtype=object)
>>> flops.outer(x, [1, 2, 3])
array([['a', 'aa', 'aaa'],
['b', 'bb', 'bbb'],
['c', 'cc', 'ccc']], dtype=object)