{"record":{"id":"195ff19ee39ad0cb","repo":"jax-ml/jax","slug":"number-of-samples-num-must-be-non-negative","errorCode":null,"errorMessage":"Number of samples, {num}, must be non-negative.","messagePattern":"Number of samples, (.+?), must be non-negative\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/array_creation.py","lineNumber":596,"sourceCode":"    Array([[ 0.  ,  5.  ],\n           [ 1.25,  6.25],\n           [ 2.5 ,  7.5 ],\n           [ 3.75,  8.75],\n           [ 5.  , 10.  ]], dtype=float32)\n  \"\"\"\n  num = core.concrete_dim_or_error(num, \"'num' argument of jnp.linspace\")\n  axis = core.concrete_or_error(operator.index, axis, \"'axis' argument of jnp.linspace\")\n  return _linspace(start, stop, num, endpoint, retstep, dtype, axis, device=device)\n\n@api.jit(static_argnames=('num', 'endpoint', 'retstep', 'dtype', 'axis', 'device'))\ndef _linspace(start: ArrayLike, stop: ArrayLike, num: int = 50,\n              endpoint: bool = True, retstep: bool = False,\n              dtype: DTypeLike | None = None,\n              axis: int = 0,\n              *, device: xc.Device | Sharding | None = None) -> Array | tuple[Array, Array]:\n  \"\"\"Implementation of linspace differentiable in start and stop args.\"\"\"\n  if num < 0:\n    raise ValueError(f\"Number of samples, {num}, must be non-negative.\")\n  start, stop = util.ensure_arraylike(\"linspace\", start, stop)\n\n  if dtype is None:\n    dtype = dtypes.to_inexact_dtype(dtypes.result_type(start, stop))\n  else:\n    dtype = dtypes.check_and_canonicalize_user_dtype(dtype, \"linspace\")\n  computation_dtype = dtypes.to_inexact_dtype(dtype)\n  start = start.astype(computation_dtype)\n  stop = stop.astype(computation_dtype)\n\n  bounds_shape = list(lax.broadcast_shapes(np.shape(start), np.shape(stop)))\n  broadcast_start = util._broadcast_to(start, bounds_shape)\n  broadcast_stop = util._broadcast_to(stop, bounds_shape)\n  axis = len(bounds_shape) + axis + 1 if axis < 0 else axis\n  bounds_shape.insert(axis, 1)\n  div = (num - 1) if endpoint else num\n  if num > 1:\n    delta: Array = lax.convert_element_type(stop - start, computation_dtype) / asarray(div, dtype=computation_dtype)","sourceCodeStart":578,"sourceCodeEnd":614,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/array_creation.py#L578-L614","documentation":"The differentiable implementation behind jnp.linspace validates num (number of samples) and rejects negative values, since a negative sample count has no mathematical meaning and cannot be traced into a valid computation.","triggerScenarios":"jnp.linspace(0, 1, num=-5) or num computed from an expression that can go negative (e.g. (b - a) // step).","commonSituations":"Computing sample counts from user parameters or deltas where rounding/division can produce -1 or lower; passing None-ish defaults that coerce to negative numbers.","solutions":["Clamp num: max(num, 0) or validate before calling","Recheck the count formula — linspace wants a count, not a step; use jnp.arange for step-based spacing"],"exampleFix":"# before\na = jnp.linspace(0.0, 1.0, num=(stop - start) // step)  # can be negative\n# after\nn = max(int((stop - start) // step), 0)\na = jnp.linspace(0.0, 1.0, num=n)","handlingStrategy":"validation","validationCode":"num = max(int(num), 0)\njnp.linspace(start, stop, num=num)","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Clamp computed sample counts","Use arange when spacing (not count) is the natural parameter"],"tags":["jax","linspace","argument-validation"],"backgroundTag":"argument-out-of-range","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}