Skip to content

Commit 401a586

Browse files
committed
perf: use a better routine maybe
1 parent 5a9acd9 commit 401a586

1 file changed

Lines changed: 28 additions & 41 deletions

File tree

jax_galsim/spergel.py

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import galsim as _galsim
22
import jax
33
import jax.numpy as jnp
4+
import numpy as np
45
from jax.tree_util import Partial as partial
56
from jax.tree_util import register_pytree_node_class
67

@@ -192,52 +193,38 @@ def calculateFluxRadius(alpha, nu, zmin=0.0, zmax=40.0):
192193
)
193194

194195

196+
# RATIONAL_POLY_VALS is the array of rational function
197+
# polynomial coefficients that define the approximation
198+
# fmt: off
199+
RATIONAL_POLY_VALS = np.array(
200+
[+3.0651031751484838e-03, +1.0881207976967715e-01, +8.5972666430068956e-01, +2.4585755707012957e+00,
201+
+3.0048610734396695e+00, +1.5239820144462637e+00, +2.2884834100438053e-01, +9.7403074230245667e-04,
202+
+5.3397788602977357e-02, +5.9314621875009266e-01, +2.3758972396906111e+00, +4.2001759224931892e+00,
203+
+3.3648483673561365e+00],
204+
dtype=np.float64,
205+
)
206+
# fmt: on
207+
RATIONAL_POLY_M = 6
208+
RATIONAL_POLY_N = RATIONAL_POLY_M
209+
210+
211+
@partial(jax.jit, static_argnames=("m", "n"))
212+
@partial(jnp.vectorize, excluded=(1, 2, 3))
213+
def _pade_func(x, coeffs, m, n):
214+
p = jnp.polyval(coeffs[:m + 1], x)
215+
q = jnp.polyval(
216+
jnp.concatenate([coeffs[m + 1:], jnp.ones(1)], axis=0),
217+
x,
218+
)
219+
return p / q
220+
221+
195222
def _spergel_hlr_pade(x):
196223
"""A Pseudo-Pade approximation for the HLR of the Spergel profile as a function of nu.
197224
198225
See dev/notebooks/spergel_hlr_flux_radius_approx.ipynb for code to generate this routine.
199226
"""
200-
# fmt: off
201-
pm = 1.2571513771129166 + x * (
202-
3.7059053890269102 + x * (
203-
2.8577090425861944 + x * (
204-
-0.30570486567039273 + x * (
205-
0.6589831675940833 + x * (
206-
3.375577680133867 + x * (
207-
2.8143565844741403 + x * (
208-
0.9292378858457211 + x * (
209-
0.12096941981286179 + x * (
210-
0.004206502758293099
211-
)
212-
)
213-
)
214-
)
215-
)
216-
)
217-
)
218-
)
219-
)
220-
qm = 1.0 + x * (
221-
2.1939178810491837 + x * (
222-
0.8281034080784796 + x * (
223-
-0.5163329765186994 + x * (
224-
0.9164871490929886 + x * (
225-
1.8988551389326231 + x * (
226-
1.042688817291684 + x * (
227-
0.22580140592548198 + x * (
228-
0.01681923980317362 + x * (
229-
0.00018168506955933716
230-
)
231-
)
232-
)
233-
)
234-
)
235-
)
236-
)
237-
)
238-
)
239-
# fmt: on
240-
return pm / qm
227+
return jnp.exp(_pade_func(x, RATIONAL_POLY_VALS, RATIONAL_POLY_M, RATIONAL_POLY_N))
241228

242229

243230
@implements(

0 commit comments

Comments
 (0)