RunMat
GitHub

cumprod — Cumulative product of scalars, vectors, matrices, or N-D tensors.

cumprod(X) multiplies elements cumulatively along a chosen dimension. The result has the same size as X, and each element stores the running product.

How cumprod works in RunMat

  • 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; if dim > 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") treats NaN values as missing. Empty prefixes yield 1, 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.

How cumprod runs 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    18

Tracking cumulative products across rows

A = [1 2 3; 4 5 6];
rowProducts = cumprod(A, 2)

Expected output:

rowProducts =
     1     2     6
     4    20   120

Reversing the accumulation direction

v = [2 3 4 5];
reverseProducts = cumprod(v, "reverse")

Expected output:

reverseProducts =
   120    60    20     5

Ignoring NaN values while multiplying

v = [2 NaN 4 NaN 3];
running = cumprod(v, "omitnan")

Expected output:

running =
     2     2     8     8    24

Computing a cumulative product inside a GPU workflow

G = gpuArray(1 + 0.1*rand(1, 5));
totals = cumprod(G);
hostResult = gather(totals)

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.

These functions work well alongside cumprod. Each page has runnable examples you can try in the browser.

prod, cumsum, sum, gpuArray, gather, all, any, cummax, cummin, diff, max, mean, median, min, nnz, std, var

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how cumprod works, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.