Binary Magic Squares: Theory & Constructions
- Binary Magic Squares are binary matrices with each row and column summing to a constant, serving as incidence matrices for regular bipartite graphs.
- Constructive generation methods use wrapped-band matrices and column-by-column algorithms that guarantee optimal O(n²) performance.
- Alternate definitions include parity patterns from classical magic squares, highlighting distinct combinatorial and symmetry-based interpretations.
Searching arXiv for recent work on Binary Magic Squares and related constrained/binary magic-square literature. Binary Magic Squares (BMS) are binary matrices for which there exists an integer such that every row sum and every column sum equals ; the same framework extends to rectangular matrices with constant row sum and constant column sum (Riou, 1 Nov 2025). 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$ (Fahimi, 2023). 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
such that there exists 0 with
1
2
Thus each row and each column contains exactly 3 ones. The same paper also treats the generated objects as random binary matrices of this type when the construction uses randomized subset selection (Riou, 1 Nov 2025).
The rectangular generalization replaces the single degree parameter 4 by a row target 5 and a column target 6. A matrix
7
is a non-square BMS if there exist integers 8 and 9 such that every row sum equals 0 and every column sum equals 1. 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 2-regular bipartite graph on two parts of size 3; in the rectangular case, it is the adjacency matrix of a bipartite graph with left part of size 4, right part of size 5, all left-degrees equal to 6, and all right-degrees equal to 7 (Riou, 1 Nov 2025). 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 8 and 9 satisfy 0. The constructive theorem states that for all 1 and all 2, there exists a valid 3 BMS with row and column sums equal to 4 (Riou, 1 Nov 2025). The proof uses a wrapped-band matrix: 5 with indices taken in 6. Intuitively, each row contains a block of 7 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 8. The construction is presented as a special case of the Gale–Ryser theorem (Riou, 1 Nov 2025).
The rectangular case is more restrictive. If 9 has every row sum equal to 0 and every column sum equal to 1, then counting the total number of ones by rows and by columns yields
2
The characterization theorem states that such a matrix exists if and only if
3
equivalently, if there exist integers 4 such that
5
Here 6 and 7 (Riou, 1 Nov 2025).
The sufficiency proof is constructive. One first builds a 8 square BMS with row and column sum 9, then tiles that pattern 0 times vertically and 1 times horizontally. Each row of the resulting 2 matrix consists of 3 copies of a row of the 4 pattern, and each column consists of 5 copies of a column, so the row sums become 6 and the column sums become 7 (Riou, 1 Nov 2025).
A notable corollary concerns coprime dimensions. If 8, then the only non-square BMS are the trivial all-zero and all-one matrices. This sharply separates the square case, where every 9 is feasible, from the rectangular case, where degree compatibility is the central constraint (Riou, 1 Nov 2025).
3. Constructive generation and inductive correctness
The principal generation procedure is column-by-column. For the square case, the input is $0/1$0 and $0/1$1; the output is $0/1$2 with all row and column sums equal to $0/1$3 (Riou, 1 Nov 2025). The algorithm maintains partial row sums
$0/1$4
together with the invariant
$0/1$5
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 $0/1$6.
At each column $0/1$7, rows are partitioned into three sets: $0/1$8
$0/1$9
$1$0
Rows in $1$1 are at the critical lower bound and must be selected in the current column. Rows in $1$2 are already saturated and must not be selected. The algorithm therefore sets
$1$3
then writes ones in column $1$4 precisely on the rows in $1$5. The only randomness lies in the choice of the subset of $1$6 (Riou, 1 Nov 2025).
Correctness is proved by induction. One lemma shows that the invariant
$1$7
holds for all rows and all steps; at $1$8, the lower and upper bounds coincide, so every row sum is exactly $1$9. A second lemma shows that $0$0 at every column, hence each column contains exactly $0$1 ones. Together these imply that any matrix returned by the algorithm is a valid BMS (Riou, 1 Nov 2025).
The non-square variant keeps the same structure but replaces the square target $0$2 by row target $0$3 and column target $0$4. The row invariant becomes
$0$5
and the selection rule becomes
$0$6
The proof of column correctness mirrors the square argument but additionally uses the global identity $0$7. The result is a slight variant of the original algorithm that provably generates all feasible rectangular constant-margin instances (Riou, 1 Nov 2025).
4. Complexity, optimality, and implementations
The asymptotic work of the square generator is $0$8. There are $0$9 columns, and at each column the algorithm scans the 0 rows to determine the sets 1, then writes at most 2 ones and updates the corresponding row sums. This gives
3
The paper calls this optimal theoretical complexity because explicitly writing or storing all entries of an 4 matrix already requires 5 operations or memory accesses (Riou, 1 Nov 2025).
A parallel model is also discussed. If 6 rows can be processed simultaneously, then one column costs 7 parallel time, so the total becomes
8
This yields wall-clock behavior of order 9, up to overheads (Riou, 1 Nov 2025).
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 00 as a NumPy or PyTorch tensor, maintain a vector of partial row sums, compute masks for 01, apply random_subset, and update the selected entries (Riou, 1 Nov 2025).
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 02 together with partial-sum arrays of shape 03. 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 (Riou, 1 Nov 2025).
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 04 is a classical magic square, then the associated binary matrix 05 is given by
06
The analysis is then performed up to the eight symmetries of the square, namely the dihedral group 07 of rotations and reflections (Fahimi, 2023).
Using that parity-based meaning, exhaustive datasets exhibit strong compression under symmetry. Among all 880 magic squares of order 08, only 09 unique binary patterns are needed; among all 10 associative magic squares of order 11, only 12 unique patterns occur; and among all 13 Franklin magic squares of order 14, only 15 unique patterns occur when patterns are identified up to rotation and reflection (Fahimi, 2023). The corresponding work also studies the same binary encodings geometrically by flattening them into vectors in 16, then applying PCA and LDA.
The PCA analysis identifies 17 distinct patterns for the 18 family, 19 for the 20 associative family, and 21 for the Franklin 22 family; the two-dimensional PCA plots show 23, 24, and 25 regions, respectively (Fahimi, 2023). LDA, using the dihedral-equivalence classes as labels, yields “7 pencil-like fingerprints” for the 26 case, and for the 27 associative case the histograms of 28 and 29 are reported to exhibit an approximate normal distribution (Fahimi, 2023).
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 30, 31, and 32 in digital form, motivated by the palindromic date 33 (Taneja, 2011). 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 34 rotational consistency may also be imposed.
The paper gives 35, 36, and 37 examples with magic sums
38
satisfying
39
and also describes 40, 41, and 42 bimagic constructions with block structure and restricted-digit palindromic entries (Taneja, 2011). 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 43 is a superset of the binary set 44, and that many ideas—rotational symmetry, palindromic concatenation, and block structures—are combinatorial patterns that can be adapted by restricting the alphabet (Taneja, 2011).
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 45 or 46, and it does not address enumeration or counting (Riou, 1 Nov 2025). 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 (Riou, 1 Nov 2025).
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 (Fahimi, 2023). 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.