5D Library › Four-Pillar Calendrical Computation and Formal Rule Engines · 中文

Chapter 4. The Ten Gods Are Z5 × Z2, the Hidden Stems Weight Vectors

Two coordinates of a stem

The ten stems pair off by element and polarity: jia and yi wood, bing and ding fire, wu and ji earth, geng and xin metal, ren and gui water, the first of each pair yang and the second yin. With jia as 0, the ii-th stem has element ⌊i/2⌋\lfloor i/2 \rfloor and polarity imod⁡2i \bmod 2. “Generates” is adding 1 in ℤ5\mathbb{Z}_5 (wood generates fire, fire earth, … water wood), and “controls” is adding 2 (wood controls earth, earth water, … metal wood).

The ten gods describe every other stem from the point of view of the day stem, the “self”. The element difference (ej−ei)mod⁡5(e_j – e_i) \bmod 5 gives five relations: same as self, generated by self, controlled by self, controlling self, generating self; matching or opposite polarity splits each in two. Same element with the same polarity, for example, is the companion, with opposite polarity the rob wealth; controlling self with the same polarity is the seven killings, with opposite polarity the direct officer. The ten gods are thus exactly the ten elements of ℤ5×ℤ2\mathbb{Z}_5 \times \mathbb{Z}_2.

Calculation

# 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.
The table of ten gods: rows are the day stem, columns the other stem; colour shows the five element relations, shade the matching or opposite polarity.

The program prints the whole table of ten gods (in two-letter codes: BJ companion, JC rob wealth, SS eating god, SG hurting officer, PC indirect wealth, ZC direct wealth, QS seven killings, ZG direct officer, PY indirect resource, ZY direct resource). It is a Latin square: every row and every column contains each of the ten gods exactly once. Moving both stems to another element, or flipping both polarities, leaves the god unchanged; this is the invariance of a group action.

A counterexample is worth noting: the ten gods are not a function of distance along the order of the stems. Two pairs one step apart in the order jia, yi, bing, ding … behave differently: yi seen from jia is rob wealth, but bing seen from yi is hurting officer; five of the ten “order distances” correspond to more than one god. Only an even number of steps fixes both the element difference and the polarity match, and with it the god. The ten gods are built on the product of element and polarity, not on the ten stems arranged in a circle.

Hidden stems

A branch’s own element is not enough for a chart; the Ziping art uses “hidden stems”: each branch hides one to three stems, the main, middle and residual qi. A chart program has to turn them into numbers, most directly into a weight vector over the five elements. The next program compares three weightings: main stem only (S1); main, middle and residual at 0.6, 0.3 and 0.1 (0.7 and 0.3 for two, S2); and the days each stem rules out of the month’s thirty in a commonly printed “days in command” table (S3; see Chapter 7).

Calculation

# 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.

The three weightings agree on each branch’s main element. They differ in how much the other elements receive: the L1 distance between S1 and S2 reaches 0.80 in branches with several hidden stems, and between S1 and S3 at most 0.93. S2 and S3 coincide in eight branches, for a simple reason: 0.6, 0.3 and 0.1 are 18, 9 and 3 days out of thirty. In one branch, hai, the days-in-command table brings in an element (earth) that the hidden-stem list lacks. “Hidden stems” are thus not one fact but several close yet different conventions; Chapter 5 shows that the choice among them affects the judgement most of all.

Summary

The ten gods are the ten elements of ℤ5×ℤ2\mathbb{Z}_5 \times \mathbb{Z}_2, and their table is a Latin square invariant under shifts of element and flips of polarity; it is not a function of distance along the order of the stems. Hidden stems can be written as weight vectors over the elements; three common weightings agree on the main element and differ by up to 0.93 on the others, and the proportions 0.6, 0.3, 0.1 share their origin with the 18, 9 and 3 days of the days-in-command table.

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).