orbix 4.0.0__tar.gz → 4.2.0__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 (89) hide show
  1. {orbix-4.0.0 → orbix-4.2.0}/CHANGELOG.md +26 -0
  2. {orbix-4.0.0 → orbix-4.2.0}/PKG-INFO +1 -1
  3. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/_version.py +2 -2
  4. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/kepler/__init__.py +6 -0
  5. orbix-4.2.0/src/orbix/kepler/longitude.py +151 -0
  6. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/kepler/markley.py +11 -1
  7. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/kepler/shortcuts/grid.py +46 -11
  8. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/orbit.py +213 -23
  9. orbix-4.2.0/src/orbix/system/__init__.py +39 -0
  10. orbix-4.2.0/src/orbix/system/charts.py +349 -0
  11. orbix-4.2.0/src/orbix/system/core.py +238 -0
  12. orbix-4.2.0/src/orbix/system/frame_orbit.py +348 -0
  13. orbix-4.2.0/src/orbix/system/observables.py +50 -0
  14. orbix-4.2.0/src/orbix/system/rotation.py +59 -0
  15. orbix-4.2.0/tests/test_charts.py +352 -0
  16. orbix-4.2.0/tests/test_frame_orbit.py +376 -0
  17. {orbix-4.0.0 → orbix-4.2.0}/tests/test_grid_solvers.py +70 -1
  18. orbix-4.2.0/tests/test_kepler_longitude.py +204 -0
  19. {orbix-4.0.0 → orbix-4.2.0}/tests/test_kepler_markley.py +22 -0
  20. {orbix-4.0.0 → orbix-4.2.0}/tests/test_orbit.py +146 -2
  21. {orbix-4.0.0 → orbix-4.2.0}/tests/test_sky_convention.py +45 -3
  22. orbix-4.2.0/tests/test_system_ephemeris.py +341 -0
  23. {orbix-4.0.0 → orbix-4.2.0}/.codespellrc +0 -0
  24. {orbix-4.0.0 → orbix-4.2.0}/.editorconfig +0 -0
  25. {orbix-4.0.0 → orbix-4.2.0}/.gitattributes +0 -0
  26. {orbix-4.0.0 → orbix-4.2.0}/.github/dependabot.yml +0 -0
  27. {orbix-4.0.0 → orbix-4.2.0}/.github/scripts/ci_summary.py +0 -0
  28. {orbix-4.0.0 → orbix-4.2.0}/.github/sibling-pins.txt +0 -0
  29. {orbix-4.0.0 → orbix-4.2.0}/.github/workflows/ci.yml +0 -0
  30. {orbix-4.0.0 → orbix-4.2.0}/.github/workflows/dependabot-automerge.yml +0 -0
  31. {orbix-4.0.0 → orbix-4.2.0}/.github/workflows/docs.yml +0 -0
  32. {orbix-4.0.0 → orbix-4.2.0}/.github/workflows/hygiene.yml +0 -0
  33. {orbix-4.0.0 → orbix-4.2.0}/.github/workflows/publish-to-pypi.yml +0 -0
  34. {orbix-4.0.0 → orbix-4.2.0}/.github/workflows/release-please.yml +0 -0
  35. {orbix-4.0.0 → orbix-4.2.0}/.github/workflows/upstream.yml +0 -0
  36. {orbix-4.0.0 → orbix-4.2.0}/.github/zizmor.yml +0 -0
  37. {orbix-4.0.0 → orbix-4.2.0}/.gitignore +0 -0
  38. {orbix-4.0.0 → orbix-4.2.0}/.pre-commit-config.yaml +0 -0
  39. {orbix-4.0.0 → orbix-4.2.0}/.python-version +0 -0
  40. {orbix-4.0.0 → orbix-4.2.0}/.readthedocs.yaml +0 -0
  41. {orbix-4.0.0 → orbix-4.2.0}/.zenodo.json +0 -0
  42. {orbix-4.0.0 → orbix-4.2.0}/CITATION.cff +0 -0
  43. {orbix-4.0.0 → orbix-4.2.0}/LICENSE +0 -0
  44. {orbix-4.0.0 → orbix-4.2.0}/README.md +0 -0
  45. {orbix-4.0.0 → orbix-4.2.0}/SECURITY.md +0 -0
  46. {orbix-4.0.0 → orbix-4.2.0}/docs/conf.py +0 -0
  47. {orbix-4.0.0 → orbix-4.2.0}/docs/examples/orbits.md +0 -0
  48. {orbix-4.0.0 → orbix-4.2.0}/docs/examples/prepared.md +0 -0
  49. {orbix-4.0.0 → orbix-4.2.0}/docs/gallery.md +0 -0
  50. {orbix-4.0.0 → orbix-4.2.0}/docs/index.md +0 -0
  51. {orbix-4.0.0 → orbix-4.2.0}/pyproject.toml +0 -0
  52. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/__init__.py +0 -0
  53. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/equations/__init__.py +0 -0
  54. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/equations/lambert.py +0 -0
  55. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/equations/orbit.py +0 -0
  56. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/equations/phase.py +0 -0
  57. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/equations/propagation.py +0 -0
  58. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/kepler/core.py +0 -0
  59. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/kepler/shortcuts/__init__.py +0 -0
  60. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/kepler/shortcuts/fixed_e.py +0 -0
  61. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/kepler/shortcuts/guess.py +0 -0
  62. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/observatory/__init__.py +0 -0
  63. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/observatory/data/L2_halo_orbit_six_month.npz +0 -0
  64. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/observatory/keepout.py +0 -0
  65. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/observatory/observatory.py +0 -0
  66. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/observatory/orbit.py +0 -0
  67. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/observatory/solar_system.py +0 -0
  68. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/py.typed +0 -0
  69. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/viz/__init__.py +0 -0
  70. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/viz/_prepare.py +0 -0
  71. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/viz/_require.py +0 -0
  72. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/viz/anim.py +0 -0
  73. {orbix-4.0.0 → orbix-4.2.0}/src/orbix/viz/orbit.py +0 -0
  74. {orbix-4.0.0 → orbix-4.2.0}/tests/conftest.py +0 -0
  75. {orbix-4.0.0 → orbix-4.2.0}/tests/test_dependency_floors.py +0 -0
  76. {orbix-4.0.0 → orbix-4.2.0}/tests/test_equations_grads.py +0 -0
  77. {orbix-4.0.0 → orbix-4.2.0}/tests/test_fixed_e_solvers.py +0 -0
  78. {orbix-4.0.0 → orbix-4.2.0}/tests/test_internal_refs_guard.py +0 -0
  79. {orbix-4.0.0 → orbix-4.2.0}/tests/test_kepler_core.py +0 -0
  80. {orbix-4.0.0 → orbix-4.2.0}/tests/test_kepler_grads.py +0 -0
  81. {orbix-4.0.0 → orbix-4.2.0}/tests/test_lambert.py +0 -0
  82. {orbix-4.0.0 → orbix-4.2.0}/tests/test_observatory.py +0 -0
  83. {orbix-4.0.0 → orbix-4.2.0}/tests/test_observatory_wrapper.py +0 -0
  84. {orbix-4.0.0 → orbix-4.2.0}/tests/test_viz_anim.py +0 -0
  85. {orbix-4.0.0 → orbix-4.2.0}/tests/test_viz_orbit.py +0 -0
  86. {orbix-4.0.0 → orbix-4.2.0}/tests/test_viz_package.py +0 -0
  87. {orbix-4.0.0 → orbix-4.2.0}/tests/test_viz_prepared.py +0 -0
  88. {orbix-4.0.0 → orbix-4.2.0}/tests/test_vs_exosims.py +0 -0
  89. {orbix-4.0.0 → orbix-4.2.0}/tools/check_internal_refs.py +0 -0
@@ -43,6 +43,32 @@
43
43
  * The broken predict/pdet functions and the `discrete_e` factory were removed
44
44
  (they did not work correctly and had no working callers).
45
45
 
46
+ ## [4.2.0](https://github.com/CoreySpohn/orbix/compare/v4.1.0...v4.2.0) (2026-11-16)
47
+
48
+
49
+ ### Features
50
+
51
+ * **system:** leading batch dimensions on the system, with ordered sums that make trailing zero-mass bodies bitwise inert ([acae82f](https://github.com/CoreySpohn/orbix/commit/acae82fa3df92ebbc57aea41dcb8d81f56ee88fe))
52
+
53
+ ## [4.1.0](https://github.com/CoreySpohn/orbix/compare/v4.0.0...v4.1.0) (2026-11-16)
54
+
55
+
56
+ ### Features
57
+
58
+ * a non-singular Kepler function in eccentric longitude with implicit derivatives and solver strategies ([0e9c214](https://github.com/CoreySpohn/orbix/commit/0e9c214146868d2798e5d8b7228466ccf0de573b))
59
+ * a non-singular orbit state, a Jacobi system and a solve-once ephemeris in orbix.system ([43f96bf](https://github.com/CoreySpohn/orbix/commit/43f96bfe71d478fbc92ecbe451e53990bc328ad7))
60
+ * a position-and-velocity orbit chart with a closed-form volume, for short arcs ([bdb724d](https://github.com/CoreySpohn/orbix/commit/bdb724d7dfaf67ee3f9910869a4c74bd729ee133))
61
+ * orbit charts with closed-form volumes against one reference measure ([388fefb](https://github.com/CoreySpohn/orbix/commit/388fefb9443c809659e0abb7ea840e7174f4d66b))
62
+ * **system:** frame orbit from cosines and sines of the classical angles; perifocal rates ([73549c2](https://github.com/CoreySpohn/orbix/commit/73549c294ba23704eda0f08b669d14d71ecb617b))
63
+ * **system:** orbit-plane coordinates and plane basis of a frame orbit ([736cb6f](https://github.com/CoreySpohn/orbix/commit/736cb6f7ac55fb9b55802e8bb04cc926d02cb2aa))
64
+ * the frame chart holds its gauge length near one by default, keeping orientations uniform ([0b5409b](https://github.com/CoreySpohn/orbix/commit/0b5409b658681ffca7f1cbc38c7a7d9253cc80fb))
65
+ * total-mass propagation, velocity, position angle, stellar reflex and radial velocity on KeplerianOrbit ([0c3b770](https://github.com/CoreySpohn/orbix/commit/0c3b7704b75f99b7a0138b2baccfe86f27150106))
66
+
67
+
68
+ ### Bug Fixes
69
+
70
+ * the Markley solver traces in float32 and the grid solvers return the dtype of their inputs ([4e83d00](https://github.com/CoreySpohn/orbix/commit/4e83d001644e390fb19c2fdb843467d89bbdeb89))
71
+
46
72
  ## [4.0.0](https://github.com/CoreySpohn/orbix/compare/v3.0.0...v4.0.0) (2026-11-13)
47
73
 
48
74
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: orbix
3
- Version: 4.0.0
3
+ Version: 4.2.0
4
4
  Summary: A JAX library of functions useful for exoplanet simulations
5
5
  Project-URL: Homepage, https://github.com/CoreySpohn/orbix
6
6
  Project-URL: Issues, https://github.com/CoreySpohn/orbix/issues
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
18
18
  commit_id: str | None
19
19
  __commit_id__: str | None
20
20
 
21
- __version__ = version = '4.0.0'
22
- __version_tuple__ = version_tuple = (4, 0, 0)
21
+ __version__ = version = '4.2.0'
22
+ __version_tuple__ = version_tuple = (4, 2, 0)
23
23
 
24
24
  __commit_id__ = commit_id = None
@@ -1,13 +1,19 @@
1
1
  """Kepler equation solvers: exact core + grid/fixed-e shortcuts."""
2
2
 
3
3
  from orbix.kepler.core import E_solve, diff_solve_trig, solve_trig
4
+ from orbix.kepler.longitude import AbstractKeplerSolver, Grid, Halley, Markley, kepler
4
5
  from orbix.kepler.markley import solve_trig as markley_solve_trig
5
6
  from orbix.kepler.shortcuts.grid import get_grid_solver
6
7
 
7
8
  __all__ = [
9
+ "AbstractKeplerSolver",
8
10
  "E_solve",
11
+ "Grid",
12
+ "Halley",
13
+ "Markley",
9
14
  "diff_solve_trig",
10
15
  "get_grid_solver",
16
+ "kepler",
11
17
  "markley_solve_trig",
12
18
  "solve_trig",
13
19
  ]
@@ -0,0 +1,151 @@
1
+ """Non-singular elliptic Kepler equation with interchangeable value solvers.
2
+
3
+ The eccentric longitude formulation follows Murray and Dermott, Solar System
4
+ Dynamics. Its implicit derivatives remain regular at zero eccentricity.
5
+ """
6
+
7
+ from abc import abstractmethod
8
+ from functools import cache
9
+
10
+ import equinox as eqx
11
+ import jax
12
+ import jax.numpy as jnp
13
+
14
+ from orbix.kepler.markley import _solve_trig_value
15
+ from orbix.kepler.shortcuts.grid import _build_trig_grid, _indices_frac
16
+
17
+
18
+ class AbstractKeplerSolver(eqx.Module):
19
+ """Value-only strategy for elliptic Kepler's equation, M = E - e*sin(E).
20
+
21
+ Inputs broadcast elementwise, with 0 <= e < 1 and any real M in radians.
22
+ Outputs have the floating input result dtype. Strategies use only static
23
+ fields so equal configurations share compiled traces.
24
+ """
25
+
26
+ @abstractmethod
27
+ def solve(self, mean_anomaly, e):
28
+ """Return (sin(E), cos(E)) without an attached derivative rule."""
29
+ raise NotImplementedError
30
+
31
+
32
+ class Markley(AbstractKeplerSolver):
33
+ """Cubic starter and fifth-order correction of Markley (1995)."""
34
+
35
+ def solve(self, mean_anomaly, e):
36
+ """Evaluate the table-free Markley algorithm elementwise."""
37
+ return _solve_trig_value(mean_anomaly, e)[1:]
38
+
39
+
40
+ class Halley(AbstractKeplerSolver):
41
+ """Fixed Halley iterations from the M + 0.85*e*sign(sin(M)) starter.
42
+
43
+ See Danby, Fundamentals of Celestial Mechanics, for iterative elliptic
44
+ Kepler solvers. Six steps reach roundoff for e <= 0.95; more steps may be
45
+ needed very close to e = 1 and periapsis.
46
+ """
47
+
48
+ steps: int = eqx.field(static=True, default=6)
49
+
50
+ def solve(self, mean_anomaly, e):
51
+ """Apply a fixed number of branch-free Halley updates."""
52
+ dtype = jnp.result_type(mean_anomaly, e, 0.0)
53
+ mean, e = jnp.broadcast_arrays(
54
+ jnp.asarray(mean_anomaly, dtype=dtype), jnp.asarray(e, dtype=dtype)
55
+ )
56
+ mean = jnp.arctan2(jnp.sin(mean), jnp.cos(mean))
57
+ initial = mean + 0.85 * e * jnp.sign(mean)
58
+
59
+ def step(_, eccentric):
60
+ sine, cosine = jnp.sin(eccentric), jnp.cos(eccentric)
61
+ residual = eccentric - e * sine - mean
62
+ slope = 1 - e * cosine
63
+ return eccentric - residual / (slope - 0.5 * residual * e * sine / slope)
64
+
65
+ eccentric = jax.lax.fori_loop(0, self.steps, step, initial)
66
+ return jnp.sin(eccentric), jnp.cos(eccentric)
67
+
68
+
69
+ class Grid(AbstractKeplerSolver):
70
+ """Bilinear trig lookup, with one table per resolution and default dtype.
71
+
72
+ Interpolation error grows near e = 1. The returned pair need not have
73
+ exactly unit norm; derivatives of the longitude function use the implicit
74
+ equation with this approximate value, not derivatives of the interpolant.
75
+ """
76
+
77
+ n_e: int = eqx.field(static=True, default=512)
78
+ n_M: int = eqx.field(static=True, default=2048)
79
+
80
+ def solve(self, mean_anomaly, e):
81
+ """Gather four table corners for broadcast inputs without internal vmap."""
82
+ dtype = jnp.result_type(mean_anomaly, e, 0.0)
83
+ mean, e = jnp.broadcast_arrays(
84
+ jnp.asarray(mean_anomaly, dtype=dtype), jnp.asarray(e, dtype=dtype)
85
+ )
86
+ table, inv_dM, inv_de = _build_trig_grid(
87
+ self.n_e, self.n_M, dtype=jnp.asarray(0.0).dtype
88
+ )
89
+ e0, m0, de, dm = _indices_frac(mean, e, inv_dM, inv_de, self.n_M, self.n_e)
90
+ pair = (
91
+ table[:, e0, m0] * ((1 - de) * (1 - dm))
92
+ + table[:, e0, m0 + 1] * ((1 - de) * dm)
93
+ + table[:, e0 + 1, m0] * (de * (1 - dm))
94
+ + table[:, e0 + 1, m0 + 1] * (de * dm)
95
+ ).astype(dtype)
96
+ return pair[0], pair[1]
97
+
98
+
99
+ @cache
100
+ def _longitude_function(solver):
101
+ """Close over a static strategy and reuse its custom-JVP function."""
102
+
103
+ @jax.custom_jvp
104
+ def solve(mean_longitude, ex, ey):
105
+ dtype = jnp.result_type(mean_longitude, ex, ey, 0.0)
106
+ lam, ex, ey = (
107
+ jnp.asarray(value, dtype=dtype) for value in (mean_longitude, ex, ey)
108
+ )
109
+ e = jnp.sqrt(ex * ex + ey * ey)
110
+ circular = e == 0
111
+ divisor = jnp.where(circular, 1.0, e)
112
+ c = jnp.where(circular, 1.0, ex / divisor)
113
+ s = jnp.where(circular, 0.0, ey / divisor)
114
+ sin_lam, cos_lam = jnp.sin(lam), jnp.cos(lam)
115
+ mean = jnp.arctan2(sin_lam * c - cos_lam * s, cos_lam * c + sin_lam * s)
116
+ sin_e, cos_e = solver.solve(mean, e)
117
+ # The exact circular value also avoids interpolation error at e = 0.
118
+ return (
119
+ jnp.where(circular, sin_lam, sin_e * c + cos_e * s),
120
+ jnp.where(circular, cos_lam, cos_e * c - sin_e * s),
121
+ )
122
+
123
+ @solve.defjvp
124
+ def solve_jvp(primals, tangents):
125
+ _, ex, ey = primals
126
+ dlam, dex, dey = (
127
+ 0.0 if getattr(tangent, "dtype", None) == jax.dtypes.float0 else tangent
128
+ for tangent in tangents
129
+ )
130
+ sine, cosine = solve(*primals)
131
+ dF = (dlam + sine * dex - cosine * dey) / (1 - ex * cosine - ey * sine)
132
+ return (sine, cosine), (cosine * dF, -sine * dF)
133
+
134
+ return solve
135
+
136
+
137
+ _DEFAULT_SOLVER = Markley()
138
+
139
+
140
+ def kepler(mean_longitude, ex, ey, *, solver=_DEFAULT_SOLVER):
141
+ """Return (sin(F), cos(F)) solving lam = F - ex*sin(F) + ey*cos(F).
142
+
143
+ Inputs broadcast to any shape, with ex**2 + ey**2 < 1 and radians for
144
+ mean_longitude. The output dtype is their floating result dtype. Here
145
+ (ex, ey) = e*(cos(p), sin(p)), where p is longitude of periapsis.
146
+
147
+ A custom JVP differentiates the implicit equation, including at circular
148
+ orbits and to higher orders. The value-only solver is closed over and is
149
+ never differentiated. Equal solver instances reuse the same function.
150
+ """
151
+ return _longitude_function(solver)(mean_longitude, ex, ey)
@@ -41,6 +41,13 @@ def E_solve_trig(M, e):
41
41
  are those of the implicit Kepler solution away from the wrap seam; at the
42
42
  seam the derivative follows the continuous, unwrapped E convention.
43
43
  """
44
+ return _solve_trig_value(M, e)
45
+
46
+
47
+ def _solve_trig_value(M, e):
48
+ """Evaluate Markley's algorithm without a derivative rule."""
49
+ dtype = jnp.result_type(M, e, 0.0)
50
+ M, e = jnp.asarray(M, dtype=dtype), jnp.asarray(e, dtype=dtype)
44
51
  reduced = jnp.fmod(M, two_pi)
45
52
  absolute = jnp.abs(reduced)
46
53
  reflected = absolute > jnp.pi
@@ -74,7 +81,10 @@ def E_solve_trig(M, e):
74
81
  * (
75
82
  1 / 120
76
83
  - x2
77
- * (1 / 5040 - x2 * (1 / 362880 - x2 * (1 / 39916800 - x2 / 6227020800)))
84
+ * (
85
+ 1 / 5040
86
+ - x2 * (1 / 362880 - x2 * (1 / 39916800 - x2 / 6227020800.0))
87
+ )
78
88
  )
79
89
  )
80
90
  )
@@ -28,7 +28,6 @@ from orbix.kepler.core import E_solve_jit
28
28
 
29
29
 
30
30
  # Caching here lets us give the same solver to multiple planets.
31
- @cache
32
31
  def get_grid_solver(
33
32
  level="scalar", jit=False, kind="bilinear", E=True, trig=True, n_e=512, n_M=2048
34
33
  ):
@@ -55,6 +54,15 @@ def get_grid_solver(
55
54
  n_M:
56
55
  The number of mean anomaly steps in the grid.
57
56
  """
57
+ dtype = jnp.asarray(0.0).dtype
58
+ # Keep cached arrays concrete even when first requested inside a JIT trace.
59
+ with jax.ensure_compile_time_eval():
60
+ return _get_grid_solver(level, jit, kind, E, trig, n_e, n_M, dtype)
61
+
62
+
63
+ @cache
64
+ def _get_grid_solver(level, jit, kind, E, trig, n_e, n_M, dtype):
65
+ """Cache by construction dtype as well as lookup configuration."""
58
66
  if not E and not trig:
59
67
  raise ValueError("get_grid_solver: at least one of E or trig must be True")
60
68
  if E and trig:
@@ -86,6 +94,18 @@ def get_grid_solver(
86
94
  return func
87
95
 
88
96
 
97
+ def _with_input_dtype(lookup):
98
+ """Preserve the floating input result dtype independently of table dtype."""
99
+
100
+ def solve(M_scalar, e_scalar):
101
+ dtype = jnp.result_type(M_scalar, e_scalar, 0.0)
102
+ return jax.tree.map(
103
+ lambda value: value.astype(dtype), lookup(M_scalar, e_scalar)
104
+ )
105
+
106
+ return solve
107
+
108
+
89
109
  def _setup(n_e: int = 1000, n_M: int = 3600):
90
110
  """Setup for grid methods."""
91
111
  e_grid = jnp.linspace(0.0, 1.0, n_e, endpoint=False)
@@ -178,7 +198,7 @@ def E_trig_lin(n_e=1024, n_M=4096):
178
198
  _lin(cosE_grid, dcosE_dind, *p),
179
199
  )
180
200
 
181
- return _lookup_scalar
201
+ return _with_input_dtype(_lookup_scalar)
182
202
 
183
203
 
184
204
  def E_lin(n_e=1024, n_M=4096):
@@ -193,7 +213,7 @@ def E_lin(n_e=1024, n_M=4096):
193
213
  p = _grid_lin_params(M_scalar, e_scalar, inv_dM, inv_de, n_M_int, n_e)
194
214
  return _lin(E_grid, dE_dind_grid, *p)
195
215
 
196
- return _lookup_scalar
216
+ return _with_input_dtype(_lookup_scalar)
197
217
 
198
218
 
199
219
  def trig_lin(n_e=1024, n_M=4096):
@@ -208,7 +228,7 @@ def trig_lin(n_e=1024, n_M=4096):
208
228
  p = _grid_lin_params(M_scalar, e_scalar, inv_dM, inv_de, n_M_int, n_e)
209
229
  return _lin(sinE_grid, dsinE_dind, *p), _lin(cosE_grid, dcosE_dind, *p)
210
230
 
211
- return _lookup_scalar
231
+ return _with_input_dtype(_lookup_scalar)
212
232
 
213
233
 
214
234
  ################################################################################
@@ -233,6 +253,12 @@ def _indices_frac(M, e, inv_dM, inv_de, n_M, n_e):
233
253
 
234
254
 
235
255
  def _E_grid_base(n_e: int, n_M: int, *, dtype=jnp.float32):
256
+ with jax.ensure_compile_time_eval():
257
+ return _cached_E_grid_base(n_e, n_M, jnp.dtype(dtype))
258
+
259
+
260
+ @cache
261
+ def _cached_E_grid_base(n_e, n_M, dtype):
236
262
  e_grid = jnp.linspace(0.0, 1.0, n_e, dtype=dtype, endpoint=False)
237
263
  M_grid = jnp.linspace(0.0, two_pi, n_M, dtype=dtype, endpoint=False)
238
264
  E_grid = jax.vmap(lambda e: E_solve_jit(M_grid, e))(e_grid)
@@ -243,7 +269,9 @@ def _E_grid_base(n_e: int, n_M: int, *, dtype=jnp.float32):
243
269
  E_grid = jnp.concatenate([E_grid, _last_E_row[jnp.newaxis, :]], axis=0)
244
270
  # Append last column of 2pi-eps to make the grid complete with one more
245
271
  # column because we always pull in a square patch of 2x2
246
- _last_E_col = jnp.repeat(two_pi - jnp.finfo(dtype).eps, n_e + 1, axis=0)
272
+ _last_E_col = jnp.repeat(
273
+ jnp.asarray(two_pi - jnp.finfo(dtype).eps, dtype=dtype), n_e + 1, axis=0
274
+ )
247
275
  E_grid = jnp.concatenate([E_grid, _last_E_col[:, jnp.newaxis]], axis=1)
248
276
  inv_dM = 1.0 / (M_grid[1] - M_grid[0])
249
277
  inv_de = 1.0 / (e_grid[1] - e_grid[0])
@@ -262,6 +290,13 @@ def _build_E_trig_grid(n_e: int, n_M: int, *, dtype=jnp.float32):
262
290
 
263
291
 
264
292
  def _build_trig_grid(n_e: int, n_M: int, *, dtype=jnp.float32):
293
+ """Return a cached trig table, materialized outside any active trace."""
294
+ with jax.ensure_compile_time_eval():
295
+ return _cached_trig_grid(n_e, n_M, jnp.dtype(dtype))
296
+
297
+
298
+ @cache
299
+ def _cached_trig_grid(n_e, n_M, dtype):
265
300
  """Return sinE-cosE tensor of shape (2, n_e, n_M+1) and inverse steps."""
266
301
  E_grid, inv_dM, inv_de = _E_grid_base(n_e, n_M, dtype=dtype)
267
302
  trig = jnp.stack([jnp.sin(E_grid), jnp.cos(E_grid)], axis=0)
@@ -309,20 +344,20 @@ def _scalar_trig_bilin(trig, inv_dM, inv_de, n_M, n_e, M_scalar, e_scalar):
309
344
 
310
345
  def E_trig_bilin(n_e=1024, n_M=4096):
311
346
  """E, sinE, cosE via bilinear interp of packed grid."""
312
- triple, inv_dM, inv_de = _build_E_trig_grid(n_e, n_M)
347
+ triple, inv_dM, inv_de = _build_E_trig_grid(n_e, n_M, dtype=jnp.asarray(0.0).dtype)
313
348
  scalar_fun = partial(_scalar_E_trig_bilin, triple, inv_dM, inv_de, n_M, n_e)
314
- return scalar_fun
349
+ return _with_input_dtype(scalar_fun)
315
350
 
316
351
 
317
352
  def E_bilin(n_e=1024, n_M=4096):
318
353
  """E via bilinear interp of packed grid."""
319
- E_grid, inv_dM, inv_de = _build_E_grid(n_e, n_M)
354
+ E_grid, inv_dM, inv_de = _build_E_grid(n_e, n_M, dtype=jnp.asarray(0.0).dtype)
320
355
  scalar_fun = partial(_scalar_E_bilin, E_grid, inv_dM, inv_de, n_M, n_e)
321
- return scalar_fun
356
+ return _with_input_dtype(scalar_fun)
322
357
 
323
358
 
324
359
  def trig_bilin(n_e=1024, n_M=4096):
325
360
  """sinE, cosE via bilinear interp of packed grid."""
326
- trig, inv_dM, inv_de = _build_trig_grid(n_e, n_M)
361
+ trig, inv_dM, inv_de = _build_trig_grid(n_e, n_M, dtype=jnp.asarray(0.0).dtype)
327
362
  scalar_fun = partial(_scalar_trig_bilin, trig, inv_dM, inv_de, n_M, n_e)
328
- return scalar_fun
363
+ return _with_input_dtype(scalar_fun)