Compute the outer product of two vectors. Given two vectors `a` and `b` of length ``M`` and ``N``, repsectively, 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} ]] P
(a, b, out=None)
| 840 | |
| 841 | @array_function_dispatch(_outer_dispatcher) |
| 842 | def outer(a, b, out=None): |
| 843 | """ |
| 844 | Compute the outer product of two vectors. |
| 845 | |
| 846 | Given two vectors `a` and `b` of length ``M`` and ``N``, repsectively, |
| 847 | the outer product [1]_ is:: |
| 848 | |
| 849 | [[a_0*b_0 a_0*b_1 ... a_0*b_{N-1} ] |
| 850 | [a_1*b_0 . |
| 851 | [ ... . |
| 852 | [a_{M-1}*b_0 a_{M-1}*b_{N-1} ]] |
| 853 | |
| 854 | Parameters |
| 855 | ---------- |
| 856 | a : (M,) array_like |
| 857 | First input vector. Input is flattened if |
| 858 | not already 1-dimensional. |
| 859 | b : (N,) array_like |
| 860 | Second input vector. Input is flattened if |
| 861 | not already 1-dimensional. |
| 862 | out : (M, N) ndarray, optional |
| 863 | A location where the result is stored |
| 864 | |
| 865 | .. versionadded:: 1.9.0 |
| 866 | |
| 867 | Returns |
| 868 | ------- |
| 869 | out : (M, N) ndarray |
| 870 | ``out[i, j] = a[i] * b[j]`` |
| 871 | |
| 872 | See also |
| 873 | -------- |
| 874 | inner |
| 875 | einsum : ``einsum('i,j->ij', a.ravel(), b.ravel())`` is the equivalent. |
| 876 | ufunc.outer : A generalization to dimensions other than 1D and other |
| 877 | operations. ``np.multiply.outer(a.ravel(), b.ravel())`` |
| 878 | is the equivalent. |
| 879 | tensordot : ``np.tensordot(a.ravel(), b.ravel(), axes=((), ()))`` |
| 880 | is the equivalent. |
| 881 | |
| 882 | References |
| 883 | ---------- |
| 884 | .. [1] G. H. Golub and C. F. Van Loan, *Matrix Computations*, 3rd |
| 885 | ed., Baltimore, MD, Johns Hopkins University Press, 1996, |
| 886 | pg. 8. |
| 887 | |
| 888 | Examples |
| 889 | -------- |
| 890 | Make a (*very* coarse) grid for computing a Mandelbrot set: |
| 891 | |
| 892 | >>> rl = np.outer(np.ones((5,)), np.linspace(-2, 2, 5)) |
| 893 | >>> rl |
| 894 | array([[-2., -1., 0., 1., 2.], |
| 895 | [-2., -1., 0., 1., 2.], |
| 896 | [-2., -1., 0., 1., 2.], |
| 897 | [-2., -1., 0., 1., 2.], |
| 898 | [-2., -1., 0., 1., 2.]]) |
| 899 | >>> im = np.outer(1j*np.linspace(2, -2, 5), np.ones((5,))) |