jax-ml/jax · error · ValueError
Weights cannot be complex types.
Error message
Weights cannot be complex types.
What it means
Weighted quantiles in JAX (_quantile with weights) only accept real-valued weights; complex weights are rejected because quantile weighting has no meaningful complex interpretation.
Source
Thrown at jax/_src/numpy/reductions.py:2521
>>> jnp.nanquantile(x, 0.5, weights=weights, method='inverted_cdf')
Array(4.0, dtype=float32)
"""
a, q = ensure_arraylike("nanquantile", a, q)
if weights is not None:
weights = ensure_arraylike("nanquantile", weights)
if overwrite_input or out is not None:
msg = ("jax.numpy.nanquantile does not support overwrite_input=True or "
"out != None")
raise ValueError(msg)
return _quantile(a, q, axis, method, keepdims, True, weights)
def _quantile(a: Array, q: Array, axis: int | tuple[int, ...] | None,
method: str, keepdims: bool, squash_nans: bool, weights: Array | None = None) -> Array:
if method not in ["linear", "lower", "higher", "midpoint", "nearest", "inverted_cdf"]:
raise ValueError("method can only be 'linear', 'lower', 'higher', 'midpoint', 'nearest' or 'inverted_cdf'")
if weights is not None:
if dtypes.issubdtype(weights.dtype, np.complexfloating):
raise ValueError("Weights cannot be complex types.")
if method != "inverted_cdf":
raise NotImplementedError(f"{method} doesn't support weights. Only method 'inverted_cdf' supports weights.")
a, weights = promote_dtypes_inexact(a, weights)
if weights.shape != a.shape:
if axis is None:
raise ValueError("Weights shape must match 'a' shape when axis is None.")
ax_tuple = canonicalize_axis_tuple(axis, a.ndim)
if weights.shape != tuple(a.shape[ax] for ax in ax_tuple):
raise ValueError(f"Weights shape {weights.shape} must match reduction axes "
f"{tuple(a.shape[ax] for ax in ax_tuple)}")
weights = lax.broadcast_in_dim(weights, a.shape, broadcast_dimensions=ax_tuple)
else:
a, = promote_dtypes_inexact(a)
keepdim = []
if dtypes.issubdtype(a.dtype, np.complexfloating):
raise ValueError("quantile does not support complex input, as the operation is poorly defined.")
if axis is None:
if keepdims:View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Convert weights to real: weights=jnp.abs(w) or w.real
- Verify weight dtype before the call in pipelines that mix complex and real data
Example fix
// before jnp.quantile(a, q, weights=complex_w, method='inverted_cdf') // after jnp.quantile(a, q, weights=jnp.abs(complex_w), method='inverted_cdf')
Defensive patterns
Strategy: type-guard
Validate before calling
import jax.numpy as jnp, numpy as np
if np.issubdtype(weights.dtype, np.complexfloating):
weights = jnp.abs(weights) Type guard
import numpy as np
def is_real_weights(w) -> bool:
return not np.issubdtype(w.dtype, np.complexfloating) Prevention
- Always materialize weights via jnp.abs or .real for spectral data
- Validate weight dtype at pipeline entry
When it happens
Trigger: Calling jnp.quantile(a, q, weights=w, method='inverted_cdf') where w has a complex dtype (complex64/complex128).
Common situations: Weights derived from complex spectra or FFT outputs without taking magnitudes; dtype promotion bugs producing complex weights unexpectedly.
Related errors
- {method} doesn't support weights. Only method 'inverted_cdf'
- Weights shape must match 'a' shape when axis is None.
- Weights shape {weights.shape} must match reduction axes {tup
- expected a 1-d array for weights
- expected w and y to have the same length
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/fefb2eae95a62e32.
Report an issue: GitHub.