Files
Jyotisha/scripts/arudha_reference/pvr_dual_lord.py
T
ClaudeandClaude Opus 5.5 093da4468d docs: renumber this branch's records BUG-1613..1617 -> BUG-1622..1626
Another session shipped a different BUG-1613 (loading curtain) to staging.
Mapping in order: 1613->1622, 1614->1623, 1615->1624, 1616->1625,
1617->1626, in BUG_HISTORY headings and bodies, CHANGELOG, ledger, research
notes, code comments, rule text, test notes and the holdout contract text.
Text only. shadbala.py and the two replay scripts are in the frozen identity,
so an ERR-110 re-freeze follows in the next commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXoyeZrYn6ryNcSe5Y2Y
2026-11-05 15:32:05 +08:00

279 lines
12 KiB
Python

#!/usr/bin/env python3
"""PVR Arudha dual-lord selection for Scorpio (Mars / Ketu) and Aquarius (Saturn / Rahu).
Opt-in reference only (#166 Q28, astrologer reply 2026-11-05; BUG-1626).
Label: "PVR 双宫主比较(参考,未进默认)". The default Arudha path
(`jaimini.calc_arudha_padas`, fixed lords Mars / Saturn), the chart page, the
consultation card and rectification scoring do NOT use this module. Moving it
into scoring needs its own acceptance (astrologer: "进入校正打分仍需独立验收").
Source: P.V.R. Narasimha Rao, *Vedic Astrology: An Integrated Approach*,
§15.5.1, print pp. 201-203 (PDF pp. 213-215). The first level that decides
stops the comparison:
1. Only one lord is in the sign in question (the dual-lord sign itself):
take the other one.
2. More companions: planets in the candidate's sign, the candidate itself not
counted. The Lagna is not a companion; Rahu / Ketu count (nine grahas).
3. More support from the three roles Jupiter, Mercury and the candidate's
dispositor (lord of the sign it occupies), each by conjunction or rasi
drishti on the candidate's sign. One planet filling two roles counts twice
(the book has an example).
4. Exalted beats not exalted.
5. Sign of the candidate: dual > fixed > movable.
6. Higher degree within the sign; Rahu / Ketu as 30 - degree.
Engineering choices (not book text; listed in every result):
- a full tie, or a dispositor that is itself a Scorpio / Aquarius dual lord
whose resolution loops back, returns both candidates with a blocked reason
instead of forcing a pick;
- a dispositor role filled by the candidate itself (e.g. Mars in Aries) is
counted, as in jaimini._co_lord_strength (pending astrologer review);
- Rahu exalted in Taurus, Ketu in Scorpio (other schools: Gemini /
Sagittarius; pending).
"""
from __future__ import annotations
import os
import sys
from collections.abc import Iterable
# scripts/ on sys.path for the sibling jaimini module (same convention as scripts/chart_dasha).
_scripts_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if _scripts_dir not in sys.path:
sys.path.insert(0, _scripts_dir)
import jaimini # noqa: E402 (read-only use; jaimini.py stays the default path)
SIGNS = jaimini.SIGNS
SIGN_LORDS = jaimini.SIGN_LORDS
METHOD = "pvr_integrated_approach_15_5_1_arudha_dual_lord"
LABEL_ZH = "PVR 双宫主比较(参考,未进默认)"
LABEL_EN = "PVR dual-lord comparison (reference, not the default)"
SOURCE = (
"P.V.R. Narasimha Rao, Vedic Astrology: An Integrated Approach, §15.5.1, "
"print pp. 201-203 (PDF pp. 213-215); astrologer review #166, 2026-11-05"
)
ENGINEERING_CHOICES = (
"full_tie_or_unresolved_dispositor_recursion_returns_both_candidates_blocked",
"dispositor_role_filled_by_the_candidate_itself_is_counted_pending_review",
"node_exaltation_rahu_taurus_ketu_scorpio_pending_review",
"lagna_is_not_a_companion_nine_grahas_counted",
)
DUAL_LORD_SIGNS: dict[int, tuple[str, str]] = {7: ("Mars", "Ketu"), 10: ("Saturn", "Rahu")}
GRAHAS = ("Sun", "Moon", "Mars", "Mercury", "Jupiter", "Venus", "Saturn", "Rahu", "Ketu")
NODES = frozenset({"Rahu", "Ketu"})
EXALTATION_SIGN = {
"Sun": 0, "Moon": 1, "Mars": 9, "Mercury": 5, "Jupiter": 3,
"Venus": 11, "Saturn": 6, "Rahu": 1, "Ketu": 7,
}
LEVEL_RULES = {
1: "only_one_lord_in_the_sign_take_the_other",
2: "more_companions",
3: "more_jupiter_mercury_dispositor_support",
4: "exalted",
5: "sign_nature_dual_fixed_movable",
6: "higher_degree_nodes_reversed",
}
def _sign(longitudes: dict[str, float], planet: str) -> int:
return int(float(longitudes[planet]) % 360.0 // 30)
def _companions(longitudes: dict[str, float], planet: str) -> int:
sign = _sign(longitudes, planet)
return sum(1 for other in GRAHAS if other != planet and other in longitudes and _sign(longitudes, other) == sign)
def _role_hits(longitudes: dict[str, float], role_planet: str, target_sign: int) -> bool:
if role_planet not in longitudes:
return False
role_sign = _sign(longitudes, role_planet)
return role_sign == target_sign or jaimini._rasi_drishti(role_sign, target_sign)
def _support_options(longitudes: dict[str, float], planet: str) -> dict[str, int]:
"""Support count for each possible dispositor (one entry unless the planet sits in Scorpio / Aquarius)."""
sign = _sign(longitudes, planet)
base = sum(_role_hits(longitudes, role, sign) for role in ("Jupiter", "Mercury"))
dispositors = DUAL_LORD_SIGNS.get(sign, (SIGN_LORDS[SIGNS[sign]],))
return {lord: base + int(_role_hits(longitudes, lord, sign)) for lord in dispositors}
def _degree_key(longitudes: dict[str, float], planet: str) -> float:
degree = float(longitudes[planet]) % 30.0
return round(30.0 - degree if planet in NODES else degree, 6)
def _modality(sign: int) -> int:
return {0: 0, 1: 1, 2: 2}[sign % 3] # movable 0 < fixed 1 < dual 2
def _compare(a: float, b: float) -> int:
return (a > b) - (a < b)
def _blocked(result: dict, reason: str) -> dict:
result.update(status="blocked", winner=None, deciding_level=None, deciding_rule=None, blocked_reason=reason)
return result
def select_pvr_arudha_lord(sign_idx: int, longitudes: dict[str, float],
_visiting: Iterable[int] | None = None) -> dict:
"""Stronger lord of Scorpio (7) or Aquarius (10) for Arudha, PVR §15.5.1.
``longitudes``: sidereal longitudes (0-360) by planet name; keys other than
the nine grahas (e.g. "Lagna", "Ascendant") are ignored. Returns the winner
or a blocked result; both candidates and a level-by-level trace always.
"""
if sign_idx not in DUAL_LORD_SIGNS:
raise ValueError(f"sign {sign_idx} has a single lord; PVR dual-lord selection covers Scorpio (7) and Aquarius (10)")
visiting = set(_visiting or ()) | {sign_idx}
a, b = DUAL_LORD_SIGNS[sign_idx]
result: dict = {
"method": METHOD,
"label": LABEL_ZH,
"label_en": LABEL_EN,
"sign": SIGNS[sign_idx],
"sign_idx": sign_idx,
"candidates": [a, b],
"default_lord": SIGN_LORDS[SIGNS[sign_idx]],
"in_default_paths": False,
"status": None,
"winner": None,
"deciding_level": None,
"deciding_rule": None,
"blocked_reason": None,
"trace": [],
"source": SOURCE,
"engineering_choices": list(ENGINEERING_CHOICES),
}
missing = [p for p in (a, b) if p not in longitudes]
if missing:
return _blocked(result, "candidate_longitude_missing:" + ",".join(missing))
def decide(level: int, values: dict, order: int, notes: list | None = None) -> dict | None:
step = {"level": level, "rule": LEVEL_RULES[level], "values": values, "decided": order != 0}
if notes:
step["notes"] = notes
result["trace"].append(step)
if order == 0:
return None
result.update(status="decided", winner=a if order > 0 else b,
deciding_level=level, deciding_rule=LEVEL_RULES[level])
return result
# 1. Only one lord in the sign itself -> the other one.
in_sign = {p: _sign(longitudes, p) == sign_idx for p in (a, b)}
order = 0
if in_sign[a] != in_sign[b]:
order = -1 if in_sign[a] else 1
if decide(1, {p: in_sign[p] for p in (a, b)}, order):
return result
# 2. Companions (candidate itself, Lagna and other non-graha keys not counted).
companions = {p: _companions(longitudes, p) for p in (a, b)}
if decide(2, companions, _compare(companions[a], companions[b])):
return result
# 3. Jupiter / Mercury / dispositor support; a planet in two roles counts twice.
options = {p: _support_options(longitudes, p) for p in (a, b)}
notes: list = []
if _sign(longitudes, a) == _sign(longitudes, b):
# Same sign: same dispositor and the same role hits, whoever the dispositor is.
outcomes = {0}
else:
outcomes = {_compare(sa, sb) for sa in options[a].values() for sb in options[b].values()}
support: dict = {}
if len(outcomes) == 1:
for p in (a, b):
support[p] = next(iter(options[p].values())) if len(set(options[p].values())) == 1 else sorted(set(options[p].values()))
if len(options[p]) > 1:
notes.append(f"{p}: dispositor is a dual lord ({'/'.join(options[p])}); the outcome does not depend on it")
order = outcomes.pop()
else:
for p in (a, b):
if len(set(options[p].values())) == 1:
support[p] = next(iter(options[p].values()))
continue
dispositor_sign = _sign(longitudes, p)
if dispositor_sign in visiting:
result["trace"].append({"level": 3, "rule": LEVEL_RULES[3], "values": options, "decided": False,
"notes": [f"{p}: the lord of {SIGNS[dispositor_sign]} is already being compared (loop)"]})
return _blocked(result, "dispositor_dual_lord_recursion_unresolved")
nested = select_pvr_arudha_lord(dispositor_sign, longitudes, visiting)
if nested["status"] != "decided":
result["trace"].append({"level": 3, "rule": LEVEL_RULES[3], "values": options, "decided": False,
"notes": [f"{p}: dispositor of {SIGNS[dispositor_sign]} unresolved ({nested['blocked_reason']})"],
"nested": nested["trace"]})
return _blocked(result, "dispositor_dual_lord_recursion_unresolved")
support[p] = options[p][nested["winner"]]
notes.append(f"{p}: dispositor of {SIGNS[dispositor_sign]} resolved to {nested['winner']} (level {nested['deciding_level']})")
order = _compare(support[a], support[b])
if decide(3, support, order, notes):
return result
# 4. Exalted.
exalted = {p: EXALTATION_SIGN.get(p) == _sign(longitudes, p) for p in (a, b)}
if decide(4, exalted, _compare(int(exalted[a]), int(exalted[b]))):
return result
# 5. Sign nature: dual > fixed > movable.
nature = {p: ("movable", "fixed", "dual")[_modality(_sign(longitudes, p))] for p in (a, b)}
if decide(5, nature, _compare(_modality(_sign(longitudes, a)), _modality(_sign(longitudes, b)))):
return result
# 6. Degree within the sign; nodes reversed.
degrees = {p: _degree_key(longitudes, p) for p in (a, b)}
if decide(6, degrees, _compare(degrees[a], degrees[b])):
return result
return _blocked(result, "full_tie")
def pvr_arudha_reference(asc_sign_idx: int, longitudes: dict[str, float]) -> dict:
"""Default (fixed-lord) Arudha next to the PVR dual-lord Arudha, for the houses on Scorpio / Aquarius.
The pada arithmetic is jaimini.calc_arudha_pada_for_house (the one Arudha
implementation, with the PVR §9.2 1st / 7th exception), fed the chosen
lord's longitude. Reference output only.
"""
rows = []
for house_num in range(1, 13):
sign_idx = (asc_sign_idx + house_num - 1) % 12
if sign_idx not in DUAL_LORD_SIGNS:
continue
default = jaimini.calc_arudha_pada_for_house(sign_idx, longitudes)
selection = select_pvr_arudha_lord(sign_idx, longitudes)
pvr_pada = None
if selection["status"] == "decided":
fixed_lord = SIGN_LORDS[SIGNS[sign_idx]]
pvr_pada = jaimini.calc_arudha_pada_for_house(sign_idx, {fixed_lord: longitudes[selection["winner"]]})
if pvr_pada:
pvr_pada = {**pvr_pada, "source_house_lord": selection["winner"]}
name = jaimini.ARUDHA_NAMES.get(house_num, f"A{house_num}")
rows.append({
"house_num": house_num,
"key": "UL" if house_num == 12 else f"A{house_num}",
"name": name,
"sign": SIGNS[sign_idx],
"default_lord": SIGN_LORDS[SIGNS[sign_idx]],
"default_pada_sign": default["sign"] if default else None,
"pvr_lord": selection["winner"],
"pvr_pada_sign": pvr_pada["sign"] if pvr_pada else None,
"differs_from_default": bool(pvr_pada and default and pvr_pada["sign"] != default["sign"]),
"selection": selection,
})
return {
"method": METHOD,
"label": LABEL_ZH,
"label_en": LABEL_EN,
"in_default_paths": False,
"note": "Reference only: default A1-A12 / AL / UL stay on the fixed lords (Mars, Saturn).",
"rows": rows,
"source": SOURCE,
}