On this page
numpy.ufunc.outer
method
- ufunc.outer(A, B, /, **kwargs)
-
Apply the ufunc
opto all pairs (a, b) with a inAand b inB.Let
M = A.ndim,N = B.ndim. Then the result,C, ofop.outer(A, B)is an array of dimension M + N such that:\[C[i_0, ..., i_{M-1}, j_0, ..., j_{N-1}] = op(A[i_0, ..., i_{M-1}], B[j_0, ..., j_{N-1}])\]For
AandBone-dimensional, this is equivalent to:r = empty(len(A),len(B)) for i in range(len(A)): for j in range(len(B)): r[i,j] = op(A[i], B[j]) # op = ufunc in question- Parameters
- Returns
-
- rndarray
-
Output array
See also
-
numpy.outer -
A less powerful version of
np.multiply.outerthatravels all inputs to 1D. This exists primarily for compatibility with old code. -
tensordot -
np.tensordot(a, b, axes=((), ()))andnp.multiply.outer(a, b)behave same for all dimensions of a and b.
Examples
>>> np.multiply.outer([1, 2, 3], [4, 5, 6]) array([[ 4, 5, 6], [ 8, 10, 12], [12, 15, 18]])A multi-dimensional example:
>>> A = np.array([[1, 2, 3], [4, 5, 6]]) >>> A.shape (2, 3) >>> B = np.array([[1, 2, 3, 4]]) >>> B.shape (1, 4) >>> C = np.multiply.outer(A, B) >>> C.shape; C (2, 3, 1, 4) array([[[[ 1, 2, 3, 4]], [[ 2, 4, 6, 8]], [[ 3, 6, 9, 12]]], [[[ 4, 8, 12, 16]], [[ 5, 10, 15, 20]], [[ 6, 12, 18, 24]]]])
© 2005–2022 NumPy Developers
Licensed under the 3-clause BSD License.
https://numpy.org/doc/1.23/reference/generated/numpy.ufunc.outer.html