jax-ml/jax · error · ValueError
The length of f along the last axis must be at least 2; got
Error message
The length of f along the last axis must be at least 2; got shape {f_arr.shape}. What it means
jax.scipy.linalg.leslie builds a Leslie population-projection matrix from fecundity coefficients f and survival rates s. A Leslie matrix needs at least two age classes (len(f) >= 2), so f with fewer than 2 elements along its last axis is rejected. Inputs are promoted with atleast_1d first, so scalars become length-1 and fail here.
Source
Thrown at jax/_src/scipy/linalg.py:2645
coefficients.
s: array of shape ``(..., N - 1)`` containing the survival coefficients.
Returns:
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.
View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Provide f with at least 2 entries along the last axis
- Check argument order: f = fecundities (length n), s = survivals (length n-1)
- Validate f_arr.shape[-1] >= 2 in a wrapper before calling
Example fix
# before L = leslie(jnp.array([0.5]), jnp.array([])) # after L = leslie(jnp.array([0.5, 1.0]), jnp.array([0.8]))
Defensive patterns
Strategy: validation
Validate before calling
f_arr = jnp.atleast_1d(f)
if f_arr.shape[-1] < 2:
raise ValueError('leslie needs at least 2 fecundity coefficients')
L = leslie(f, s) Try / catch
try:
leslie(f, s)
except ValueError as e:
if 'must be at least 2' in str(e):
raise ValueError('model needs >= 2 age classes') from e
raise Prevention
- Enforce minimum model size (2 age classes) in your demographic data loader
- Type-check inputs to domain-specific constructors before passing them through
- Write smoke tests with the smallest valid model (f of length 2, s of length 1)
When it happens
Trigger: Calling leslie(f, s) where f has shape (1,), f is a scalar, or a batched f whose last axis has length 1.
Common situations: Testing with toy single-age-class data; passing wrong argument order (a single survival rate as f); degenerate batches where the last axis was squeezed.
Related errors
- numpy masked arrays are not supported as direct inputs to JA
- Invalid compute type {c_type}. Current supported values are
- numpy masked arrays are not supported as direct inputs to JA
- key cannot be empty
- Mesh axis names cannot be None. Got: {axis_names}
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/c6747a0238b69d11.
Report an issue: GitHub.