cumprod — Compute cumulative products in MATLAB and RunMat.
cumprod(X) multiplies elements cumulatively along a chosen dimension. The output has the same size as X, and each element stores the running product under MATLAB semantics.
Syntax
B = cumprod(A)
B = cumprod(A, dim)
B = cumprod(A, direction)
B = cumprod(A, nanflag)
B = cumprod(A, dim, direction)
B = cumprod(A, direction, dim)All supported cumprod forms
B = cumprod(A)
B = cumprod(A, dim)
B = cumprod(A, direction)
B = cumprod(A, nanflag)
B = cumprod(A, dim, direction)
B = cumprod(A, direction, dim)
B = cumprod(A, dim, nanflag)
B = cumprod(A, nanflag, dim)
B = cumprod(A, direction, nanflag)
B = cumprod(A, nanflag, direction)
B = cumprod(A, dim, direction, nanflag)
B = cumprod(A, dim, nanflag, direction)
B = cumprod(A, direction, dim, nanflag)
B = cumprod(A, direction, nanflag, dim)
B = cumprod(A, nanflag, dim, direction)
B = cumprod(A, nanflag, direction, dim)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input scalar or array. |
dim | Any | No | [] | Dimension selector (placeholder [] keeps default dimension). |
direction | StringScalar | No | "forward" | Scan direction: "forward" or "reverse". |
nanflag | StringScalar | No | "includenan" | Missing-value mode: "includenan"/"includemissing" or "omitnan"/"omitmissing". |
Returns
| Name | Type | Description |
|---|---|---|
B | NumericArray | Cumulative product result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:cumprod:InvalidArgument | Dimension, direction, or missing-value argument grammar is invalid. | cumprod: invalid argument |
RunMat:cumprod:InvalidInput | Input value type is unsupported for cumulative product reduction. | cumprod: invalid input |
RunMat:cumprod:Internal | Reduction execution fails due to conversion, provider, or allocation operations. | cumprod: internal reduction failure |
How cumprod works
- By default, the running product is taken along the first dimension whose length is greater than
1. cumprod(X, dim)lets you choose the dimension explicitly; ifdim > ndims(X), the input is returned unchanged.- Passing
[]for the dimension argument keeps the default dimension (MATLAB treats it as a placeholder). cumprod(..., "reverse")accumulates from the end toward the beginning;"forward"(default) works from start to finish.cumprod(..., "omitnan")treatsNaNvalues as missing. Empty prefixes yield1, the multiplicative identity.- Synonyms such as
"omitmissing"/"includemissing"are accepted for MATLAB compatibility. - The function supports real or complex scalars and dense tensors. Logical inputs are promoted to double precision.
Does RunMat run cumprod on the GPU?
When data already lives on the GPU, RunMat asks the active acceleration provider for a device-side prefix-product implementation. The runtime calls the cumprod_scan hook with the chosen dimension, direction, and NaN mode. Providers that lack this hook—or that report an error for the requested options—trigger a gather to host memory, perform the cumulative product on the CPU, and return the dense tensor result. Residency metadata is cleared so downstream operations can re-promote the tensor when profitable.
GPU memory and residency
Manual gpuArray calls are optional. RunMat promotes tensors automatically when the planner predicts a benefit, keeping fused expressions resident on the device. Explicit gpuArray is still supported for MATLAB compatibility or when you want to guarantee GPU residency before entering a critical loop.
Examples
Running products down each column (default dimension)
A = [1 2 3; 4 5 6];
columnProducts = cumprod(A)Expected output:
columnProducts =
1 2 3
4 10 18Tracking cumulative products across rows
A = [1 2 3; 4 5 6];
rowProducts = cumprod(A, 2)Expected output:
rowProducts =
1 2 6
4 20 120Reversing the accumulation direction
v = [2 3 4 5];
reverseProducts = cumprod(v, "reverse")Expected output:
reverseProducts =
120 60 20 5Ignoring NaN values while multiplying
v = [2 NaN 4 NaN 3];
running = cumprod(v, "omitnan")Expected output:
running =
2 2 8 8 24Computing a cumulative product inside a GPU workflow
G = gpuArray(1 + 0.1*rand(1, 5));
totals = cumprod(G);
hostResult = gather(totals)Using cumprod with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how cumprod changes the result.
Run a small cumprod example, explain the result, then change one input and compare the output.
FAQ
Does cumprod change the size of the input?⌄
No. The output always equals the input shape.
What happens if I request a dimension larger than ndims(X)?⌄
The input is returned unchanged, matching MATLAB behaviour.
How does "omitnan" treat leading NaN values?⌄
They are ignored, so the cumulative product uses the multiplicative identity 1 until a finite value appears.
Can I combine "reverse" and "omitnan"?⌄
Yes. The options can be specified in any order and RunMat mirrors MATLAB’s results.
Does the GPU path respect "omitnan"?⌄
Only when the active provider offers a native prefix-product kernel with missing-value support. Otherwise the runtime gathers to the host to preserve MATLAB semantics.
Related Math functions
Reduction
all · any · cummax · cummin · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var
Elementwise
abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · heaviside · 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
Structure
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how cumprod is executed, line by line, in Rust.
- View the source for cumprod 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.