gamma — Compute gamma function values element-wise in MATLAB and RunMat.
Y = gamma(X) evaluates the Euler gamma function element-by-element for real single- or double-precision input.
Syntax
Y = gamma(X)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
X | Any | Yes | — | Real single or double input. |
Returns
| Name | Type | Description |
|---|---|---|
Y | NumericArray | Gamma-function result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:gamma:InvalidArgument | The invocation has more than one input. | gamma: invalid argument |
RunMat:gamma:InvalidInput | Input is not real single or double numeric data. | gamma: invalid input |
RunMat:gamma:Internal | Internal gather, provider, or tensor construction failed. | gamma: internal error |
How gamma works
- Preserves single or double precision and the input tensor shape.
- Integer, logical, character, string, complex, and sparse inputs are rejected.
- Returns
Infat non-positive integers, mirroring the poles in the analytic definition. - Keeps real-valued GPU tensors on their owning provider. If a native result does not satisfy the output contract, RunMat discards it and uses the owner-preserving host fallback.
Does RunMat run gamma on the GPU?
RunMat rejects resident integer, logical, and complex inputs before provider execution. For admitted real floating inputs, it accepts native unary_gamma output only when shape, device, real storage, precision, and owning provider match; malformed output is discarded before host fallback, and rejected non-alias allocations are freed safely before re-upload to the original owner.
GPU memory and residency
Yes. Real single- and double-precision gpuArray inputs remain associated with their owning provider whether execution uses a validated native unary_gamma result or the owner-preserving host fallback.
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 6Using 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 typed integer, logical, character, or complex inputs work?⌄
No. The compatibility contract accepts real single and double input only. A numeric literal such as 5 is double, so gamma(5) remains valid.
Can I keep results on the GPU?⌄
Yes. RunMat uses the owning provider's unary_gamma hook when its output satisfies the required shape, precision, storage, and ownership contract. Otherwise it computes on the host and uploads the result back to that owner.
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.
Related Math functions
Elementwise
abs · angle · bsxfun · complex · conj · double · erf · erfcinv · exp · expm1 · factorial · flintmax · gammaln · heaviside · hypot · idivide · imag · intmax · intmin · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · realmax · realmin · realsqrt · rescale · sign · single · sqrt · swapbytes · times · typecast · uint16 · uint32 · uint8
Trigonometry
acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · cospi · deg2rad · pol2cart · rad2deg · sin · sind · sinh · sinpi · tan · tand · tanh
Reduction
all · any · bounds · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · maxk · mean · median · min · mink · movmax · movmean · movmedian · movmin · movprod · movstd · movsum · movvar · nnz · prod · rms · std · sum · trapz · var
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm
Signal
blackman · butter · buttord · cheb2ord · conv · conv2 · deconv · downsample · envelope · filter · filtfilt · fir1 · freqz · gauspuls · hamming · hann · hilbert · periodogram · pulstran · pwelch · rectpuls · resample · sawtooth · sinc · spectrogram · square · tripuls · unwrap · upsample · zplane
Optim
coneprog · fminbnd · fminunc · fsolve · fzero · integral · linprog · lsqcurvefit · lsqnonlin · optimoptions · optimset · quad · secondordercone
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
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.