5D Library › 纳甲装卦与奇门遁甲排盘的组合计算 · English
第一章 京房八宫与纳甲:异或掩码与闭式
八宫是八个掩码
第 64 本把六十四卦写成六维布尔立方体 :初爻到上爻六个二进制位,阳为 1。京房的八宫卦序在这个立方体上有一个干净的描述 (Jing Fang (attributed) and Lu Ji (commentary), n.d.; Smith 2008)。每一宫从一个纯卦(上下卦相同)出发:一世变初爻,二世再变二爻,直到五世变到五爻;游魂把五世的四爻变回,归魂再把下卦变回本宫。用掩码写,八代分别翻转
这些爻。一宫的八卦就是纯卦与这八个掩码的异或。
推导
# 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.程序验证了 64 卦恰好每卦属于一宫,乾宫依文王卦序为 1、44、33、12、20、23、35、14(乾、姤、遁、否、观、剥、晋、大有),坤宫为 2、24、19、11、34、43、5、8,与通行的八宫表一致。
更有意思的是第三列:每个掩码的上半与下半做异或,八个结果互不相同。由于纯卦的上下卦相同,任何一卦的「上卦异或下卦」正好等于它的掩码的这个值。所以只看一卦的上下卦之异或,就知道它是第几世、世爻在哪一爻;再用下卦异或掩码的下半,就得到它的本宫。前六代(本宫到五世)的异或码 000、100、110、111、011、001 每一步只变一位,是一个格雷码。世爻依次在六、一、二、三、四、五、四、三爻,应爻与世爻相隔三爻。
纳甲:每卦三个数
纳甲把天干与地支配到六爻上 (Jing Fang (attributed) and Lu Ji (commentary), n.d.; Wang, n.d.)。天干:乾内甲外壬,坤内乙外癸,其余六卦内外同干(震庚、巽辛、坎戊、离己、艮丙、兑丁)。地支的通行表看起来要背,其实由一条规则生成。
推导
# 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.阳卦(乾、震、坎、艮)从一个起始地支每爻进二位,阴卦(坤、巽、离、兑)每爻退二位,外卦从内卦起支后六位开始。乾、震起子,坎起寅,艮起辰;坤起未,巽起丑,离起卯,兑起巳。程序验证这一闭式与通行的纳支表八卦逐爻相同。由于每次进退两位,阳卦只用到子、寅、辰、午、申、戌六个阳支,阴卦只用到六个阴支,这正是第 66 本所说干支奇偶约束的另一个面貌。
小结
京房八宫是纯卦与八个固定掩码的异或;上卦异或下卦决定一卦的世代与世爻,前六代构成格雷码。纳甲的地支由每卦一个起支、一个方向(阳进阴退两位)与一个天干生成,外卦从内卦起支后六位开始。
参考文献
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).