RunMat
GitHub

cumtrapz — Cumulative trapezoidal numerical integration of sampled data.

cumtrapz(Y) computes the cumulative trapezoidal integral of sampled data in Y. The output has the same size as Y, with the first element along the working dimension set to zero and later elements holding the running integral.

How cumtrapz works

  • cumtrapz(Y) assumes unit spacing between adjacent samples.
  • cumtrapz(X, Y) accepts a scalar spacing, a vector of coordinates whose length matches the working dimension, or an array X with the same size as Y.
  • cumtrapz(..., dim) selects the working dimension explicitly. Dimensions larger than ndims(Y) behave like singleton axes, so the result stays zero along those slices.
  • Logical inputs are promoted to double precision before integration. Complex-valued Y inputs are integrated component-wise and preserve their complex output type.
  • When the selected dimension has length 0 or 1, the output is zero along that dimension because there are no intervals to accumulate.

How RunMat runs cumtrapz on the GPU

RunMat does not yet expose a native provider hook for cumtrapz. When the input is a gpuArray, the runtime gathers the data to host memory, performs the cumulative trapezoidal integration with MATLAB-compatible dimension and spacing rules, and re-uploads real-valued outputs so downstream GPU work stays resident.

GPU memory and residency

Manual gpuArray promotion is optional. If sampled data already lives on the GPU, RunMat preserves real-valued cumulative results as gpuArrays by re-uploading the output after the host fallback path.

Examples

Computing a cumulative integral with explicit coordinates

x = [0 1 3];
y = [0 1 2];
q = cumtrapz(x, y)

Expected output:

q = [0 0.5 3.5]

Using unit spacing for sampled data

y = [1 2 3];
q = cumtrapz(y)

Expected output:

q = [0 1.5 4]

Accumulating across rows in a matrix

A = [1 2 3; 4 5 6];
q = cumtrapz(A, 2)

Expected output:

q =
     0    1.5    4
     0    4.5   10

Running cumtrapz on GPU data

G = gpuArray([1 2 3]);
q = cumtrapz(G);
result = gather(q)

Expected output:

result = [0 1.5 4]

FAQ

How is cumtrapz different from trapz?

trapz returns the final integrated value, while cumtrapz returns the running integral at every sample.

Why is the first element zero?

There is no interval before the first sample, so the cumulative integral begins at zero.

What forms of X are accepted?

A scalar spacing, a vector of coordinates matching the integration dimension, or an array with the same size as Y.

Does cumtrapz support complex inputs?

Yes. RunMat accumulates the real and imaginary components independently and returns a complex result.

Does cumtrapz keep gpuArray residency?

For real-valued results, yes. RunMat currently gathers GPU inputs to the host, computes the cumulative trapezoidal integral, and re-uploads the result so downstream GPU code can stay resident.

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

trapz, cumsum, diff, gpuArray, gather

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how cumtrapz 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 for free

Open the sandbox and start running MATLAB code in seconds. No account required.