{"record":{"id":"bc21fc98678744df","repo":"jax-ml/jax","slug":"fiedler-companion-requires-the-last-axis-of-a-to","errorCode":null,"errorMessage":"fiedler_companion requires the last axis of 'a' to have nonzero length, but got an array of shape {a.shape}.","messagePattern":"fiedler_companion requires the last axis of 'a' to have nonzero length, but got an array of shape (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/linalg.py","lineNumber":2770,"sourceCode":"    A Fiedler companion matrix of shape ``(..., N - 1, N - 1)``.\n\n  Note:\n    Unlike :func:`scipy.linalg.fiedler_companion`, this function does not\n    check at runtime that ``a[..., 0]`` is non-zero; if the leading\n    coefficient is zero, the result will contain ``inf`` or ``nan`` entries.\n\n  Examples:\n    >>> a = jnp.array([1., -16., 86., -176., 105.])\n    >>> jax.scipy.linalg.fiedler_companion(a)\n    Array([[ 16., -86.,   1.,   0.],\n           [  1.,   0.,   0.,   0.],\n           [  0., 176.,   0., -105.],\n           [  0.,   1.,   0.,   0.]], dtype=float32)\n  \"\"\"\n  a, = promote_args_inexact(\"fiedler_companion\", a)\n  a = jnp.atleast_1d(a)\n  if a.shape[-1] == 0:\n    raise ValueError(\n        \"fiedler_companion requires the last axis of 'a' to have nonzero \"\n        f\"length, but got an array of shape {a.shape}.\")\n  return _fiedler_companion(a)\n\n@partial(jnp_vectorize.vectorize, signature=\"(n)->(m,m)\")\ndef _fiedler_companion(a: Array) -> Array:\n  n = a.shape[0] - 1\n  if n == 0:\n    return jnp.empty_like(a, shape=(0, 0))\n  a = a / a[0]\n  if n == 1:\n    return -a[1:].reshape(1, 1)\n  # Build the matrix with full-grid masked assignments so static shapes are\n  # preserved under jit and vectorize. The pentadiagonal layout is:\n  #   c[0, 0]               = -a[1]            (first column top)\n  #   c[1, 0]               = 1                (first column second row)\n  #   c[i,   i+1]           = -a[i+2]          (super-diag, even i, i+1 < n)\n  #   c[i,   i+2]           = 1                (second super, even i, i+2 < n)","sourceCodeStart":2752,"sourceCodeEnd":2788,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/linalg.py#L2752-L2788","documentation":"jax.scipy.linalg.fiedler_companion builds a symmetric companion-like matrix from polynomial coefficients; it requires a non-empty last axis (a.shape[-1] != 0). Unlike companion(), length-1 input is allowed here — only a fully empty coefficient array raises.","triggerScenarios":"Calling fiedler_companion with an empty array (shape (0,) or a batch with last dim 0), often from filtering a coefficient array down to nothing.","commonSituations":"High-pass filtering polynomial coefficients so all are removed; constructing coefficients from loops that produce zero iterations; empty batches after masking.","solutions":["Ensure the coefficient array has at least one element","Debug why upstream filtering/selection emptied the array (print a.shape before the call)","Default to skipping the computation when a.shape[-1] == 0"],"exampleFix":"# before\nC = fiedler_companion(coeffs[mask])  # mask removes all entries\n# after\nif coeffs[mask].shape[-1] == 0:\n    raise ValueError('no coefficients selected')\nC = fiedler_companion(coeffs[mask])","handlingStrategy":"validation","validationCode":"a = jnp.asarray(a)\nif a.shape[-1] == 0:\n    raise ValueError('coefficient array is empty')\nC = fiedler_companion(a)","typeGuard":"def has_nonempty_last_axis(x) -> bool:\n    return jnp.asarray(x).shape[-1] > 0","tryCatchPattern":"try:\n    fiedler_companion(a)\nexcept ValueError as e:\n    if 'nonzero length' in str(e):\n        raise ValueError('no polynomial coefficients after filtering') from e\n    raise","preventionTips":["Check .size / last-axis length after boolean-mask filtering of coefficients","Guard against zero-iteration loops that build coefficient arrays","Skip matrix construction for degenerate (empty) polynomials"],"tags":["jax","fiedler-companion","polynomial","empty-array"],"backgroundTag":"empty-array-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}