jax-ml/jax · error · ValueError

jnp.interp: complex x values not supported.

Error message

jnp.interp: complex x values not supported.

What it means

jnp.interp does not support complex interpolation points (x); the underlying sort and search must operate on real values. Only real-valued x arrays are accepted.

Source

Thrown at jax/_src/numpy/lax_numpy.py:2622

  del x, xp, fp

  if isinstance(left, str):
    if left != 'extrapolate':
      raise ValueError("the only valid string value of `left` is "
                       f"'extrapolate', but got: {left!r}")
    extrapolate_left = True
  else:
    extrapolate_left = False
  if isinstance(right, str):
    if right != 'extrapolate':
      raise ValueError("the only valid string value of `right` is "
                       f"'extrapolate', but got: {right!r}")
    extrapolate_right = True
  else:
    extrapolate_right = False

  if dtypes.issubdtype(x_arr.dtype, np.complexfloating):
    raise ValueError("jnp.interp: complex x values not supported.")

  if period is not None:
    if np.ndim(period) != 0:
      raise ValueError(f"period must be a scalar; got {period}")
    period = ufuncs.abs(period)
    x_arr = x_arr % period
    xp_arr = xp_arr % period
    xp_arr, fp_arr = lax.sort_key_val(xp_arr, fp_arr)
    xp_arr = concatenate([xp_arr[-1:] - period, xp_arr, xp_arr[:1] + period])
    fp_arr = concatenate([fp_arr[-1:], fp_arr, fp_arr[:1]])

  i = clip(searchsorted(xp_arr, x_arr, side='right'), 1, len(xp_arr) - 1)
  df = fp_arr[i] - fp_arr[i - 1]
  dx = xp_arr[i] - xp_arr[i - 1]
  delta = x_arr - xp_arr[i - 1]

  epsilon = np.spacing(np.finfo(xp_arr.dtype).eps)
  dx0 = lax.abs(dx) <= epsilon  # Prevent NaN gradients when `dx` is small.

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Take the real part: jnp.interp(jnp.real(x), xp, fp)
  2. Cast x to real: x.astype(jnp.float32) before calling
  3. Trace upstream dtype promotion making x complex

Example fix

// before
jnp.interp(z, xp, fp)  # z is complex
// after
jnp.interp(jnp.real(z), xp, fp)
Defensive patterns

Strategy: validation

Validate before calling

if jnp.iscomplexobj(x):
    x = jnp.real(x)

Type guard

def is_real_array(a) -> bool:
    return not jnp.iscomplexobj(a)

Prevention

When it happens

Trigger: Calling jnp.interp on x with complex dtype, e.g. jnp.interp(jnp.array([1+2j]), xp, fp), including complex inputs that arise from promotion with complex xp/fp via promote_dtypes_inexact.

Common situations: Signal-processing pipelines where x is derived from FFT output or complex-valued features; dtype accidentally promoted to complex by another operand.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/32890a2c83d3b020. Report an issue: GitHub.