RunMat
GitHub

prod — Multiply elements of scalars, vectors, matrices, or N-D tensors with MATLAB-compatible options.

prod(X) multiplies the elements of scalars, vectors, matrices, and higher-dimensional tensors. When no dimension is supplied, the reduction runs along the first non-singleton dimension.

How prod works in RunMat

  • prod(X) on an m × n matrix returns a row vector (1 × n) with column-wise products.
  • prod(X, 2) returns a column vector (m × 1) containing row-wise products.
  • prod(X, dims) accepts a vector of dimensions (for example [1 3]) and collapses each listed axis while leaving the others untouched.
  • prod(X, 'all') flattens every dimension into a single scalar product.
  • Logical inputs are promoted to double precision (true → 1.0, false → 0.0) unless you request 'native' or 'like' output classes.
  • prod(___, 'omitnan') ignores NaN values; if every element in the slice is NaN, the result becomes 1, the multiplicative identity.
  • prod(___, 'includenan') (default) propagates NaN whenever a NaN appears in that slice.
  • prod(___, outtype) accepts 'double', 'default', or 'native' to control the output class.
  • prod(___, 'like', prototype) matches the numeric class and residency of prototype when supported by the active provider.
  • Empty inputs or reductions along dimensions with size 0 return ones that follow MATLAB shape semantics.

How prod runs on the GPU

When RunMat Accelerate is active, tensors that already reside on the GPU remain on the device. The runtime calls reduce_prod_dim (or reduce_prod for whole-array products) on the active provider when available. Requests that require 'omitnan', multi-axis reductions, or class coercions fall back to the host implementation, compute the correct MATLAB result, and re-upload only when a 'like' prototype demands GPU residency.

GPU memory and residency

You usually do **not** need to call gpuArray yourself in RunMat. The fusion planner keeps residency on the GPU for fused expressions, and reduction kernels execute on the device whenever the provider exposes the necessary hooks. To match MathWorks MATLAB behaviour—or to bootstrap GPU residency explicitly—you can still create GPU arrays manually.

Examples

Multiplying the elements of a matrix

A = [1 2 3; 4 5 6];
colProd = prod(A);
rowProd = prod(A, 2)

Expected output:

colProd = [4 10 18];
rowProd = [6; 120]

Multiplying across multiple dimensions

B = reshape(1:24, [3 4 2]);
prod13 = prod(B, [1 3])

Expected output:

prod13 =
     16380   587520  4021920  16030080

Multiplying with NaN values ignored

values = [2 NaN 4];
cleanProd = prod(values, 'omitnan')

Expected output:

cleanProd = 8

Multiplying on the GPU and matching an existing prototype

G = gpuArray(ones(1024, 1024) + 0.01);
proto = gpuArray(zeros(1, 1));
gpuResult = prod(G, 'like', proto);
result = gather(gpuResult)

Multiplying all elements of an array into a scalar

P = prod(1:10, 'all')

Expected output:

P = 3628800

Multiplying with native output type

ints = int16([2 3 4]);
nativeProd = prod(ints, 'native')

Expected output:

nativeProd = int16(24)

FAQ

When should I use the prod function?

Use prod whenever you need multiplicative reductions: geometric means, determinant-like products, or scaling chains of factors.

Does prod produce double arrays by default?

Yes. Unless you request 'native' or provide a 'like' prototype, the result is a dense double-precision array on the host.

What does prod(A) return?

If you call prod(A) where A is an array, the result is a new array of the same shape as A with products taken along the first non-singleton dimension.

How do I compute the product of a specific dimension?

Pass the dimension as the second argument (prod(A, 2) for row-wise products) or provide a dimension vector (prod(A, [1 3])) to collapse multiple axes at once.

What happens if all elements are NaN and I request 'omitnan'?

The result becomes 1, matching MATLAB's multiplicative identity semantics for empty slices.

Does prod preserve integer classes?

Only when you explicitly request 'native' or 'like'. Otherwise, integers are promoted to double precision so you do not have to manage overflow manually.

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

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

Open-source implementation

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