gamma — Compute gamma function values element-wise in MATLAB and RunMat.
Y = gamma(X) evaluates the Euler gamma function element-by-element. Positive integers map to (n-1)!, and real/complex branch behavior follows MATLAB semantics.
Syntax
Y = gamma(X)
Y = gamma(X, "like", prototype)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X | Any | Yes | — | Real or complex numeric input. |
like | StringScalar | Yes | — | Literal string "like". |
prototype | LikePrototype | Yes | — | Output class/device prototype. |
Returns
| Name | Type | Description |
|---|---|---|
Y | NumericArray | Gamma-function result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:gamma:InvalidArgument | Optional arguments are malformed or unsupported. | gamma: invalid argument |
RunMat:gamma:InvalidInput | Input value or prototype cannot be converted to supported numeric forms. | gamma: invalid input |
RunMat:gamma:GpuUnsupported | GPU output via "like" is requested but no compatible provider is active. | gamma: gpu output not supported |
RunMat:gamma:Internal | Internal gather/provider/tensor construction failed. | gamma: internal error |
How gamma works
- 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
Infat 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'.
Does RunMat run gamma on the GPU?
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.
GPU memory and residency
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.
Examples
Converting integers to factorials automatically
gamma(5)Expected output:
ans = 24Evaluating half-integer inputs
gamma(0.5)Expected output:
ans = 1.7725Handling negative non-integers
gamma(-0.5)Expected output:
ans = -3.5449Applying gamma element-wise to arrays
A = [1 2; 3 4];
B = gamma(A)Expected output:
B =
1 1
2 6Working with complex numbers
z = 0.5 + 1i;
g = gamma(z)Expected output:
g = 0.8182 - 0.7633iUsing 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]Using gamma with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how gamma changes the result.
Run a small gamma example, explain the result, then change one input and compare the output.
FAQ
How is gamma(n) related to factorials?⌄
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.
Related Math functions
Elementwise
abs · angle · complex · conj · double · exp · expm1 · factorial · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · sign · single · sqrt · times
Trigonometry
acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh
Reduction
all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var
Structure
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how gamma is executed, line by line, in Rust.
- View the source for gamma in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.