{"record":{"id":"fefb2eae95a62e32","repo":"jax-ml/jax","slug":"weights-cannot-be-complex-types","errorCode":null,"errorMessage":"Weights cannot be complex types.","messagePattern":"Weights cannot be complex types\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/reductions.py","lineNumber":2521,"sourceCode":"    >>> jnp.nanquantile(x, 0.5, weights=weights, method='inverted_cdf')\n    Array(4.0, dtype=float32)\n  \"\"\"\n  a, q = ensure_arraylike(\"nanquantile\", a, q)\n  if weights is not None:\n    weights = ensure_arraylike(\"nanquantile\", weights)\n  if overwrite_input or out is not None:\n    msg = (\"jax.numpy.nanquantile does not support overwrite_input=True or \"\n           \"out != None\")\n    raise ValueError(msg)\n  return _quantile(a, q, axis, method, keepdims, True, weights)\n\ndef _quantile(a: Array, q: Array, axis: int | tuple[int, ...] | None,\n              method: str, keepdims: bool, squash_nans: bool, weights: Array | None = None) -> Array:\n  if method not in [\"linear\", \"lower\", \"higher\", \"midpoint\", \"nearest\", \"inverted_cdf\"]:\n    raise ValueError(\"method can only be 'linear', 'lower', 'higher', 'midpoint', 'nearest' or 'inverted_cdf'\")\n  if weights is not None:\n    if dtypes.issubdtype(weights.dtype, np.complexfloating):\n      raise ValueError(\"Weights cannot be complex types.\")\n    if method != \"inverted_cdf\":\n      raise NotImplementedError(f\"{method} doesn't support weights. Only method 'inverted_cdf' supports weights.\")\n    a, weights = promote_dtypes_inexact(a, weights)\n    if weights.shape != a.shape:\n      if axis is None:\n        raise ValueError(\"Weights shape must match 'a' shape when axis is None.\")\n      ax_tuple = canonicalize_axis_tuple(axis, a.ndim)\n      if weights.shape != tuple(a.shape[ax] for ax in ax_tuple):\n        raise ValueError(f\"Weights shape {weights.shape} must match reduction axes \"\n                          f\"{tuple(a.shape[ax] for ax in ax_tuple)}\")\n      weights = lax.broadcast_in_dim(weights, a.shape, broadcast_dimensions=ax_tuple)\n  else:\n    a, = promote_dtypes_inexact(a)\n  keepdim = []\n  if dtypes.issubdtype(a.dtype, np.complexfloating):\n    raise ValueError(\"quantile does not support complex input, as the operation is poorly defined.\")\n  if axis is None:\n    if keepdims:","sourceCodeStart":2503,"sourceCodeEnd":2539,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/reductions.py#L2503-L2539","documentation":"Weighted quantiles in JAX (_quantile with weights) only accept real-valued weights; complex weights are rejected because quantile weighting has no meaningful complex interpretation.","triggerScenarios":"Calling jnp.quantile(a, q, weights=w, method='inverted_cdf') where w has a complex dtype (complex64/complex128).","commonSituations":"Weights derived from complex spectra or FFT outputs without taking magnitudes; dtype promotion bugs producing complex weights unexpectedly.","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"],"exampleFix":"// before\njnp.quantile(a, q, weights=complex_w, method='inverted_cdf')\n// after\njnp.quantile(a, q, weights=jnp.abs(complex_w), method='inverted_cdf')","handlingStrategy":"type-guard","validationCode":"import jax.numpy as jnp, numpy as np\n\nif np.issubdtype(weights.dtype, np.complexfloating):\n    weights = jnp.abs(weights)","typeGuard":"import numpy as np\n\ndef is_real_weights(w) -> bool:\n    return not np.issubdtype(w.dtype, np.complexfloating)","tryCatchPattern":null,"preventionTips":["Always materialize weights via jnp.abs or .real for spectral data","Validate weight dtype at pipeline entry"],"tags":["jax","numpy","quantile","weights","complex-numbers"],"backgroundTag":"unsupported-dtype-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}