5D Library › Dyadic Phase Coupling, Bilinear Compatibility Tensors and Spatiotemporal Grid Games · 中文
Chapter 5. The Luoshu, the Eight Doors and Permutation Groups
The nine palaces are a finite set
Methods of choosing times and directions such as Qimen Dunjia and the flying stars of the nine palaces all work on the same three-by-three grid: nine palaces, each with a Luoshu number, and the eight doors laid around the outer ring, turning or “flying” with time. Stripped of their names, all these operations are one kind of mathematical object: permutations of nine positions. This chapter writes them as permutations and asks what group they generate, and what they preserve and destroy.
The Luoshu is unique up to symmetry
The Luoshu is a magic square of order three: the numbers 1 to 9 in three rows and three columns, each row, column and both diagonals summing to 15.
Calculation
The centre must be 5. The four lines through the centre (middle row, middle column, both diagonals) together use each of the eight numbers other than the centre exactly once, and the centre four times. The four lines sum to , which also equals for a centre , so .
The tenth program checks all arrangements and carries out Yang Hui’s construction.
Calculation
# Chapter 5. The Luoshu square: enumeration and Yang Hui's rule.
# A 3 x 3 normal magic square holds 1..9 with every row, column and both
# diagonals summing to 15. The program (1) checks all 9! = 362,880
# arrangements, (2) shows that the solutions are the 8 images of one square
# under the symmetries of the square (4 rotations, 4 reflections: the
# dihedral group D4), (3) counts the "semi-magic" arrangements (rows and
# columns only), and (4) carries out Yang Hui's rule of 1275 on a diamond:
# "nine numbers in slanting rows; exchange top and bottom; exchange left and
# right; the four corners step out".
from itertools import permutations
LINES = [(0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8),
(0, 4, 8), (2, 4, 6)]
def magic(sq, lines=LINES):
return all(sq[a] + sq[b] + sq[c] == 15 for a, b, c in lines)
def rot(sq):
return tuple(sq[i] for i in (6, 3, 0, 7, 4, 1, 8, 5, 2))
def flip(sq):
return tuple(sq[i] for i in (2, 1, 0, 5, 4, 3, 8, 7, 6))
def d4(sq):
out, s = set(), sq
for _ in range(4):
out.add(s)
out.add(flip(s))
s = rot(s)
return out
def show(sq):
return " / ".join(" ".join(str(v) for v in sq[r * 3:r * 3 + 3])
for r in range(3))
if __name__ == "__main__":
sols = [p for p in permutations(range(1, 10)) if magic(p)]
semi = sum(magic(p, LINES[:6]) for p in permutations(range(1, 10)))
print(" magic squares among 362,880 arrangements: {}".format(len(sols)))
print(" centre value in every one: {}".format(
sorted({s[4] for s in sols})))
print(" all 8 are D4 images of one square: {}".format(
set(sols) == d4(sols[0])))
print(" semi-magic (rows and columns only): {}".format(semi))
# Yang Hui on a 5 x 5 board, (row, col) -> number
board = {(0, 2): 1, (1, 1): 4, (1, 3): 2, (2, 0): 7, (2, 2): 5,
(2, 4): 3, (3, 1): 8, (3, 3): 6, (4, 2): 9}
board[(0, 2)], board[(4, 2)] = board[(4, 2)], board[(0, 2)]
board[(2, 0)], board[(2, 4)] = board[(2, 4)], board[(2, 0)]
moved = {(1, 2): board.pop((0, 2)), (3, 2): board.pop((4, 2)),
(2, 1): board.pop((2, 0)), (2, 3): board.pop((2, 4))}
board.update(moved)
yang = tuple(board[(r, c)] for r in (1, 2, 3) for c in (1, 2, 3))
print(" Yang Hui's rule gives {} magic: {}".format(
show(yang), magic(yang)))
print(" (nine on top, one below, three left, seven right)")
print("\nReading: up to the eight symmetries of the square there is")
print("exactly one 3 x 3 magic square, and its centre is forced to be 5")
print("(the four lines through the centre use every number once plus the")
print("centre three more times: 4 x 15 = 45 + 3c). Yang Hui's four moves")
print("produce it directly.")
# magic squares among 362,880 arrangements: 8
# centre value in every one: [5]
# all 8 are D4 images of one square: True
# semi-magic (rows and columns only): 72
# Yang Hui's rule gives 4 9 2 / 3 5 7 / 8 1 6 magic: True
# (nine on top, one below, three left, seven right)
#
# Reading: up to the eight symmetries of the square there is
# exactly one 3 x 3 magic square, and its centre is forced to be 5
# (the four lines through the centre use every number once plus the
# centre three more times: 4 x 15 = 45 + 3c). Yang Hui's four moves
# produce it directly.There are only 8 magic squares of order three, all with 5 at the centre, and all 8 are images of one another under the symmetry group of the square (four rotations and four reflections, written ). In other words, up to symmetry there is exactly one magic square of order three: the Luoshu. “Semi-magic” squares, which need only the rows and columns (not the diagonals), number 72.
Source text
Yang Hui’s construction in the Xugu zhaiqi suanfa (1275). The usual quotation is:
九子斜排,上下對易,左右相更,四維挺出。 “Nine numbers in slanting rows; exchange top and bottom; exchange left and right; the four corners step out.”
with the result described as “wear nine, tread on one; three on the left, seven on the right; two and four the shoulders, six and eight the feet” (Cammann 1960; Swetz 2008). The program carries it out as: set 1 to 9 slantwise in a diamond, exchange the top and bottom ends (1 and 9), exchange the left and right ends (7 and 3), then let the four corner numbers fill the four corners of the square — giving 4 9 2 / 3 5 7 / 8 1 6, the Luoshu.
Note
Checking the text. The Xugu zhaiqi suanfa dates from 1275 and gives construction rules for magic squares; the sixteen-character rule above has been checked against the Yang Hui suanfa in the Yijiatang congshu.
Two motions: flying and turning
Two traditional motions act on the nine palaces. Flying: the content of palace moves to palace (9 back to 1), along a path through the centre in the order of the Luoshu numbers — forwards in the yang half of the year, backwards in the yin half. Turning: the eight outer palaces, in the ring order Kan, Gen, Zhen, Xun, Li, Kun, Dui, Qian (Luoshu numbers 1, 8, 3, 4, 9, 2, 7, 6), each pass their content to the next palace once per time slot while the centre stays put; this is how the eight doors — rest, life, harm, block, view, death, fright, open — turn.
The eleventh program writes the two motions as permutations (flying) and (turning) of the nine positions, finds their orders and parities and which powers keep the square magic, and computes by breadth-first search the group the two generate.
Calculation
# Chapter 5. Two motions on the nine palaces as permutations.
# Cells 0..8 are read row by row with south on top, and the Luoshu numbers
# sit on them as 4 9 2 / 3 5 7 / 8 1 6. Two traditional motions:
# f "flying palaces": whatever stands on palace n moves to palace n + 1
# (9 back to 1) - a 9-cycle through the centre, in Luoshu order;
# r "turning the ring": the eight outer palaces in the ring order
# 1 8 3 4 9 2 7 6 (Kan, Gen, Zhen, Xun, Li, Kun, Dui, Qian) each pass
# their content one step on; the centre stays.
# The program reports each motion's order and parity, which powers keep the
# square magic, whether the motions commute, and the size of the group the
# two generate (breadth-first closure over all products).
L = (4, 9, 2, 3, 5, 7, 8, 1, 6)
CELL_OF = {n: L.index(n) for n in range(1, 10)}
RING = [CELL_OF[n] for n in (1, 8, 3, 4, 9, 2, 7, 6)]
def perm_from_map(m):
return tuple(m[i] for i in range(9)) # content of i goes to m[i]
F = perm_from_map({CELL_OF[n]: CELL_OF[n % 9 + 1] for n in range(1, 10)})
R = perm_from_map({**{RING[i]: RING[(i + 1) % 8] for i in range(8)},
CELL_OF[5]: CELL_OF[5]})
def compose(p, q): # first p, then q
return tuple(q[p[i]] for i in range(9))
def apply(p, sq):
out = [0] * 9
for i in range(9):
out[p[i]] = sq[i]
return tuple(out)
def order(p):
k, q = 1, p
while q != tuple(range(9)):
q, k = compose(q, p), k + 1
return k
def parity(p):
seen, s = set(), 0
for i in range(9):
if i in seen:
continue
j, n = i, 0
while j not in seen:
seen.add(j)
j, n = p[j], n + 1
s += n - 1
return "even" if s % 2 == 0 else "odd"
def is_magic(sq):
lines = [(0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7),
(2, 5, 8), (0, 4, 8), (2, 4, 6)]
return all(sq[a] + sq[b] + sq[c] == 15 for a, b, c in lines)
def powers_magic(p):
out, q = [], p
for k in range(1, order(p)):
if is_magic(apply(q, L)):
out.append(k)
q = compose(q, p)
return out
if __name__ == "__main__":
for name, p in (("f (flying)", F), ("r (ring)", R)):
print(" {:11s} order {} {} magic after powers {}".format(
name, order(p), parity(p), powers_magic(p)))
print(" f then r equals r then f: {}".format(
compose(F, R) == compose(R, F)))
seen, frontier = {tuple(range(9))}, [tuple(range(9))]
while frontier:
nxt = []
for p in frontier:
for g in (F, R):
q = compose(p, g)
if q not in seen:
seen.add(q)
nxt.append(q)
frontier = nxt
magic_count = sum(is_magic(apply(p, L)) for p in seen)
print(" group generated by f and r: {:,} arrangements".format(
len(seen)))
print(" of which magic squares: {}".format(magic_count))
print("\nReading: turning the ring by two steps is a quarter-turn of")
print("the square and keeps it magic; flying the palaces never does.")
print("The two motions do not commute, and together they reach all 9!")
print("arrangements: the traditional moves generate the full symmetric")
print("group, of which only 8 elements keep the square magic.")
# f (flying) order 9 even magic after powers []
# r (ring) order 8 odd magic after powers [2, 4, 6]
# f then r equals r then f: False
# group generated by f and r: 362,880 arrangements
# of which magic squares: 8
#
# Reading: turning the ring by two steps is a quarter-turn of
# the square and keeps it magic; flying the palaces never does.
# The two motions do not commute, and together they reach all 9!
# arrangements: the traditional moves generate the full symmetric
# group, of which only 8 elements keep the square magic.Flying, , is a 9-cycle of order 9 and an even permutation; none of its non-trivial powers keeps the square magic — no chart “flown” from the Luoshu is a magic square. Turning, , is an 8-cycle on the outer ring of order 8 and an odd permutation; its 2nd, 4th and 6th powers are the quarter-, half- and three-quarter turns of the square and so keep it magic, while its odd powers exchange edge and corner palaces and destroy it.
The two motions do not commute: flying then turning differs from turning then flying. More important are the last two lines: together and generate all 362,880 arrangements, the full symmetric group on nine elements, of which only 8 keep the square magic. A direct consequence: if a system of rules allowed any combination of flying and turning, then from the Luoshu every arrangement could be reached; all the constraint in the system comes from the order in which it prescribes the motions, not from the two motions themselves.
Summary
Up to symmetry there is exactly one magic square of order three, the Luoshu; its centre must be 5, and Yang Hui’s four moves produce it directly. Flying is a 9-cycle on the nine positions and never keeps the square magic; turning is an 8-cycle on the outer ring, and two steps of it make a quarter-turn of the square, which does. The two motions do not commute and together generate the full symmetric group on nine elements, all 362,880 of them, of which only 8 keep the square magic: the structure of the traditional rules lies entirely in how they restrict the combination of motions.
References
Fan, Y. (2026). Dyadic Phase Coupling, Bilinear Compatibility Tensors and Spatiotemporal Grid Games: Asymmetric Kuramoto Locking, Stopping-Time Separation Hazards and Magic-Square Permutation Operators [双人相位耦合、双线性相容张量与时空网格博弈] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090172 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).