5D Library › 六维超立方体游走、卦变马尔可夫链与模运算星宫晶格 · English

第五章 紫微斗数的安星法则是模 12 的映射

十二宫与命宫

紫微斗数把一个人的命盘画在十二个宫位上,每个宫位对应一个地支。把子、丑、寅……亥依次记为 0 到 11,十二宫就是 ℤ12\mathbb{Z}_{12}。排盘的第一步由农历出生月份 mm(1 到 12)与出生时辰 hh(子时为 0,亥时为 11)定出命宫与身宫:从寅宫起正月,顺数到生月,再逆数到生时为命宫,顺数到生时为身宫 (Anonymous 1607)。

推导

命宫、身宫与十二宫。 L=(2+(m−1)−h)mod⁡12,B=(2+(m−1)+h)mod⁡12.L = (2 + (m-1) – h) \bmod 12, \qquad B = (2 + (m-1) + h) \bmod 12 . 十二宫从命宫起逆排:命宫 LL、兄弟 L−1L-1、夫妻 L−2L-2、子女 L−3L-3、财帛 L−4L-4、疾厄 L−5L-5、迁移 L−6L-6、交友(仆役)L−7L-7、官禄 L−8L-8、田宅 L−9L-9、福德 L−10L-10、父母 L−11L-11。所以迁移是对宫 L+6L + 6,财帛与官禄是 L+8L + 8 与 L+4L + 4——命宫、财帛、官禄三宫正好构成 ℤ12\mathbb{Z}_{12} 中由 {0,4,8}\{0, 4, 8\} 平移得到的内接正三角形。

宫干由年干按五虎遁定出寅宫之干,再依次排下去;第 37 本讨论过五虎遁是一个二对一的仿射映射。命宫的干支决定它的纳音五行,纳音五行决定「五行局」:水二局、木三局、金四局、土五局、火六局。第十段程序计算这些映射。

推导

# Chapter 5. Zi Wei Dou Shu: the Life Palace and the "element bureau".
# Branches are numbered 0..11 (Zi, Chou, Yin, ... Hai); stems 0..9 (Jia ..
# Gui). Birth month m (1..12, lunar) and birth hour h (0 = Zi .. 11 = Hai).
#   Life palace   start at Yin (2) as month 1, count forward to month m,
#                 then backward to hour h:   L = (2 + (m - 1) - h) mod 12
#   Body palace   forward instead:           (2 + (m - 1) + h) mod 12
#   12 palaces    Life, Siblings, Spouse, Children, Wealth, Health,
#                 Travel, Friends, Career, Property, Fortune, Parents at
#                 L, L - 1, ..., L - 11 (counted backward)
#   palace stems  the stem on Yin is 2 (year stem mod 5) + 2 (mod 10) - the
#                 "five tigers" rule of Book 37 - and stems run on from Yin
#   bureau        the na yin element of the Life palace's stem-branch pair
#                 gives the bureau: water 2, wood 3, metal 4, earth 5, fire 6
BRANCH = "Zi Chou Yin Mao Chen Si Wu Wei Shen You Xu Hai".split()
PALACES = ("Life Siblings Spouse Children Wealth Health Travel Friends "
           "Career Property Fortune Parents").split()
NAYIN = "metal fire wood earth metal fire water earth metal wood " \
        "water earth fire wood water".split() * 2   # 30 pairs of the 60
BUREAU = {"water": 2, "wood": 3, "metal": 4, "earth": 5, "fire": 6}

def life(m, h):
    return (2 + (m - 1) - h) % 12

def body(m, h):
    return (2 + (m - 1) + h) % 12

def stem_of(branch, year_stem):
    yin_stem = (2 * (year_stem % 5) + 2) % 10
    return (yin_stem + (branch - 2) % 12) % 10

def sexagenary(stem, branch):
    for k in range(60):
        if k % 10 == stem and k % 12 == branch:
            return k
    raise ValueError("stem and branch of different parity")

def bureau(year_stem, life_branch):
    k = sexagenary(stem_of(life_branch, year_stem), life_branch)
    return BUREAU[NAYIN[k // 2]]

if __name__ == "__main__":
    counts = [0] * 12
    for m in range(1, 13):
        for h in range(12):
            counts[life(m, h)] += 1
    print("   Life palace over 144 month-hour pairs: each branch {} times"
          .format(sorted(set(counts))))
    m, h = 3, 4
    L = life(m, h)
    print("   example month 3, hour Chen: Life in {}, Body in {}".format(
        BRANCH[L], BRANCH[body(m, h)]))
    print("   " + ", ".join("{} {}".format(PALACES[i], BRANCH[(L - i) % 12])
                          for i in range(0, 12, 2)))
    print("   opposite (Travel) = L + 6; Wealth = L - 4 = L + 8;"
          " Career = L - 8 = L + 4")
    tally = {}
    print("\n   bureau by year stem (Jia/Ji .. Wu/Gui) and Life branch:")
    for ys in range(5):
        row = [bureau(ys, b) for b in range(12)]
        for v in row:
            tally[v] = tally.get(v, 0) + 1
        print("   stem {}  ".format(ys) + " ".join(str(v) for v in row))
    print("   bureau counts over the 60 cells: " + ", ".join(
        "{}: {}".format(k, tally[k]) for k in sorted(tally)))
    print("\nReading: the Life and Body palaces are affine maps mod 12 of")
    print("month and hour, each branch equally often. The bureau is a table")
    print("look-up (the na yin cycle of the sixty stem-branch pairs); each")
    print("year stem gives its own pattern, and over the 60 cells each of")
    print("the five bureaus occurs exactly 12 times.")
#    Life palace over 144 month-hour pairs: each branch [12] times
#    example month 3, hour Chen: Life in Zi, Body in Shen
#    Life Zi, Spouse Xu, Wealth Shen, Travel Wu, Career Chen, Fortune Yin
#    opposite (Travel) = L + 6; Wealth = L - 4 = L + 8; Career = L - 8 = L + 4
#
#    bureau by year stem (Jia/Ji .. Wu/Gui) and Life branch:
#    stem 0  2 2 6 6 3 3 5 5 4 4 6 6
#    stem 1  6 6 5 5 4 4 3 3 2 2 5 5
#    stem 2  5 5 3 3 2 2 4 4 6 6 3 3
#    stem 3  3 3 4 4 6 6 2 2 5 5 4 4
#    stem 4  4 4 2 2 5 5 6 6 3 3 2 2
#    bureau counts over the 60 cells: 2: 12, 3: 12, 4: 12, 5: 12, 6: 12
#
# Reading: the Life and Body palaces are affine maps mod 12 of
# month and hour, each branch equally often. The bureau is a table
# look-up (the na yin cycle of the sixty stem-branch pairs); each
# year stem gives its own pattern, and over the 60 cells each of
# the five bureaus occurs exactly 12 times.

命宫与身宫是生月与生时的仿射映射,144 种组合中每个地支恰好出现 12 次。五行局是一次查表:每个年干类别有自己的一行,例如甲己年命宫在子丑为水二局、寅卯为火六局;在全部 60 个格中,五种局各出现 12 次。

安紫微星:商与余数的奇偶校正

安紫微星的口诀通常这样运算:以生日(初一到三十)除以局数;不能整除时,向生日「借」最少的 xx 使其能被整除,得到商 qq;从寅宫起数 qq 宫(寅为第一宫);借的数 xx 为奇数则逆退 xx 宫,为偶数(包括不借)则顺进 xx 宫。

推导

分段仿射映射。 记 x=(−d)mod⁡nx = (-d) \bmod n,q=(d+x)/nq = (d + x)/n,则 Z(d,n)=(2+q−1+(−1)xx)mod⁡12.Z(d, n) = \big(2 + q – 1 + (-1)^{x}\, x\big) \bmod 12 . 天府星是紫微关于寅申轴的反射:F=(4−Z)mod⁡12F = (4 – Z) \bmod 12。紫微一系的天机、太阳、武曲、天同、廉贞在 Z−1,Z−3,Z−4,Z−5,Z−8Z – 1, Z – 3, Z – 4, Z – 5, Z – 8;天府一系的太阴、贪狼、巨门、天相、天梁、七杀、破军在 F+1,F+2,F+3,F+4,F+5,F+6,F+10F + 1, F + 2, F + 3, F + 4, F + 5, F + 6, F + 10。

推导

# Chapter 5. Placing the Zi Wei star: a piecewise affine map mod 12.
# For lunar day d (1..30) and bureau n (2..6): find the smallest x >= 0
# with (d + x) divisible by n, and q = (d + x) / n. Count q palaces from
# Yin (Yin itself is 1): position Yin + q - 1. If the borrowed amount x is
# odd, step back x palaces; if x is even (including 0), step forward x.
# The Tian Fu star mirrors Zi Wei across the Yin-Shen axis: F = 4 - Z.
# The other twelve main stars sit at fixed offsets: Tian Ji Z-1, Tai Yang
# Z-3, Wu Qu Z-4, Tian Tong Z-5, Lian Zhen Z-8; Tai Yin F+1, Tan Lang F+2,
# Ju Men F+3, Tian Xiang F+4, Tian Liang F+5, Qi Sha F+6, Po Jun F+10.
BRANCH = "Zi Chou Yin Mao Chen Si Wu Wei Shen You Xu Hai".split()
ZI_GROUP = {"Zi Wei": 0, "Tian Ji": -1, "Tai Yang": -3, "Wu Qu": -4,
            "Tian Tong": -5, "Lian Zhen": -8}
FU_GROUP = {"Tian Fu": 0, "Tai Yin": 1, "Tan Lang": 2, "Ju Men": 3,
            "Tian Xiang": 4, "Tian Liang": 5, "Qi Sha": 6, "Po Jun": 10}

def ziwei(day, bureau):
    x = (-day) % bureau
    q = (day + x) // bureau
    pos = 2 + q - 1
    pos += -x if x % 2 else x
    return pos % 12

def tianfu(z):
    return (4 - z) % 12

def chart(z):
    f = tianfu(z)
    out = {s: (z + o) % 12 for s, o in ZI_GROUP.items()}
    out.update({s: (f + o) % 12 for s, o in FU_GROUP.items()})
    return out

if __name__ == "__main__":
    print("   day 1 in each bureau: " + ", ".join(
        "{} -> {}".format(n, BRANCH[ziwei(1, n)]) for n in range(2, 7)))
    print("   (tables: water 2 Chou, wood 3 Chen, metal 4 Hai,"
          " earth 5 Wu, fire 6 You)")
    print("\n   Zi Wei position counts over days 1-30:")
    print("   bureau  " + " ".join("{:>4}".format(b) for b in BRANCH))
    for n in range(2, 7):
        c = [0] * 12
        for d in range(1, 31):
            c[ziwei(d, n)] += 1
        print("     {}     ".format(n) + " ".join("{:4d}".format(v)
                                                 for v in c))
    mirror = all(tianfu(tianfu(z)) == z for z in range(12))
    fixed = [BRANCH[z] for z in range(12) if tianfu(z) == z]
    charts = {tuple(sorted(chart(z).items())) for z in range(12)}
    print("\n   Tian Fu map is an involution: {}; fixed on {}".format(
        mirror, " and ".join(fixed)))
    print("   distinct layouts of the 14 main stars: {}".format(len(charts)))
    print("\nReading: the rule is a piecewise affine map: a quotient by the")
    print("bureau plus a parity-dependent correction. Its positions are not")
    print("uniform within a bureau. Every main star follows from Zi Wei by")
    print("a rotation or by the reflection across Yin-Shen, so there are")
    print("only 12 possible layouts of the fourteen main stars.")
#    day 1 in each bureau: 2 -> Chou, 3 -> Chen, 4 -> Hai, 5 -> Wu, 6 -> You
#    (tables: water 2 Chou, wood 3 Chen, metal 4 Hai, earth 5 Wu, fire 6 You)
#
#    Zi Wei position counts over days 1-30:
#    bureau    Zi Chou  Yin  Mao Chen   Si   Wu  Wei Shen  You   Xu  Hai
#      2        2    3    4    4    3    2    2    2    2    2    2    2
#      3        1    2    2    2    3    3    3    3    3    3    3    2
#      4        1    2    3    3    4    4    4    3    2    1    1    2
#      5        1    2    3    3    4    3    4    3    2    2    1    2
#      6        2    3    3    3    3    3    3    2    2    2    2    2
#
#    Tian Fu map is an involution: True; fixed on Yin and Shen
#    distinct layouts of the 14 main stars: 12
#
# Reading: the rule is a piecewise affine map: a quotient by the
# bureau plus a parity-dependent correction. Its positions are not
# uniform within a bureau. Every main star follows from Zi Wei by
# a rotation or by the reflection across Yin-Shen, so there are
# only 12 possible layouts of the fourteen main stars.

程序先用通行排盘表的五个已知值核对公式:各局初一的紫微分别在丑、辰、亥、午、酉,全部吻合。在同一局内,紫微的位置并不均匀:例如水二局的紫微落在寅、卯的天数是落在子的两倍;金四局落在辰、巳、午的天数是落在子、酉、戌的四倍。

更重要的是最后两行。天府映射是一个对合(做两次回到原处),只在寅与申两宫不动——它是十二边形的一条对称轴上的反射。其余十二颗主星都是紫微或天府的固定平移,所以十四主星的排布只有 12 种:一旦知道紫微在哪一宫,全部主星的位置都确定了。从群论的角度看,主星盘是一个固定模板在二面体群 D12D_{12}(12 个旋转与 12 个反射)作用下的像,而实际用到的只是其中的 12 个旋转与一条固定的反射轴。

十二宫在 \mathbb{Z}_{12} 上的排列(命宫置于子宫)。金色三角形为命、财、官三宫(\{0, 4, 8\} 的平移),蓝色虚线连接命宫与对宫迁移。

注

对「按出生资料精确排盘」的含义。 主星盘只有 12 种,所以单靠十四主星,命盘之间的差别全部来自紫微落在哪一宫与命宫落在哪一宫——12 乘 12 种组合。辅星、四化、流年流月增加了更多的组合,但主星结构本身的信息量是有限的,可以精确地数出来。

小结

命宫与身宫是生月与生时的模 12 仿射映射;五行局是命宫干支纳音的查表,五种局在 60 个格中各占 12 个。安紫微星诀是一个分段仿射映射:除以局数取商,再按所借之数的奇偶向前或向后校正;天府是紫微关于寅申轴的反射,其余主星都是固定平移。因此十四主星的排布只有 12 种,主星盘是二面体群作用下的一个轨道。

参考文献

Anonymous. 1607. Ziwei Doushu [Zi Wei Dou Shu], 3 Juan, in the Wanli Supplement to the Daoist Canon (Wanli Xu Daozang). In Chinese; Author Unknown, Traditionally Attributed to Chen Tuan.

Fan, Y. (2026). Six-Dimensional Hypercube Walks, Hexagram Mutation Markov Chains and Modular Star-Palace Lattices: Boolean Cube Spectral Gaps, Yarrow-versus-Coin Probability Biases and Twelve-Palace Dihedral Projections [六维超立方体游走、卦变马尔可夫链与模运算星宫晶格] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090176 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).