Exchange of Order of Summations over Finite Sets/Subset of Cartesian Product

Theorem
Let $\mathbb A$ be one of the standard number systems $\N, \Z, \Q, \R, \C$.

Let $S, T$ be finite sets.

Let $S \times T$ be their cartesian product. Let $D\subset S\times T$ be a subset.

Let $\pi_1 : D \to S$ and $\pi_2 : D \to T$ be the restrictions of the projections of $S\times T$.

Then we have an equality of summations over finite sets:
 * $\displaystyle \sum_{s \mathop \in S} \sum_{t \mathop \in \pi_2(\pi_1^{-1}(s))} f(s, t) = \sum_{t \mathop \in T} \sum_{s \mathop \in \pi_1(\pi_2^{-1}(t)) } f(s, t)$

where $\pi_1^{-1}(s)$ denotes the inverse image of $s$ under $\pi_1$.

Outline of Proof
We extend $f$ by $0$ outside $D$ and apply Exchange of Order of Summation over Cartesian Product of Finite Sets.

That extending $f$ by $0$ does not change the summation follows from Summation over Finite Set of Zero and Sum over Disjoint Union of Finite Sets.

Proof
Define an extension $\overline f$ of $f$ to $S \times T$ by:
 * $\overline f(s,t) = \begin{cases}

f(s,t) & : (s,t) \in D \\ 0     & : (s,t) \notin D \end{cases}$

Then for all $s \in S$, by:
 * Preimage of Disjoint Union is Disjoin Union
 * Sum over Disjoint Union of Finite Sets
 * Summation over Finite Set of Zero
 * $\displaystyle \sum_{t \mathop \in \pi_2(\pi_1^{-1}(s))} f(s, t) = \sum_{t \mathop \in T} \overline f(s, t)$

Thus $\displaystyle \sum_{s \mathop \in S} \sum_{t \mathop \in \pi_2(\pi_1^{-1}(s))} f(s, t) = \sum_{s \mathop \in S} \sum_{t \mathop \in T} \overline f(s, t)$

Similarly, $\displaystyle \sum_{t \mathop \in T} \sum_{s \mathop \in \pi_1(\pi_2^{-1}(t))} f(s, t) = \sum_{t \mathop \in T} \sum_{s \mathop \in S} \overline f(s, t)$

By Exchange of Order of Summation over Cartesian Product of Finite Sets, the result follows.