elome 0.16.1__tar.gz

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.
Files changed (62) hide show
  1. elome-0.16.1/LICENSE +21 -0
  2. elome-0.16.1/PKG-INFO +92 -0
  3. elome-0.16.1/README.md +60 -0
  4. elome-0.16.1/elome/__init__.py +90 -0
  5. elome-0.16.1/elome/anchored.py +273 -0
  6. elome-0.16.1/elome/anvil.py +658 -0
  7. elome-0.16.1/elome/api.py +1143 -0
  8. elome-0.16.1/elome/dtypes.py +117 -0
  9. elome-0.16.1/elome/ellip.py +177 -0
  10. elome-0.16.1/elome/exposure.py +227 -0
  11. elome-0.16.1/elome/flux.py +46 -0
  12. elome-0.16.1/elome/greens.py +84 -0
  13. elome-0.16.1/elome/hybrid.py +575 -0
  14. elome-0.16.1/elome/kepler.py +271 -0
  15. elome-0.16.1/elome/ld.py +174 -0
  16. elome-0.16.1/elome/metal.py +1875 -0
  17. elome-0.16.1/elome/metal_hybrid.py +816 -0
  18. elome-0.16.1/elome/metal_oblate.py +1541 -0
  19. elome-0.16.1/elome/oblate.py +870 -0
  20. elome-0.16.1/elome/oblate_tau.py +379 -0
  21. elome-0.16.1/elome/orbit.py +76 -0
  22. elome-0.16.1/elome/poly.py +179 -0
  23. elome-0.16.1/elome/solution.py +250 -0
  24. elome-0.16.1/elome/trig.py +80 -0
  25. elome-0.16.1/elome/vjp.py +212 -0
  26. elome-0.16.1/elome.egg-info/PKG-INFO +92 -0
  27. elome-0.16.1/elome.egg-info/SOURCES.txt +60 -0
  28. elome-0.16.1/elome.egg-info/dependency_links.txt +1 -0
  29. elome-0.16.1/elome.egg-info/requires.txt +15 -0
  30. elome-0.16.1/elome.egg-info/top_level.txt +1 -0
  31. elome-0.16.1/pyproject.toml +41 -0
  32. elome-0.16.1/setup.cfg +4 -0
  33. elome-0.16.1/tests/test_anchored.py +191 -0
  34. elome-0.16.1/tests/test_anvil.py +141 -0
  35. elome-0.16.1/tests/test_api.py +281 -0
  36. elome-0.16.1/tests/test_batched_frontend.py +132 -0
  37. elome-0.16.1/tests/test_cel.py +123 -0
  38. elome-0.16.1/tests/test_docs_examples.py +74 -0
  39. elome-0.16.1/tests/test_ecc_anvil.py +215 -0
  40. elome-0.16.1/tests/test_entry_contract.py +459 -0
  41. elome-0.16.1/tests/test_exposure.py +263 -0
  42. elome-0.16.1/tests/test_flux.py +205 -0
  43. elome-0.16.1/tests/test_grad.py +152 -0
  44. elome-0.16.1/tests/test_hybrid.py +568 -0
  45. elome-0.16.1/tests/test_hybrid_api.py +532 -0
  46. elome-0.16.1/tests/test_hybrid_metal.py +703 -0
  47. elome-0.16.1/tests/test_kepler.py +300 -0
  48. elome-0.16.1/tests/test_large_occultor.py +489 -0
  49. elome-0.16.1/tests/test_ld_basis.py +447 -0
  50. elome-0.16.1/tests/test_light_curve_mx.py +595 -0
  51. elome-0.16.1/tests/test_metal.py +572 -0
  52. elome-0.16.1/tests/test_oblate.py +365 -0
  53. elome-0.16.1/tests/test_oblate_api.py +266 -0
  54. elome-0.16.1/tests/test_oblate_metal.py +356 -0
  55. elome-0.16.1/tests/test_oblate_tau.py +441 -0
  56. elome-0.16.1/tests/test_poly.py +225 -0
  57. elome-0.16.1/tests/test_real_data_target.py +221 -0
  58. elome-0.16.1/tests/test_solution.py +141 -0
  59. elome-0.16.1/tests/test_tau_ecc.py +511 -0
  60. elome-0.16.1/tests/test_tau_kernel.py +538 -0
  61. elome-0.16.1/tests/test_trig.py +54 -0
  62. elome-0.16.1/tests/test_vjp.py +143 -0
elome-0.16.1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 David Kipping
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 all
13
+ 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 THE
21
+ SOFTWARE.
elome-0.16.1/PKG-INFO ADDED
@@ -0,0 +1,92 @@
1
+ Metadata-Version: 2.4
2
+ Name: elome
3
+ Version: 0.16.1
4
+ Summary: Elómë: analytic, differentiable transit light curves on Apple Silicon (MLX), with a batman-style frontend
5
+ Author: David Kipping
6
+ License-Expression: MIT
7
+ Project-URL: Documentation, https://elome.readthedocs.io
8
+ Project-URL: Source, https://github.com/davidkipping/elome
9
+ Project-URL: Issues, https://github.com/davidkipping/elome/issues
10
+ Keywords: exoplanets,transits,light curves,limb darkening,oblate planets,MLX,Metal,Apple Silicon
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Operating System :: MacOS
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Topic :: Scientific/Engineering :: Astronomy
15
+ Requires-Python: >=3.10
16
+ Description-Content-Type: text/markdown
17
+ License-File: LICENSE
18
+ Requires-Dist: mlx>=0.30
19
+ Requires-Dist: numpy
20
+ Provides-Extra: test
21
+ Requires-Dist: pytest; extra == "test"
22
+ Requires-Dist: scipy; extra == "test"
23
+ Requires-Dist: batman-package; extra == "test"
24
+ Requires-Dist: mpmath; extra == "test"
25
+ Provides-Extra: docs
26
+ Requires-Dist: sphinx>=7; extra == "docs"
27
+ Requires-Dist: myst-nb>=1.1; extra == "docs"
28
+ Requires-Dist: sphinx-book-theme>=1.1; extra == "docs"
29
+ Requires-Dist: sphinx-copybutton; extra == "docs"
30
+ Requires-Dist: sphinx-design; extra == "docs"
31
+ Dynamic: license-file
32
+
33
+ # Elómë &nbsp;<picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/davidkipping/elome/main/docs/assets/elome_tengwar_dark.svg"><img src="https://raw.githubusercontent.com/davidkipping/elome/main/docs/assets/elome_tengwar_light.svg" alt="Elómë in Tengwar" height="44" align="top"></picture>
34
+
35
+ **Analytic, differentiable transit light curves on Apple Silicon.**
36
+
37
+ [![Documentation](https://readthedocs.org/projects/elome/badge/?version=latest)](https://elome.readthedocs.io)
38
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/davidkipping/elome/blob/main/LICENSE)
39
+
40
+ Elómë computes exoplanet transit light curves exactly and in closed form,
41
+ with analytic gradients in every parameter. It runs on
42
+ [MLX](https://github.com/ml-explore/mlx), in float32 on the Apple-Silicon
43
+ GPU through fused Metal kernels, or in float64 on the CPU. It models
44
+ spherical planets with uniform, linear, quadratic, polynomial and
45
+ *hybrid* limb darkening, and oblate planets with the hybrid laws, which
46
+ keep their light curves elementary. The frontend follows
47
+ [batman](https://github.com/lkreidberg/batman).
48
+
49
+ *Elómë* (eh-LOH-meh) is Quenya for "star-dimness".
50
+
51
+ ## Installation
52
+
53
+ ```bash
54
+ pip install elome
55
+ ```
56
+
57
+ Elómë needs a Mac with Apple Silicon for its GPU kernels, Python 3.10 or
58
+ later, MLX and NumPy.
59
+
60
+ ## Example
61
+
62
+ ```python
63
+ import numpy as np
64
+ import elome
65
+
66
+ params = elome.TransitParams()
67
+ params.t0, params.per, params.rp, params.a = 0.0, 3.0, 0.1, 10.0
68
+ params.inc, params.ecc, params.w = 88.0, 0.0, 90.0
69
+ params.limb_dark, params.u = "quadratic", [0.4, 0.25]
70
+
71
+ t = np.linspace(-0.1, 0.1, 1000)
72
+ flux = elome.TransitModel(params, t).light_curve(params)
73
+ ```
74
+
75
+ ## Documentation
76
+
77
+ **[elome.readthedocs.io](https://elome.readthedocs.io)**: installation,
78
+ guides for spherical and oblate planets, fitting with samplers, tutorials
79
+ and the API reference.
80
+
81
+ ## Citation
82
+
83
+ If Elómë contributes to your research, please cite the Elómë paper
84
+ (Kipping, in preparation, *Journal of Open Source Software*,
85
+ [doi:10.21105/joss.XXXXX](https://doi.org/10.21105/joss.XXXXX)). If you
86
+ use the hybrid limb-darkening laws or oblate planets, please also cite
87
+ the hybrid-law papers; see
88
+ [Citing Elómë](https://elome.readthedocs.io/en/latest/about/citing.html).
89
+
90
+ ## License
91
+
92
+ MIT; see [LICENSE](https://github.com/davidkipping/elome/blob/main/LICENSE).
elome-0.16.1/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # Elómë &nbsp;<picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/davidkipping/elome/main/docs/assets/elome_tengwar_dark.svg"><img src="https://raw.githubusercontent.com/davidkipping/elome/main/docs/assets/elome_tengwar_light.svg" alt="Elómë in Tengwar" height="44" align="top"></picture>
2
+
3
+ **Analytic, differentiable transit light curves on Apple Silicon.**
4
+
5
+ [![Documentation](https://readthedocs.org/projects/elome/badge/?version=latest)](https://elome.readthedocs.io)
6
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/davidkipping/elome/blob/main/LICENSE)
7
+
8
+ Elómë computes exoplanet transit light curves exactly and in closed form,
9
+ with analytic gradients in every parameter. It runs on
10
+ [MLX](https://github.com/ml-explore/mlx), in float32 on the Apple-Silicon
11
+ GPU through fused Metal kernels, or in float64 on the CPU. It models
12
+ spherical planets with uniform, linear, quadratic, polynomial and
13
+ *hybrid* limb darkening, and oblate planets with the hybrid laws, which
14
+ keep their light curves elementary. The frontend follows
15
+ [batman](https://github.com/lkreidberg/batman).
16
+
17
+ *Elómë* (eh-LOH-meh) is Quenya for "star-dimness".
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ pip install elome
23
+ ```
24
+
25
+ Elómë needs a Mac with Apple Silicon for its GPU kernels, Python 3.10 or
26
+ later, MLX and NumPy.
27
+
28
+ ## Example
29
+
30
+ ```python
31
+ import numpy as np
32
+ import elome
33
+
34
+ params = elome.TransitParams()
35
+ params.t0, params.per, params.rp, params.a = 0.0, 3.0, 0.1, 10.0
36
+ params.inc, params.ecc, params.w = 88.0, 0.0, 90.0
37
+ params.limb_dark, params.u = "quadratic", [0.4, 0.25]
38
+
39
+ t = np.linspace(-0.1, 0.1, 1000)
40
+ flux = elome.TransitModel(params, t).light_curve(params)
41
+ ```
42
+
43
+ ## Documentation
44
+
45
+ **[elome.readthedocs.io](https://elome.readthedocs.io)**: installation,
46
+ guides for spherical and oblate planets, fitting with samplers, tutorials
47
+ and the API reference.
48
+
49
+ ## Citation
50
+
51
+ If Elómë contributes to your research, please cite the Elómë paper
52
+ (Kipping, in preparation, *Journal of Open Source Software*,
53
+ [doi:10.21105/joss.XXXXX](https://doi.org/10.21105/joss.XXXXX)). If you
54
+ use the hybrid limb-darkening laws or oblate planets, please also cite
55
+ the hybrid-law papers; see
56
+ [Citing Elómë](https://elome.readthedocs.io/en/latest/about/citing.html).
57
+
58
+ ## License
59
+
60
+ MIT; see [LICENSE](https://github.com/davidkipping/elome/blob/main/LICENSE).
@@ -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"
@@ -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)