Skip to content

fix(materials): a real negative permittivity takes the decaying root under jax 0.12.1 - #257

Merged
yonghakim merged 1 commit into
mainfrom
claude/gifted-lovelace-8g402n
Nov 25, 2026
Merged

yonghakim merged 1 commit into
mainfrom
claude/gifted-lovelace-8g402n

Conversation

@yonghakim

Copy link
Copy Markdown
Member

Under jax and jaxlib 0.12.1, refractive_index_from_eps returned the gain root for a real negative permittivity: refractive_index_from_eps(jnp.asarray(-2.0)) gave −1.4142j, eagerly and under jax.jit. A user installing the newest jax got a gain medium for a lossless metal written as a real number. This is one of the three failures #251 found on 0.12.1.

Cause and fix

  • Cause. The real array was promoted to complex as eps * (1 + 0j). jax 0.12.1 forms a real-times-complex product as (x * 1, x * 0), so a negative x gets the imaginary part −0.0. That puts the value on the lower lip of the square root's cut, and principal_sqrt, which honours the sign of a zero, returns the gain root. jax 0.12.0.1 gave +0.0.
  • Fix. The promotion is now eps + 0j. It keeps the caller's width (float32 → complex64), and the imaginary part is +0.0 on every backend, since −0.0 + 0.0 and 0.0 + 0.0 are both +0.0. I measured NumPy, PyTorch, and JAX 0.12.0.1 and 0.12.1, eager and jit, float32 and float64. This is the only such promotion in src/.
  • Test. A new test asks for Im(n) > 0 at the caller's width on NumPy, JAX eager, JAX jit and PyTorch, at 32 and 64 bits.

Checks

  • With jax 0.12.0.1 (the recorded version): the 11 test modules that use refractive_index_from_eps give 828 passed and 36 skipped. The quick profile passes (17), and ruff check . passes.
  • With jax 0.12.1 in a separate environment (PYTHONPATH=src, no torch): tests/materials/test_material.py passes. Before the fix, test_refractive_index_from_eps_runs_on_a_jax_array fails there, as it did in CI run 45438350938.

Not done here

  • .github/constraints/ still records jax 0.12.0.1, so this PR's CI runs the fix on that version only; I checked 0.12.1 locally.
  • The other two failures on 0.12.1 are not fixed:
    • test_a_layer_with_no_chain_keeps_its_derivative_bits reproduces locally. The forward value keeps its bits, but the jit gradient with and without the chain capacity differs by 1–2 ulp (3.7e-16 relative on ETM, 1.7e-15 on SMM). The difference goes away when the chain term is computed but not added; selecting L instead of adding −0 does not restore the bits. Finding the cause needs the compiled backward pass.
    • test_a_traced_uniform_twin_is_the_eager_answer[theta 1.4] (1.0168e-13 on CI, against a bound of 1e-13) does not reproduce here. Traced against eager is 3.5e-14 with 0.12.1, and 6.4e-14 with XLA and OpenBLAS limited to AVX2.

Generated by Claude Code

…under jax 0.12.1

refractive_index_from_eps promoted a real array to complex as eps * (1 + 0j). jax and
jaxlib 0.12.1 form that real-times-complex product as (x * 1, x * 0), and x * 0 is -0.0 for
a negative x: the value then sits on the lower lip of the square root's cut, and
principal_sqrt -- which honours the sign of a zero imaginary part -- returns the gain root.
refractive_index_from_eps(jnp.asarray(-2.0)) gave -1.4142j, eagerly and under jax.jit, where
0.12.0.1 gave +1.4142j. A user installing the newest jax got a gain medium for a lossless
metal written as a real number.

The promotion is now eps + 0j: the same width-preserving promotion (float32 -> complex64),
and an imaginary part of +0.0 on every backend, since -0.0 + 0.0 and 0.0 + 0.0 are both
+0.0. Measured: NumPy, PyTorch, and JAX 0.12.0.1 and 0.12.1, eager and jit, float32 and
float64 -- no sign bit set. It is the only such promotion in src/.

A new test asks for Im(n) > 0 and the caller's width on NumPy, JAX eager, JAX jit and
PyTorch at 32 and 64 bits.

Checked: tests/materials/test_material.py with jax 0.12.0.1 (the recorded version; 40 + 8
passed) and with jax 0.12.1 in a separate environment, PYTHONPATH=src, no torch (the jax
cases pass, torch skipped). Before the fix, test_refractive_index_from_eps_runs_on_a_jax_array
fails there, as it did in CI run 45438350938.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RKzHyBMczzzhKMXEVgazPy
@yonghakim
yonghakim merged commit 11f3343 into main Nov 25, 2026
59 checks passed
@yonghakim
yonghakim deleted the claude/gifted-lovelace-8g402n branch November 25, 2026 05:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants