{"record":{"id":"114a05e1d346ee20","repo":"jax-ml/jax","slug":"expected-non-empty-vector-for-x","errorCode":null,"errorMessage":"expected non-empty vector for x","messagePattern":"expected non-empty vector for x","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/polynomial.py","lineNumber":241,"sourceCode":"    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:\n    w_arr, = promote_dtypes_inexact(w)\n    if w_arr.ndim != 1:\n      raise TypeError(\"expected a 1-d array for weights\")\n    if w_arr.shape[0] != y_arr.shape[0]:","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/polynomial.py#L223-L259","documentation":"A least-squares fit with zero data points is underdetermined/meaningless: the Vandermonde matrix would have zero rows and the normal equations singular. jnp.polyfit therefore rejects empty x with a TypeError before doing any linear algebra.","triggerScenarios":"jnp.polyfit(jnp.array([]), y, deg); x filtered by a mask that removed all points; empty minibatches or empty time windows fed to a fitter.","commonSituations":"Runtime edge cases where a filter/window selects zero samples (market data gaps, empty sensor buffers); test code iterating over groups where some group is empty.","solutions":["Skip the fit when x.size == 0 (return NaNs or previous coefficients).","Assert non-empty input before calling: if x.size == 0: raise/return early.","Fix upstream filtering so at least deg+1 points remain."],"exampleFix":"// before\nc = jnp.polyfit(x[mask], y[mask], deg)  # mask all-False\n// after\nif mask.sum() > deg:\n    c = jnp.polyfit(x[mask], y[mask], deg)\nelse:\n    c = last_known_coeffs  # or NaN placeholder","handlingStrategy":"validation","validationCode":"if x.size == 0:\n    raise ValueError('no samples to fit')  # or return NaN\nc = jnp.polyfit(x, y, deg)","typeGuard":"def has_samples(x) -> bool:\n    return getattr(x, 'size', 0) > 0","tryCatchPattern":null,"preventionTips":["Skip fits on empty windows/filtered groups","Require at least deg+1 points for a meaningful fit"],"tags":["jax","numpy","polynomial","polyfit","empty-array","edge-case"],"backgroundTag":"empty-array-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}