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 does the prod function behave in MATLAB / RunMat?
prod(X)on anm × nmatrix 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')ignoresNaNvalues; if every element in the slice isNaN, the result becomes1, the multiplicative identity.prod(___, 'includenan')(default) propagatesNaNwhenever aNaNappears in that slice.prod(___, outtype)accepts'double','default', or'native'to control the output class.prod(___, 'like', prototype)matches the numeric class and residency ofprototypewhen supported by the active provider.- Empty inputs or reductions along dimensions with size
0return ones that follow MATLAB shape semantics.
GPU behavior
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 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 of using prod in MATLAB / RunMat
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 16030080Multiplying with NaN values ignored
values = [2 NaN 4];
cleanProd = prod(values, 'omitnan')Expected output:
cleanProd = 8Multiplying 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 = 3628800Multiplying 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.
See also
sum, mean, cumprod, gpuArray, gather
Source & Feedback
- Source code: `crates/runmat-runtime/src/builtins/math/reduction/prod.rs`
- Found a bug? Open an issue with a minimal reproduction.