RunMat
  • Pricing
RunMat
GitHub
GitHub
DownloadSign InTry in Browser
DesktopRuntimeServer
RunMat

Run math blazing fast

GitHubX (Twitter)LinkedIn

Company

  • About
  • Pricing
  • Contact
  • License
  • Privacy

Learn

  • Docs
  • Blog
  • Benchmarks
  • RunMat vs MATLAB Online

Get product updates and release notes from the RunMat team.

© 2026 Dystr · Made withfor the scientific community.

RunMat™ is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

/
See all docs
Builtin Reference
    • 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

trapz — Approximate integrals of sampled data using the trapezoidal rule with MATLAB-compatible dimension handling.

trapz(Y) approximates integrals of single- or double-precision sampled data using trapezoidal segments. By default integration runs along the first non-singleton dimension, with MATLAB-compatible explicit-dimension forms supported. Typed-integer sample data and spacing are available in RunMat mode when every value can enter the binary64 integration domain exactly.

Syntax

Q = trapz(Y)
Q = trapz(Y, dim)
Q = trapz(X, Y)
Q = trapz(X, Y, dim)

Inputs

NameTypeRequiredDefaultDescription
YAnyYes—Sample values.
dimAnyYes—Integration dimension.
XAnyYes—Sample points or spacing.

Returns

NameTypeDescription
QNumericArrayTrapezoidal integral output.

Errors

IdentifierWhenMessage
RunMat:trapz:InvalidArgumentInput argument count, dimension selector, or spacing arguments are invalid.trapz: invalid argument
RunMat:trapz:InvalidInputInput values cannot be converted to supported numeric integration domains.trapz: invalid input
RunMat:trapz:InternalIntegration execution fails during gather, allocation, or provider promotion.trapz: internal integration failure

How trapz works

  • trapz(Y) assumes unit spacing between adjacent samples.
  • trapz(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.
  • trapz(..., dim) selects the working dimension explicitly. Dimensions larger than ndims(Y) behave like singleton axes, so the result is 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.
  • Single-precision sample data produces single-precision output; typed-integer sample data is a RunMat extension and produces double output.
  • Native typed-integer Y and X roles are independently compatibility-gated and require exact binary64 conversion. Native typed-integer dim is an ordinary exact structural selector.
  • When the selected dimension has length 0 or 1, trapz returns zeros because there are no intervals to integrate.

Does RunMat run trapz on the GPU?

Real, logical, and complex-interleaved gpuArray sample inputs use provider-backed trapezoidal integration for unit spacing, scalar spacing, coordinate-vector spacing, and same-size coordinate-tensor spacing.

Spacing inputs must be real-valued. Provider-missing cases use the host fallback path to preserve MATLAB-compatible semantics.

GPU memory and residency

Manual gpuArray promotion is optional. If real, logical, or complex-interleaved sampled data already lives on the GPU, RunMat preserves the output as a gpuArray through the provider trapz_dim path.

Examples

Integrating sampled sine data with explicit coordinates

x = 0:0.01:pi;
y = sin(x);
area = trapz(x, y)

Expected output:

area ≈ 2

Using unit spacing for a row vector

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

Expected output:

q = 4

Integrating each row independently

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

Expected output:

q =
     4
    10

Running trapz on GPU data

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

Expected output:

result = 4

Using trapz with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how trapz changes the result.

Run a small trapz example, explain the result, then change one input and compare the output.

FAQ

What spacing does trapz(Y) assume?⌄

It assumes adjacent samples are one unit apart.

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.

What happens for a scalar or singleton slice?⌄

The integral is zero because there are no trapezoids to sum.

Does trapz support complex inputs?⌄

Yes. RunMat integrates the real and imaginary parts independently and returns a complex result.

Does trapz keep gpuArray residency?⌄

For real, logical, and complex-valued gpuArray sample inputs, yes. RunMat uses the acceleration provider's trapz_dim hook with real unit, scalar, vector, or tensor spacing.

Related Math functions

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 · var

Elementwise

abs · angle · bsxfun · complex · conj · double · erf · erfcinv · exp · expm1 · factorial · flintmax · gamma · 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

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

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · decomposition · eig · eigs · lu · qr · svd

Solve

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

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

Symbolic

digits · int · limit · piecewise · sym · syms · vpa

Fft

fft · fft2 · fftn · fftshift · ifft · ifft2 · ifftn · ifftshift

Interpolation

griddedInterpolant · interp1 · interp1q · interp2 · pchip · ppval · spline

Discrete

lcm · primes

Ode

ode15s · ode23 · ode45

Poly

polyder · polyfit · polyint · polyval · roots

Open-source implementation

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

  • View the source for trapz 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.

Getting started · Benchmarks · Pricing

Download RunMat

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

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • How trapz works
  • Does RunMat run trapz on the GPU?
  • GPU memory and residency
  • Examples
  • Integrating sampled sine data with explicit coordinates
  • Using unit spacing for a row vector
  • Integrating each row independently
  • Running trapz on GPU data
  • Using trapz with coding agents
  • FAQ
  • Related Math functions
  • Reduction
  • Elementwise
  • Trigonometry
  • Structure
  • Signal
  • Rounding
  • Factor
  • Solve
  • Optim
  • Ops
  • Symbolic
  • Fft
  • Interpolation
  • Discrete
  • Ode
  • Poly
  • Open-source implementation
  • About RunMat