RunMat
GitHub

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

NameTypeRequiredDefaultDescription
AAnyYesInput scalar or array.
dimAnyNo[]Dimension selector (placeholder [] keeps default dimension).
directionStringScalarNo"forward"Scan direction: "forward" or "reverse".
nanflagStringScalarNo"includenan"Missing-value mode: "includenan"/"includemissing" or "omitnan"/"omitmissing".

Returns

NameTypeDescription
BNumericArrayCumulative product result.

Errors

IdentifierWhenMessage
RunMat:cumprod:InvalidArgumentDimension, direction, or missing-value argument grammar is invalid.cumprod: invalid argument
RunMat:cumprod:InvalidInputInput value type is unsupported for cumulative product reduction.cumprod: invalid input
RunMat:cumprod:InternalReduction 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; 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.

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    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)

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.

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

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · eig · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · null · pinv · rank · rcond · rref

Symbolic

digits · int · limit · sym · syms · vpa

Fft

fft · fft2 · fftshift · ifft · ifft2 · ifftshift

Interpolation

interp1 · interp2 · pchip · ppval · spline

Ode

ode15s · ode23 · ode45

Open-source implementation

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

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.