{"record":{"id":"c6747a0238b69d11","repo":"jax-ml/jax","slug":"the-length-of-f-along-the-last-axis-must-be-at-lea","errorCode":null,"errorMessage":"The length of f along the last axis must be at least 2; got shape {f_arr.shape}.","messagePattern":"The length of f 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":2645,"sourceCode":"      coefficients.\n    s: array of shape ``(..., N - 1)`` containing the survival coefficients.\n\n  Returns:\n    A Leslie matrix of shape ``(..., N, N)``.\n\n  Examples:\n    >>> jax.scipy.linalg.leslie(jnp.array([0.1, 2.0, 1.0, 0.1]),\n    ...                         jnp.array([0.2, 0.8, 0.7]))\n    Array([[0.1, 2. , 1. , 0.1],\n           [0.2, 0. , 0. , 0. ],\n           [0. , 0.8, 0. , 0. ],\n           [0. , 0. , 0.7, 0. ]], dtype=float32)\n  \"\"\"\n  check_arraylike(\"leslie\", f, s)\n  f_arr = jnp.atleast_1d(f)\n  s_arr = jnp.atleast_1d(s)\n  if f_arr.shape[-1] < 2:\n    raise ValueError(\n        \"The length of f along the last axis must be at least 2; \"\n        f\"got shape {f_arr.shape}.\")\n  if s_arr.shape[-1] != f_arr.shape[-1] - 1:\n    raise ValueError(\n        \"Incorrect lengths for f and s. The length of s along the last axis \"\n        f\"must be one less than the length of f; got f shape {f_arr.shape} \"\n        f\"and s shape {s_arr.shape}.\")\n  return _leslie(f_arr, s_arr)\n\n@partial(jnp_vectorize.vectorize, signature=\"(n),(m)->(n,n)\")\ndef _leslie(f: Array, s: Array) -> Array:\n  f, s = promote_dtypes(f, s)\n  return jnp.diag(s, k=-1).at[0].set(f)\n\n\ndef companion(a: ArrayLike) -> Array:\n  r\"\"\"Construct a companion matrix.\n","sourceCodeStart":2627,"sourceCodeEnd":2663,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/linalg.py#L2627-L2663","documentation":"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.","triggerScenarios":"Calling leslie(f, s) where f has shape (1,), f is a scalar, or a batched f whose last axis has length 1.","commonSituations":"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.","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"],"exampleFix":"# before\nL = leslie(jnp.array([0.5]), jnp.array([]))\n# after\nL = leslie(jnp.array([0.5, 1.0]), jnp.array([0.8]))","handlingStrategy":"validation","validationCode":"f_arr = jnp.atleast_1d(f)\nif f_arr.shape[-1] < 2:\n    raise ValueError('leslie needs at least 2 fecundity coefficients')\nL = leslie(f, s)","typeGuard":null,"tryCatchPattern":"try:\n    leslie(f, s)\nexcept ValueError as e:\n    if 'must be at least 2' in str(e):\n        raise ValueError('model needs >= 2 age classes') from e\n    raise","preventionTips":["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)"],"tags":["jax","leslie-matrix","input-validation"],"backgroundTag":"argument-length-validation","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}