{"record":{"id":"1a120c29da37c5d2","repo":"jax-ml/jax","slug":"the-length-of-a-along-the-last-axis-must-be-at-l","errorCode":null,"errorMessage":"The length of `a` along the last axis must be at least 2; got shape {a.shape}.","messagePattern":"The length of `a` along the last axis must be at least 2; got shape (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/linalg.py","lineNumber":2692,"sourceCode":"\n  Returns:\n    A companion matrix of shape ``(..., N - 1, N - 1)``.\n\n  Note:\n    Unlike :func:`scipy.linalg.companion`, this function does not check at\n    runtime that ``a[..., 0]`` is non-zero; if the leading coefficient is\n    zero, the result will contain ``inf`` or ``nan`` entries.\n\n  Examples:\n    >>> jax.scipy.linalg.companion(jnp.array([1., -10., 31., -30.]))\n    Array([[ 10., -31.,  30.],\n           [  1.,   0.,   0.],\n           [  0.,   1.,   0.]], dtype=float32)\n  \"\"\"\n  a, = promote_args_inexact(\"companion\", a)\n  a = jnp.atleast_1d(a)\n  if a.shape[-1] < 2:\n    raise ValueError(\n        \"The length of `a` along the last axis must be at least 2; \"\n        f\"got shape {a.shape}.\")\n  return _companion(a)\n\n@partial(jnp_vectorize.vectorize, signature=\"(n)->(m,m)\")\ndef _companion(a: Array) -> Array:\n  first_row = -a[1:] / a[0]\n  m = a.shape[0] - 1\n  out = jnp.eye(m, m, k=-1, dtype=first_row.dtype)\n  return out.at[0].set(first_row)\n\n\ndef fiedler(a: ArrayLike) -> Array:\n  r\"\"\"Construct a symmetric Fiedler matrix.\n\n  JAX implementation of :func:`scipy.linalg.fiedler`.\n\n  The Fiedler matrix has entries :math:`F_{ij} = |a_i - a_j|` for","sourceCodeStart":2674,"sourceCodeEnd":2710,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/linalg.py#L2674-L2710","documentation":"jax.scipy.linalg.companion returns the companion matrix of a polynomial with at least degree 1, so the coefficient array a needs length >= 2 along its last axis (leading coefficient first). After atleast_1d promotion, scalars and length-1 arrays raise ValueError.","triggerScenarios":"Calling companion(jnp.array([1.0])) or companion(2.0); batched input whose last axis was reduced to size 1.","commonSituations":"Passing a constant instead of polynomial coefficients; trimming roots/coefficient lists one element too many; building coefficients dynamically where a degree-0 case slips through.","solutions":["Supply at least two coefficients, e.g. companion([1, 2, 3]) for x^2 + 2x + 3","Guard degree-0/constant polynomials in caller code (they have no companion matrix)","Check a.shape[-1] >= 2 before calling"],"exampleFix":"# before\nC = companion(jnp.array([5.0]))\n# after\nC = companion(jnp.array([5.0, 1.0]))  # 5 + x","handlingStrategy":"validation","validationCode":"a = jnp.atleast_1d(a)\nif a.shape[-1] < 2:\n    raise ValueError('polynomial must have degree >= 1 (>= 2 coefficients)')\nC = companion(a)","typeGuard":null,"tryCatchPattern":"try:\n    companion(a)\nexcept ValueError as e:\n    if 'at least 2' in str(e):\n        a = jnp.concatenate([a, jnp.zeros_like(a)])  # only if sensible\n        companion(a)\n    else: raise","preventionTips":["Reject constant polynomials in your polynomial utilities before calling companion","Validate coefficient-vector length against expected degree","Avoid trimming coefficient arrays past their leading terms"],"tags":["jax","companion-matrix","polynomial","input-validation"],"backgroundTag":"argument-length-validation","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}