{"record":{"id":"4a1e9e05910cdee0","repo":"jax-ml/jax","slug":"two-arguments-must-have-the-same-rank-x-ndim-vs","errorCode":null,"errorMessage":"two-arguments must have the same rank ({x.ndim} vs {y.ndim}).","messagePattern":"two-arguments must have the same rank \\((.+?) vs (.+?)\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/signal.py","lineNumber":668,"sourceCode":"    raise ValueError(\n        f\"Unknown boundary option '{boundary}', \"\n        f\"must be one of: {list(boundary_funcs.keys())}\")\n\n  axis = core.concrete_or_error(operator.index, axis, \"axis of windowed-FFT\")\n  axis = canonicalize_axis(axis, x.ndim)\n\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:","sourceCodeStart":650,"sourceCodeEnd":686,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/signal.py#L650-L686","documentation":"For cross-spectral density, x and y must have the same number of dimensions so their outer (non-transform) axes can be paired and broadcast. A rank mismatch (e.g. 1-D vs 2-D) makes segment-wise pairing ill-defined, so it is rejected. Note the message has an f-string bug: values are not interpolated in some JAX versions.","triggerScenarios":"csd(x, y) where x is shape (1000,) and y is (1, 1000) or (32, 1000); mixing a single channel with a multichannel signal.","commonSituations":"Comparing a reference signal against a batch/channels-first array; forgetting that a leading axis adds a rank.","solutions":["Make ranks equal: add a leading axis with x[None] or remove one with y[0] / squeeze","Broadcast explicitly to a common outer shape before calling csd","Verify x.ndim == y.ndim in a precondition check"],"exampleFix":"// before\nf, P = jax.scipy.signal.csd(x, Y)  # x:(N,), Y:(C,N)\n// after\nf, P = jax.scipy.signal.csd(jnp.broadcast_to(x, Y.shape), Y)","handlingStrategy":"type-guard","validationCode":"if x.ndim != y.ndim:\n    if x.ndim < y.ndim:\n        x = x[None]  # promote, adjust to your layout\n    else:\n        y = y[None]","typeGuard":"def same_rank(a, b):\n    a, b = jnp.asarray(a), jnp.asarray(b)\n    if a.ndim == b.ndim:\n        return a, b\n    raise ValueError(f'rank mismatch: {a.ndim} vs {b.ndim}')","tryCatchPattern":null,"preventionTips":["Check .ndim equality before cross-spectral calls","Be explicit about channel axes: (N,) vs (1, N) vs (C, N)"],"tags":["jax","scipy","csd","rank-mismatch","shape-validation"],"backgroundTag":"wrong-dimensionality-input","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}