Home / NumPy / Broadcasting: Implicit outer product
Example
Denote numpy.add
by \(\stackrel{?}{+}\) and let
then
\begin{align*} A \stackrel{?}{+} \vec{v} & = A + 1 \vec{v}^{\mathsf{T}} \\ & = \begin{pmatrix} 100 & 200 \\ 300 & 400 \end{pmatrix} + \vec{1} {\begin{pmatrix} 1 \\ 2 \end{pmatrix}}^{\!\!\mathsf{T}} \\ & = \begin{pmatrix} 100 & 200 \\ 300 & 400 \end{pmatrix} + \begin{pmatrix} 1 \\ 1 \end{pmatrix} \begin{pmatrix} 1 & 2 \end{pmatrix} \\ & = \begin{pmatrix} 100 & 200 \\ 300 & 400 \end{pmatrix} + \begin{pmatrix} 1 \\ 1 \end{pmatrix} \begin{pmatrix} 1 & 2 \end{pmatrix} \\ & = \begin{pmatrix} 100 & 200 \\ 300 & 400 \end{pmatrix} + \begin{pmatrix} 1 & 2 \\ 1 & 2 \end{pmatrix} \\ & = \begin{pmatrix} 101 & 202 \\ 301 & 402 \end{pmatrix} \end{align*}and
import numpy as np
a = np.array([[100, 200], [300, 400]])
v = np.array([1, 2])
a + v
array([[101, 202], [301, 402]])