{"record":{"id":"84ab41266c762cb7","repo":"jax-ml/jax","slug":"x-and-y-cannot-be-broadcast-together","errorCode":null,"errorMessage":"x and y cannot be broadcast together.","messagePattern":"x and y cannot be broadcast together\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/signal.py","lineNumber":674,"sourceCode":"\n  if y is None:\n    check_arraylike('spectral_helper', x)\n    x, = promote_dtypes_inexact(x)\n    y_arr = x  # place-holder for type checking\n    outershape = tuple_delete(x.shape, axis)\n  else:\n    if mode != 'psd':\n      raise ValueError(\"two-argument mode is available only when mode=='psd'\")\n    check_arraylike('spectral_helper', x, y)\n    x, y_arr = promote_dtypes_inexact(x, y)\n    if x.ndim != y_arr.ndim:\n      raise ValueError(\"two-arguments must have the same rank ({x.ndim} vs {y.ndim}).\")\n    # Check if we can broadcast the outer axes together\n    try:\n      outershape = jnp.broadcast_shapes(tuple_delete(x.shape, axis),\n                                        tuple_delete(y_arr.shape, axis))\n    except ValueError as err:\n      raise ValueError('x and y cannot be broadcast together.') from err\n\n  result_dtype = dtypes.to_complex_dtype(x.dtype)\n  freq_dtype = np.finfo(result_dtype).dtype\n\n  nperseg_int: int = 0\n  nfft_int: int = 0\n  noverlap_int: int = 0\n\n  if nperseg is not None:  # if specified by user\n    nperseg_int = core.concrete_or_error(\n        int, nperseg, \"nperseg of windowed-FFT\")\n    if nperseg_int < 1:\n      raise ValueError('nperseg must be a positive integer')\n  # parse window; if array like, then set nperseg = win.shape\n  win, nperseg_int = signal_helper._triage_segments(\n      window, nperseg if nperseg is None else nperseg_int,\n      input_length=x.shape[axis], dtype=x.dtype)\n","sourceCodeStart":656,"sourceCodeEnd":692,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/signal.py#L656-L692","documentation":"After rank equality is established, the outer axes (all axes except the transform axis) of x and y must be mutually broadcastable (same length or 1). If not, jnp.broadcast_shapes raises internally and it is re-raised with this clearer message.","triggerScenarios":"csd with x shape (16, 1000) and y shape (8, 1000) — channel counts 16 vs 8 cannot broadcast.","commonSituations":"Multichannel recordings with mismatched channel counts; batches whose leading dims drifted apart after slicing.","solutions":["Slice or reshape both inputs to matching outer dimensions","Pad/repeat the smaller outer dim with jnp.broadcast_to where semantically valid","Add an assertion on outer shapes before calling csd"],"exampleFix":"// before\nf, Pxy = jax.scipy.signal.csd(x, y)  # (16,N) vs (8,N)\n// after\nn = min(x.shape[0], y.shape[0])\nf, Pxy = jax.scipy.signal.csd(x[:n], y[:n])","handlingStrategy":"validation","validationCode":"outershape = jnp.broadcast_shapes(x.shape[:-1], y.shape[:-1])  # raises early if incompatible\nx, y = jnp.broadcast_to(x, outershape + x.shape[-1:]), jnp.broadcast_to(y, outershape + y.shape[-1:])","typeGuard":null,"tryCatchPattern":"try:\n    f, P = jax.scipy.signal.csd(x, y)\nexcept ValueError as e:\n    if 'broadcast' in str(e):\n        n = min(x.shape[0], y.shape[0])\n        f, P = jax.scipy.signal.csd(x[:n], y[:n])\n    else:\n        raise","preventionTips":["Assert matching channel counts before csd","Slice to the common leading dimension when batches differ"],"tags":["jax","scipy","csd","broadcasting","shape-mismatch"],"backgroundTag":"broadcast-shape-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}