5D Library › 四柱排盘的历法计算与规则引擎 · English

第四章 十神是 Z5 × Z2,藏干是权重向量

天干的两个坐标

十天干按五行与阴阳两两成对:甲乙木、丙丁火、戊己土、庚辛金、壬癸水,每对中前阳后阴。以甲为 0,第 ii 个天干的五行是 ⌊i/2⌋\lfloor i/2 \rfloor,阴阳是 imod⁡2i \bmod 2。五行相生是在 ℤ5\mathbb{Z}_5 上加 1(木生火、火生土……水生木),相克是加 2(木克土、土克水……金克木)。

十神是以日干为「我」,看其他天干与我的关系。五行之差 (ej−ei)mod⁡5(e_j – e_i) \bmod 5 给出五种关系:同我、我生、我克、克我、生我;阴阳同异再把每一种一分为二。例如同我而阴阳相同为比肩,不同为劫财;克我而阴阳相同为七杀,不同为正官。于是十神恰好是 ℤ5×ℤ2\mathbb{Z}_5 \times \mathbb{Z}_2 的十个元素。

推导

# Chapter 4. The ten gods as the group Z_5 x Z_2.
# Stem i (0 = jia ... 9 = gui) has element e = i // 2 (wood, fire, earth,
# metal, water) and polarity p = i % 2 (0 yang, 1 yin). In the cycle
# wood -> fire -> earth -> metal -> water -> wood, "generates" is +1 and
# "controls" is +2 (mod 5). The ten god of stem j seen from the day
# stem i depends on (e_j - e_i) mod 5 and on whether p_j = p_i:
#   0 same     : BJ companion        / JC rob wealth
#   1 i makes  : SS eating god       / SG hurting officer
#   2 i rules  : PC indirect wealth  / ZC direct wealth
#   3 rules i  : QS seven killings   / ZG direct officer
#   4 makes i  : PY indirect resource/ ZY direct resource
# (first code: same polarity; second: opposite polarity).
STEMS = ("jia", "yi", "bing", "ding", "wu", "ji", "geng", "xin", "ren",
         "gui")
CODES = (("BJ", "JC"), ("SS", "SG"), ("PC", "ZC"), ("QS", "ZG"),
         ("PY", "ZY"))

def god(i, j):
    de = (j // 2 - i // 2) % 5
    return CODES[de][0 if i % 2 == j % 2 else 1]

if __name__ == "__main__":
    print("   day stem   " + " ".join("{:>4s}".format(s) for s in STEMS))
    for i in range(10):
        print("   {:9s}  ".format(STEMS[i]) + " ".join(
            "{:>4s}".format(god(i, j)) for j in range(10)))
    rows = all(len({god(i, j) for j in range(10)}) == 10 for i in range(10))
    cols = all(len({god(i, j) for i in range(10)}) == 10 for j in range(10))
    print("   every row and column holds all ten gods (Latin square): {}"
          .format(rows and cols))
    inv = all(god(i, j) == god((i + 2 * a) % 10 ^ b, (j + 2 * a) % 10 ^ b)
              for i in range(10) for j in range(10) for a in range(5)
              for b in (0, 1))
    print("   unchanged when both stems shift element or flip polarity: {}"
          .format(inv))
    by_step = {}
    for i in range(10):
        for j in range(10):
            by_step.setdefault((j - i) % 10, set()).add(god(i, j))
    mixed = sum(len(v) > 1 for v in by_step.values())
    print("   stem-order steps (j - i mod 10) that give more than one god:"
          " {} of 10".format(mixed))
    print("\nReading: the ten gods are the ten elements of Z_5 x Z_2, the")
    print("element step and the polarity match. They are a function of")
    print("that pair, not of the distance in the order jia ... gui: only")
    print("steps of an even number of stems give a single god.")
#    day stem    jia   yi bing ding   wu   ji geng  xin  ren  gui
#    jia          BJ   JC   SS   SG   PC   ZC   QS   ZG   PY   ZY
#    yi           JC   BJ   SG   SS   ZC   PC   ZG   QS   ZY   PY
#    bing         PY   ZY   BJ   JC   SS   SG   PC   ZC   QS   ZG
#    ding         ZY   PY   JC   BJ   SG   SS   ZC   PC   ZG   QS
#    wu           QS   ZG   PY   ZY   BJ   JC   SS   SG   PC   ZC
#    ji           ZG   QS   ZY   PY   JC   BJ   SG   SS   ZC   PC
#    geng         PC   ZC   QS   ZG   PY   ZY   BJ   JC   SS   SG
#    xin          ZC   PC   ZG   QS   ZY   PY   JC   BJ   SG   SS
#    ren          SS   SG   PC   ZC   QS   ZG   PY   ZY   BJ   JC
#    gui          SG   SS   ZC   PC   ZG   QS   ZY   PY   JC   BJ
#    every row and column holds all ten gods (Latin square): True
#    unchanged when both stems shift element or flip polarity: True
#    stem-order steps (j - i mod 10) that give more than one god: 5 of 10
#
# Reading: the ten gods are the ten elements of Z_5 x Z_2, the
# element step and the polarity match. They are a function of
# that pair, not of the distance in the order jia ... gui: only
# steps of an even number of stems give a single god.
十神表:行为日干,列为其他天干;颜色表示五种五行关系,深浅表示阴阳同异。

程序印出完整的十神表(程序用两个字母的缩写:BJ 比肩、JC 劫财、SS 食神、SG 伤官、PC 偏财、ZC 正财、QS 七杀、ZG 正官、PY 偏印、ZY 正印)。它是一个拉丁方:每一行、每一列都恰好出现十神各一次。把两个天干同时换一个五行,或同时翻转阴阳,十神不变,这就是群作用的不变性。

一个值得注意的反例:十神不是天干次序上的距离的函数。在甲乙丙丁……的次序上相隔一位的两对天干,甲见乙为劫财,乙见丙却是伤官;十种「次序距离」中有五种对应不止一个十神。只有相隔偶数位时,五行之差固定、阴阳同异也固定,十神才唯一。十神的结构是五行与阴阳的乘积,而不是十天干排成的一个圈。

地支藏干

地支本身的五行不足以排盘,子平术用「藏干」:每个地支藏一到三个天干,分本气、中气、余气。排盘软件必须把它们变成数,最直接的是一个五行的权重向量。下面的程序比较三种权重:只取本气(S1);本气、中气、余气按 0.6、0.3、0.1(两个时按 0.7、0.3,S2);按一种通行的「人元司令」表,把每月三十天分给各干的天数(S3,见第七章)。

推导

# Chapter 4. Hidden stems as weight vectors over the five elements.
# Each branch hides one to three stems (main, middle, residual). Three
# weightings of the same list are compared:
#   S1 main stem only;
#   S2 main / middle / residual = 0.6 / 0.3 / 0.1 (two stems: 0.7 / 0.3);
#   S3 the "days in command" of one commonly printed table (30 days
#      per month, shared among stems; see Chapter 7).
# Stems are indexed 0-9 (jia ... gui); element = stem // 2.
BRANCHES = ("zi", "chou", "yin", "mao", "chen", "si", "wu", "wei", "shen",
            "you", "xu", "hai")
ELEMENTS = ("wood", "fire", "earth", "metal", "water")
HIDDEN = ((9,), (5, 9, 7), (0, 2, 4), (1,), (4, 1, 9), (2, 4, 6), (3, 5),
          (5, 3, 1), (6, 8, 4), (7,), (4, 7, 3), (8, 0))
DAYS = (((8, 10), (9, 20)), ((9, 9), (7, 3), (5, 18)),
        ((4, 7), (2, 7), (0, 16)), ((0, 10), (1, 20)),
        ((1, 9), (9, 3), (4, 18)), ((4, 5), (6, 9), (2, 16)),
        ((2, 10), (5, 9), (3, 11)), ((3, 9), (1, 3), (5, 18)),
        ((4, 10), (8, 3), (6, 17)), ((6, 10), (7, 20)),
        ((7, 9), (3, 3), (4, 18)), ((4, 7), (0, 5), (8, 18)))

def vector(pairs):
    v = [0.0] * 5
    for stem, w in pairs:
        v[stem // 2] += w
    return v

def schemes(b):
    h = HIDDEN[b]
    w2 = {1: (1.0,), 2: (0.7, 0.3), 3: (0.6, 0.3, 0.1)}[len(h)]
    return (vector([(h[0], 1.0)]), vector(zip(h, w2)),
            vector([(s, d / 30) for s, d in DAYS[b]]))

def l1(a, b):
    return sum(abs(x - y) for x, y in zip(a, b))

if __name__ == "__main__":
    print("   branch  main element  S1-S2  S1-S3  S2-S3   only in S3")
    tot = [0.0, 0.0, 0.0]
    for b, name in enumerate(BRANCHES):
        v1, v2, v3 = schemes(b)
        d = (l1(v1, v2), l1(v1, v3), l1(v2, v3))
        tot = [t + x for t, x in zip(tot, d)]
        extra = [ELEMENTS[e] for e in range(5) if v3[e] > 0 and v2[e] == 0]
        print("   {:6s}  {:12s}  {:5.2f}  {:5.2f}  {:5.2f}   {}".format(
            name, ELEMENTS[HIDDEN[b][0] // 2], *d, " ".join(extra) or "-"))
    print("   mean L1 distance: S1-S2 {:.3f}, S1-S3 {:.3f}, S2-S3 {:.3f}"
          .format(*[t / 12 for t in tot]))
    same_main = all(max(range(5), key=lambda e: schemes(b)[2][e])
                    == HIDDEN[b][0] // 2 for b in range(12))
    print("   S3's largest element is the main stem's element in every"
          " branch: {}".format(same_main))
    print("\nReading: all three weightings agree on each branch's main")
    print("element; they differ in how much weight the other elements get")
    print("(up to 0.93 of the total). S2 and S3 coincide in eight branches:")
    print("0.6 / 0.3 / 0.1 is 18 / 9 / 3 days out of 30. In one branch the")
    print("days-in-command table brings in an element the list lacks.")
#    branch  main element  S1-S2  S1-S3  S2-S3   only in S3
#    zi      water          0.00   0.00   0.00   -
#    chou    earth          0.80   0.80   0.00   -
#    yin     wood           0.80   0.93   0.27   -
#    mao     wood           0.00   0.00   0.00   -
#    chen    earth          0.80   0.80   0.00   -
#    si      fire           0.80   0.93   0.40   -
#    wu      fire           0.60   0.60   0.00   -
#    wei     earth          0.80   0.80   0.00   -
#    shen    metal          0.80   0.87   0.47   -
#    you     metal          0.00   0.00   0.00   -
#    xu      earth          0.80   0.80   0.00   -
#    hai     water          0.60   0.80   0.47   earth
#    mean L1 distance: S1-S2 0.567, S1-S3 0.611, S2-S3 0.133
#    S3's largest element is the main stem's element in every branch: True
#
# Reading: all three weightings agree on each branch's main
# element; they differ in how much weight the other elements get
# (up to 0.93 of the total). S2 and S3 coincide in eight branches:
# 0.6 / 0.3 / 0.1 is 18 / 9 / 3 days out of 30. In one branch the
# days-in-command table brings in an element the list lacks.

三种权重在每个地支的主五行上一致。差别在其余五行分到多少:S1 与 S2 的 L1 距离在藏干多的地支达到 0.80,S1 与 S3 最多 0.93。S2 与 S3 在八个地支上完全相同,原因很简单:0.6、0.3、0.1 正是三十天中的 18、9、3 天。在亥这一个地支上,人元司令表多出一个藏干表里没有的五行(土)。所以「藏干」不是一个单一的事实,而是几种互相接近、但不完全相同的约定;第五章会看到,选哪一种对判断的影响最大。

小结

十神是 ℤ5×ℤ2\mathbb{Z}_5 \times \mathbb{Z}_2 的十个元素,十神表是一个在五行平移与阴阳翻转下不变的拉丁方;它不是天干次序距离的函数。藏干可以写成五行权重向量,三种通行权重在主五行上一致,在次要五行上相差可达 0.93;0.6、0.3、0.1 的比例与人元司令的 18、9、3 天同源。

Fan, Y. (2026). Four-Pillar Calendrical Computation and Formal Rule Engines: Julian-Day Sexagenary Cycles, Midnight-Boundary Disagreements, Birth-Time Sensitivity Bounds and Weighted-Graph Strength Scoring [四柱排盘的历法计算与规则引擎] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090180 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).