{"record":{"id":"c4c6d87b9d10ee58","repo":"jax-ml/jax","slug":"polygamma-does-not-support-complex-valued-inputs","errorCode":null,"errorMessage":"polygamma does not support complex-valued inputs.","messagePattern":"polygamma does not support complex-valued inputs\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/special.py","lineNumber":1357,"sourceCode":"\n  Args:\n    n: arraylike, integer-valued. The order of the derivative.\n    x: arraylike, real-valued. The value at which to evaluate the function.\n\n  Returns:\n    array\n\n  See also:\n    - :func:`jax.scipy.special.gamma`\n    - :func:`jax.scipy.special.digamma`\n  \"\"\"\n  if not dtypes.issubdtype(lax.dtype(n), np.integer):\n    raise ValueError(\n        f\"Argument `n` to polygamma must be of integer type. Got dtype {lax.dtype(n)}.\"\n    )\n  n_arr, x_arr = promote_args_inexact(\"polygamma\", n, x)\n  if dtypes.issubdtype(x_arr.dtype, np.complexfloating):\n    raise ValueError(\"polygamma does not support complex-valued inputs.\")\n  return lax.polygamma(n_arr, x_arr)\n\n\n# Normal distributions\n\n# Functions \"ndtr\" and \"ndtri\" are derived from calculations made in:\n# https://root.cern.ch/doc/v608/SpecFuncCephesInv_8cxx_source.html\n# The \"spence\" function is also based on the Cephes library with\n# the corresponding spence.c file located in the tarball:\n# https://netlib.org/cephes/misc.tgz\n# In the following email exchange, the author gives his consent to redistribute\n# derived works under an Apache 2.0 license.\n#\n# From: Stephen Moshier <steve@moshier.net>\n# Date: Sat, Jun 9, 2018 at 2:36 PM\n# Subject: Re: Licensing cephes under Apache (BSD-like) license.\n# To: rif <rif@google.com>\n#","sourceCodeStart":1339,"sourceCodeEnd":1375,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/special.py#L1339-L1375","documentation":"jax.scipy.special.polygamma rejects complex-valued x after promotion. The order n must also be integer (checked separately); this second check fires when x_arr becomes complex, since lax.polygamma is only implemented for real inputs.","triggerScenarios":"Calling polygamma(n, 1+2j) or polygamma(n, x) where x is a complex array, or where n/x promotion with a complex constant upgrades the pair to complex.","commonSituations":"Complex differentiation of gamma-family functions in physics code (e.g. derivatives of log-gamma of complex arguments); SciPy's complex-capable polygamma ported to JAX; complex parameters leaking from upstream transforms.","solutions":["Use real x: polygamma(n, jnp.real(x))","For complex polygamma needs, compute via mpmath outside the JAX graph or implement with digamma recurrences on complex logs yourself","Trace where the complex dtype enters and keep the pipeline real"],"exampleFix":"// before\njax.scipy.special.polygamma(1, z)  # z complex\n// after\njax.scipy.special.polygamma(1, jnp.real(z))","handlingStrategy":"type-guard","validationCode":"x = jnp.real(x)\nif np.issubdtype(jnp.result_type(x, n_promoted), np.complexfloating):\n    raise TypeError('polygamma needs real x')","typeGuard":"def real_x(x):\n    return x if not np.issubdtype(jnp.dtype(x), np.complexfloating) else None","tryCatchPattern":null,"preventionTips":["Keep gamma-family function arguments real in JAX; use mpmath for complex","Check both n (integer) and x (real) before calling polygamma"],"tags":["jax","scipy-special","polygamma","complex-dtype"],"backgroundTag":"unsupported-complex-input","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}