方法
ufunc.
outer
应用UFUNC op 所有带A的对(A,B) A 和B在 B .
让 M = A.ndim , N = B.ndim . 然后结果, C 的 op.outer(A, B) 是维度m+n的数组,如下所示:
M = A.ndim
N = B.ndim
op.outer(A, B)
System Message: WARNING/2 (C [i_0, ..., i_{{M-1}}, j_0, ..., j_{{N-1}}] = [i_0, ..., i_{{M-1}}] ,B [j_0, ..., j_{{N-1}}] ))
latex exited with error [stdout] This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2019/dev/Debian) (preloaded format=latex) restricted \write18 enabled. entering extended mode (./math.tex LaTeX2e <2018-12-01> (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2018/09/03 v1.4i Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo)) (/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty For additional information on amsmath, use the `?' option. (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amscls/amsthm.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amssymb.sty (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amsfonts.sty)) (/usr/share/texlive/texmf-dist/tex/latex/anyfontsize/anyfontsize.sty) (/usr/share/texlive/texmf-dist/tex/latex/tools/bm.sty) (./math.aux) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsa.fd) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsb.fd) ! Package inputenc Error: Unicode character , (U+FF0C) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ... ,B [j_0, ..., j_{{N-1}}] )\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ... ,B [j_0, ..., j_{{N-1}}] )\end{split} ! Package inputenc Error: Unicode character , (U+FF0C) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ... ,B [j_0, ..., j_{{N-1}}] )\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ... ,B [j_0, ..., j_{{N-1}}] )\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 600 bytes). Transcript written on math.log.
为了 A 和 B 一维,相当于:
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
第一阵列
第二阵列
传递给ufunc的参数。典型地 dtype 或 out .
dtype
输出数组
参见
numpy.outer
一个不太强大的版本 np.multiply.outer 那个 ravel 将所有输入设为1D。这主要是为了与旧代码兼容。
np.multiply.outer
ravel
tensordot
np.tensordot(a, b, axes=((), ())) 和 np.multiply.outer(a, b) 对于a和b的所有尺寸,表现相同。
np.tensordot(a, b, axes=((), ()))
np.multiply.outer(a, b)
实例
>>> np.multiply.outer([1, 2, 3], [4, 5, 6]) array([[ 4, 5, 6], [ 8, 10, 12], [12, 15, 18]])
多维示例:
>>> 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]]]])