{"record":{"id":"a35df68603743ee9","repo":"jax-ml/jax","slug":"argument-n-to-polygamma-must-be-of-integer-type","errorCode":null,"errorMessage":"Argument `n` to polygamma must be of integer type. Got dtype {lax.dtype(n)}.","messagePattern":"Argument `n` to polygamma must be of integer type\\. Got dtype (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/special.py","lineNumber":1352,"sourceCode":"\n     \\mathrm{polygamma}(n, x) = \\psi^{(n)}(x) = \\frac{\\mathrm{d}^{n+1}}{\\mathrm{d}x^{n+1}} \\log \\Gamma(x)\n\n  where :math:`\\psi` is the :func:`~jax.scipy.special.digamma` function and\n  :math:`\\Gamma` is the :func:`~jax.scipy.special.gamma` function.\n\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#","sourceCodeStart":1334,"sourceCodeEnd":1370,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/special.py#L1334-L1370","documentation":"jax.scipy.special.polygamma(n, x) requires the order n to be an integer dtype (it uses n for integer-dependent dispatch in lax.polygamma). If lax.dtype(n) is not an integer subtype (e.g., float32 or complex), it raises this ValueError with the offending dtype.","triggerScenarios":"Calling polygamma(1.0, x) (float n), polygamma(jnp.asarray(2.0), x), or any n produced by arithmetic that yields floating dtype, e.g. n = 2/1 in Python 3 giving 2.0.","commonSituations":"Passing a Python division result as the order; batched/vectorized n that got cast to float by stacking with floats; porting code where n arrived from a config as a float string; forgetting operator.index on traced values.","solutions":["Cast n to integer: polygamma(int(n), x) or polygamma(jnp.asarray(n, jnp.int32), x)","Fix the producer: use integer division // or int() at the source so n stays integral","Validate n's dtype before the call and fail fast with your own error"],"exampleFix":"// before\njax.scipy.special.polygamma(2/2, x)  # n is 1.0 float\n// after\njax.scipy.special.polygamma(2//2, x)  # n is int 1","handlingStrategy":"type-guard","validationCode":"n = int(n) if isinstance(n, (int, np.integer)) else operator.index(n)\n# or: n = jnp.asarray(n, jnp.int32)","typeGuard":"def is_integer_order(n):\n    return np.issubdtype(jnp.dtype(n), np.integer) or isinstance(n, (int, np.integer))","tryCatchPattern":null,"preventionTips":["Use integer literals and // division for order arguments","Cast config-loaded numeric strings with int(), not float()"],"tags":["jax","scipy-special","polygamma","dtype-validation"],"backgroundTag":"integer-argument-required","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}