{"record":{"id":"1af2dc0d71b02b36","repo":"jax-ml/jax","slug":"x-must-have-either-the-same-number-of-entries-as","errorCode":null,"errorMessage":"`x` must have either the same number of entries as `alpha` or one entry fewer; got x.shape={x.shape}, alpha.shape={alpha.shape}","messagePattern":"`x` must have either the same number of entries as `alpha` or one entry fewer; got x\\.shape=(.+?), alpha\\.shape=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/stats/dirichlet.py","lineNumber":62,"sourceCode":"  Args:\n    x: arraylike, value at which to evaluate the PDF\n    alpha: arraylike, distribution shape parameter\n\n  Returns:\n    array of logpdf values.\n\n  See Also:\n    :func:`jax.scipy.stats.dirichlet.pdf`\n  \"\"\"\n  return _logpdf(*promote_dtypes_inexact(x, alpha))\n\ndef _logpdf(x: Array, alpha: Array) -> Array:\n  if alpha.ndim != 1:\n    raise ValueError(\n      f\"`alpha` must be one-dimensional; got alpha.shape={alpha.shape}\"\n    )\n  if x.shape[0] not in (alpha.shape[0], alpha.shape[0] - 1):\n    raise ValueError(\n      \"`x` must have either the same number of entries as `alpha` \"\n      f\"or one entry fewer; got x.shape={x.shape}, alpha.shape={alpha.shape}\"\n    )\n  one = _lax_const(x, 1)\n  if x.shape[0] != alpha.shape[0]:\n    x = jnp.concatenate([x, lax.sub(one, x.sum(0, keepdims=True))], axis=0)\n  normalize_term = jnp.sum(gammaln(alpha)) - gammaln(jnp.sum(alpha))\n  if x.ndim > 1:\n    alpha = lax.broadcast_in_dim(alpha, alpha.shape + (1,) * (x.ndim - 1), (0,))\n  log_probs = lax.sub(jnp.sum(xlogy(lax.sub(alpha, one), x), axis=0), normalize_term)\n  return jnp.where(_is_simplex(x), log_probs, -np.inf)\n\n\ndef pdf(x: ArrayLike, alpha: ArrayLike) -> Array:\n  r\"\"\"Dirichlet probability distribution function.\n\n  JAX implementation of :obj:`scipy.stats.dirichlet` ``pdf``.\n","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/stats/dirichlet.py#L44-L80","documentation":"For jax.scipy.stats.dirichlet.logpdf/pdf, x along axis 0 must have exactly len(alpha) entries (full simplex) or len(alpha)-1 entries (last coordinate implicit, computed as 1 - sum(x)). Any other leading dimension raises this error.","triggerScenarios":"Passing x with shape (k+2, ...) or (k-3, ...) when alpha has k entries; passing x whose batch axis is on axis 0 instead of matching alpha's length.","commonSituations":"Feeding unbatched x of wrong length; confusing the batch dimension with the category dimension; forgetting that the implicit form drops exactly one coordinate, not an arbitrary number.","solutions":["Reshape x so x.shape[0] is alpha.shape[0] or alpha.shape[0]-1","If the last simplex coordinate was dropped, keep only one dropped value; supply the full k-length x if you dropped more","Put batch dimensions on axes other than axis 0 or use vmap"],"exampleFix":"// before\nalpha = jnp.array([2.0, 3.0, 4.0])\nx = jnp.array([0.5])  # wrong length\nlp = jax.scipy.stats.dirichlet.logpdf(x, alpha)\n// after\nx = jnp.array([0.5, 0.3])  # k-1 entries; last = 1 - 0.8\nlp = jax.scipy.stats.dirichlet.logpdf(x, alpha)","handlingStrategy":"validation","validationCode":"k = alpha.shape[0]\nassert x.shape[0] in (k, k - 1), f'x.shape[0] must be {k} or {k-1}'","typeGuard":"def dirichlet_x_valid(x, alpha) -> bool:\n    return jnp.asarray(x).shape[0] in (jnp.asarray(alpha).shape[0], jnp.asarray(alpha).shape[0] - 1)","tryCatchPattern":null,"preventionTips":["Prefer passing the full k-length simplex vector to avoid implicit-coordinate confusion","Keep batch dims off axis 0","Assert shapes in a small helper before distribution calls"],"tags":["jax","scipy","dirichlet","shape-validation"],"backgroundTag":"invalid-array-shape","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}