Skip to content

Algorithm Overview

stembranch computes everything from first principles — no lookup tables for astronomical quantities. This page describes the major algorithm modules and how they connect.

Architecture

Module Inventory

Astronomical Foundations

ModuleFileDescription
Solar Longitudesolar-longitude.tsVSOP87D + DE441 correction + IAU2000B nutation
Solar Termssolar-terms.tsNewton-Raphson root finding for 24 節氣
Lunar Calendarlunar.tsMeeus Ch. 49 new moons + solar term interpolation
Delta Tdelta-t.tsΔT = TT - UT (Espenak & Meeus + sxwnl tables)
True Solar Timetrue-solar-time.tsEquation of Time via VSOP87D apparent RA
Eclipseseclipses.tsSolar/lunar eclipse detection
Julian Dayjulian-day.tsJDN conversions, Julian/Gregorian calendar

Calendar & Pillars

ModuleFileDescription
Four Pillarsfour-pillars.ts年月日時 stem-branch assignment
Hidden Stemshidden-stems.ts地支藏干 (hidden stems in branches)
Ten Relationsten-relations.ts十神 (ten gods/relations between stems)
Branch Relationsbranch-relations.ts六合/三合/六衝/三刑/六害/自刑
Element Strengthelement-strength.ts旺相休囚死 (seasonal element phases)
Luck Pillarsluck-pillars.ts大運/小運 (major/minor luck periods)

Almanac & Reference

ModuleFileDescription
Almanac Flagsalmanac-flags.ts29 神煞 (spirit killers)
Day Fitnessday-fitness.ts建除十二神 (12-day cycle)
Virtue Starsvirtue-stars.ts天德/月德 and combinations
Deity Directionsdeity-directions.ts財神/喜神/福神/貴神 directions
Flying Starsflying-stars.ts九宮飛星 (9-palace flying stars)
Lunar Mansionslunar-mansions.ts二十八宿 (28 lunar mansions)

Divination Systems

ModuleFileDescription
Six Rensix-ren.ts大六壬 (Da Liu Ren)
Mystery Gatesmystery-gates.ts奇門遁甲 (Qi Men Dun Jia)
Zi Weizi-wei.ts紫微斗數 (Purple Star Astrology)

Key Design Principles

  1. No lookup tables for astronomy. Solar terms are computed by solving for the Sun's ecliptic longitude reaching multiples of 15°. The solar longitude comes from evaluating the full VSOP87D series.

  2. Validated against JPL DE441. The DE441 correction was fitted against 1,008 JPL Horizons data points spanning 209–2493 CE, achieving mean 1.05s / max 3.05s accuracy.

  3. Deterministic. Given the same input date, the output is always the same. No randomness, no external state.

  4. Zero dependencies. All astronomical constants, polynomial coefficients, and lookup tables are embedded in the source code.