jax-ml/jax · error · ValueError

Incorrect lengths for f and s. The length of s along the las

Error message

Incorrect lengths for f and s. The length of s along the last axis must be one less than the length of f; got f shape {f_arr.shape} and s shape {s_arr.shape}.

What it means

leslie requires len(s) == len(f) - 1 along the last axis: n fecundities need exactly n-1 survival probabilities (one fewer, since the last age class has no successor). The ValueError includes both shapes for easy diagnosis.

Source

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

    A Leslie matrix of shape ``(..., N, N)``.

  Examples:
    >>> jax.scipy.linalg.leslie(jnp.array([0.1, 2.0, 1.0, 0.1]),
    ...                         jnp.array([0.2, 0.8, 0.7]))
    Array([[0.1, 2. , 1. , 0.1],
           [0.2, 0. , 0. , 0. ],
           [0. , 0.8, 0. , 0. ],
           [0. , 0. , 0.7, 0. ]], dtype=float32)
  """
  check_arraylike("leslie", f, s)
  f_arr = jnp.atleast_1d(f)
  s_arr = jnp.atleast_1d(s)
  if f_arr.shape[-1] < 2:
    raise ValueError(
        "The length of f along the last axis must be at least 2; "
        f"got shape {f_arr.shape}.")
  if s_arr.shape[-1] != f_arr.shape[-1] - 1:
    raise ValueError(
        "Incorrect lengths for f and s. The length of s along the last axis "
        f"must be one less than the length of f; got f shape {f_arr.shape} "
        f"and s shape {s_arr.shape}.")
  return _leslie(f_arr, s_arr)

@partial(jnp_vectorize.vectorize, signature="(n),(m)->(n,n)")
def _leslie(f: Array, s: Array) -> Array:
  f, s = promote_dtypes(f, s)
  return jnp.diag(s, k=-1).at[0].set(f)


def companion(a: ArrayLike) -> Array:
  r"""Construct a companion matrix.

  JAX implementation of :func:`scipy.linalg.companion`.

  Given polynomial coefficients :math:`a = [a_0, a_1, \ldots, a_{n-1}]` with
  :math:`a_0 \neq 0`, the companion matrix is the :math:`(n-1) \times (n-1)`

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Trim s to len(f)-1: s = s[:len(f)-1] or s[..., :-1]
  2. Regenerate s from the demographic data with the correct count
  3. Assert s.shape[-1] == f.shape[-1] - 1 before calling

Example fix

# before
f = jnp.array([0.1, 2.0, 1.5, 0.7]); s = jnp.array([0.8, 0.9, 0.95, 0.0])
L = leslie(f, s)  # len(s) == len(f) -> raises
# after
L = leslie(f, s[:-1])
Defensive patterns

Strategy: validation

Validate before calling

f_arr, s_arr = jnp.atleast_1d(f), jnp.atleast_1d(s)
if s_arr.shape[-1] != f_arr.shape[-1] - 1:
    s_arr = s_arr[..., :f_arr.shape[-1] - 1]  # or raise
L = leslie(f_arr, s_arr)

Try / catch

try:
    leslie(f, s)
except ValueError as e:
    if 'Incorrect lengths' in str(e):
        raise ValueError(f'expected len(s)=len(f)-1, got {len(f)}, {len(s)}') from e
    raise

Prevention

When it happens

Trigger: Calling leslie with f of length n and s of length n, n-2, or any length other than n-1; s includes a trailing 0 for the final class.

Common situations: Off-by-one errors when constructing s to 'match' f; porting data files where s was padded to equal length; batching f and s sliced inconsistently.

Related errors


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