5D Library › Najia Hexagram Encoding and Qimen Dunjia Combinatorial Engines · 中文
Chapter 1. Jing Fang’s Eight Palaces and the Najia: XOR Masks and a Closed Form
The eight palaces are eight masks
Volume 64 wrote the sixty-four hexagrams as the six-dimensional Boolean cube : six binary digits from the bottom line to the top, yang = 1. Jing Fang’s order of the eight palaces has a clean description on this cube (Jing Fang (attributed) and Lu Ji (commentary), n.d.; Smith 2008). Each palace starts from a pure hexagram (the same trigram twice): the first generation changes line 1, the second also line 2, and so on up to the fifth, which has changed lines 1 to 5; the “wandering soul” changes line 4 of the fifth generation back, and the “returning soul” changes the lower trigram back to the palace’s own. As masks, the eight generations flip the lines
The eight hexagrams of a palace are the pure hexagram XOR these eight masks.
Calculation
# Chapter 1. Jing Fang's eight palaces as bit flips on (Z_2)^6.
# A hexagram is six bits, line 1 (bottom) to line 6 (top), yang = 1;
# a trigram is three bits, bottom first. Each palace starts from a pure
# hexagram (the same trigram twice) and flips a fixed set of lines:
# generations 1-5 flip lines 1, 1-2, 1-3, 1-4, 1-5; the "wandering
# soul" flips 1-3 and 5; the "returning soul" flips only line 5.
# The world line (shi) of each generation and the response line (ying,
# three lines away) follow the usual table.
TRIGRAMS = {"Qian": (1, 1, 1), "Dui": (1, 1, 0), "Li": (1, 0, 1),
"Zhen": (1, 0, 0), "Xun": (0, 1, 1), "Kan": (0, 1, 0),
"Gen": (0, 0, 1), "Kun": (0, 0, 0)}
GENERATIONS = (("pure", (), 6), ("1st", (1,), 1), ("2nd", (1, 2), 2),
("3rd", (1, 2, 3), 3), ("4th", (1, 2, 3, 4), 4),
("5th", (1, 2, 3, 4, 5), 5),
("wandering", (1, 2, 3, 5), 4), ("returning", (5,), 3))
# King Wen numbers: KW[upper][lower]
ORDER = ("Qian", "Zhen", "Kan", "Gen", "Kun", "Xun", "Li", "Dui")
KW_ROWS = ((1, 25, 6, 33, 12, 44, 13, 10), (34, 51, 40, 62, 16, 32, 55, 54),
(5, 3, 29, 39, 8, 48, 63, 60), (26, 27, 4, 52, 23, 18, 22, 41),
(11, 24, 7, 15, 2, 46, 36, 19), (9, 42, 59, 53, 20, 57, 37, 61),
(14, 21, 64, 56, 35, 50, 30, 38), (43, 17, 47, 31, 45, 28, 49, 58))
KW = {(ORDER[u], ORDER[l]): KW_ROWS[u][l] for u in range(8) for l in range(8)}
NAME = {bits: n for n, bits in TRIGRAMS.items()}
def flip(hexagram, lines):
return tuple(b ^ (i + 1 in lines) for i, b in enumerate(hexagram))
def palace_members(t):
pure = TRIGRAMS[t] + TRIGRAMS[t]
return [flip(pure, lines) for _, lines, _ in GENERATIONS]
def kw(h):
return KW[NAME[h[3:]], NAME[h[:3]]]
if __name__ == "__main__":
print(" King Wen table is a bijection onto 1..64: {}".format(
sorted(KW.values()) == list(range(1, 65))))
seen = {}
for t in TRIGRAMS:
for g, h in enumerate(palace_members(t)):
seen.setdefault(h, []).append((t, g))
print(" every hexagram lies in exactly one palace: {}".format(
len(seen) == 64 and all(len(v) == 1 for v in seen.values())))
for t in ("Qian", "Kun"):
print(" {:4s} palace (King Wen): ".format(t) + " ".join(
str(kw(h)) for h in palace_members(t)))
print(" generation lines flipped upper XOR lower shi ying")
codes = []
for name, lines, shi in GENERATIONS:
m = flip((0,) * 6, lines)
x = tuple(a ^ b for a, b in zip(m[:3], m[3:]))
codes.append(x)
ying = shi + 3 if shi <= 3 else shi - 3
print(" {:10s} {:13s} {} {} {}".format(
name, ",".join(map(str, lines)) or "-", "".join(map(str, x)),
shi, ying))
gray = all(sum(a != b for a, b in zip(codes[i], codes[i + 1])) == 1
for i in range(5))
print(" the eight XOR codes are all different: {}; generations 0-5"
.format(len(set(codes)) == 8))
print(" form a Gray code (one bit changes per step): {}".format(gray))
print("\nReading: a palace is the pure hexagram XOR a fixed set of eight")
print("masks. Which generation a hexagram belongs to, and hence its")
print("world line, is read off from upper XOR lower alone; its palace")
print("is then the lower trigram XOR the mask's lower half.")
# King Wen table is a bijection onto 1..64: True
# every hexagram lies in exactly one palace: True
# Qian palace (King Wen): 1 44 33 12 20 23 35 14
# Kun palace (King Wen): 2 24 19 11 34 43 5 8
# generation lines flipped upper XOR lower shi ying
# pure - 000 6 3
# 1st 1 100 1 4
# 2nd 1,2 110 2 5
# 3rd 1,2,3 111 3 6
# 4th 1,2,3,4 011 4 1
# 5th 1,2,3,4,5 001 5 2
# wandering 1,2,3,5 101 4 1
# returning 5 010 3 6
# the eight XOR codes are all different: True; generations 0-5
# form a Gray code (one bit changes per step): True
#
# Reading: a palace is the pure hexagram XOR a fixed set of eight
# masks. Which generation a hexagram belongs to, and hence its
# world line, is read off from upper XOR lower alone; its palace
# is then the lower trigram XOR the mask's lower half.The program checks that each of the 64 hexagrams belongs to exactly one palace; in King Wen numbers the Qian palace is 1, 44, 33, 12, 20, 23, 35, 14 (Qian, Gou, Dun, Pi, Guan, Bo, Jin, Da You) and the Kun palace 2, 24, 19, 11, 34, 43, 5, 8, as in the usual table of the eight palaces.
The third column is more interesting: the XOR of each mask’s upper and lower halves gives eight different values. Since a pure hexagram has equal upper and lower trigrams, “upper XOR lower” of any hexagram equals exactly this value of its mask. Upper XOR lower alone therefore tells a hexagram’s generation and the position of its world line; the lower trigram XOR the lower half of the mask then gives its palace. The codes of the first six generations (pure to fifth), 000, 100, 110, 111, 011, 001, change one bit at a time: a Gray code. The world line sits on lines 6, 1, 2, 3, 4, 5, 4, 3 in turn, and the response line three lines away.
Najia: three numbers per trigram
The najia attaches stems and branches to the six lines (Jing Fang (attributed) and Lu Ji (commentary), n.d.; Wang, n.d.). Stems: Qian has jia inside and ren outside, Kun yi inside and gui outside, and the other six trigrams the same stem inside and out (Zhen geng, Xun xin, Kan wu, Li ji, Gen bing, Dui ding). The usual table of branches looks as if it must be memorised, but one rule generates it.
Calculation
# Chapter 1. Najia: stems and branches on the six lines.
# Each trigram carries a stem (inner = lower position, outer = upper)
# and three branches. The table below is the one usually printed; the
# program checks it against a closed form: yang trigrams step +2 through
# the twelve branches, yin trigrams step -2, and the outer trigram starts
# six branches after the inner one.
BRANCHES = ("zi", "chou", "yin", "mao", "chen", "si", "wu", "wei", "shen",
"you", "xu", "hai")
STEM = {"Qian": ("jia", "ren"), "Kun": ("yi", "gui"),
"Zhen": ("geng", "geng"), "Xun": ("xin", "xin"),
"Kan": ("wu", "wu"), "Li": ("ji", "ji"),
"Gen": ("bing", "bing"), "Dui": ("ding", "ding")}
TABLE = { # inner three then outer three, bottom line first
"Qian": "zi yin chen wu shen xu", "Zhen": "zi yin chen wu shen xu",
"Kan": "yin chen wu shen xu zi", "Gen": "chen wu shen xu zi yin",
"Kun": "wei si mao chou hai you", "Xun": "chou hai you wei si mao",
"Li": "mao chou hai you wei si", "Dui": "si mao chou hai you wei"}
START = {"Qian": 0, "Zhen": 0, "Kan": 2, "Gen": 4,
"Kun": 7, "Xun": 1, "Li": 3, "Dui": 5}
YANG = {"Qian", "Zhen", "Kan", "Gen"}
def closed_form(t):
step = 2 if t in YANG else -2
inner = [(START[t] + step * k) % 12 for k in range(3)]
outer = [(START[t] + 6 + step * k) % 12 for k in range(3)]
return [BRANCHES[b] for b in inner + outer]
if __name__ == "__main__":
ok = all(closed_form(t) == TABLE[t].split() for t in TABLE)
print(" printed table = closed form (+-2 steps, outer = inner + 6):"
" {}".format(ok))
for t in ("Qian", "Kun", "Kan", "Li"):
print(" {:4s} inner {:4s} {:16s} outer {:4s} {}".format(
t, STEM[t][0], " ".join(TABLE[t].split()[:3]), STEM[t][1],
" ".join(TABLE[t].split()[3:])))
yang_use = sorted({b for t in YANG for b in TABLE[t].split()},
key=BRANCHES.index)
yin_use = sorted({b for t in TABLE if t not in YANG
for b in TABLE[t].split()}, key=BRANCHES.index)
print(" branches on yang trigrams: {}".format(" ".join(yang_use)))
print(" branches on yin trigrams: {}".format(" ".join(yin_use)))
print("\nReading: the najia table is generated by three numbers per")
print("trigram (a starting branch, a direction and the stem); yang")
print("trigrams use only the six yang branches and yin trigrams only")
print("the six yin branches, as the parity rule of the cycle requires.")
# printed table = closed form (+-2 steps, outer = inner + 6): True
# Qian inner jia zi yin chen outer ren wu shen xu
# Kun inner yi wei si mao outer gui chou hai you
# Kan inner wu yin chen wu outer wu shen xu zi
# Li inner ji mao chou hai outer ji you wei si
# branches on yang trigrams: zi yin chen wu shen xu
# branches on yin trigrams: chou mao si wei you hai
#
# Reading: the najia table is generated by three numbers per
# trigram (a starting branch, a direction and the stem); yang
# trigrams use only the six yang branches and yin trigrams only
# the six yin branches, as the parity rule of the cycle requires.Yang trigrams (Qian, Zhen, Kan, Gen) advance two branches per line from a starting branch, yin trigrams (Kun, Xun, Li, Dui) go back two, and the outer trigram starts six branches after the inner one. Qian and Zhen start at zi, Kan at yin, Gen at chen; Kun at wei, Xun at chou, Li at mao, Dui at si. The program checks that this closed form reproduces the usual table line by line for all eight trigrams. Because every step is two branches, yang trigrams use only the six yang branches zi, yin, chen, wu, shen and xu, and yin trigrams only the six yin branches: another face of the parity constraint of stems and branches discussed in Volume 66.
Summary
Jing Fang’s eight palaces are pure hexagrams XOR eight fixed masks; upper XOR lower tells a hexagram’s generation and world line, and the first six generations form a Gray code. The najia branches are generated by one starting branch, one direction (yang forward, yin back, two at a time) and one stem per trigram, the outer trigram starting six branches after the inner.
References
Fan, Y. (2026). Najia Hexagram Encoding and Qimen Dunjia Combinatorial Engines: Eight-Palace Group Actions, Sexagenary Void Cosets, Split-Supplement versus Intercalation Divergences and Nine-Palace Rotor Permutations [纳甲装卦与奇门遁甲排盘的组合计算] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090182 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).