{"record":{"id":"0e6d9a4f063cbd5c","repo":"jax-ml/jax","slug":"incorrect-lengths-for-f-and-s-the-length-of-s-alo","errorCode":null,"errorMessage":"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}.","messagePattern":"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 (.+?) and s shape (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/linalg.py","lineNumber":2649,"sourceCode":"    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\n  JAX implementation of :func:`scipy.linalg.companion`.\n\n  Given polynomial coefficients :math:`a = [a_0, a_1, \\ldots, a_{n-1}]` with\n  :math:`a_0 \\neq 0`, the companion matrix is the :math:`(n-1) \\times (n-1)`","sourceCodeStart":2631,"sourceCodeEnd":2667,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/linalg.py#L2631-L2667","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Trim s to len(f)-1: s = s[:len(f)-1] or s[..., :-1]","Regenerate s from the demographic data with the correct count","Assert s.shape[-1] == f.shape[-1] - 1 before calling"],"exampleFix":"# before\nf = jnp.array([0.1, 2.0, 1.5, 0.7]); s = jnp.array([0.8, 0.9, 0.95, 0.0])\nL = leslie(f, s)  # len(s) == len(f) -> raises\n# after\nL = leslie(f, s[:-1])","handlingStrategy":"validation","validationCode":"f_arr, s_arr = jnp.atleast_1d(f), jnp.atleast_1d(s)\nif s_arr.shape[-1] != f_arr.shape[-1] - 1:\n    s_arr = s_arr[..., :f_arr.shape[-1] - 1]  # or raise\nL = leslie(f_arr, s_arr)","typeGuard":null,"tryCatchPattern":"try:\n    leslie(f, s)\nexcept ValueError as e:\n    if 'Incorrect lengths' in str(e):\n        raise ValueError(f'expected len(s)=len(f)-1, got {len(f)}, {len(s)}') from e\n    raise","preventionTips":["Treat len(s) == len(f) - 1 as an invariant in data ingestion","Write a unit test asserting the off-by-one contract","When padding survival tables, remember the final age class has no survival entry"],"tags":["jax","leslie-matrix","off-by-one","argument-length-validation"],"backgroundTag":"argument-length-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}