Writing
Adding two matrices: theory and a Python example
The definition
Let \(A\) and \(B\) be two \(m \times n\) matrices:
$$
A =
\begin{pmatrix}
a_{11} & \cdots & a_{1n} \\
\vdots & \ddots & \vdots \\
a_{m1} & \cdots & a_{mn}
\end{pmatrix}_{m \times n},
\qquad
B =
\begin{pmatrix}
b_{11} & \cdots & b_{1n} \\
\vdots & \ddots & \vdots \\
b_{m1} & \cdots & b_{mn}
\end{pmatrix}_{m \times n}.
$$
Their sum is obtained by adding corresponding entries. In other words, \((A+B)_{ij} = a_{ij} + b_{ij}\) for every \(i = 1, \dots, m\) and \(j = 1, \dots, n\):
$$
A+B =
\begin{pmatrix}
a_{11}+b_{11} & \cdots & a_{1n}+b_{1n} \\
\vdots & \ddots & \vdots \\
a_{m1}+b_{m1} & \cdots & a_{mn}+b_{mn}
\end{pmatrix}_{m \times n}.
$$
Note that the sum is only defined when both matrices have the same size.
A Python example
With NumPy, the + operator adds two arrays entry by entry,
which is exactly the definition above.
import numpy as np
# Create two 3x3 matrices
A = np.array([[5, 7, 3],
[4, 5, 9],
[4, 2, 1]])
B = np.array([[2, 9, 8],
[4, 7, 3],
[5, 8, 6]])
# Add the matrices
result = A + B
print(result)
What the code computes
$$
A =
\begin{bmatrix}
5 & 7 & 3 \\
4 & 5 & 9 \\
4 & 2 & 1
\end{bmatrix},
\qquad
B =
\begin{bmatrix}
2 & 9 & 8 \\
4 & 7 & 3 \\
5 & 8 & 6
\end{bmatrix}
$$
$$
A+B =
\begin{bmatrix}
5+2 & 7+9 & 3+8 \\
4+4 & 5+7 & 9+3 \\
4+5 & 2+8 & 1+6
\end{bmatrix}
=
\begin{bmatrix}
7 & 16 & 11 \\
8 & 12 & 12 \\
9 & 10 & 7
\end{bmatrix}
$$