{"record":{"id":"9e0433222e1d1451","repo":"jax-ml/jax","slug":"entr-does-not-support-complex-valued-inputs","errorCode":null,"errorMessage":"entr does not support complex-valued inputs.","messagePattern":"entr does not support complex-valued inputs\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/special.py","lineNumber":1040,"sourceCode":"     \\mathrm{entr}(x) = \\begin{cases}\n       -x\\log(x) & x > 0 \\\\\n       0 & x = 0\\\\\n       -\\infty & \\mathrm{otherwise}\n     \\end{cases}\n\n  Args:\n    x: arraylike, real-valued.\n\n  Returns:\n    array containing entropy values.\n\n  See also:\n    - :func:`jax.scipy.special.kl_div`\n    - :func:`jax.scipy.special.rel_entr`\n  \"\"\"\n  x, = promote_args_inexact(\"entr\", x)\n  if dtypes.issubdtype(x.dtype, np.complexfloating):\n    raise ValueError(\"entr does not support complex-valued inputs.\")\n  return lax.select(lax.lt(x, _lax_const(x, 0)),\n                    lax.full_like(x, -np.inf),\n                    lax.neg(_xlogx(x)))\n\n\ndef boxcox(x: ArrayLike, lmbda: ArrayLike) -> Array:\n  r\"\"\"Box-Cox power transformation.\n\n  JAX implementation of :obj:`scipy.special.boxcox`.\n\n  .. math::\n\n     \\mathrm{boxcox}(x, \\lambda) = \\begin{cases}\n       (x^\\lambda - 1) / \\lambda & \\lambda \\ne 0 \\\\\n       \\log(x) & \\lambda = 0\n     \\end{cases}\n\n  Defined for :math:`x > 0`; returns ``nan`` for non-positive ``x``.","sourceCodeStart":1022,"sourceCodeEnd":1058,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/special.py#L1022-L1058","documentation":"jax.scipy.special.entr (elementary entropy x*log(x) with 0 for x=0 and -inf for x<0) is defined only for real numbers. It raises ValueError when the promoted input dtype is complex because the lax.lt comparison and _xlogx logic assume real ordering.","triggerScenarios":"Calling entr(x) with complex x, e.g. entr(jnp.array([1+1j])) or complex inputs reaching the entropy helpers _entropy_small_mu/_entropy_medium_mu of a distribution implementation.","commonSituations":"Computing entropy of distributions whose parameters went complex (e.g., complex rate parameters); complex-valued loss debugging in information-theory code; accidental complex promotion when mixing Python complex scalars with arrays.","solutions":["Pass real inputs: entr(jnp.real(x))","Inspect where the complex dtype originates — usually a complex rate/log-rate parameter; keep distribution parameters real","Guard with a dtype assertion before calling entr"],"exampleFix":"// before\njax.scipy.special.entr(mu)  # mu is complex\n// after\njax.scipy.special.entr(jnp.real(mu))","handlingStrategy":"validation","validationCode":"x = jnp.asarray(x, jnp.float32)  # upcast only valid if imag is zero\nassert not np.issubdtype(x.dtype, np.complexfloating)","typeGuard":"def real_or_none(x):\n    d = jnp.dtype(x)\n    return None if np.issubdtype(d, np.complexfloating) else x","tryCatchPattern":null,"preventionTips":["Check distribution parameter dtypes (rate, scale) for complex leakage before entropy computations","Unit-test loss functions with random real inputs to lock in real dtypes"],"tags":["jax","scipy-special","entropy","complex-dtype"],"backgroundTag":"unsupported-complex-input","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}