Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

special.gamma

Computes the gamma function for real or complex scalar and array-like inputs. It generalizes factorials and optionally stores results in a supplied output array.

How it works

scipy.special.gamma evaluates the mathematical gamma function for real or complex scalar and array-like inputs. It supports analytic continuation beyond the defining integral's region and can write results into an optional output array.

Syntax

scipy.special.gamma(z, out=None)

Parameters

NameTypeDescription
zarray_likeReal or complex valued argument.
outndarray, optionalOptional array receiving the function values.

Returns

scalar or ndarray: Values of the gamma function

Key points

  • The input may contain real or complex values.
  • The optional out parameter lets callers provide an array for receiving results.
  • Gamma extends the factorial concept beyond nonnegative integers.

Common mistakes

  • At its poles, including non-positive integer arguments, the function is singular; avoid using those inputs when finite results are required.

Example: Evaluate gamma values for an array

import numpy as np
from scipy.special import gamma

x = np.array([1.0, 2.0, 3.0, 0.5])
print(gamma(x))  # [1.         1.         2.         1.77245385]

Evaluates the gamma function elementwise for a NumPy array.

When to use it

Generalized factorial calculations

Convert factorial-like expressions involving non-integer or complex arguments into numerical values.

Vectorized numerical evaluation

Evaluate special-function values across NumPy arrays without explicit Python loops.

Memory-aware array workflows

Reuse preallocated storage during repeated array computations.

Interview questions

QWhat does scipy.special.gamma compute, and what does it return?

gamma(z) computes the gamma function, which generalizes factorials: for a natural number n, Γ(n + 1) equals n!. It accepts real or complex array-like input and returns a scalar or ndarray.

Follow-up: How can factorial values be expressed with gamma? For a nonnegative integer n, use gamma(n + 1), because the recurrence and Γ(1) = 1 establish that identity.

QWhat is the mathematical definition and domain behavior of gamma?

The defining integral applies when the real part of z is positive. SciPy extends the function to the rest of the complex plane through analytic continuation.

Follow-up: What happens at nonnegative integer poles? The function is singular there, and the limiting infinity sign depends on the approach direction.

QWhat is the purpose of the out parameter?

The optional out argument supplies an ndarray that receives the computed values. This is useful when the caller wants results stored in preallocated output memory rather than a newly allocated result.

Follow-up: What input shapes are practical? Array-like inputs are handled elementwise, producing a scalar for scalar input or an ndarray for array input.

  • scipy.special.gammaln
  • scipy.special.loggamma
  • scipy.special.rgamma

Source: scipy.special.gamma. Checked 2026-10-15.