elome 0.16.1__py3-none-any.whl

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.
elome/__init__.py ADDED
@@ -0,0 +1,90 @@
1
+ """Elómë — analytic, differentiable transit light curves on Apple
2
+ Silicon (MLX).
3
+
4
+ The name is Quenya: él "star" + lómë "dusk, dimness" -- a star dimmed,
5
+ which is what a transit is. Pronounced /ɛˈloː.mɛ/, "eh-LOH-meh", stressed
6
+ on the long ó, the final ë spoken.
7
+
8
+ Quadratic limb-darkened transits per Agol, Luger & Foreman-Mackey (2020),
9
+ with a batman-style frontend for everyday use:
10
+
11
+ import elome
12
+ params = elome.TransitParams()
13
+ ...
14
+ m = elome.TransitModel(params, t)
15
+ flux = m.light_curve(params)
16
+
17
+ and a float32-conditioned batched backend (``elome.anvil``) for
18
+ GPU sampling with the anvil MCMC engine — which is
19
+ an optional integration dependency; the core needs only mlx and numpy.
20
+ """
21
+
22
+ from .api import TransitModel, TransitParams
23
+ from .ellip import cel, cel3
24
+ from .flux import flux_dev, light_curve
25
+ from .greens import greens_transform_np, quad_g_coeffs, quad_norm
26
+ from .kepler import (kepler, kepler_E, kepler_E_sincos,
27
+ separation_keplerian)
28
+ from .anchored import anchor_constants, separation_anchored
29
+ from .poly import flux_dev_poly, sn_dev_poly
30
+ from .hybrid import (HYBRID2, HYBRID2_EPS, HYBRID4, HYBRID4_EPS,
31
+ HYBRID5, HybridLaw, LAWS as HYBRID_LAWS,
32
+ flux_dev_hybrid, hybrid_norms, shape_cols)
33
+ from .oblate import flux_dev_oblate, shape_cols_oblate
34
+ from .oblate_tau import contact_offsets_oblate
35
+ from .ld import (hybrid2_from_q, hybrid2_from_q_np, hybrid2_to_q_np,
36
+ q_from_simplex_np, q_to_u, q_to_u_np, simplex_from_q,
37
+ simplex_from_q_np, u_to_q_np)
38
+ from .metal import flux_dev_from_tau, flux_dev_metal, metal_available
39
+ from .solution import sn_dev, sn_dev_with_aux
40
+ from .vjp import flux_dev_analytic
41
+
42
+ __all__ = [
43
+ "TransitModel",
44
+ "TransitParams",
45
+ "cel",
46
+ "cel3",
47
+ "flux_dev",
48
+ "flux_dev_analytic",
49
+ "flux_dev_poly",
50
+ "sn_dev_poly",
51
+ "light_curve",
52
+ "flux_dev_from_tau",
53
+ "flux_dev_metal",
54
+ "metal_available",
55
+ "greens_transform_np",
56
+ "quad_g_coeffs",
57
+ "quad_norm",
58
+ "kepler",
59
+ "kepler_E",
60
+ "kepler_E_sincos",
61
+ "separation_keplerian",
62
+ "separation_anchored",
63
+ "anchor_constants",
64
+ "q_to_u",
65
+ "q_to_u_np",
66
+ "u_to_q_np",
67
+ "HybridLaw",
68
+ "HYBRID2",
69
+ "HYBRID2_EPS",
70
+ "HYBRID4_EPS",
71
+ "flux_dev_oblate",
72
+ "shape_cols_oblate",
73
+ "contact_offsets_oblate",
74
+ "HYBRID4",
75
+ "HYBRID5",
76
+ "HYBRID_LAWS",
77
+ "flux_dev_hybrid",
78
+ "hybrid_norms",
79
+ "shape_cols",
80
+ "hybrid2_from_q",
81
+ "hybrid2_from_q_np",
82
+ "hybrid2_to_q_np",
83
+ "simplex_from_q",
84
+ "simplex_from_q_np",
85
+ "q_from_simplex_np",
86
+ "sn_dev",
87
+ "sn_dev_with_aux",
88
+ ]
89
+
90
+ __version__ = "0.16.1"
elome/anchored.py ADDED
@@ -0,0 +1,273 @@
1
+ """Transit-anchored eccentric orbit: the fp32-safe low-eccentricity path.
2
+
3
+ The direct formulation (solve Kepler for E from M = phi + M_tra, then
4
+ rotate the orbital-plane Cartesians by w) is exact but badly conditioned
5
+ in float32 as e -> 0, and the damage lands on the *gradients*, not the
6
+ values. Sampling in (k, h) = (sqrt(e) cos w, sqrt(e) sin w) makes
7
+ dw/dk = -h/e, which diverges; the divergence cancels against a matching
8
+ term routed through M_tra, so the finite answer is a difference of two
9
+ O(a/e) quantities. Measured through the graph path, that costs 6e-4
10
+ relative gradient error at e = 1e-3, 5% at 1e-5 and 40% at 1e-6
11
+ (benchmarks/kh_grad_conditioning.py) — i.e. HMC would be sampling
12
+ noise exactly where the (k, h) parameterization is supposed to shine.
13
+
14
+ This module removes the cancellation structurally by anchoring the
15
+ solve at inferior conjunction. Writing E = E0 + delta with E0 the
16
+ eccentric anomaly at transit, Kepler's equation becomes
17
+
18
+ phi = delta + es (1 - cos delta) - ec sin delta,
19
+ es = e sin E0, ec = e cos E0, phi = 2 pi (t - t0) / P,
20
+
21
+ and the sky-plane Cartesians become
22
+
23
+ u / a = e cos w (cos delta - 1) - B1 sin delta
24
+ v / a = A2 cos delta - B2 sin delta - e sin w
25
+
26
+ with (writing Delta = f0 - E0 = O(e), omb = 1 - sqrt(1 - e^2) = O(e^2))
27
+
28
+ B1 = cos Delta - omb cos E0 sin w
29
+ A2 = cos Delta - omb sin E0 cos w
30
+ B2 = -sin Delta + omb cos E0 cos w.
31
+
32
+ The identity that kills the cancellation is A1 = cos E0 cos w -
33
+ beta sin E0 sin w = e cos w exactly, which follows from the conjunction
34
+ condition (cos E0 - e) cos w = beta sin E0 sin w. Every w-dependence
35
+ that survives is multiplied by e or e^2, and the two quantities that
36
+ carry w at O(1) — e cos w and e sin w — are computed directly from
37
+ (k, h) as k sqrt(e) and h sqrt(e), never through w = atan2(h, k). So no
38
+ 1/e amplification is ever formed. At e = 0 the equations degenerate
39
+ exactly to the circular orbit (Delta = omb = 0, B1 = A2 = 1, B2 = 0,
40
+ delta = phi).
41
+
42
+ The solve keeps the cost of the direct path: the Markley starter (whose
43
+ ~1e-4 accuracy makes its own cancellation irrelevant, and whose
44
+ gradients are identically zero by the implicit function theorem) plus
45
+ ONE sincos and one fifth-order refinement carried out entirely in the
46
+ anchored, O(e)-conditioned coefficients.
47
+ """
48
+
49
+ from __future__ import annotations
50
+
51
+ import math
52
+
53
+ import mlx.core as mx
54
+
55
+ from .kepler import _markley_starter, _unbroadcast
56
+ from .trig import sincos
57
+
58
+ __all__ = ["anchor_constants", "anchor_constants_ew", "solve_sincos_delta",
59
+ "separation_anchored"]
60
+
61
+ _TWO_PI = 2.0 * math.pi
62
+ _PI = math.pi
63
+
64
+
65
+ def _sqrt_any(x):
66
+ if isinstance(x, mx.array):
67
+ return mx.sqrt(mx.maximum(x, 0.0))
68
+ return math.sqrt(max(x, 0.0))
69
+
70
+
71
+ def _one_minus_cos(s, c):
72
+ """1 - cos, from (sin, cos), without cancellation at either end.
73
+
74
+ The inactive branch's denominator is set to 1, not floored: a floored
75
+ 1 + c is forward-safe, but its VJP -s^2/(1+c)^2 blows up as c -> -1
76
+ and the mask's zero cotangent then yields 0 * inf = NaN. delta ~ pi
77
+ is an ordinary apastron-side geometry, so this is reached in normal
78
+ sampling, not just at a measure-zero point.
79
+ """
80
+ pos = c > 0.0
81
+ den = mx.where(pos, 1.0 + c, mx.ones_like(c))
82
+ return mx.where(pos, s * s / den, 1.0 - c)
83
+
84
+
85
+ def anchor_constants(k, h):
86
+ """(k, h) = (sqrt(e) cos w, sqrt(e) sin w) -> anchored orbit constants.
87
+
88
+ Returns (e, ecw, esw, es, ec, B1, A2, B2). Every output is a smooth
89
+ function of (k, h) with a bounded derivative, including at e = 0 —
90
+ that is the whole point of the module. All of them are per-chain
91
+ scalars, so this runs in the MLX graph and its Jacobian rides
92
+ ordinary autodiff; only the per-point solve goes into a kernel.
93
+ """
94
+ e = k * k + h * h
95
+ sq = mx.sqrt(mx.maximum(e, 1e-30))
96
+ ecw = k * sq # e cos w, no division by sqrt(e)
97
+ esw = h * sq # e sin w
98
+ # cos w / sin w are needed only inside O(e^2) corrections below, so
99
+ # their O(1) noise at e -> 0 is harmless. The denominator is set to
100
+ # 1 (not merely floored) where the branch is inactive: a floored
101
+ # denominator is forward-safe but its VJP -k/safe^2 overflows and
102
+ # the mask's zero cotangent then gives 0 * inf = NaN.
103
+ pos = e > 0.0
104
+ denom = mx.where(pos, mx.maximum(sq, 1e-30), mx.ones_like(sq))
105
+ cw = mx.where(pos, k / denom, mx.ones_like(k))
106
+ sw = mx.where(pos, h / denom, mx.zeros_like(h))
107
+ # w from the GUARDED (sin w, cos w), never from (h, k) directly:
108
+ # arctan2(0, 0) has an undefined gradient, and e = 0 is an interior
109
+ # point of the (k, h) disc that a sampler genuinely visits.
110
+ w = mx.arctan2(sw, cw)
111
+ return _anchor_core(e, ecw, esw, cw, sw, w)
112
+
113
+
114
+ def anchor_constants_ew(e, w):
115
+ """(e, w) -> anchored orbit constants, w in radians: the same tuple as
116
+ ``anchor_constants``, for callers holding orbital elements.
117
+
118
+ (k, h) is the right pair for a *sampler* -- it keeps e -> 0 an interior
119
+ point. But it is singular as a map from (e, w): dk/de = cos w / (2
120
+ sqrt e) is infinite at e = 0, so a gradient in e taken through (k, h)
121
+ is inf * 0 = NaN exactly there, although F is perfectly differentiable
122
+ in e (one-sided) at e = 0 -- eccentricity changes the duration at
123
+ first order. With w given, nothing here needs sqrt(e) or a division:
124
+ every constant is a smooth function of (e, w) on [0, 1) x R, and every
125
+ w-dependence is multiplied by e, so d/dw is exactly 0 at e = 0.
126
+ """
127
+ sw, cw = sincos(w)
128
+ return _anchor_core(e, e * cw, e * sw, cw, sw, w)
129
+
130
+
131
+ def _anchor_core(e, ecw, esw, cw, sw, w):
132
+ beta = mx.sqrt(mx.maximum(1.0 - e * e, 0.0))
133
+ omb = e * e / (1.0 + beta) # 1 - beta, without cancellation
134
+
135
+ # Delta = f0 - E0 with f0 = pi/2 - w, from the half-angle relation
136
+ # tan(E0/2) = kk tan(f0/2). Numerator carries the explicit O(e)
137
+ # factor 1 - kk; the denominator is bounded away from zero, and
138
+ # f0/2 in [-pi/4, 3pi/4) never reaches the tan pole.
139
+ s2, c2 = sincos(0.5 * (0.5 * _PI - w))
140
+ kk = mx.sqrt(mx.maximum((1.0 - e) / (1.0 + e), 0.0))
141
+ one_m_kk = 2.0 * e / ((1.0 + e) * (1.0 + kk))
142
+ delta_a = 2.0 * mx.arctan2(one_m_kk * s2 * c2, c2 * c2 + kk * s2 * s2)
143
+ sD, cD = sincos(delta_a)
144
+
145
+ # E0 = pi/2 - w - Delta => sin E0 = cos(w + Delta), cos E0 = sin(w + Delta)
146
+ sE0 = cw * cD - sw * sD
147
+ cE0 = sw * cD + cw * sD
148
+ es = ecw * cD - esw * sD # e sin E0
149
+ ec = esw * cD + ecw * sD # e cos E0
150
+
151
+ B1 = cD - omb * cE0 * sw
152
+ A2 = cD - omb * sE0 * cw
153
+ B2 = -sD + omb * cE0 * cw
154
+ return e, ecw, esw, es, ec, B1, A2, B2
155
+
156
+
157
+ @mx.custom_function
158
+ def solve_sincos_delta(phi: mx.array, es: mx.array, ec: mx.array):
159
+ """(sin delta, cos delta) solving phi = delta + es (1-cos delta)
160
+ - ec sin delta. One sincos; gradients by implicit differentiation."""
161
+ zero = phi * 0.0 + es * 0.0 + ec * 0.0
162
+ phi = phi + zero
163
+ es = es + zero
164
+ ec = ec + zero
165
+
166
+ e = mx.sqrt(mx.maximum(es * es + ec * ec, 0.0))
167
+ E0 = mx.arctan2(es, ec)
168
+ M_tra = E0 - es
169
+
170
+ # wrap the *standard* mean anomaly, and carry phi along with it so
171
+ # the anchored residual below stays consistent with the fold.
172
+ M = phi + M_tra
173
+ nw = mx.round(M / _TWO_PI)
174
+ M_w = M - _TWO_PI * nw
175
+ phi_w = phi - _TWO_PI * nw
176
+
177
+ # starter: Markley on the standard equation, differenced against E0.
178
+ # ~1e-4 accurate; the difference cancels about one digit, which the
179
+ # refinement below discards entirely.
180
+ sign = mx.where(M_w >= 0.0, 1.0 + zero, -1.0 + zero)
181
+ d0 = _markley_starter(mx.abs(M_w), e) * sign - E0
182
+
183
+ sd, cd = sincos(d0) # the ONLY trig call
184
+ omc = _one_minus_cos(sd, cd)
185
+ f_0 = d0 + es * omc - ec * sd - phi_w
186
+ f_1 = 1.0 + es * sd - ec * cd # = 1 - e cos E > 0
187
+ f_2 = es * cd + ec * sd # = e sin E
188
+ f_3 = -es * sd + ec * cd # = e cos E
189
+ d_3 = -f_0 / (f_1 - 0.5 * f_0 * f_2 / f_1)
190
+ d_4 = -f_0 / (f_1 + 0.5 * d_3 * f_2 + d_3 * d_3 * f_3 / 6.0)
191
+ dd = -f_0 / (f_1 + 0.5 * d_4 * f_2 + d_4 * d_4 * f_3 / 6.0
192
+ - d_4 * d_4 * d_4 * f_2 / 24.0)
193
+
194
+ dd2 = dd * dd
195
+ sdd = dd * (1.0 - dd2 / 6.0 * (1.0 - dd2 / 20.0))
196
+ cdd = 1.0 - dd2 * 0.5 * (1.0 - dd2 / 12.0)
197
+ return sd * cdd + cd * sdd, cd * cdd - sd * sdd
198
+
199
+
200
+ @solve_sincos_delta.vjp
201
+ def _solve_delta_vjp(primals, cotangents, outputs):
202
+ phi, es, ec = primals
203
+ ct_s, ct_c = cotangents
204
+ sind, cosd = outputs
205
+ # g(delta) = delta + es (1-cos d) - ec sin d = phi
206
+ # D = dg/ddelta = 1 + es sin d - ec cos d = 1 - e cos E
207
+ # ddelta = [dphi - (1-cos d) des + sin d dec] / D
208
+ g = ct_s * cosd - ct_c * sind
209
+ omc = _one_minus_cos(sind, cosd)
210
+ # D = 1 - e cos E >= 1 - e > 0 over the supported range
211
+ D = mx.maximum(1.0 + es * sind - ec * cosd, 1e-12)
212
+ gD = g / D
213
+ shapes = [p.shape if isinstance(p, mx.array) else () for p in primals]
214
+ return (_unbroadcast(gD, shapes[0]),
215
+ _unbroadcast(-gD * omc, shapes[1]),
216
+ _unbroadcast(gD * sind, shapes[2]))
217
+
218
+
219
+ def cos_inc_from_b(a, b, e, esw, e_max: float):
220
+ """cos i from the impact parameter at inferior conjunction,
221
+ b = a cos i (1 - e^2) / (1 + e sin w), with 1 - e^2 floored at
222
+ 1 - e_max^2. The one copy: the tau kernels' plug-in (metal._ecc_shape),
223
+ anvil's eccentric model and the oblate orbit all call it."""
224
+ return b * (1.0 + esw) / (a * mx.maximum(1.0 - e * e, 1.0 - e_max * e_max))
225
+
226
+
227
+ def separation_anchored(phi, k, h, a, ci, eps: float = 1.1920929e-07,
228
+ consts=None):
229
+ """(z, front) for an eccentric orbit, anchored at inferior conjunction.
230
+
231
+ phi = 2 pi (t - t0) / P (t0 = transit time), (k, h) the
232
+ (sqrt(e) cos w, sqrt(e) sin w) pair, a in stellar radii, ci = cos i.
233
+ Equivalent to ``kepler.separation_keplerian`` to round-off, and
234
+ conditioned so that fp32 *gradients* survive e -> 0. ``consts``, if
235
+ given, replaces (k, h) with an ``anchor_constants[_ew]`` tuple.
236
+ """
237
+ if consts is None:
238
+ consts = anchor_constants(k, h)
239
+ e, ecw, esw, es, ec, B1, A2, B2 = consts
240
+ sind, cosd = solve_sincos_delta(phi, es, ec)
241
+ omc = _one_minus_cos(sind, cosd)
242
+ u = a * (-ecw * omc - B1 * sind)
243
+ v = a * (A2 * cosd - B2 * sind - esw)
244
+ z = mx.sqrt(mx.maximum(u * u + (v * ci) ** 2, (10.0 * eps) ** 2))
245
+ return z, v > 0.0
246
+
247
+
248
+ #: column order of the packed per-chain orbit constants consumed by the
249
+ #: v3 Metal kernel; kept in lockstep with ``metal._ORB_COLS``.
250
+ ORB_COLS = ("ecw", "esw", "es", "ec", "b1", "a2", "b2",
251
+ "ecc", "e0", "mtra", "ci")
252
+
253
+
254
+ def pack_orbit_constants(k, h, ci, consts=None):
255
+ """(k, h, ci) -> (n, 11) per-chain constants for the fused model kernel.
256
+
257
+ The last three of the first eight columns feed the Cartesian tail and
258
+ the solve; ``ecc``/``e0``/``mtra`` seed the Markley starter and the
259
+ 2-pi fold *only*, so they are detached: the implicit function theorem
260
+ makes the converged root independent of its starter, and rint is
261
+ locally constant, so their true gradient contribution is zero.
262
+ Detaching also keeps E0 = atan2(es, ec) — undefined at e = 0 — out
263
+ of the autodiff graph entirely. ``consts``, if given, replaces (k, h)
264
+ with an ``anchor_constants[_ew]`` tuple.
265
+ """
266
+ if consts is None:
267
+ consts = anchor_constants(k, h)
268
+ e, ecw, esw, es, ec, B1, A2, B2 = consts
269
+ E0 = mx.stop_gradient(mx.arctan2(es, ec))
270
+ cols = [ecw, esw, es, ec, B1, A2, B2,
271
+ mx.stop_gradient(e), E0, mx.stop_gradient(E0 - es),
272
+ ci * mx.ones_like(ecw)]
273
+ return mx.stack([mx.reshape(c, (-1,)) for c in cols], axis=1)