dot — Compute dot products in MATLAB and RunMat.
dot(A, B) evaluates inner products between matching slices of A and B, optionally along a selected dimension. Complex inputs use conjugate-on-first-argument semantics consistent with MATLAB and RunMat.
Syntax
C = dot(A, B)
C = dot(A, B, dim)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Left operand. |
B | Any | Yes | — | Right operand. |
dim | NumericScalar | Yes | — | Reduction dimension. |
Returns
| Name | Type | Description |
|---|---|---|
C | NumericArray | Dot product result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:dot:InvalidArgument | Argument count or dimension argument is invalid. | dot: invalid argument |
RunMat:dot:InvalidInput | Inputs are unsupported or incompatible. | dot: A and B must be the same size. |
RunMat:dot:Internal | Runtime cannot materialize dot outputs. | dot: internal runtime failure |
How dot works
- Vectors need the same number of elements and may use different row/column orientations. Matrices and N-D arrays must have identical sizes.
- When no dimension is supplied, the function reduces along the first non-singleton dimension (
dim = 1when all dimensions are singleton). dot(A, B, dim)collapses dimensiondim(1-based) while leaving every other dimension untouched.- Complex inputs conjugate the first argument before multiplication; real inputs use a straight element-wise product.
- Single inputs produce single results, including on the host/fallback path; otherwise the floating result is double.
- Empty reductions yield zeros of the appropriate shape; length mismatches raise
A and B must be the same size. - Public MATLAB data inputs are single or double. RunMat additionally accepts logical and all eight typed-integer classes only in RunMat extension mode.
- When both extension operands are typed integers, each product is formed from authoritative integer storage before crossing to the floating reduction/output domain, avoiding pre-product loss above flintmax.
- Typed-integer
dimis a documented control input, is available in compatibility modes, and is decoded exactly in every supported integer class.
Does RunMat run dot on the GPU?
Native dispatch never sends integer or logical storage to a floating dot hook. It requires proven common ownership/device and accepts a fresh result only after owner, device, shape, real noninteger storage, and operand-requested precision validation; malformed native results trigger the host fallback. Global-provider fallback for an unknown device is not accepted as ownership. Outputs that alias either input or the fallback residency anchor are rejected and never freed as provider-owned results. Other rejected allocations are freed only through a proven actual owner. Fallback gathers through each handle owner and restores real output to the first resident owner only after applying the same owner, device, shape, storage, class-metadata, freshness, and requested-precision checks.
GPU memory and residency
Same-owner, same-device real floating inputs can use the owning provider's native dot hook. Other supported resident forms gather through the originating owner and restore real results there when its precision can represent the output. Complex outputs remain host-side until provider complex dot support is available.
Examples
Computing the dot product of row vectors
A = [1 2 3];
B = [4 5 6];
val = dot(A, B)Expected output:
val = 32Dotting column vectors to obtain a scalar
u = [1; 3; 5];
v = [2; 4; 6];
val = dot(u, v)Expected output:
val = 44Applying dot along a chosen dimension
X = [1 2 3; 4 5 6];
Y = [6 5 4; 3 2 1];
cols = dot(X, Y, 1) % collapse rows
rows = dot(X, Y, 2) % collapse columnsExpected output:
cols = [18 20 18];
rows = [28; 28]Dotting complex vectors uses conjugation on the first input
a = [1+2i, 3-4i];
b = [2-3i, -1+5i];
val = dot(a, b)Expected output:
val = -27 + 4iEvaluating dot on gpuArray inputs
G1 = gpuArray([1 2 3 4]);
G2 = gpuArray([4 3 2 1]);
G = dot(G1, G2);
result = gather(G)Expected output:
result = 20Using dot with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how dot changes the result.
Run a small dot example, explain the result, then change one input and compare the output.
FAQ
Does dot require vectors?⌄
No. Any pair of tensors with identical sizes works; specifying a dimension lets you dot slices of higher-dimensional arrays.
How does the optional dimension behave?⌄
dot(A, B, dim) collapses the dimth dimension (1-based). Dimensions greater than the array rank have length 1 and therefore leave the data unchanged.
What happens with complex numbers?⌄
The first input is conjugated before multiplication so the result matches MATLAB's hermitian inner product.
Are empty inputs supported?⌄
Yes. If the reduction dimension has length 0 the result is filled with zeros of the appropriate shape.
Will the result stay on the GPU?⌄
When a provider is active the runtime uploads real-valued results back to the device. Complex outputs stay on the host until GPU complex support is available.
What error is raised for size mismatches?⌄
When A and B differ in size dot raises: A and B must be the same size. matching MATLAB's wording.
Does dot accept logical or integer inputs?⌄
Only as explicit RunMat extensions. Current public MATLAB documentation lists single and double data inputs. RunMat mode accepts logical and all eight integer classes; compatibility modes reject those data forms. Typed-integer dim remains documented and available.
Can I request conjugation of the second argument instead?⌄
No. MATLAB's dot is fixed to conjugate the first argument. Use sum(A .* conj(B)) manually if you need the opposite orientation.
Related Linalg functions
Ops
cross · ctranspose · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how dot is executed, line by line, in Rust.
- View the source for dot 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.