RunMat
GitHub

prod — Multiply elements across dimensions in MATLAB and RunMat.

prod(X) multiplies elements of X along a selected dimension or across all elements. Default dimension behavior and option handling follow MATLAB semantics.

Syntax

B = prod(A)
B = prod(A, dim)
B = prod(A, "all")
B = prod(A, nanflag)
B = prod(A, outtype)
B = prod(A, dim, nanflag)
B = prod(A, nanflag, dim)
B = prod(A, "like", prototype)

Inputs

NameTypeRequiredDefaultDescription
AAnyYesInput array.
dimAnyNo[]Dimension selector or vector of dimensions.
allStringScalarNo"all"Reduce across all dimensions.
nanflagStringScalarNo"includenan"NaN handling mode: "includenan" or "omitnan".
outtypeStringScalarNo"double"Output class specifier: "double", "default", "native", or "like".
likeStringScalarNo"like"Prototype keyword.
prototypeAnyYesPrototype value controlling output class/device.

Returns

NameTypeDescription
BNumericArrayProduct reduction result.

Errors

IdentifierWhenMessage
RunMat:prod:InvalidArgumentDimension, nanflag, or output class argument grammar is invalid.prod: invalid argument
RunMat:prod:InvalidInputInput value type is unsupported for prod reduction.prod: invalid input
RunMat:prod:ComplexUnsupportedComplex inputs are passed to prod where complex support is not implemented.prod: complex inputs are not yet supported
RunMat:prod:InternalReduction execution fails due to conversion, provider, allocation, or coercion operations.prod: internal reduction failure

How prod works

  • 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.

Does RunMat run prod 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)

Using prod with coding agents

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

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

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.

Reduction

all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · 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 prod 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.