{"record":{"id":"31dfb61241b5dfdd","repo":"jax-ml/jax","slug":"input-must-be-a-rank-1-array","errorCode":null,"errorMessage":"Input must be a rank-1 array.","messagePattern":"Input must be a rank-1 array\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/polynomial.py","lineNumber":111,"sourceCode":"  Examples:\n    >>> coeffs = jnp.array([0, 1, 2])\n\n    The default behavior matches numpy and strips leading zeros:\n\n    >>> jnp.roots(coeffs)\n    Array([-2.+0.j], dtype=complex64)\n\n    With ``strip_zeros=False``, extra roots are set to NaN:\n\n    >>> jnp.roots(coeffs, strip_zeros=False)\n    Array([-2. +0.j, nan+nanj], dtype=complex64)\n  \"\"\"\n  p = ensure_arraylike(\"roots\", p)\n  p, = promote_dtypes_inexact(p)\n  p_arr = atleast_1d(p)\n  del p\n  if p_arr.ndim != 1:\n    raise ValueError(\"Input must be a rank-1 array.\")\n  if p_arr.size < 2:\n    return array([], dtype=dtypes.to_complex_dtype(p_arr.dtype))\n  num_leading_zeros = _where(all(p_arr == 0), len(p_arr), argmin(p_arr == 0))\n\n  if strip_zeros:\n    num_leading_zeros = core.concrete_or_error(int, num_leading_zeros,\n      \"The error occurred in the jnp.roots() function. To use this within a \"\n      \"JIT-compiled context, pass strip_zeros=False, but be aware that leading zeros \"\n      \"will result in some returned roots being set to NaN.\")\n    return _roots_no_zeros(p_arr[num_leading_zeros:])\n  else:\n    return _roots_with_zeros(p_arr, num_leading_zeros)\n\n\n@export\n@api.jit(static_argnames=('deg', 'rcond', 'full', 'cov'))\ndef polyfit(x: ArrayLike, y: ArrayLike, deg: int, rcond: float | None = None,\n            full: bool = False, w: ArrayLike | None = None, cov: bool = False","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/polynomial.py#L93-L129","documentation":"jnp.roots finds polynomial roots by building a companion matrix from the coefficient vector, which requires a rank-1 (1-D) coefficient array. After atleast_1d promotion, any input with ndim != 1 (a matrix, batched coefficients, or scalar plus a stray axis) is rejected.","triggerScenarios":"jnp.roots(jnp.array([[1, 0, -1]])) (shape (1, 3)); passing batched coefficient arrays; passing a 2-D coefficient matrix from a fitting routine.","commonSituations":"Wrapping coefficients in an extra axis when loading from datasets; expecting vectorized/batched root-finding (jnp.roots does not batch); scalars becoming shape-(1,) is fine, but (n, 1) column vectors are not.","solutions":["Flatten the coefficients: jnp.roots(p.ravel()) or squeeze extra axes.","Ensure you pass a plain 1-D coefficient vector, highest degree first.","For batches, vmap over individual coefficient vectors."],"exampleFix":"// before\nr = jnp.roots(p)  # p.shape == (3, 1)\n// after\nr = jnp.roots(p.ravel())","handlingStrategy":"validation","validationCode":"p = jnp.ravel(jnp.asarray(p))\nassert p.ndim == 1, p.shape\nr = jnp.roots(p)","typeGuard":"def is_rank1(p) -> bool:\n    return getattr(p, 'ndim', 0) == 1","tryCatchPattern":null,"preventionTips":["Ravel coefficients before calling roots","jnp.roots does not batch — vmap manually"],"tags":["jax","numpy","polynomial","roots","shape-validation"],"backgroundTag":"invalid-shape-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}