{"record":{"id":"7190c0e630082a1c","repo":"jax-ml/jax","slug":"bcoo-slice-indices-must-have-size-mat-ndim-mat-n","errorCode":null,"errorMessage":"bcoo_slice: indices must have size mat.ndim={mat.ndim}","messagePattern":"bcoo_slice: indices must have size mat\\.ndim=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/experimental/sparse/bcoo.py","lineNumber":1984,"sourceCode":"      indices of each slice.\n    limit_indices: sequence of integers of length `mat.ndim` specifying the ending\n      indices of each slice\n    strides: (not implemented) sequence of integers of length `mat.ndim` specifying\n      the stride for each slice\n\n  Returns:\n    out: BCOO array containing the slice.\n  \"\"\"\n  if not isinstance(mat, BCOO):\n    raise TypeError(f\"bcoo_slice: input should be BCOO array, got type(mat)={type(mat)}\")\n  start_indices = [operator.index(i) for i in start_indices]\n  limit_indices = [operator.index(i) for i in limit_indices]\n  if strides is not None:\n    strides = [operator.index(i) for i in strides]\n  else:\n    strides = [1] * mat.ndim\n  if len(start_indices) != len(limit_indices) != len(strides) != mat.ndim:\n    raise ValueError(f\"bcoo_slice: indices must have size mat.ndim={mat.ndim}\")\n  if len(strides) != mat.ndim:\n    raise ValueError(f\"len(strides) = {len(strides)}; expected {mat.ndim}\")\n  if any(s <= 0 for s in strides):\n    raise ValueError(f\"strides must be a sequence of positive integers; got {strides}\")\n\n  if not all(0 <= start <= end <= size\n             for start, end, size in safe_zip(start_indices, limit_indices, mat.shape)):\n    raise ValueError(f\"bcoo_slice: invalid indices. Got {start_indices=}, \"\n                     f\"{limit_indices=} and shape={mat.shape}\")\n\n  start_batch, start_sparse, start_dense = split_list(start_indices, [mat.n_batch, mat.n_sparse])\n  end_batch, end_sparse, end_dense = split_list(limit_indices, [mat.n_batch, mat.n_sparse])\n  stride_batch, stride_sparse, stride_dense = split_list(strides, [mat.n_batch, mat.n_sparse])\n\n  data_slices = []\n  index_slices = []\n  for i, (start, end, stride) in enumerate(zip(start_batch, end_batch, stride_batch)):\n    data_slices.append(slice(None) if mat.data.shape[i] != mat.shape[i] else slice(start, end, stride))","sourceCodeStart":1966,"sourceCodeEnd":2002,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/experimental/sparse/bcoo.py#L1966-L2002","documentation":"bcoo_slice validates that start_indices, limit_indices, and (if given) strides all have length equal to mat.ndim. The chained comparison len(start_indices) != len(limit_indices) != len(strides) != mat.ndim fails when any of these lengths mismatch the array's rank, raising ValueError.","triggerScenarios":"Calling bcoo_slice with start_indices/limit_indices lists whose length differs from mat.ndim, or supplying strides of the wrong length, e.g. slicing a 2-D BCOO with a single (1,) start/limit tuple and no strides handled incorrectly.","commonSituations":"Assuming slicing only applies to sparse/dense dimensions and forgetting batch dimensions; reusing index lists computed for a different-shaped array; passing Python scalars instead of per-dimension sequences.","solutions":["Make all three sequences have exactly mat.ndim entries: one start, one limit, and one stride per dimension","Use strides=None to let bcoo_slice default strides to 1 in every dimension","Build indices programmatically from mat.ndim rather than hardcoding"],"exampleFix":"# before (2-D mat)\nbcoo_slice(mat, start_indices=(0,), limit_indices=(4,), strides=(1,))\n# after\nbcoo_slice(mat, start_indices=(0, 0), limit_indices=(4, 4), strides=(1, 1))","handlingStrategy":"validation","validationCode":"assert len(start_indices) == len(limit_indices) == mat.ndim\nstrides = strides or [1] * mat.ndim\nassert len(strides) == mat.ndim","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive index-list lengths from mat.ndim at runtime","Prefer strides=None unless you actually need striding"],"tags":["jax","sparse","bcoo","validation","shape-mismatch"],"backgroundTag":"index-length-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}