{"record":{"id":"49cb720adfd2da5d","repo":"jax-ml/jax","slug":"expected-deg-0","errorCode":null,"errorMessage":"expected deg >= 0","messagePattern":"expected deg >= 0","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/polynomial.py","lineNumber":237,"sourceCode":"    s: [1.67 0.47 0.04]\n    rcond: 4.7683716e-07\n\n    If ``cov=True`` and ``full=False``, returns a tuple of arrays having\n    polynomial coefficients and covariance matrix.\n\n    >>> p, C = jnp.polyfit(x, y, 2, cov=True)\n    >>> p.shape, C.shape\n    ((3, 3), (3, 3, 3))\n  \"\"\"\n  if w is None:\n    x_arr, y_arr = ensure_arraylike(\"polyfit\", x, y)\n  else:\n    x_arr, y_arr, w = ensure_arraylike(\"polyfit\", x, y, w)\n  del x, y\n  deg = core.concrete_or_error(int, deg, \"deg must be int\")\n  order = deg + 1\n  if deg < 0:\n    raise ValueError(\"expected deg >= 0\")\n  if x_arr.ndim != 1:\n    raise TypeError(\"expected 1D vector for x\")\n  if x_arr.size == 0:\n    raise TypeError(\"expected non-empty vector for x\")\n  if y_arr.ndim < 1 or y_arr.ndim > 2:\n    raise TypeError(\"expected 1D or 2D array for y\")\n  if x_arr.shape[0] != y_arr.shape[0]:\n    raise TypeError(\"expected x and y to have same length\")\n\n  if rcond is None:\n    rcond = len(x_arr) * float(finfo(x_arr.dtype).eps)\n  rcond = core.concrete_or_error(float, rcond, \"rcond must be float\")\n  # set up least squares equation for powers of x\n  lhs = vander(x_arr, order)\n  rhs = y_arr\n\n  # apply weighting\n  if w is not None:","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/polynomial.py#L219-L255","documentation":"jnp.polyfit(x, y, deg) performs a least-squares polynomial fit and needs deg to be a non-negative Python integer (it is converted via core.concrete_or_error(int, ...)). A negative degree has no meaning — there is no polynomial of degree -1 — so it raises ValueError before building the Vandermonde matrix.","triggerScenarios":"jnp.polyfit(x, y, deg=-1); deg computed as order-1 where order=0; deg passed as a tracer under jit (sibling concrete_or_error failure).","commonSituations":"Off-by-one when converting between 'number of coefficients' (order) and 'degree' (order-1); looping over degrees and including 0 or negatives; config-driven degree values validated only as ints, not as >= 0.","solutions":["Clamp/validate deg: use max(deg, 0) only if that's actually intended; otherwise fix the source of the negative value.","Check the order-vs-degree convention: order = deg + 1, so deg = n_coeffs - 1.","Pass a static Python int, not a traced value, when under jit."],"exampleFix":"// before\ncoeffs = jnp.polyfit(x, y, deg=n_coeffs - 1)  # n_coeffs == 0\n// after\ncoeffs = jnp.polyfit(x, y, deg=max(n_coeffs - 1, 0))\n// or assert n_coeffs >= 1 before the call","handlingStrategy":"validation","validationCode":"deg = int(deg)\nif deg < 0:\n    raise ValueError(f'deg must be >= 0, got {deg}')\nc = jnp.polyfit(x, y, deg)","typeGuard":"def valid_deg(d) -> bool:\n    return isinstance(d, int) and not isinstance(d, bool) and d >= 0","tryCatchPattern":null,"preventionTips":["Remember deg = n_coeffs - 1","Pass degree as a static Python int"],"tags":["jax","numpy","polynomial","polyfit","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}