jax-ml/jax · error · ValueError

fiedler_companion requires the last axis of 'a' to have nonz

Error message

fiedler_companion requires the last axis of 'a' to have nonzero length, but got an array of shape {a.shape}.

What it means

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.

Source

Thrown at jax/_src/scipy/linalg.py:2770

    A Fiedler companion matrix of shape ``(..., N - 1, N - 1)``.

  Note:
    Unlike :func:`scipy.linalg.fiedler_companion`, this function does not
    check at runtime that ``a[..., 0]`` is non-zero; if the leading
    coefficient is zero, the result will contain ``inf`` or ``nan`` entries.

  Examples:
    >>> a = jnp.array([1., -16., 86., -176., 105.])
    >>> jax.scipy.linalg.fiedler_companion(a)
    Array([[ 16., -86.,   1.,   0.],
           [  1.,   0.,   0.,   0.],
           [  0., 176.,   0., -105.],
           [  0.,   1.,   0.,   0.]], dtype=float32)
  """
  a, = promote_args_inexact("fiedler_companion", a)
  a = jnp.atleast_1d(a)
  if a.shape[-1] == 0:
    raise ValueError(
        "fiedler_companion requires the last axis of 'a' to have nonzero "
        f"length, but got an array of shape {a.shape}.")
  return _fiedler_companion(a)

@partial(jnp_vectorize.vectorize, signature="(n)->(m,m)")
def _fiedler_companion(a: Array) -> Array:
  n = a.shape[0] - 1
  if n == 0:
    return jnp.empty_like(a, shape=(0, 0))
  a = a / a[0]
  if n == 1:
    return -a[1:].reshape(1, 1)
  # Build the matrix with full-grid masked assignments so static shapes are
  # preserved under jit and vectorize. The pentadiagonal layout is:
  #   c[0, 0]               = -a[1]            (first column top)
  #   c[1, 0]               = 1                (first column second row)
  #   c[i,   i+1]           = -a[i+2]          (super-diag, even i, i+1 < n)
  #   c[i,   i+2]           = 1                (second super, even i, i+2 < n)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Ensure the coefficient array has at least one element
  2. Debug why upstream filtering/selection emptied the array (print a.shape before the call)
  3. Default to skipping the computation when a.shape[-1] == 0

Example fix

# before
C = fiedler_companion(coeffs[mask])  # mask removes all entries
# after
if coeffs[mask].shape[-1] == 0:
    raise ValueError('no coefficients selected')
C = fiedler_companion(coeffs[mask])
Defensive patterns

Strategy: validation

Validate before calling

a = jnp.asarray(a)
if a.shape[-1] == 0:
    raise ValueError('coefficient array is empty')
C = fiedler_companion(a)

Type guard

def has_nonempty_last_axis(x) -> bool:
    return jnp.asarray(x).shape[-1] > 0

Try / catch

try:
    fiedler_companion(a)
except ValueError as e:
    if 'nonzero length' in str(e):
        raise ValueError('no polynomial coefficients after filtering') from e
    raise

Prevention

When it happens

Trigger: 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.

Common situations: High-pass filtering polynomial coefficients so all are removed; constructing coefficients from loops that produce zero iterations; empty batches after masking.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/bc21fc98678744df. Report an issue: GitHub.