Skip to content
fn

pdist

→Tensor
pdist(input: Tensor, p: float = 2.0)

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

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

out[k(i,j)]=(∑m=1M∣xi,m−xj,m∣p)1/pfor 0≤i<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

inputTensor
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(N−1)/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. For p=2 that is lucid.cdist's default compute_mode: pair-by-pair differences up to 25 rows, the matrix-product expansion past that. Both keep a distance whose square leaves the float range (rows near 1e-20 or 1e25), as lucid.cdist documents.

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., 1., 1.414])

Used by 1