Skip to main content

Special Mathematical Functions

The torch.special module provides special mathematical functions including gamma functions, error functions, Bessel functions, and orthogonal polynomials.

Gamma Functions

gammaln

torch.special.gammaln(input, *, out=None)
Computes the natural logarithm of the absolute value of the gamma function.
input
Tensor
The input tensor.
Returns
Tensor
Natural log of the absolute value of Gamma function: ln(|Γ(input)|)

digamma

torch.special.digamma(input, *, out=None)
Computes the logarithmic derivative of the gamma function (also called psi function).
input
Tensor
The input tensor.
Returns
Tensor
Digamma function: ψ(x) = d/dx ln(Γ(x)) = Γ'(x)/Γ(x)

polygamma

torch.special.polygamma(n, input, *, out=None)
Computes the nth derivative of the digamma function.
n
int
The order of the polygamma function (n ≥ 0).
input
Tensor
The input tensor.

multigammaln

torch.special.multigammaln(input, p, *, out=None)
Computes the multivariate log-gamma function with dimension p.
input
Tensor
The input tensor.
p
int
The number of dimensions.

gammainc / gammaincc

torch.special.gammainc(input, other, *, out=None)
torch.special.gammaincc(input, other, *, out=None)
Computes the regularized lower/upper incomplete gamma function.
input
Tensor
The first non-negative input tensor.
other
Tensor
The second non-negative input tensor.

Error Functions

erf

torch.special.erf(input, *, out=None)
Computes the error function.
input
Tensor
The input tensor.
Returns
Tensor
Error function: erf(x) = (2/√π) ∫₀ˣ e^(-t²) dt

erfc

torch.special.erfc(input, *, out=None)
Computes the complementary error function: erfc(x) = 1 - erf(x).

erfcx

torch.special.erfcx(input, *, out=None)
Computes the scaled complementary error function: erfcx(x) = e^(x²) * erfc(x).

erfinv

torch.special.erfinv(input, *, out=None)
Computes the inverse error function.
input
Tensor
The input tensor (values must be in (-1, 1)).

ndtr

torch.special.ndtr(input, *, out=None)
Computes the area under the standard Gaussian probability density function, integrated from minus infinity to input.
Returns
Tensor
Standard normal CDF: Φ(x) = (1/√(2π)) ∫₋∞ˣ e^(-t²/2) dt

ndtri

torch.special.ndtri(input, *, out=None)
Computes the inverse of ndtr (quantile function for standard normal distribution).

log_ndtr

torch.special.log_ndtr(input, *, out=None)
Computes the log of the standard normal CDF.

Bessel Functions

bessel_j0 / bessel_j1

torch.special.bessel_j0(input, *, out=None)
torch.special.bessel_j1(input, *, out=None)
Bessel function of the first kind of order 0 and 1.

bessel_y0 / bessel_y1

torch.special.bessel_y0(input, *, out=None)
torch.special.bessel_y1(input, *, out=None)
Bessel function of the second kind of order 0 and 1.

modified_bessel_i0 / modified_bessel_i1

torch.special.modified_bessel_i0(input, *, out=None)
torch.special.modified_bessel_i1(input, *, out=None)
Modified Bessel function of the first kind of order 0 and 1.

modified_bessel_k0 / modified_bessel_k1

torch.special.modified_bessel_k0(input, *, out=None)
torch.special.modified_bessel_k1(input, *, out=None)
Modified Bessel function of the second kind of order 0 and 1.

i0 / i0e / i1 / i1e

torch.special.i0(input, *, out=None)
torch.special.i0e(input, *, out=None)  # exponentially scaled
torch.special.i1(input, *, out=None)
torch.special.i1e(input, *, out=None)  # exponentially scaled
Modified Bessel functions (aliases and scaled versions).

spherical_bessel_j0

torch.special.spherical_bessel_j0(input, *, out=None)
Spherical Bessel function of the first kind of order 0.

Orthogonal Polynomials

chebyshev_polynomial_t / u / v / w

torch.special.chebyshev_polynomial_t(input, n, *, out=None)
torch.special.chebyshev_polynomial_u(input, n, *, out=None)
torch.special.chebyshev_polynomial_v(input, n, *, out=None)
torch.special.chebyshev_polynomial_w(input, n, *, out=None)
Chebyshev polynomials of the first, second, third, and fourth kinds.
input
Tensor
The input values.
n
Tensor
Degree of the polynomial.

hermite_polynomial_h / he

torch.special.hermite_polynomial_h(input, n, *, out=None)  # Physicist's
torch.special.hermite_polynomial_he(input, n, *, out=None)  # Probabilist's
Hermite polynomials.

laguerre_polynomial_l

torch.special.laguerre_polynomial_l(input, n, *, out=None)
Laguerre polynomial.

legendre_polynomial_p

torch.special.legendre_polynomial_p(input, n, *, out=None)
Legendre polynomial.

Other Special Functions

expit

torch.special.expit(input, *, out=None)
Computes the expit (logistic sigmoid) function: expit(x) = 1/(1 + e^(-x)).

logit

torch.special.logit(input, eps=None, *, out=None)
Computes the logit function (inverse of expit).
input
Tensor
The input tensor.
eps
float
default:"None"
Epsilon for input clamping. If provided, input is clamped to [eps, 1-eps].

xlog1py / xlogy

torch.special.xlog1py(input, other, *, out=None)
torch.special.xlogy(input, other, *, out=None)
Computes input * log1p(other) and input * log(other) with special handling for zero inputs.

entr

torch.special.entr(input, *, out=None)
Computes the entropy: entr(x) = -x * ln(x) for x > 0, 0 for x = 0, and -∞ for x < 0.

exp2 / expm1 / log1p

torch.special.exp2(input, *, out=None)  # 2^x
torch.special.expm1(input, *, out=None)  # e^x - 1
torch.special.log1p(input, *, out=None)  # log(1 + x)
Exponential and logarithm variants.

sinc

torch.special.sinc(input, *, out=None)
Computes the normalized sinc function: sinc(x) = sin(πx)/(πx) for x ≠ 0, 1 for x = 0.

zeta

torch.special.zeta(input, other, *, out=None)
Computes the Hurwitz zeta function: ζ(x, q) = Σ(k=0 to ∞) 1/(k+q)^x.

airy_ai

torch.special.airy_ai(input, *, out=None)
Airy function Ai(x).

Example Usage

import torch
import torch.special as special

# Gamma function logarithm
x = torch.tensor([1.0, 2.0, 3.0, 4.0])
gammaln_x = special.gammaln(x)
print(f"ln(Γ(x)): {gammaln_x}")
# Note: Γ(n) = (n-1)! for positive integers
# So Γ(4) = 3! = 6, ln(6) ≈ 1.79

# Digamma function
digamma_x = special.digamma(x)
print(f"ψ(x): {digamma_x}")

# Polygamma (second derivative)
polygamma_x = special.polygamma(2, x)
print(f"ψ''(x): {polygamma_x}")

# Incomplete gamma functions
a = torch.tensor([2.0, 3.0])
x = torch.tensor([1.0, 2.0])
lower = special.gammainc(a, x)
upper = special.gammaincc(a, x)
print(f"Lower + Upper = {lower + upper}")  # Should be all 1's

Use Cases

  • ndtr, ndtri: Normal distribution CDF and quantiles
  • erf, erfc: Related to normal distribution
  • gammaln: Log-likelihood in gamma distributions
  • digamma, polygamma: Maximum likelihood estimation
  • Bessel functions: Solving wave equations, filters
  • sinc: Sinc interpolation, digital signal processing
  • Orthogonal polynomials: Spectral methods, approximation theory
  • expit, logit: Logistic regression, binary classification
  • softmax, log_softmax: Multi-class classification
  • xlogy, entr: Information theory, KL divergence
  • gammaln: Variational inference, Bayesian methods