---
title: 'Binary Magic Squares: Theory & Constructions'
url: https://www.emergentmind.com/topics/binary-magic-squares-bms
type: topic
---

# Binary Magic Squares: Theory & Constructions

Searching arXiv for recent work on Binary Magic Squares and related constrained/binary magic-square literature.
Binary Magic Squares (BMS) are binary matrices \(M \in \{0,1\}^{n\times n}\) for which there exists an integer \(k\) such that every row sum and every column sum equals \(k\); the same framework extends to rectangular matrices \(M \in \{0,1\}^{m\times n}\) with constant row sum \(a\) and constant column sum \(b\) [2511.00547]. In this formulation, a BMS is exactly the incidence or adjacency matrix of a regular bipartite graph with constant degree on each side. A distinct, secondary usage also exists in the magic-square literature, where “binary magic square” denotes the \(0/1\) parity pattern obtained from an ordinary magic square by mapping odd entries to \(1\) and even entries to \(0\) [2307.09996]. The modern algorithmic literature on BMS is centered on the first meaning: equal-margin binary matrices with provable existence criteria and efficient constructive generation.

## 1. Definitions and combinatorial interpretation

In the square case, a Binary Magic Square is a matrix
\[
M=(m_{ij})_{i,j}\in\{0,1\}^{n\times n}
\]
such that there exists \(k\in\{0,\dots,n\}\) with
\[
\forall i\in\{0,\dots,n-1\},\quad \sum_{j=0}^{n-1} m_{ij}=k,
\]
\[
\forall j\in\{0,\dots,n-1\},\quad \sum_{i=0}^{n-1} m_{ij}=k.
\]
Thus each row and each column contains exactly \(k\) ones. The same paper also treats the generated objects as random binary matrices of this type when the construction uses randomized subset selection [2511.00547].

The rectangular generalization replaces the single degree parameter \(k\) by a row target \(a\) and a column target \(b\). A matrix
\[
M=(m_{ij})_{i,j}\in\{0,1\}^{m\times n}
\]
is a non-square BMS if there exist integers \(a\in\{0,\dots,n\}\) and \(b\in\{0,\dots,m\}\) such that every row sum equals \(a\) and every column sum equals \(b\). This is the constant-margin case of a binary matrix with prescribed row and column sums.

The graph-theoretic interpretation is exact. In the square case, the matrix is the incidence matrix of a \(k\)-regular bipartite graph on two parts of size \(n\); in the rectangular case, it is the adjacency matrix of a bipartite graph with left part of size \(m\), right part of size \(n\), all left-degrees equal to \(a\), and all right-degrees equal to \(b\) [2511.00547]. This places BMS at the intersection of regular graph realization and constrained binary matrix generation.

## 2. Existence theory for square and rectangular BMS

For square matrices, existence is unconditional once \(n\) and \(k\) satisfy \(k\in\{0,\dots,n\}\). The constructive theorem states that for all \(n\in\mathbb{N}\) and all \(k\in\{0,\dots,n\}\), there exists a valid \(n\times n\) BMS with row and column sums equal to \(k\) [2511.00547]. The proof uses a wrapped-band matrix:
\[
m_{ij} =
\begin{cases}
1 & \text{if } i \leq j < i+k \quad\text{or}\quad i \leq j+n < i+k,\\
0 & \text{otherwise},
\end{cases}
\]
with indices taken in \(\{0,\dots,n-1\}\). Intuitively, each row contains a block of \(k\) consecutive ones, and the block may wrap around the matrix boundary. Direct row- and column-sum calculations show that every row and every column sums to \(k\). The construction is presented as a special case of the Gale–Ryser theorem [2511.00547].

The rectangular case is more restrictive. If \(M\in\{0,1\}^{m\times n}\) has every row sum equal to \(a\) and every column sum equal to \(b\), then counting the total number of ones by rows and by columns yields
\[
am = bn.
\]
The characterization theorem states that such a matrix exists if and only if
\[
\frac{b}{a} = \frac{m}{n},
\]
equivalently, if there exist integers \(q,q',m',n'\) such that
\[
m=qm',\quad n=qn',\quad b=q'm',\quad a=q'n'.
\]
Here \(q=\gcd(m,n)\) and \(q'\in\{0,\dots,q\}\) [2511.00547].

The sufficiency proof is constructive. One first builds a \(q\times q\) square BMS with row and column sum \(q'\), then tiles that pattern \(m'\) times vertically and \(n'\) times horizontally. Each row of the resulting \(m\times n\) matrix consists of \(n'\) copies of a row of the \(q\times q\) pattern, and each column consists of \(m'\) copies of a column, so the row sums become \(n' q' = a\) and the column sums become \(m' q' = b\) [2511.00547].

A notable corollary concerns coprime dimensions. If \(\gcd(m,n)=1\), then the only non-square BMS are the trivial all-zero and all-one matrices. This sharply separates the square case, where every \(k\in\{0,\dots,n\}\) is feasible, from the rectangular case, where degree compatibility is the central constraint [2511.00547].

## 3. Constructive generation and inductive correctness

The principal generation procedure is column-by-column. For the square case, the input is \(n\in\mathbb{N}\) and \(k\in\{0,\dots,n\}\); the output is \(M\in\{0,1\}^{n\times n}\) with all row and column sums equal to \(k\) [2511.00547]. The algorithm maintains partial row sums
\[
s_i(t)=\sum_{j=0}^{t} m_{ij},
\]
together with the invariant
\[
k+t-n \leq s_i(t) \leq k.
\]
The upper bound prevents any row from exceeding its final target, while the lower bound guarantees that enough capacity remains in later columns for the row to reach exactly \(k\).

At each column \(t\), rows are partitioned into three sets:
\[
A_1(t)=\{i\mid s_i(t)=k+t-n\},
\]
\[
A_2(t)=\{i\mid k+t-n < s_i(t) < k\},
\]
\[
A_3(t)=\{i\mid s_i(t)=k\}.
\]
Rows in \(A_1\) are at the critical lower bound and must be selected in the current column. Rows in \(A_3\) are already saturated and must not be selected. The algorithm therefore sets
\[
E := A_1 \cup \texttt{random\_subset}(A_2,\,k-|A_1|),
\]
then writes ones in column \(t\) precisely on the rows in \(E\). The only randomness lies in the choice of the subset of \(A_2\) [2511.00547].

Correctness is proved by induction. One lemma shows that the invariant
\[
k+t-n \leq s_i(t) \leq k
\]
holds for all rows and all steps; at \(t=n\), the lower and upper bounds coincide, so every row sum is exactly \(k\). A second lemma shows that \(|E(t)|=k\) at every column, hence each column contains exactly \(k\) ones. Together these imply that any matrix returned by the algorithm is a valid BMS [2511.00547].

The non-square variant keeps the same structure but replaces the square target \(k\) by row target \(a\) and column target \(b\). The row invariant becomes
\[
a+t-n \leq s_i(t) \leq a,
\]
and the selection rule becomes
\[
E := A_1 \cup \texttt{random\_subset}(A_2,\,b-|A_1|).
\]
The proof of column correctness mirrors the square argument but additionally uses the global identity \(am=bn\). The result is a slight variant of the original algorithm that provably generates all feasible rectangular constant-margin instances [2511.00547].

## 4. Complexity, optimality, and implementations

The asymptotic work of the square generator is \(O(n^2)\). There are \(n\) columns, and at each column the algorithm scans the \(n\) rows to determine the sets \(A_1,A_2,A_3\), then writes at most \(k\leq n\) ones and updates the corresponding row sums. This gives
\[
C(n)=\mathcal{O}(n^2).
\]
The paper calls this optimal theoretical complexity because explicitly writing or storing all entries of an \(n\times n\) matrix already requires \(\Theta(n^2)\) operations or memory accesses [2511.00547].

A parallel model is also discussed. If \(p\) rows can be processed simultaneously, then one column costs \(\mathcal{O}(\lceil n/p\rceil)\) parallel time, so the total becomes
\[
C(n)=\mathcal{O}\!\left(n\left\lceil\frac{n}{p}\right\rceil\right).
\]
This yields wall-clock behavior of order \(n^2/p\), up to overheads [2511.00547].

Two Python implementations are publicly released as pip-installable packages hosted on GitHub: a standard CPU version and a PyTorch version that can use GPU acceleration. The implementation strategy is a direct translation of the algorithmic specification: represent \(M\) as a NumPy or PyTorch tensor, maintain a vector of partial row sums, compute masks for \(A_1,A_2,A_3\), apply `random_subset`, and update the selected entries [2511.00547].

The GPU-oriented implementation exploits parallelism in two ways. First, operations within a single BMS are vectorized as elementwise or boolean-tensor operations. Second, several BMS can be generated in parallel by batching tensors of shape \((B,n,n)\) together with partial-sum arrays of shape \((B,n)\). The paper does not include detailed timing tables or graphs in the LaTeX text, but it notes that many BMS can be generated in parallel and that this enables significant speedups compared to a naive CPU implementation [2511.00547].

## 5. Alternative meanings of “binary magic square” and symmetry-based encodings

A separate literature uses “binary magic square” in a different sense: not as a matrix with equal binary margins, but as the parity pattern of an ordinary magic square. In that definition, if \(M=(m_{ij})\) is a classical magic square, then the associated binary matrix \(B=(b_{ij})\) is given by
\[
b_{ij} =
\begin{cases}
1 & \text{if } m_{ij}\text{ is odd},\\
0 & \text{if } m_{ij}\text{ is even}.
\end{cases}
\]
The analysis is then performed up to the eight symmetries of the square, namely the dihedral group \(D_4\) of rotations and reflections [2307.09996].

Using that parity-based meaning, exhaustive datasets exhibit strong compression under symmetry. Among all 880 magic squares of order \(4\), only \(8\) unique binary patterns are needed; among all \(48{,}544\) associative magic squares of order \(5\), only \(15\) unique patterns occur; and among all \(368{,}640\) Franklin magic squares of order \(8\), only \(6\) unique patterns occur when patterns are identified up to rotation and reflection [2307.09996]. The corresponding work also studies the same binary encodings geometrically by flattening them into vectors in \(\mathbb{R}^{n^2}\), then applying PCA and LDA.

The PCA analysis identifies \(24\) distinct patterns for the \(4\times 4\) family, \(44\) for the \(5\times 5\) associative family, and \(32\) for the Franklin \(8\times 8\) family; the two-dimensional PCA plots show \(9\), \(6\), and \(9\) regions, respectively [2307.09996]. LDA, using the dihedral-equivalence classes as labels, yields “7 pencil-like fingerprints” for the \(4\times 4\) case, and for the \(5\times 5\) associative case the histograms of \(LD1\) and \(LD2\) are reported to exhibit an approximate normal distribution [2307.09996].

This terminology is materially different from the equal-margin definition. In the parity-coded usage, the binary matrix is a shadow of an ordinary magic square and need not have constant row and column sums. In the equal-margin usage, the binary matrix itself is the primary combinatorial object. The coexistence of these meanings is a source of terminological ambiguity in the literature.

## 6. Constrained-digit relatives, limitations, and open directions

A broader adjacent literature studies magic squares built from restricted digit alphabets rather than from binary matrices with fixed margins. One example constructs upside-down, bimagic, and palindromic squares using only the digits \(0\), \(1\), and \(2\) in digital form, motivated by the palindromic date \(11.02.2011\) [1102.2394]. In that setting, a magic square retains the classical requirement that all row sums, column sums, and the two main diagonals have the same value, and additional properties such as bimagicity, pan-diagonality, or \(180^\circ\) rotational consistency may also be imposed.

The paper gives \(3\times 3\), \(4\times 4\), and \(5\times 5\) examples with magic sums
\[
S1_{3\times3}=3333,\quad S1_{4\times4}=4444,\quad S1_{5\times5}=5555,
\]
satisfying
\[
3333^2+4444^2=5555^2,
\]
and also describes \(9\times 9\), \(16\times 16\), and \(25\times 25\) bimagic constructions with block structure and restricted-digit palindromic entries [1102.2394]. This is not strict BMS theory in the equal-margin sense, but it is relevant as constrained-digit magic-square research. The same source explicitly notes that the digit set \(\{0,1,2\}\) is a superset of the binary set \(\{0,1\}\), and that many ideas—rotational symmetry, palindromic concatenation, and block structures—are combinatorial patterns that can be adapted by restricting the alphabet [1102.2394].

Within the equal-margin BMS line, several limitations are explicit. The generation algorithm proves correctness and efficiency for producing some BMS with given parameters, but it does not claim uniform sampling over all BMS for fixed \((n,k)\) or \((m,n,a,b)\), and it does not address enumeration or counting [2511.00547]. Potential future directions mentioned implicitly or naturally suggested include studying the distribution induced by `random_subset`, integrating the generator into MCMC schemes, and exploring additional structural constraints beyond constant margins [2511.00547].

In the parity-based line of work, the open problems are different. The unresolved questions include the general enumeration of binary parity patterns for higher orders, characterization of which binary patterns are realizable for specific structured families such as Franklin squares, analytic explanations of the observed PCA and LDA geometry, and the effect of binary encodings other than parity [2307.09996]. Taken together, these directions show that “Binary Magic Squares” is now best understood as a bifurcated topic: one branch studies constant-margin binary matrices as regular bipartite graph realizations, and another studies binary encodings of classical magic-square families under symmetry.

Source: https://www.emergentmind.com/topics/binary-magic-squares-bms