meiri 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7a6aca0b4b26b006121d50c2d433a06e4f6d8f6fb882a4563a4a0cf3eea84602
4
+ data.tar.gz: 7cdf9944133443886f26af87dac2d41978eb1bcee5fb923db98e48f1e4551ab6
5
+ SHA512:
6
+ metadata.gz: 58b2886572eb59cb8713886ad5e5997a322ed930fb501424ef9de8c859c2606397a0f9d9506e9d0df727198b17da9c536932e3fad3351142d698666c3aabe8b1
7
+ data.tar.gz: 5316411fbe187866f0332f0509ff781e554755ed234036071c027dd193ed846e8c88a0de511f162a66b5b8377d61790e68ed31077e8098d91594c86d05a3496a
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 ikotome
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # meiri(命理)
2
+
3
+ 四柱推命の命式を計算するRubyライブラリです。生年月日時から四柱(年柱・月柱・日柱・時柱)を求め、通変星・十二運を導き出します。
4
+
5
+ ## 特徴
6
+
7
+ - **暦データ不要** — 月柱の境界となる節入り時刻を、太陽黄経の天文計算(VSOP87切り詰め級数 + ΔT補正 + 二分法探索)でその場で算出します。万年暦のデータファイルを同梱していません
8
+ - **精度** — 2024年の12節すべてで国立天文台の暦要項と30秒以内で一致することをテストで確認しています
9
+ - **2層構造** — 暦の計算(`Meiri::Mannenreki`)と四柱推命の解釈(`Meiri::ShityuSuimei`)を分離した設計です
10
+
11
+ ## インストール
12
+
13
+ Gemfileに追加:
14
+
15
+ ```ruby
16
+ gem 'meiri'
17
+ ```
18
+
19
+ または直接インストール:
20
+
21
+ ```
22
+ $ gem install meiri
23
+ ```
24
+
25
+ ## 使い方
26
+
27
+ ```ruby
28
+ require 'meiri'
29
+
30
+ # 1981年8月7日 22:00生まれ 男性
31
+ chart = Meiri::ShityuSuimei::Chart.new(year: 1981, month: 8, day: 7, hour: 22, min: 0, gender: :male)
32
+
33
+ # 四柱: [天干index, 地支index] のペア(甲=0〜癸=9、子=0〜亥=11)
34
+ chart.year_pillar # => [7, 9] 辛酉
35
+ chart.month_pillar # => [1, 7] 乙未
36
+ chart.day_pillar # => [3, 5] 丁巳
37
+ chart.hour_pillar # => [7, 11] 辛亥
38
+
39
+ # 通変星: 日干から見た各柱の天干との関係(10種)
40
+ chart.year_tsuhensei # => :偏財
41
+ chart.month_tsuhensei # => :偏印
42
+ chart.hour_tsuhensei # => :偏財
43
+
44
+ # 十二運: 日干から見た各柱の地支の運勢ステージ(12種)
45
+ chart.year_juniun # => :長生
46
+ chart.month_juniun # => :冠帯
47
+ chart.day_juniun # => :帝旺
48
+ chart.hour_juniun # => :胎
49
+ ```
50
+
51
+ indexから干支の名前に変換するには定数を使います:
52
+
53
+ ```ruby
54
+ stem, branch = chart.year_pillar
55
+ Meiri::Mannenreki::Constants::TENKAN[stem] # => :辛
56
+ Meiri::Mannenreki::Constants::TISHI[branch] # => :酉
57
+ ```
58
+
59
+ 時刻がわからない場合は `hour` / `min` を省略できます(正午として扱います)。
60
+
61
+ ## 計算の仕組み
62
+
63
+ 四柱推命では月の切り替わりが暦の月ではなく「節入り」(太陽黄経が15°刻みの特定の角度に達する瞬間)で決まります。meiriはこの節入り時刻を、
64
+
65
+ 1. Meeus『Astronomical Algorithms』のVSOP87切り詰め級数で太陽の視黄経を計算し
66
+ 2. ΔT(地球時と世界時の差)をNASAの多項式近似で補正し
67
+ 3. 目標の黄経に達する時刻を二分法で探索する
68
+
69
+ という手順でその場で求めています。対応範囲は1900〜2050年です。
70
+
71
+ - 年柱: 立春を境界として `(西暦 - 4) % 60`
72
+ - 月柱: 節入りで月支を決定し、五虎遁で月干を導出
73
+ - 日柱: ユリウス日から `(JD + 49) % 60`
74
+ - 時柱: 時支と五子遁による時干
75
+
76
+ ## 今後の予定
77
+
78
+ - 蔵干
79
+ - 干合・支合・支冲
80
+ - 大運・歳運
81
+ - 流派差のアダプタ対応
82
+
83
+ ## 開発
84
+
85
+ ```
86
+ $ bundle install
87
+ $ bundle exec rspec # テスト
88
+ $ bundle exec rubocop # コードチェック
89
+ ```
90
+
91
+ ## ライセンス
92
+
93
+ [MIT License](LICENSE.txt)
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ikotome/meiri.
@@ -0,0 +1,88 @@
1
+ require 'date'
2
+ require_relative 'solar_longitude'
3
+ module Meiri
4
+ module Mannenreki
5
+ module Calendar
6
+ # 十二節(節気のみ、中気は含まない)
7
+ # 各要素: [太陽黄経(度), 探索ヒント月, 探索ヒント日]
8
+ # - 黄経: find_sekki に渡す目標値
9
+ # - ヒント月日: find_sekki の探索起点(概略日付、±数日ずれてよい)
10
+ #
11
+ # 配列の index がそのまま「節 index」:
12
+ # 月支 index = (節index + 1) % 12
13
+ # 小寒(0)→丑(1), 立春(1)→寅(2), 啓蟄(2)→卯(3), ...
14
+ # 立冬(10)→亥(11), 大雪(11)→子(0) ← 12月だけ折り返す
15
+ SETSU = [
16
+ [285, 1, 5], # 小寒 → 丑
17
+ [315, 2, 4], # 立春 → 寅
18
+ [345, 3, 6], # 啓蟄 → 卯
19
+ [ 15, 4, 5], # 清明 → 辰
20
+ [ 45, 5, 6], # 立夏 → 巳
21
+ [ 75, 6, 6], # 芒種 → 午
22
+ [105, 7, 7], # 小暑 → 未
23
+ [135, 8, 8], # 立秋 → 申
24
+ [165, 9, 8], # 白露 → 酉
25
+ [195, 10, 8], # 寒露 → 戌
26
+ [225, 11, 7], # 立冬 → 亥
27
+ [255, 12, 7], # 大雪 → 子
28
+ ].map(&:freeze).freeze
29
+
30
+ # エラーメッセージ・デバッグ表示用
31
+ SETSU_NAMES = %w[小寒 立春 啓蟄 清明 立夏 芒種 小暑 立秋 白露 寒露 立冬 大雪].freeze
32
+
33
+ def self.pillar(y, m, d, h=12, min=0)
34
+ {
35
+ year: year_pillar(y, m, d, h, min),
36
+ month: month_pillar(y, m, d, h, min),
37
+ day: day_pillar(y, m, d, h, min),
38
+ hour: hour_pillar(y, m, d, h, min)
39
+ }
40
+ end
41
+
42
+ def self.hour_pillar(y, m, d, h, min)
43
+ day_stem = day_pillar(y, m, d, h, min)[0] # 日干index
44
+ tishi_index = ((h + 1) / 2) % 12 # 時支index = (時刻 + 1) / 2 を 12で割った余り
45
+ tenkan_index = (day_stem * 2 + tishi_index) % 10 # 時干index = (日干 * 2 + 時支index) を 10で割った余り
46
+ [tenkan_index, tishi_index]
47
+ end
48
+
49
+ def self.day_pillar(y, m, d, h, min)
50
+ jd = Date.new(y, m, d).jd # ユリウス日。紀元前4713年1月1日からの通し日数で、日付を整数1つで表せる。
51
+ idx60 = (jd + 49) % 60 # 60干支のindex。紀元前2637年2月15日を0とする。
52
+ [idx60 % 10, idx60 % 12] # 天干index, 地支index
53
+ end
54
+
55
+ def self.month_pillar(y, m, d, h, min)
56
+ birth = DateTime.new(y, m, d, h, min, 0)
57
+ setsu_index = nil
58
+
59
+ # 大雪 -> 小雪の順に遡って、birth より前の節気を探す。見つかったら setsu_index にその index を入れる。
60
+ SETSU.each_with_index.to_a.reverse.each do |(lon, sm, sd), idx|
61
+ sekki = SolarLongitude.find_sekki(lon, y, sm, sd)
62
+ if birth >= sekki # 節気前なら前の節気を使う
63
+ setsu_index = idx
64
+ break
65
+ end
66
+ end
67
+ setsu_index = 11 if setsu_index.nil? # 立春以降なら最後の節気を使う
68
+
69
+ branch = (setsu_index + 1) % 12 # 月支index = (節index + 1) % 12
70
+ year_stem = year_pillar(y, m, d, h, min)[0]
71
+ month_from_tora = (branch - 2) % 12
72
+ stem = (year_stem * 2 + 2 + month_from_tora) % 10
73
+ [stem, branch]
74
+ end
75
+
76
+
77
+ def self.year_pillar(y, m, d, h, min)
78
+ if DateTime.new(y, m, d, h, min, 0) < SolarLongitude.find_sekki(315, y, 2, 4) # 立春前なら前年の干支を使う
79
+ idx60 = (y - 1 - 4) % 60
80
+ else
81
+ idx60 = (y - 4) % 60
82
+ end
83
+ [idx60 % 10, idx60 % 12] # 天干index, 地支index
84
+ end
85
+
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,23 @@
1
+ module Meiri
2
+ module Mannenreki
3
+ module Constants
4
+ TENKAN = [:甲, :乙, :丙, :丁, :戊, :己, :庚, :辛, :壬, :癸]
5
+ TISHI = [:子, :丑, :寅, :卯, :辰, :巳, :午, :未, :申, :酉, :戌, :亥]
6
+
7
+ # 地支index → 5行index(0=木, 1=火, 2=土, 3=金, 4=水)
8
+ # 子丑寅卯辰巳午未申酉戌亥
9
+ # 水土木木土火火土金金土水
10
+ TISHI_GOGYOU = [4,2,0,0,2,1,1,2,3,3,2,4]
11
+
12
+ # 地支index → 天干index (本気のみ)
13
+ # 子丑寅卯辰巳午未申酉戌亥
14
+ # 壬己甲乙戊丁丙己庚辛戊甲
15
+ ZOKAN = [9,5,0,1,4,2,3,5,6,7,4,8]
16
+
17
+ # 天干index → 長生となる地支index
18
+ # 甲乙丙丁戊己庚辛壬癸
19
+ # 亥午寅酉寅酉申子巳卯
20
+ CHOSEI_TISHI = [11,6,2,9,2,9,5,0,8,3]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,266 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # 太陽の見かけの黄経 (Meeus "Astronomical Algorithms" Ch.25 高精度版)
5
+ # VSOP87を切り詰めた地球の日心座標展開を利用
6
+
7
+ require 'date'
8
+
9
+ module Meiri
10
+ module Mannenreki
11
+ module SolarLongitude
12
+ # Meeus付録IIIより、地球のVSOP87展開 (係数は [A, B, C] の順)
13
+ # L = A * cos(B + C * tau)
14
+
15
+ L0 = [
16
+ [175_347_046.0, 0.0, 0.0],
17
+ [ 3_341_656.0, 4.6692568, 6283.07585],
18
+ [ 34_894.0, 4.6261, 12566.1517],
19
+ [ 3_497.0, 2.7441, 5753.3849],
20
+ [ 3_418.0, 2.8289, 3.5231],
21
+ [ 3_136.0, 3.6277, 77713.7715],
22
+ [ 2_676.0, 4.4181, 7860.4194],
23
+ [ 2_343.0, 6.1352, 3930.2097],
24
+ [ 1_324.0, 0.7425, 11506.7698],
25
+ [ 1_273.0, 2.0371, 529.6910],
26
+ [ 1_199.0, 1.1096, 1577.3435],
27
+ [ 990.0, 5.233, 5884.927],
28
+ [ 902.0, 2.045, 26.298],
29
+ [ 857.0, 3.508, 398.149],
30
+ [ 780.0, 1.179, 5223.694],
31
+ [ 753.0, 2.533, 5507.553],
32
+ [ 505.0, 4.583, 18849.228],
33
+ [ 492.0, 4.205, 775.523],
34
+ [ 357.0, 2.920, 0.067],
35
+ [ 317.0, 5.849, 11790.629],
36
+ [ 284.0, 1.899, 796.298],
37
+ [ 271.0, 0.315, 10977.079],
38
+ [ 243.0, 0.345, 5486.778],
39
+ [ 206.0, 4.806, 2544.314],
40
+ [ 205.0, 1.869, 5573.143],
41
+ [ 202.0, 2.458, 6069.777],
42
+ [ 156.0, 0.833, 213.299],
43
+ [ 132.0, 3.411, 2942.463],
44
+ [ 126.0, 1.083, 20.775],
45
+ [ 115.0, 0.645, 0.980],
46
+ [ 103.0, 0.636, 4694.003],
47
+ [ 102.0, 0.976, 15720.839],
48
+ [ 102.0, 4.267, 7.114],
49
+ [ 99.0, 6.21, 2146.17],
50
+ [ 98.0, 0.68, 155.42],
51
+ [ 86.0, 5.98, 161000.69],
52
+ [ 85.0, 1.30, 6275.96],
53
+ [ 85.0, 3.67, 71430.70],
54
+ [ 80.0, 1.81, 17260.15],
55
+ [ 79.0, 3.04, 12036.46],
56
+ [ 75.0, 1.76, 5088.63],
57
+ [ 74.0, 3.50, 3154.69],
58
+ [ 74.0, 4.68, 801.82],
59
+ [ 70.0, 0.83, 9437.76],
60
+ [ 62.0, 3.98, 8827.39],
61
+ [ 61.0, 1.82, 7084.90],
62
+ [ 57.0, 2.78, 6286.60],
63
+ [ 56.0, 4.39, 14143.50],
64
+ [ 56.0, 3.47, 6279.55],
65
+ [ 52.0, 0.19, 12139.55],
66
+ [ 52.0, 1.33, 1748.02],
67
+ [ 51.0, 0.28, 5856.48],
68
+ [ 49.0, 0.49, 1194.45],
69
+ [ 41.0, 5.37, 8429.24],
70
+ [ 41.0, 2.40, 19651.05],
71
+ [ 39.0, 6.17, 10447.39],
72
+ [ 37.0, 6.04, 10213.29],
73
+ [ 37.0, 2.57, 1059.38],
74
+ [ 36.0, 1.71, 2352.87],
75
+ [ 36.0, 1.78, 6812.77],
76
+ [ 33.0, 0.59, 17789.85],
77
+ [ 30.0, 0.44, 83996.85],
78
+ [ 30.0, 2.74, 1349.87],
79
+ [ 25.0, 3.16, 4690.48]
80
+ ].freeze
81
+
82
+ L1 = [
83
+ [628_331_966_747.0, 0.0, 0.0],
84
+ [ 206_059.0, 2.678235, 6283.07585],
85
+ [ 4_303.0, 2.6351, 12566.1517],
86
+ [ 425.0, 1.590, 3.523],
87
+ [ 119.0, 5.796, 26.298],
88
+ [ 109.0, 2.966, 1577.344],
89
+ [ 93.0, 2.59, 18849.23],
90
+ [ 72.0, 1.14, 529.69],
91
+ [ 68.0, 1.87, 398.15],
92
+ [ 67.0, 4.41, 5507.55],
93
+ [ 59.0, 2.89, 5223.69],
94
+ [ 56.0, 2.17, 155.42],
95
+ [ 45.0, 0.40, 796.30],
96
+ [ 36.0, 0.47, 775.52],
97
+ [ 29.0, 2.65, 7.11],
98
+ [ 21.0, 5.34, 0.98],
99
+ [ 19.0, 1.85, 5486.78],
100
+ [ 19.0, 4.97, 213.30],
101
+ [ 17.0, 2.99, 6275.96],
102
+ [ 16.0, 0.03, 2544.31],
103
+ [ 16.0, 1.43, 2146.17],
104
+ [ 15.0, 1.21, 10977.08],
105
+ [ 12.0, 2.83, 1748.02],
106
+ [ 12.0, 3.26, 5088.63],
107
+ [ 12.0, 5.27, 1194.45],
108
+ [ 12.0, 2.08, 4694.00],
109
+ [ 11.0, 0.77, 553.57],
110
+ [ 10.0, 1.30, 6286.60],
111
+ [ 10.0, 4.24, 1349.87],
112
+ [ 9.0, 2.70, 242.73],
113
+ [ 9.0, 5.64, 951.72],
114
+ [ 8.0, 5.30, 2352.87],
115
+ [ 6.0, 2.65, 9437.76],
116
+ [ 6.0, 4.67, 4690.48]
117
+ ].freeze
118
+
119
+ L2 = [
120
+ [52_919.0, 0.0, 0.0],
121
+ [ 8_720.0, 1.0721, 6283.0758],
122
+ [ 309.0, 0.867, 12566.152],
123
+ [ 27.0, 0.05, 3.52],
124
+ [ 16.0, 5.19, 26.30],
125
+ [ 16.0, 3.68, 155.42],
126
+ [ 10.0, 0.76, 18849.23],
127
+ [ 9.0, 2.06, 77713.77],
128
+ [ 7.0, 0.83, 775.52],
129
+ [ 5.0, 4.66, 1577.34],
130
+ [ 4.0, 1.03, 7.11],
131
+ [ 4.0, 3.44, 5573.14],
132
+ [ 3.0, 5.14, 796.30],
133
+ [ 3.0, 6.05, 5507.55],
134
+ [ 3.0, 1.19, 242.73],
135
+ [ 3.0, 6.12, 529.69],
136
+ [ 3.0, 0.31, 398.15],
137
+ [ 3.0, 2.28, 553.57],
138
+ [ 2.0, 4.38, 5223.69],
139
+ [ 2.0, 3.75, 0.98]
140
+ ].freeze
141
+
142
+ L3 = [
143
+ [289.0, 5.844, 6283.076],
144
+ [ 35.0, 0.0, 0.0],
145
+ [ 17.0, 5.49, 12566.15],
146
+ [ 3.0, 5.20, 155.42],
147
+ [ 1.0, 4.72, 3.52],
148
+ [ 1.0, 5.30, 18849.23],
149
+ [ 1.0, 5.97, 242.73]
150
+ ].freeze
151
+
152
+ L4 = [
153
+ [114.0, 3.142, 0.0],
154
+ [ 8.0, 4.13, 6283.08],
155
+ [ 1.0, 3.84, 12566.15]
156
+ ].freeze
157
+
158
+ L5 = [
159
+ [1.0, 3.14, 0.0]
160
+ ].freeze
161
+
162
+ # 系列の合計を計算 (係数 × cos(B + C*τ))
163
+ def self.sum_series(series, tau)
164
+ series.sum { |a, b, c| a * Math.cos(b + c * tau) }
165
+ end
166
+
167
+ # ΔT (TT - UT) 秒単位の近似
168
+ # NASAの多項式 (1900-2050範囲で使える簡易版)
169
+ def self.delta_t(year, month)
170
+ y = year + (month - 0.5) / 12.0
171
+
172
+ case y
173
+ when 1900...1920
174
+ t = y - 1900
175
+ -2.79 + 1.494119 * t - 0.0598939 * t**2 + 0.0061966 * t**3 - 0.000197 * t**4
176
+ when 1920...1941
177
+ t = y - 1920
178
+ 21.20 + 0.84493 * t - 0.076100 * t**2 + 0.0020936 * t**3
179
+ when 1941...1961
180
+ t = y - 1950
181
+ 29.07 + 0.407 * t - t**2 / 233.0 + t**3 / 2547.0
182
+ when 1961...1986
183
+ t = y - 1975
184
+ 45.45 + 1.067 * t - t**2 / 260.0 - t**3 / 718.0
185
+ when 1986...2005
186
+ t = y - 2000
187
+ 63.86 + 0.3345 * t - 0.060374 * t**2 + 0.0017275 * t**3 + 0.000651814 * t**4 + 0.00002373599 * t**5
188
+ when 2005...2050
189
+ t = y - 2000
190
+ 62.92 + 0.32217 * t + 0.005589 * t**2
191
+ else
192
+ # 範囲外はざっくり
193
+ 62.0
194
+ end
195
+ end
196
+
197
+ # 太陽の見かけの黄経 (度)
198
+ # dt_ut: DateTime (UT扱い)
199
+ def self.apparent(dt_ut)
200
+ # ΔT補正: UT → TT
201
+ dt_sec = delta_t(dt_ut.year, dt_ut.month)
202
+ jde = dt_ut.ajd.to_f + dt_sec / 86_400.0
203
+
204
+ tau = (jde - 2_451_545.0) / 365_250.0
205
+
206
+ # 地球の日心黄経 (rad)
207
+ l_sum = sum_series(L0, tau) +
208
+ sum_series(L1, tau) * tau +
209
+ sum_series(L2, tau) * tau**2 +
210
+ sum_series(L3, tau) * tau**3 +
211
+ sum_series(L4, tau) * tau**4 +
212
+ sum_series(L5, tau) * tau**5
213
+ l_earth = (l_sum / 1e8) * 180 / Math::PI # rad→deg
214
+ l_earth %= 360
215
+
216
+ # 太陽の地心黄経 = 地球の日心黄経 + 180°
217
+ theta = (l_earth + 180) % 360
218
+
219
+ # FK5補正 (Meeus 25.9): 世紀数
220
+ t_c = (jde - 2_451_545.0) / 36_525.0
221
+ delta_theta = -0.09033 / 3600.0 # 秒 → 度
222
+ # 追加のFK5補正項は緯度に効くもの、黄経への影響は上記で十分
223
+ theta_fk5 = theta + delta_theta
224
+
225
+ # 章動 (Δψ) 主要項のみ (Meeus 22.1 の簡略版)
226
+ omega = 125.04452 - 1934.136261 * t_c
227
+ omega_rad = omega * Math::PI / 180
228
+ l_moon = (218.3165 + 481_267.8813 * t_c) * Math::PI / 180
229
+ l_sun_geo = (280.4665 + 36_000.7698 * t_c) * Math::PI / 180
230
+ delta_psi = -17.20 * Math.sin(omega_rad) -
231
+ 1.32 * Math.sin(2 * l_sun_geo) -
232
+ 0.23 * Math.sin(2 * l_moon) +
233
+ 0.21 * Math.sin(2 * omega_rad)
234
+ delta_psi_deg = delta_psi / 3600.0 # 秒 → 度
235
+
236
+ # 光行差 (簡易版: R≈1で近似)
237
+ aberration = -20.4898 / 3600.0 # 秒 → 度
238
+
239
+ (theta_fk5 + delta_psi_deg + aberration) % 360
240
+ end
241
+
242
+ # 二分法で節入り時刻を探す
243
+ JST = Rational(9, 24)
244
+
245
+ def self.find_sekki(target_deg, year, hint_month, hint_day)
246
+ hint = DateTime.new(year, hint_month, hint_day, 12, 0, 0)
247
+ low = hint - 3
248
+ high = hint + 3
249
+
250
+ 70.times do
251
+ mid = low + (high - low) / 2
252
+ lon = SolarLongitude.apparent(mid - JST)
253
+ diff = ((lon - target_deg + 540) % 360) - 180
254
+ if diff < 0
255
+ low = mid
256
+ else
257
+ high = mid
258
+ end
259
+ break if (high - low).to_f * 86_400 < 0.5
260
+ end
261
+
262
+ low + (high - low) / 2
263
+ end
264
+ end
265
+ end
266
+ end
@@ -0,0 +1,53 @@
1
+ require 'meiri/mannenreki/calendar'
2
+ require 'meiri/shityu_suimei/tsuhensei'
3
+ require 'meiri/shityu_suimei/juniun'
4
+
5
+ module Meiri
6
+ module ShityuSuimei
7
+ # 命式: 生年月日時から四柱(年柱・月柱・日柱・時柱)を求め、
8
+ # そこから通変星・十二運を導き出す。四柱推命の中心となるクラス。
9
+ #
10
+ # chart = Chart.new(year: 2024, month: 2, day: 4, gender: :male)
11
+ # chart.year_pillar # => [9, 3] (癸卯)
12
+ # chart.year_tsuhensei # => :正財
13
+ # chart.day_juniun # => :墓
14
+ class Chart
15
+ def initialize(year:, month:, day:, hour: 12, min: 0, gender:)
16
+ @gender = gender
17
+ @pillars = Mannenreki::Calendar.pillar(year, month, day, hour, min)
18
+ end
19
+
20
+ # 各柱は [天干index, 地支index] のペア
21
+ # (天干: 甲=0〜癸=9、地支: 子=0〜亥=11)
22
+ def year_pillar = @pillars[:year]
23
+ def month_pillar = @pillars[:month]
24
+ def day_pillar = @pillars[:day]
25
+ def hour_pillar = @pillars[:hour]
26
+
27
+ # 通変星: 日干から見た各柱の「天干」との関係(10種)
28
+ # 日柱は日干自身との比較になるので出さない
29
+ def year_tsuhensei = tsuhensei_for(year_pillar)
30
+ def month_tsuhensei = tsuhensei_for(month_pillar)
31
+ def hour_tsuhensei = tsuhensei_for(hour_pillar)
32
+
33
+ # 十二運: 日干から見た各柱の「地支」の運勢ステージ(12種)
34
+ # こちらは地支が対象なので日柱も含めて4つ出せる
35
+ def year_juniun = juniun_for(year_pillar)
36
+ def month_juniun = juniun_for(month_pillar)
37
+ def day_juniun = juniun_for(day_pillar)
38
+ def hour_juniun = juniun_for(hour_pillar)
39
+
40
+ private
41
+
42
+ # pillar[0] = 天干index を日干と比較する
43
+ def tsuhensei_for(pillar)
44
+ Tsuhensei.of(day_stem_index: day_pillar[0], target_stem_index: pillar[0])
45
+ end
46
+
47
+ # pillar[1] = 地支index を日干と比較する
48
+ def juniun_for(pillar)
49
+ Juniun.of(day_stem_index: day_pillar[0], target_branch_index: pillar[1])
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,17 @@
1
+ require 'meiri/mannenreki/constants'
2
+ module Meiri
3
+ module ShityuSuimei
4
+ # 十二運: 日干から見た地支の運勢ステージ12種
5
+ module Juniun
6
+ NAMES = [:長生, :沐浴, :冠帯, :建禄, :帝旺, :衰,
7
+ :病, :死, :墓, :絶, :胎, :養].freeze
8
+
9
+ def self.of(day_stem_index:, target_branch_index:)
10
+ start = Mannenreki::Constants::CHOSEI_TISHI[day_stem_index]
11
+ # 陽干 (偶数) は順行、陰干 (奇数) は逆行
12
+ step = day_stem_index.even? ? (target_branch_index - start) : (start - target_branch_index)
13
+ NAMES[step % 12]
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,34 @@
1
+ module Meiri
2
+ module ShityuSuimei
3
+ # 通変星: 日干から見た対象天干の関係を10種に分類する
4
+ #
5
+ # 五行のdiff (0..4) と 陰陽一致(bool) の組み合わせで
6
+ # 10種類の通変星が一意に決まる
7
+ module Tsuhensei
8
+ # 五行diffごとの [同陰陽の通変星, 異陰陽の通変星]
9
+ # diff=0: 同元素
10
+ # diff=1: 我生 (日干がtargetを生む)
11
+ # diff=2: 我剋 (日干がtargetを剋す)
12
+ # diff=3: 剋我 (targetが日干を剋す)
13
+ # diff=4: 生我 (targetが日干を生む)
14
+ NAMES = [
15
+ [:比肩, :劫財], # diff=0
16
+ [:食神, :傷官], # diff=1
17
+ [:偏財, :正財], # diff=2
18
+ [:偏官, :正官], # diff=3
19
+ [:偏印, :印綬], # diff=4
20
+ ].freeze
21
+
22
+ def self.of(day_stem_index:, target_stem_index:)
23
+ day_element = day_stem_index / 2
24
+ target_element = target_stem_index / 2
25
+ diff = (target_element - day_element) % 5
26
+ # 陰陽の一致: index の偶奇が同じなら同陰陽
27
+ same_yinyang = day_stem_index.even? == target_stem_index.even?
28
+
29
+ NAMES[diff][same_yinyang ? 0 : 1]
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Meiri
2
+ VERSION = "0.1.0"
3
+ end
data/lib/meiri.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "meiri/version"
2
+ require "meiri/shityu_suimei/chart"
3
+
4
+ module Meiri
5
+ class Error < StandardError; end
6
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: meiri
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ikotome
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: bundler
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '1.17'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '1.17'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '3'
32
+ - !ruby/object:Gem::Dependency
33
+ name: rake
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - "~>"
37
+ - !ruby/object:Gem::Version
38
+ version: '13.0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - "~>"
44
+ - !ruby/object:Gem::Version
45
+ version: '13.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '3.0'
53
+ type: :development
54
+ prerelease: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '3.0'
60
+ description: A Ruby library for Shichu Suimei (Four Pillars of Destiny). Calculates
61
+ the four pillars, tsuhensei (ten gods) and juniun (twelve stages) from a birth date.
62
+ Sekki (solar term) times are computed internally from solar longitude using a truncated
63
+ VSOP87 series, so no external ephemeris data is required.
64
+ email:
65
+ - kotomizu.0728@gmail.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - LICENSE.txt
71
+ - README.md
72
+ - lib/meiri.rb
73
+ - lib/meiri/mannenreki/calendar.rb
74
+ - lib/meiri/mannenreki/constants.rb
75
+ - lib/meiri/mannenreki/solar_longitude.rb
76
+ - lib/meiri/shityu_suimei/chart.rb
77
+ - lib/meiri/shityu_suimei/juniun.rb
78
+ - lib/meiri/shityu_suimei/tsuhensei.rb
79
+ - lib/meiri/version.rb
80
+ homepage: https://github.com/ikotome/meiri
81
+ licenses:
82
+ - MIT
83
+ metadata:
84
+ homepage_uri: https://github.com/ikotome/meiri
85
+ source_code_uri: https://github.com/ikotome/meiri
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '3.4'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 4.0.10
101
+ specification_version: 4
102
+ summary: Four Pillars of Destiny (四柱推命) calculator
103
+ test_files: []