diff --git a/autogalaxy/profiles/light_and_mass_profiles.py b/autogalaxy/profiles/light_and_mass_profiles.py index 767f3ffd3..6c8741deb 100644 --- a/autogalaxy/profiles/light_and_mass_profiles.py +++ b/autogalaxy/profiles/light_and_mass_profiles.py @@ -85,14 +85,13 @@ def __init__( effective_radius The radius containing half the light of this light profile. mass_to_light_ratio_base - The base mass-to-light ratio of the profile, which is the mass-to-light ratio of the Gaussian before it - is scaled by values that adjust its mass-to-light ratio based on the reference radius and gradient. + The mass-to-light ratio of a Gaussian whose `sigma` equals `mass_to_light_radius`. mass_to_light_gradient - The mass-to-light radial gradient of the profile, whereby positive values means there is more mass - per unit light within the reference radius. + The power-law slope of the mass-to-light ratio with `sigma`, M/L proportional to + `(sigma / mass_to_light_radius) ** -mass_to_light_gradient`: positive values mean the mass-to-light + ratio falls outward (more mass per unit light within the reference radius). mass_to_light_radius - The radius where the mass-to-light ratio is equal to the base mass-to-light ratio, such that there will be - more of less mass per unit light within this radius depending on the mass-to-light gradient. + The reference radius at which the mass-to-light ratio equals `mass_to_light_ratio_base`. """ lp.Gaussian.__init__( self, @@ -370,7 +369,9 @@ def __init__( mass_to_light_ratio The mass-to-light ratio of the light profiles. mass_to_light_gradient - The mass-to-light radial gradient. + The power-law slope of the mass-to-light ratio with radius, M/L proportional to + `(R / effective_radius) ** -mass_to_light_gradient`: positive values mean the mass-to-light ratio + falls outward, and it equals `mass_to_light_ratio` on the ellipse of the effective radius. """ lp.Sersic.__init__( self, @@ -420,7 +421,9 @@ def __init__( mass_to_light_ratio The mass-to-light ratio of the light profiles mass_to_light_gradient - The mass-to-light radial gradient. + The power-law slope of the mass-to-light ratio with radius, M/L proportional to + `(R / effective_radius) ** -mass_to_light_gradient`: positive values mean the mass-to-light ratio + falls outward, and it equals `mass_to_light_ratio` on the ellipse of the effective radius. """ SersicGradient.__init__( @@ -463,7 +466,9 @@ def __init__( mass_to_light_ratio The mass-to-light ratio of the light profiles mass_to_light_gradient - The mass-to-light radial gradient. + The power-law slope of the mass-to-light ratio with radius, M/L proportional to + `(R / effective_radius) ** -mass_to_light_gradient`: positive values mean the mass-to-light ratio + falls outward, and it equals `mass_to_light_ratio` on the ellipse of the effective radius. """ SersicGradient.__init__( @@ -505,7 +510,9 @@ def __init__( mass_to_light_ratio The mass-to-light ratio of the light profiles mass_to_light_gradient - The mass-to-light radial gradient. + The power-law slope of the mass-to-light ratio with radius, M/L proportional to + `(R / effective_radius) ** -mass_to_light_gradient`: positive values mean the mass-to-light ratio + falls outward, and it equals `mass_to_light_ratio` on the ellipse of the effective radius. """ SersicGradientSph.__init__( diff --git a/autogalaxy/profiles/light_linear_and_mass_profiles.py b/autogalaxy/profiles/light_linear_and_mass_profiles.py index cfdc8be77..8aef19984 100644 --- a/autogalaxy/profiles/light_linear_and_mass_profiles.py +++ b/autogalaxy/profiles/light_linear_and_mass_profiles.py @@ -76,14 +76,13 @@ def __init__( effective_radius The radius containing half the light of this light profile. mass_to_light_ratio_base - The base mass-to-light ratio of the profile, which is the mass-to-light ratio of the Gaussian before it - is scaled by values that adjust its mass-to-light ratio based on the reference radius and gradient. + The mass-to-light ratio of a Gaussian whose `sigma` equals `mass_to_light_radius`. mass_to_light_gradient - The mass-to-light radial gradient of the profile, whereby positive values means there is more mass - per unit light within the reference radius. + The power-law slope of the mass-to-light ratio with `sigma`, M/L proportional to + `(sigma / mass_to_light_radius) ** -mass_to_light_gradient`: positive values mean the mass-to-light + ratio falls outward (more mass per unit light within the reference radius). mass_to_light_radius - The radius where the mass-to-light ratio is equal to the base mass-to-light ratio, such that there will be - more of less mass per unit light within this radius depending on the mass-to-light gradient. + The reference radius at which the mass-to-light ratio equals `mass_to_light_ratio_base`. """ lp_linear.Gaussian.__init__( self, @@ -359,7 +358,9 @@ def __init__( mass_to_light_ratio The mass-to-light ratio of the light profiles. mass_to_light_gradient - The mass-to-light radial gradient. + The power-law slope of the mass-to-light ratio with radius, M/L proportional to + `(R / effective_radius) ** -mass_to_light_gradient`: positive values mean the mass-to-light ratio + falls outward, and it equals `mass_to_light_ratio` on the ellipse of the effective radius. """ lp_linear.Sersic.__init__( self, @@ -408,7 +409,9 @@ def __init__( mass_to_light_ratio The mass-to-light ratio of the light profiles mass_to_light_gradient - The mass-to-light radial gradient. + The power-law slope of the mass-to-light ratio with radius, M/L proportional to + `(R / effective_radius) ** -mass_to_light_gradient`: positive values mean the mass-to-light ratio + falls outward, and it equals `mass_to_light_ratio` on the ellipse of the effective radius. """ SersicGradient.__init__( @@ -451,7 +454,9 @@ def __init__( mass_to_light_ratio The mass-to-light ratio of the light profiles mass_to_light_gradient - The mass-to-light radial gradient. + The power-law slope of the mass-to-light ratio with radius, M/L proportional to + `(R / effective_radius) ** -mass_to_light_gradient`: positive values mean the mass-to-light ratio + falls outward, and it equals `mass_to_light_ratio` on the ellipse of the effective radius. """ SersicGradient.__init__( @@ -493,7 +498,9 @@ def __init__( mass_to_light_ratio The mass-to-light ratio of the light profiles mass_to_light_gradient - The mass-to-light radial gradient. + The power-law slope of the mass-to-light ratio with radius, M/L proportional to + `(R / effective_radius) ** -mass_to_light_gradient`: positive values mean the mass-to-light ratio + falls outward, and it equals `mass_to_light_ratio` on the ellipse of the effective radius. """ SersicGradientSph.__init__( diff --git a/autogalaxy/profiles/mass/stellar/gaussian_gradient.py b/autogalaxy/profiles/mass/stellar/gaussian_gradient.py index 544f21237..0d84a814c 100644 --- a/autogalaxy/profiles/mass/stellar/gaussian_gradient.py +++ b/autogalaxy/profiles/mass/stellar/gaussian_gradient.py @@ -15,18 +15,24 @@ def __init__( mass_to_light_radius: float = 1.0, ): r""" - The elliptical Gaussian light profile with a gradient in its mass to light conversion. + The elliptical Gaussian mass profile with a gradient in its mass-to-light conversion. - $\Psi (r) = \Psi_{o} \frac{(\sigma + 0.01)}{R_{ref}}^{\Tau}$ + Each Gaussian takes a single mass-to-light ratio set by its `sigma`, following a power law in radius: + + $\Upsilon(\sigma) = \Upsilon_{0} \left( \frac{\sigma}{R_{\rm ref}} \right)^{-\Gamma}$ Where: - $\Psi (r)$ is the 1D convergence profile of the Gaussian [dimensionless]. - $\Psi_{o}$ is the base mass-to-light ratio of the profile [dimensionless]. + $\Upsilon(\sigma)$ is the mass-to-light ratio of the Gaussian [dimensionless]. + $\Upsilon_{0}$ is `mass_to_light_ratio_base`, the mass-to-light ratio at the reference radius [dimensionless]. $\sigma$ is the sigma value of the Gaussian [arc-seconds]. - $r$ is the radius from the centre of the profile [arc-seconds]. - $R_{ref}$ is the reference radius where the mass-to-light ratio is equal to $\Psi_{o} [arc-seconds]. - $\Tau$ is the mass-to-light gradient of the profile [dimensionless]. + $R_{\rm ref}$ is `mass_to_light_radius`, the reference radius [arc-seconds]. + $\Gamma$ is `mass_to_light_gradient` [dimensionless]. + + A positive `mass_to_light_gradient` means the mass-to-light ratio falls outward: Gaussians with + `sigma` below the reference radius carry more mass per unit light than those above it. This is the + same convention as `SersicGradient`, so the same positive gradient makes both profiles' mass-to-light + ratio fall with radius. Parameters ---------- @@ -40,14 +46,13 @@ def __init__( sigma The sigma value of the Gaussian. mass_to_light_ratio_base - The base mass-to-light ratio of the profile, which is the mass-to-light ratio of the Gaussian before it - is scaled by values that adjust its mass-to-light ratio based on the reference radius and gradient. + The mass-to-light ratio of a Gaussian whose `sigma` equals `mass_to_light_radius`. mass_to_light_gradient - The mass-to-light radial gradient of the profile, whereby positive values means there is more mass - per unit light within the reference radius. + The power-law slope of the mass-to-light ratio with `sigma`, M/L proportional to + `(sigma / mass_to_light_radius) ** -mass_to_light_gradient`: positive values mean the mass-to-light + ratio falls outward. mass_to_light_radius - The radius where the mass-to-light ratio is equal to the base mass-to-light ratio, such that there will be - more of less mass per unit light within this radius depending on the mass-to-light gradient. + The reference radius at which the mass-to-light ratio equals `mass_to_light_ratio_base`. """ self.mass_to_light_ratio_base = mass_to_light_ratio_base @@ -55,8 +60,7 @@ def __init__( self.mass_to_light_radius = mass_to_light_radius mass_to_light_ratio = ( self.mass_to_light_ratio_base - * ((sigma + 0.01) / self.mass_to_light_radius) - ** self.mass_to_light_gradient + * (sigma / self.mass_to_light_radius) ** -self.mass_to_light_gradient ) super().__init__( diff --git a/autogalaxy/profiles/mass/stellar/sersic.py b/autogalaxy/profiles/mass/stellar/sersic.py index 2a759114c..da2877b8c 100644 --- a/autogalaxy/profiles/mass/stellar/sersic.py +++ b/autogalaxy/profiles/mass/stellar/sersic.py @@ -23,89 +23,71 @@ def cse_settings_from( decomposition resolution (``total_cses`` / ``sample_points``) used to decompose a Sersic convergence profile into cored steep ellipsoids. - The standard (``mass_to_light_gradient <= 0.5``) path is written branch-free - using ``xp`` (``jnp.where`` / ``jnp.log10`` under JAX) so it traces cleanly - inside ``jax.jit`` when ``sersic_index`` / ``effective_radius`` are tracers: - Python ``if`` statements cannot branch on a tracer value, and ``jax.jit`` - requires static array shapes, so ``total_cses`` / ``sample_points`` are frozen - to the conservative maximum previously used (50 / 80) rather than tuned per - Sersic index. The per-index ``upper_dex`` / ``lower_dex`` *ranges* are preserved - exactly via ``xp.where`` so accuracy is unchanged (only the CSE count can rise). - - The ``mass_to_light_gradient > 0.5`` path (radial-gradient Sersic profiles) is - left as the original NumPy branching — it is only reached with a concrete - gradient value and is not part of the standard JAX-traced Sersic path. + Both the standard (``mass_to_light_gradient <= 0.5``) ranges and the steep-gradient + (``mass_to_light_gradient > 0.5``, radial-gradient Sersic profiles) ranges are written + branch-free using ``xp`` (``jnp.where`` / ``jnp.log10`` under JAX), and the two are + selected with ``xp.where`` on the gradient. This lets the profile trace inside + ``jax.jit`` when ``sersic_index``, ``effective_radius`` or ``mass_to_light_gradient`` + are tracers: Python ``if`` statements cannot branch on a tracer value. ``jax.jit`` + also requires static array shapes, so ``total_cses`` / ``sample_points`` are frozen + to the conservative maximum previously used on either path (50 / 80) rather than + tuned per Sersic index. The per-index ``upper_dex`` / ``lower_dex`` *ranges* are + preserved exactly, so accuracy is unchanged (only the CSE count can rise). """ - if mass_to_light_gradient > 0.5: - if effective_radius > 0.2: - lower_dex = 6.0 - upper_dex = np.min( - [np.log10((18.0 / sersic_constant) ** sersic_index), 1.1] - ) - - if sersic_index <= 1.2: - total_cses = 50 - sample_points = 80 - elif sersic_index > 3.8: - total_cses = 40 - sample_points = 50 - lower_dex = 6.5 - else: - total_cses = 30 - sample_points = 50 - - else: - if sersic_index <= 1.2: - upper_dex = 1.0 - total_cses = 50 - sample_points = 80 - lower_dex = 4.5 - - elif sersic_index > 3.8: - total_cses = 40 - sample_points = 50 - lower_dex = 6.0 - upper_dex = 1.5 - else: - upper_dex = 1.1 - lower_dex = 6.0 - total_cses = 30 - sample_points = 50 - else: - # Static shapes for jax.jit: frozen to the former per-index maximum. - total_cses = 50 - sample_points = 80 - - log_effective_radius = xp.log10(effective_radius) - base_upper_dex = xp.minimum( - xp.log10((23.0 / sersic_constant) ** sersic_index), - 0.85 - log_effective_radius, - ) - - # upper_dex / lower_dex select the same per-index ranges as the original - # if/elif ladder, expressed branch-free so a tracer sersic_index traces. - upper_dex = xp.where( - sersic_index <= 0.8, - xp.log10((16.0 / sersic_constant) ** sersic_index), + # Static shapes for jax.jit: frozen to the former per-index maximum. + total_cses = 50 + sample_points = 80 + + # Standard ranges (mass_to_light_gradient <= 0.5). + log_effective_radius = xp.log10(effective_radius) + base_upper_dex = xp.minimum( + xp.log10((23.0 / sersic_constant) ** sersic_index), + 0.85 - log_effective_radius, + ) + + # upper_dex / lower_dex select the same per-index ranges as the original + # if/elif ladder, expressed branch-free so a tracer sersic_index traces. + standard_upper_dex = xp.where( + sersic_index <= 0.8, + xp.log10((16.0 / sersic_constant) ** sersic_index), + xp.where( + sersic_index <= 0.9, + xp.log10((18.0 / sersic_constant) ** sersic_index), + base_upper_dex, + ), + ) + standard_lower_dex = xp.where( + sersic_index <= 0.8, + 4.0 + log_effective_radius, + xp.where( + sersic_index <= 0.9, + 4.3 + log_effective_radius, xp.where( - sersic_index <= 0.9, - xp.log10((18.0 / sersic_constant) ** sersic_index), - base_upper_dex, + sersic_index > 3.8, + 4.5 + log_effective_radius, + 3.5 + log_effective_radius, ), - ) - lower_dex = xp.where( - sersic_index <= 0.8, - 4.0 + log_effective_radius, - xp.where( - sersic_index <= 0.9, - 4.3 + log_effective_radius, - xp.where( - sersic_index > 3.8, - 4.5 + log_effective_radius, - 3.5 + log_effective_radius, - ), - ), - ) + ), + ) + + # Steep-gradient ranges (mass_to_light_gradient > 0.5), the same values as the + # former nested if/else on effective_radius > 0.2 and the Sersic index. + large_upper_dex = xp.minimum( + xp.log10((18.0 / sersic_constant) ** sersic_index), 1.1 + ) + large_lower_dex = xp.where(sersic_index > 3.8, 6.5, 6.0) + small_upper_dex = xp.where( + sersic_index <= 1.2, 1.0, xp.where(sersic_index > 3.8, 1.5, 1.1) + ) + small_lower_dex = xp.where(sersic_index <= 1.2, 4.5, 6.0) + + is_large = effective_radius > 0.2 + gradient_upper_dex = xp.where(is_large, large_upper_dex, small_upper_dex) + gradient_lower_dex = xp.where(is_large, large_lower_dex, small_lower_dex) + + is_steep_gradient = mass_to_light_gradient > 0.5 + upper_dex = xp.where(is_steep_gradient, gradient_upper_dex, standard_upper_dex) + lower_dex = xp.where(is_steep_gradient, gradient_lower_dex, standard_lower_dex) return upper_dex, lower_dex, total_cses, sample_points diff --git a/autogalaxy/profiles/mass/stellar/sersic_gradient.py b/autogalaxy/profiles/mass/stellar/sersic_gradient.py index 422a53e97..b98ba2030 100644 --- a/autogalaxy/profiles/mass/stellar/sersic_gradient.py +++ b/autogalaxy/profiles/mass/stellar/sersic_gradient.py @@ -19,8 +19,20 @@ def __init__( mass_to_light_ratio: float = 1.0, mass_to_light_gradient: float = 0.0, ): - """ - Setup a Sersic mass and light profiles. + r""" + A Sersic stellar mass profile whose mass-to-light ratio varies as a power law of radius. + + The convergence is the Sersic light profile multiplied by a radially varying mass-to-light ratio: + + $\kappa(R) = \Upsilon \left( \frac{R}{R_{\rm e}} \right)^{-\Gamma} I(R)$ + + where $R$ is the same (circularised) radius the light profile $I(R)$ is evaluated at, $R_{\rm e}$ is + the effective radius, $\Upsilon$ is `mass_to_light_ratio` and $\Gamma$ is `mass_to_light_gradient`. + + The mass-to-light ratio therefore equals `mass_to_light_ratio` exactly on the ellipse of the effective + radius, for any axis ratio. A positive `mass_to_light_gradient` means the mass-to-light ratio falls + outward (more mass per unit light inside the effective radius); a negative value means it rises + outward. This is the same convention as `GaussianGradient`. Parameters ---------- @@ -35,9 +47,10 @@ def __init__( sersic_index Controls the concentration of the profile (lower -> less concentrated, higher -> more concentrated). mass_to_light_ratio - The mass-to-light ratio of the light profile. + The mass-to-light ratio on the ellipse of the effective radius. mass_to_light_gradient - The mass-to-light radial gradient. + The power-law slope $\Gamma$ of the mass-to-light ratio with radius, M/L proportional to + $(R / R_{\rm e})^{-\Gamma}$: positive values mean the mass-to-light ratio falls outward. """ super().__init__( centre=centre, @@ -62,17 +75,14 @@ def convergence_2d_from(self, grid: aa.type.Grid2DLike, xp=np, **kwargs): """ return self.convergence_func( - self.eccentric_radii_grid_from(grid=grid, xp=xp, **kwargs) + self.eccentric_radii_grid_from(grid=grid, xp=xp, **kwargs), xp=xp ) def convergence_func(self, grid_radius: float, xp=np) -> float: return ( self.mass_to_light_ratio - * ( - ((self.axis_ratio() * grid_radius) / self.effective_radius) - ** -self.mass_to_light_gradient - ) - * self.image_2d_via_radii_from(grid_radius) + * ((grid_radius / self.effective_radius) ** -self.mass_to_light_gradient) + * self.image_2d_via_radii_from(grid_radius, xp=xp) ) def decompose_convergence_via_mge(self, **kwargs): @@ -83,10 +93,7 @@ def sersic_gradient_2D(r): return ( self.mass_to_light_ratio * self.intensity - * ( - ((self.axis_ratio() * r) / self.effective_radius) - ** -self.mass_to_light_gradient - ) + * ((r / self.effective_radius) ** -self.mass_to_light_gradient) * np.exp( -self.sersic_constant * (((r / self.effective_radius) ** (1.0 / self.sersic_index)) - 1.0) @@ -143,10 +150,7 @@ def sersic_gradient_2D(r): return ( self.mass_to_light_ratio * self.intensity - * ( - ((self.axis_ratio(xp) * r) / scaled_effective_radius) - ** -self.mass_to_light_gradient - ) + * ((r / scaled_effective_radius) ** -self.mass_to_light_gradient) * xp.exp( -self.sersic_constant * ( @@ -192,7 +196,9 @@ def __init__( mass_to_light_ratio The mass-to-light ratio of the light profile. mass_to_light_gradient - The mass-to-light radial gradient. + The power-law slope of the mass-to-light ratio with radius, M/L proportional to + `(R / effective_radius) ** -mass_to_light_gradient`: positive values mean the mass-to-light ratio + falls outward. """ super().__init__( centre=centre, diff --git a/test_autogalaxy/profiles/mass/stellar/test_gaussian_gradient.py b/test_autogalaxy/profiles/mass/stellar/test_gaussian_gradient.py index b5185a989..d3d342952 100644 --- a/test_autogalaxy/profiles/mass/stellar/test_gaussian_gradient.py +++ b/test_autogalaxy/profiles/mass/stellar/test_gaussian_gradient.py @@ -45,4 +45,42 @@ def test__init_constructor__gradient_1_scales_values_correctly(): assert mp.mass_to_light_ratio_base == 1.0 assert mp.mass_to_light_gradient == 1.0 assert mp.mass_to_light_radius == 5.0 - assert mp.mass_to_light_ratio == 0.602 + # (sigma / mass_to_light_radius) ** -mass_to_light_gradient = (3 / 5) ** -1 + assert mp.mass_to_light_ratio == pytest.approx(5.0 / 3.0, 1.0e-8) + + +def test__mass_to_light_ratio__equals_base_at_reference_radius(): + mp = ag.mp.GaussianGradient( + sigma=0.7, + mass_to_light_ratio_base=2.0, + mass_to_light_gradient=1.3, + mass_to_light_radius=0.7, + ) + + assert mp.mass_to_light_ratio == pytest.approx(2.0, 1.0e-12) + + +def test__mass_to_light_ratio__same_sign_convention_as_sersic_gradient(): + # The same positive gradient makes both profiles' M/L fall outward, with the same power law. + gaussian_ratios = [ + ag.mp.GaussianGradient( + sigma=sigma, + mass_to_light_ratio_base=1.0, + mass_to_light_gradient=1.0, + mass_to_light_radius=1.0, + ).mass_to_light_ratio + for sigma in (0.5, 2.0) + ] + + sersic_kwargs = dict( + intensity=1.0, effective_radius=1.0, sersic_index=2.0, mass_to_light_ratio=1.0 + ) + grid = ag.Grid2DIrregular([[0.0, 0.5], [0.0, 2.0]]) + sersic_ratios = np.asarray( + ag.mp.SersicGradient( + **sersic_kwargs, mass_to_light_gradient=1.0 + ).convergence_2d_from(grid=grid) + ) / np.asarray(ag.mp.Sersic(**sersic_kwargs).convergence_2d_from(grid=grid)) + + assert gaussian_ratios == pytest.approx([2.0, 0.5], 1.0e-12) + assert sersic_ratios == pytest.approx(gaussian_ratios, 1.0e-6) diff --git a/test_autogalaxy/profiles/mass/stellar/test_sersic_gradient.py b/test_autogalaxy/profiles/mass/stellar/test_sersic_gradient.py index c011d8ae6..9f2169c0d 100644 --- a/test_autogalaxy/profiles/mass/stellar/test_sersic_gradient.py +++ b/test_autogalaxy/profiles/mass/stellar/test_sersic_gradient.py @@ -38,7 +38,7 @@ def test__deflections_2d_via_cse_from__gradient_1(): mass_to_light_gradient=1.0, ) - deflections_via_integral = np.array([[3.60324873535244, 2.3638898009652]]) + deflections_via_integral = np.array([[2.882598979610964, 1.8911115687770221]]) deflections_via_cse = mp.deflections_2d_via_cse_from( grid=ag.Grid2DIrregular([[0.1625, 0.1625]]) ) @@ -57,7 +57,7 @@ def test__deflections_2d_via_cse_from__gradient_neg_1(): mass_to_light_gradient=-1.0, ) - deflections_via_integral = np.array([[0.97806399756448, 0.725459334118341]]) + deflections_via_integral = np.array([[1.222580447264419, 0.9068235185942723]]) deflections_via_cse = mp.deflections_2d_via_cse_from( grid=ag.Grid2DIrregular([[0.1625, 0.1625]]) ) @@ -114,7 +114,7 @@ def test__deflections_yx_2d_from__elliptical_vs_spherical(): def test__convergence_2d_from__sersic_gradient_config_1(): - # ((axis_ratio*radius/effective_radius)**-mass_to_light_gradient) = (1/0.6)**-1.0 = 0.6 + # ((radius/effective_radius)**-mass_to_light_gradient) = (1/0.6)**-1.0 = 0.6 mp = ag.mp.SersicGradient( centre=(0.0, 0.0), ell_comps=(0.0, 0.0), @@ -131,7 +131,7 @@ def test__convergence_2d_from__sersic_gradient_config_1(): def test__convergence_2d_from__sersic_gradient_config_2(): - # ((axis_ratio*radius/effective_radius)**-mass_to_light_gradient) = (1.5/2.0)**1.0 = 0.75 + # ((radius/effective_radius)**-mass_to_light_gradient) = (1.5/2.0)**1.0 = 0.75 mp = ag.mp.SersicGradient( ell_comps=(0.0, 0.0), @@ -178,7 +178,7 @@ def test__convergence_2d_from__sersic_gradient_mass_to_light_2(): def test__convergence_2d_from__sersic_gradient_elliptical(): - # ((axis_ratio*radius/effective_radius)**-mass_to_light_gradient) = ((0.5*1.41)/2.0)**-1.0 = 2.836 + # M/L = (radius / effective_radius) ** -mass_to_light_gradient, radius the eccentric radius. mp = ag.mp.SersicGradient( ell_comps=(0.0, 0.333333), intensity=3.0, @@ -190,7 +190,22 @@ def test__convergence_2d_from__sersic_gradient_elliptical(): convergence = mp.convergence_2d_from(grid=ag.Grid2DIrregular([[1.0, 0.0]])) - assert convergence == pytest.approx(2.836879 * 5.38066670129, abs=2e-01) + sersic = ag.mp.Sersic( + ell_comps=(0.0, 0.333333), + intensity=3.0, + effective_radius=2.0, + sersic_index=2.0, + mass_to_light_ratio=1.0, + ) + grid_eval = ag.Grid2DIrregular([[1.0, 0.0]]) + radius = sersic.eccentric_radii_grid_from( + grid=sersic.transformed_to_reference_frame_grid_from(grid=grid_eval) + )[0] + + assert convergence == pytest.approx( + (radius / 2.0) ** -1.0 * sersic.convergence_2d_from(grid=grid_eval)[0], 1e-6 + ) + assert convergence == pytest.approx(7.6097141, 1e-4) def test__convergence_2d_from__elliptical_vs_spherical(): @@ -321,3 +336,106 @@ def test__compare_to_sersic__gradient_0_vs_sersic(): sersic_grad_deflections.array[0, 1], 1e-3 ) assert sersic_deflections[0, 1] == pytest.approx(0.79374, 1e-3) + + +def test__mass_to_light_ratio__equals_input_on_effective_radius_ellipse(): + # The pivot of the gradient is the ellipse of the effective radius for any axis ratio, so the + # mass-to-light ratio there equals `mass_to_light_ratio` (it used to be R_e / q). + ell_comps = ag.convert.ell_comps_from(axis_ratio=0.6, angle=30.0) + + kwargs = dict( + centre=(0.1, -0.2), + ell_comps=ell_comps, + intensity=1.0, + effective_radius=1.0, + sersic_index=2.0, + mass_to_light_ratio=2.0, + ) + + sersic = ag.mp.Sersic(**kwargs) + + # Points on the effective-radius ellipse: the eccentric radius is linear in the distance from + # the centre, so scaling each offset by its eccentric radius puts it on the R_e = 1 ellipse. + centre = np.array([0.1, -0.2]) + phi = np.linspace(0.0, 2.0 * np.pi, 7, endpoint=False) + offsets = np.stack([np.sin(phi), np.cos(phi)], axis=1) + def eccentric_radii_from(grid): + return np.asarray( + sersic.eccentric_radii_grid_from( + grid=sersic.transformed_to_reference_frame_grid_from(grid=grid) + ) + ) + + eccentric_radii = eccentric_radii_from(ag.Grid2DIrregular(centre + offsets)) + grid = ag.Grid2DIrregular(centre + offsets / eccentric_radii[:, None]) + + assert eccentric_radii_from(grid) == pytest.approx(1.0, 1.0e-6) + + for mass_to_light_gradient in (-1.0, 0.5, 1.5): + gradient = ag.mp.SersicGradient( + **kwargs, mass_to_light_gradient=mass_to_light_gradient + ) + + ratio = np.asarray(gradient.convergence_2d_from(grid=grid)) / np.asarray( + sersic.convergence_2d_from(grid=grid) + ) + + assert ratio == pytest.approx(1.0, 1.0e-6) + + +def test__mass_to_light_ratio__positive_gradient_falls_outward(): + # Positive gradient: M/L falls outward, M/L(R) = M/L * (R / R_e) ** -gradient. + kwargs = dict( + intensity=1.0, effective_radius=1.0, sersic_index=2.0, mass_to_light_ratio=1.0 + ) + + gradient = ag.mp.SersicGradient(**kwargs, mass_to_light_gradient=1.0) + sersic = ag.mp.Sersic(**kwargs) + + grid = ag.Grid2DIrregular([[0.0, 0.5], [0.0, 2.0]]) + + ratio = np.asarray(gradient.convergence_2d_from(grid=grid)) / np.asarray( + sersic.convergence_2d_from(grid=grid) + ) + + assert ratio == pytest.approx([2.0, 0.5], 1.0e-6) + + +@pytest.mark.parametrize("mass_to_light_gradient", [0.2, 1.0]) +def test__jax_jit__free_gradient_likelihood_matches_numpy(mass_to_light_gradient): + # A free (traced) gradient used to raise a tracer-boolean error in `cse_settings_from`. + # Both values cover both sides of its `mass_to_light_gradient > 0.5` selection. + jax = pytest.importorskip("jax") + import jax.numpy as jnp + + grid = ag.Grid2D.uniform(shape_native=(8, 8), pixel_scales=0.2) + + def make(gradient): + return ag.mp.SersicGradient( + centre=(0.05, -0.05), + ell_comps=(0.1, 0.05), + intensity=1.0, + effective_radius=0.8, + sersic_index=2.5, + mass_to_light_ratio=1.0, + mass_to_light_gradient=gradient, + ) + + data = np.asarray( + make(0.6).deflections_yx_2d_from(grid=grid).array + ) + + @jax.jit + def log_likelihood(gradient): + deflections = make(gradient).deflections_yx_2d_from(grid=grid, xp=jnp) + return -0.5 * jnp.sum((deflections.array - data) ** 2) + + deflections_np = np.asarray( + make(mass_to_light_gradient).deflections_yx_2d_from(grid=grid).array + ) + log_likelihood_np = -0.5 * np.sum((deflections_np - data) ** 2) + + log_likelihood_jax = log_likelihood(jnp.array(mass_to_light_gradient)) + + assert np.isfinite(float(log_likelihood_jax)) + assert float(log_likelihood_jax) == pytest.approx(log_likelihood_np, rel=1.0e-3)