{"record":{"id":"b7a76286b55a49e2","repo":"jax-ml/jax","slug":"n-must-be-a-positive-integer-got-n","errorCode":null,"errorMessage":"n must be a positive integer; got {n}.","messagePattern":"n must be a positive integer; got (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/linalg.py","lineNumber":2851,"sourceCode":"\n  Returns:\n    A convolution matrix of shape ``(..., k, n)``, where ``k`` depends on\n    ``mode`` as described above.\n\n  See also:\n    :func:`jax.scipy.linalg.toeplitz`\n\n  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","sourceCodeStart":2833,"sourceCodeEnd":2869,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/linalg.py#L2833-L2869","documentation":"jax.scipy.linalg.convolution_matrix builds an n-column convolution matrix and requires n to be a positive Python integer (it goes through operator.index, which also rejects floats/strings with TypeError). n <= 0 raises this ValueError because a zero/negative output width is meaningless.","triggerScenarios":"Calling convolution_matrix(a, 0), convolution_matrix(a, -3), or with a numpy scalar/float like np.int64(5) wrapped incorrectly or n as a traced JAX value (operator.index fails during tracing).","commonSituations":"Computing n from a difference that evaluates to 0 (e.g. len(b) - len(a)); passing n as a jnp scalar under jit; passing n as float 5.0.","solutions":["Ensure n is a plain positive int (use int(n) if it's a numpy/float value)","Fix the arithmetic producing n <= 0 and handle the degenerate case before calling","Under jit, mark n as static or pass it as a Python constant"],"exampleFix":"# before\nC = convolution_matrix(a, len(b) - len(a))  # may be 0\n# after\nn = len(b) - len(a)\nif n <= 0:\n    raise ValueError('insufficient output length')\nC = convolution_matrix(a, n)","handlingStrategy":"validation","validationCode":"import operator\nn = operator.index(n)  # raises TypeError early for non-integers\nif n <= 0:\n    raise ValueError(f'n must be positive, got {n}')\nC = convolution_matrix(a, n)","typeGuard":"def is_positive_int(n) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n > 0","tryCatchPattern":"try:\n    convolution_matrix(a, n)\nexcept ValueError as e:\n    if 'n must be a positive integer' in str(e):\n        raise ValueError('output width computed as non-positive; check inputs') from e\n    raise","preventionTips":["Compute output lengths with max(n, 1) guards after subtraction-based arithmetic","Convert numpy scalars to int before passing to static-argument APIs","Under jit, keep integer size arguments as Python constants (static)"],"tags":["jax","convolution-matrix","argument-validation"],"backgroundTag":"invalid-numeric-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}