Skip to content
fn

vecdot

→Tensor
vecdot(x: Tensor, y: Tensor, dim: int = -1)

Compute a batched vector dot product along an axis.

Reduces the chosen axis with a sum of element-wise products, the first operand conjugated:

(x⋅y)… = ∑kx…,k,…‾ y…,k,….(x \cdot y)_{\ldots} \,=\, \sum_{k} \overline{x_{\ldots, k, \ldots}}\, y_{\ldots, k, \ldots}.

Parameters

First operand.
Second operand, broadcast-compatible with x.
dim(int, keyword - only)= -1
Axis to contract. Default -1.

Returns

Tensor

Reduced tensor with dim removed.

Raises

DtypeMismatch
If x and y have different dtypes -- a 0-d operand beside a vector and a float16 beside a float32 included. The reference does not promote the pair (a RuntimeError and a TypeError).

Notes

Equivalent to (x.conj() * y).sum(dim=dim) — for real inputs simply (x * y).sum(dim=dim). Useful for computing many independent dot products in one shot (e.g., per-row inner products of two matrices).

The conjugate makes vecdot(x, x) the squared norm ∑k∣xk∣2\sum_k |x_k|^2 of a complex vector. Without it, the complex result was the bilinear ∑kxkyk\sum_k x_k y_k instead.

Examples

>>> import lucid
>>> from lucid.linalg import vecdot
>>> x = lucid.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
>>> y = lucid.tensor([[1.0, 0.0, -1.0], [0.0, 1.0, 0.0]])
>>> vecdot(x, y)
tensor([-2., 5.])
>>> vecdot(lucid.tensor([1 + 2j, 3 - 1j]), lucid.tensor([2 - 1j, 1 + 1j]))
tensor((2.-1.j), dtype=lucid.complex64)

Used by 1