View all functions

CategoryMath: Elementwise
GPUYes

What does the gamma function do in MATLAB / RunMat?

Y = gamma(X) evaluates the Euler gamma function element-by-element. For positive integers the result is (n-1)!, half-integers map to scaled square-roots of π, and complex arguments follow the analytic continuation Γ(z) = ∫_0^∞ t^{z-1} e^{-t} dt.

How does the gamma function behave in MATLAB / RunMat?

  • Respects MATLAB’s broadcasting rules for all dense tensor inputs.
  • Promotes logical and integer values to double precision before evaluation; character arrays are converted through their Unicode code points.
  • Computes complex values with a Lanczos approximation and the reflection identity so results match MATLAB for every quadrant.
  • Returns Inf at non-positive integers, mirroring the poles in the analytic definition.
  • Keeps real-valued GPU tensors on device when the provider implements unary_gamma; otherwise it gathers to the host, evaluates, and reapplies any requested residency via 'like'.

gamma Function GPU Execution Behaviour

RunMat Accelerate first calls the active provider’s unary_gamma hook. With the WGPU backend this kernel runs entirely on device using a Lanczos approximation. Providers that decline the hook trigger an automatic gather to the host. After computing the double-precision result, RunMat re-uploads the tensor when you pass 'like', gpuArray(...). Complex outputs always stay on the host because current providers expose real-valued buffers only.

Examples of using the gamma function in MATLAB / RunMat

Converting integers to factorials automatically

gamma(5)

Expected output:

ans = 24

Evaluating half-integer inputs

gamma(0.5)

Expected output:

ans = 1.7725

Handling negative non-integers

gamma(-0.5)

Expected output:

ans = -3.5449

Applying gamma element-wise to arrays

A = [1 2; 3 4];
B = gamma(A);

Expected output:

B =
     1     1
     2     6

Working with complex numbers

z = 0.5 + 1i;
g = gamma(z);

Expected output (rounded):

g = 0.8182 - 0.7633i

Using gamma with GPU tensors

G = gpuArray([0.5 1.5 2.5]);
out = gamma(G);
result = gather(out);

Expected output:

result = [1.7725 0.8862 1.3293]

GPU residency in RunMat (Do I need gpuArray?)

Usually not. Accelerate keeps tensors on the GPU when the provider exposes a unary_gamma kernel (the default WGPU backend does). Otherwise RunMat gathers the tensor, evaluates the gamma function on the CPU, and uploads the result again only when you explicitly request GPU residency via 'like', gpuArray(...). Complex results remain on the host because today’s GPU providers operate on real buffers.

FAQ

For positive integers n, gamma(n) = (n-1)!. This identity underpins the factorial extension used throughout probability, statistics, and combinatorics.

What happens at non-positive integers?

gamma has simple poles at 0, -1, -2, .... RunMat mirrors MATLAB by returning Inf at those points and signalling the singularity without throwing an error.

Are half-integers supported exactly?

Yes. Values such as gamma(0.5) = sqrt(pi) are computed with a Lanczos approximation that provides double-precision accuracy consistent with MATLAB.

Do complex inputs work?

Absolutely. RunMat evaluates the analytic continuation using the reflection identity Γ(z) = π / (sin(πz) Γ(1-z)) for Re(z) < 0.5, so complex arguments behave the same as in MATLAB.

Can I keep results on the GPU?

Yes for real-valued outputs whenever the active provider exposes unary_gamma (including the WGPU backend). If the provider lacks the hook, RunMat falls back to the host but re-uploads the tensor when you request a real-valued GPU prototype via 'like'. Complex outputs stay on the host for now.

What about overflow?

Large positive inputs eventually overflow to Inf, just like MATLAB. Negative inputs near poles produce signed infinities consistent with the analytic behaviour.

Does gamma accept 'like' prototypes?

Yes. Provide 'like', T to control residency and numeric class. Complex prototypes are honoured on the host; GPU prototypes must be real.

See Also

log, exp, sqrt, gpuArray, gather

Source & Feedback