fn

pdist

Tensor
pdist(x: Tensor, p: float = 2.0)
source

Pairwise LpL_p distances between rows of a 2-D tensor.

For an input of shape (N, M), returns the N(N1)/2N \cdot (N - 1) / 2 strictly-upper-triangular pairwise distances in row-major order:

out[k(i,j)]=(m=1Mxi,mxj,mp)1/pfor 0i<j<N\mathrm{out}[k(i, j)] = \left( \sum_{m=1}^{M} | x_{i, m} - x_{j, m} |^p \right)^{1/p} \quad \text{for } 0 \le i < j < N

where k(i, j) enumerates pairs in row-major order.

Parameters

xTensor
2-D input of shape (N, M).
pfloat= 2.0
Exponent of the LpL_p norm. Common values: 2 for Euclidean (default), 1 for Manhattan, float("inf") for Chebyshev.

Returns

Tensor

1-D tensor of length N(N1)/2N (N - 1) / 2 (empty when N<2N < 2).

Notes

Internally computed by extracting the strict upper triangle of the full lucid.cdist matrix, which keeps the implementation simple and fully differentiable but is O(N2)O(N^2) in both compute and intermediate memory — for very large N, prefer a blocked / streaming pairwise routine.

Examples

>>> import lucid
>>> from lucid.nn.functional import pdist
>>> x = lucid.tensor([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]])
>>> pdist(x, p=2)
Tensor([1.0000, 1.0000, 1.4142])