{"record":{"id":"f6258ed4a6cc0d20","repo":"jax-ml/jax","slug":"len-a-must-be-at-least-1-got-shape-a-arr-shape","errorCode":null,"errorMessage":"len(a) must be at least 1; got shape {a_arr.shape}.","messagePattern":"len\\(a\\) must be at least 1; got shape (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/linalg.py","lineNumber":2859,"sourceCode":"  Examples:\n    >>> jax.scipy.linalg.convolution_matrix(jnp.array([-1, 4, -2]), 5, mode='same')\n    Array([[ 4, -1,  0,  0,  0],\n           [-2,  4, -1,  0,  0],\n           [ 0, -2,  4, -1,  0],\n           [ 0,  0, -2,  4, -1],\n           [ 0,  0,  0, -2,  4]], dtype=int32)\n  \"\"\"\n  n = operator.index(n)\n  if n <= 0:\n    raise ValueError(f\"n must be a positive integer; got {n}.\")\n  check_arraylike(\"convolution_matrix\", a)\n  a_arr = jnp.asarray(a)\n  if a_arr.ndim == 0:\n    raise ValueError(\n        \"convolution_matrix: a must be at least 1-dimensional, got a scalar.\")\n  m = a_arr.shape[-1]\n  if m < 1:\n    raise ValueError(f\"len(a) must be at least 1; got shape {a_arr.shape}.\")\n  if mode not in ('full', 'valid', 'same'):\n    raise ValueError(\n        f\"mode must be one of 'full', 'valid', 'same'; got {mode!r}.\")\n  pad_widths = [(0, 0)] * (a_arr.ndim - 1) + [(0, n - 1)]\n  az = jnp.pad(a_arr, pad_widths)\n  raz = jnp.pad(jnp.flip(a_arr, axis=-1), pad_widths)\n  L = m + n - 1\n  if mode == 'same':\n    trim = min(n, m) - 1\n    tb = trim // 2\n    te = trim - tb\n  elif mode == 'valid':\n    tb = min(n, m) - 1\n    te = tb\n  else:  # 'full'\n    tb = 0\n    te = 0\n  col0 = lax.slice_in_dim(az, tb, L - te, axis=-1)","sourceCodeStart":2841,"sourceCodeEnd":2877,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/linalg.py#L2841-L2877","documentation":"After the ndim check, convolution_matrix also verifies the kernel has at least one element along its last axis (m = a.shape[-1] >= 1). An empty array (e.g. shape (0,) or (3, 0)) raises this ValueError since there is nothing to convolve with.","triggerScenarios":"Passing an empty list [], jnp.zeros((0,)), or an empty batch slice to convolution_matrix.","commonSituations":"Dynamically trimmed/filtered kernels that end up empty; batches where a mask removed all elements of one item; upstream data-loading returning zero taps.","solutions":["Skip or special-case empty kernels before the call","Verify kernel length > 0 with a debug assert/print of a.shape","Fix the filtering logic that emptied the kernel"],"exampleFix":"# before\nC = convolution_matrix(taps[taps != 0], n)  # could be empty\n# after\nk = taps[taps != 0]\nC = convolution_matrix(k if k.size else jnp.zeros(1), n)","handlingStrategy":"validation","validationCode":"a_arr = jnp.asarray(a)\nif a_arr.shape[-1] < 1:\n    raise ValueError('kernel must have at least one element')\nC = convolution_matrix(a_arr, n)","typeGuard":"def has_nonempty_last_axis(x) -> bool:\n    return jnp.asarray(x).shape[-1] > 0","tryCatchPattern":"try:\n    convolution_matrix(a, n)\nexcept ValueError as e:\n    if 'len(a) must be at least 1' in str(e):\n        raise ValueError('empty convolution kernel') from e\n    raise","preventionTips":["Check .size after filtering kernels with boolean masks","Skip or short-circuit convolutions for empty kernels","Log kernel shapes in signal-processing pipelines"],"tags":["jax","convolution-matrix","empty-array"],"backgroundTag":"empty-array-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}