fn

matmul

Tensor
matmul(input: Tensor, other: Tensor | Scalar)
source

Matrix product with full broadcasting semantics.

Implements general matrix multiplication following the standard broadcasting rules:

  • If both inputs are 1-D, the result is the scalar dot product.
  • If both inputs are 2-D, the result is the standard matrix product.
  • If either input is 1-D it is temporarily promoted to 2-D by prepending / appending a length-1 axis; the inserted axis is removed from the output.
  • If either input has more than two dimensions, the leading batch dimensions are broadcast against each other and matmul is performed on the trailing two axes.

Parameters

inputTensor
Left operand. Shape (..., n, k) or (k,).
otherTensor or scalar
Right operand. Shape (..., k, m) or (k,). Must be contraction-compatible with input.

Returns

Tensor

Matrix product with shape broadcast(batch_input, batch_other) + (n, m) after removing any axes inserted during 1-D promotion.

Notes

Mathematical definition for 2-D operands of shapes (n, k) and (k, m):

Cij=r=1kAirBrj\mathbf{C}_{ij} = \sum_{r=1}^{k} \mathbf{A}_{ir}\,\mathbf{B}_{rj}

Autograd: L/A=(L/C)B\partial L / \partial A = (\partial L / \partial C) B^{\top}, L/B=A(L/C)\partial L / \partial B = A^{\top} (\partial L / \partial C), with batch dimensions handled via broadcasting and reduction.

Examples

>>> import lucid
>>> a = lucid.tensor([[1.0, 2.0], [3.0, 4.0]])
>>> b = lucid.tensor([[5.0, 6.0], [7.0, 8.0]])
>>> lucid.matmul(a, b)
Tensor([[19., 22.], [43., 50.]])