{"record":{"id":"69dd771b5c0ed015","repo":"jax-ml/jax","slug":"input-must-be-1d-or-non-empty-square-2d-array","errorCode":null,"errorMessage":"input must be 1d or non-empty square 2d array.","messagePattern":"input must be 1d or non-empty square 2d array\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/polynomial.py","lineNumber":376,"sourceCode":"    >>> x = jnp.array([[2, 1, 5],\n    ...                [3, 4, 7],\n    ...                [1, 3, 5]])\n    >>> jnp.round(jnp.poly(x))\n    Array([  1.+0.j, -11.-0.j,   9.+0.j, -15.+0.j], dtype=complex64)\n  \"\"\"\n  seq_of_zeros = ensure_arraylike('poly', seq_of_zeros)\n  seq_of_zeros, = promote_dtypes_inexact(seq_of_zeros)\n  seq_of_zeros_arr = atleast_1d(seq_of_zeros)\n  del seq_of_zeros\n\n  sh = seq_of_zeros_arr.shape\n  if len(sh) == 2 and sh[0] == sh[1] and sh[0] != 0:\n    # import at runtime to avoid circular import\n    from jax._src.numpy import linalg\n    seq_of_zeros_arr = linalg.eigvals(seq_of_zeros_arr)\n\n  if seq_of_zeros_arr.ndim != 1:\n    raise ValueError(\"input must be 1d or non-empty square 2d array.\")\n\n  dt = seq_of_zeros_arr.dtype\n  if len(seq_of_zeros_arr) == 0:\n    return ones((), dtype=dt)\n\n  a = ones((1,), dtype=dt)\n  for k in range(len(seq_of_zeros_arr)):\n    a = convolve(a, array([1, -seq_of_zeros_arr[k]], dtype=dt), mode='full')\n\n  return a\n\n\n@export\n@api.jit(static_argnames=['unroll'])\ndef polyval(p: ArrayLike, x: ArrayLike, *, unroll: int = 16) -> Array:\n  r\"\"\"Evaluates the polynomial at specific values.\n\n  JAX implementations of :func:`numpy.polyval`.","sourceCodeStart":358,"sourceCodeEnd":394,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/polynomial.py#L358-L394","documentation":"Thrown by jnp.poly when the input is neither a 1-D sequence of polynomial roots nor a non-empty square 2-D matrix. A square 2-D matrix is converted to eigenvalues (companion-matrix style, like numpy's poly), so any other shape (e.g. rectangular 2-D, empty 2-D, 3-D) is invalid.","triggerScenarios":"Calling jnp.poly on a 2-D array that is rectangular (rows != cols) or 0x0, or on a 3-D array; passing a scalar-shaped array also fails the ndim==1 check.","commonSituations":"Passing a coefficient array by mistake (poly expects roots, not coefficients — use polyval for coefficients); passing a batch of root vectors as a 2-D array expecting vectorized behavior; empty input edge case.","solutions":["Pass a 1-D array of roots: jnp.poly(roots_1d)","If you meant to evaluate a polynomial from coefficients, use jnp.polyval(coeffs, x) instead","For matrices, ensure the array is square and non-empty (eigenvalues are computed)"],"exampleFix":"// before\njnp.poly(coeffs_2d_rectangular)\n// after\njnp.poly(jnp.ravel(roots))          # 1-D roots\n# or, if input is really coefficients:\njnp.polyval(coeffs, x)","handlingStrategy":"validation","validationCode":"def as_roots(a):\n    import jax.numpy as jnp\n    a = jnp.asarray(a)\n    assert a.ndim == 1 or (a.ndim == 2 and a.shape[0] == a.shape[1] and a.shape[0] > 0), \\\n        f'poly expects 1-D roots or square matrix, got {a.shape}'\n    return a","typeGuard":"def is_valid_poly_input(a) -> bool:\n    import jax.numpy as jnp\n    a = jnp.asarray(a)\n    return a.ndim == 1 or (a.ndim == 2 and a.shape[0] == a.shape[1] > 0)","tryCatchPattern":null,"preventionTips":["Remember poly takes roots, not coefficients — use polyval for coefficients","Flatten batches of roots and vmap instead of passing 2-D"],"tags":["jax","numpy-poly","input-shape"],"backgroundTag":"invalid-array-shape","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}