Matrix Multiply
Shippingtensor_matmulnumpy @ matmul: 1-D/2-D/N-D with batch broadcasting; result unit = product of operand units
Signature
Inputs
aSignal|Arrayrequired— Left operand — Vector (1-D), Matrix (2-D) or higher-rank Tensor.bSignal|Arrayrequired— Right operand — inner dimensions must be compatible with A.
Outputs
resultSignal|Array— The matrix product, with unit equal to A's unit times B's unit.
Description
Matrix Multiply implements the full numpy matmul (@) contract on a and b, dispatching to a native MKL/BLAS backend. It is the workhorse for composing linear maps — rotating coordinate frames, applying a filter matrix to a state vector, propagating a covariance, or batching many small products at once.
Semantics follow numpy exactly, so rank drives behavior:
- 1-D · 1-D contracts to a scalar (the inner/dot product).
- 2-D · 1-D and 1-D · 2-D temporarily promote the vector to a matrix, multiply, then strip the promoted axis — so the result is a vector, not a spurious matrix.
- N-D · N-D treats the last two axes as the matrices and broadcasts the leading batch dimensions. This is genuine batched matmul, not
np.dot's outer-product stacking.
Because every output element is a sum of products, the result carries the product of the operand units: multiplying a matrix by a vector yields . Matmul is stateless — the output depends only on the current inputs.
Mathematics
Examples
Composing two 2×2 maps
With and , the result is . This is the canonical 'chain two linear transforms' operation — wire the output into a third matmul to compose a whole pipeline.
Applying a matrix to a vector
Feed a rotation matrix into a and a length-3 position vector into b. Because the 1-D operand is auto-promoted and then stripped, result is a length-3 vector — the rotated point — rather than a matrix.
Applications
- Applying rotation, projection, or change-of-basis matrices to coordinate vectors in robotics and graphics.
- State-space propagation: computing $x_{k+1}=A x_k$ or output $y=C x$ in control and estimation loops.
- Batched small-matrix products over a leading time or trial axis via broadcasting.
- Composing chained linear filters or mixing matrices into a single equivalent operator.
Neat
It faithfully reproduces numpy's rank-dependent quirks — including auto-promoting a 1-D operand and then stripping the promoted axis — so graphs behave identically to reference numpy code.
Leading dimensions broadcast like `matmul`, not `dot`: an $(N,3,3)$ times $(3,)$ input yields $N$ independent matrix-vector products without an explicit loop.
Unit propagation is automatic — the composed unit is the product of the two operand units, so a physically meaningful matmul keeps dimensional bookkeeping honest.
Known issues
Inner-dimension mismatches surface as a backend error rather than a silent broadcast; the last-two-axes contract must be compatible.
Like numpy, 1-D·1-D collapses to a scalar rather than a length-1 array — downstream nodes expecting an array shape must account for this.