Skip to content

pairwise_distances

pairwise_distances(x: array, y: array) -> array

Compute pairwise distances between points in vectors x and y.

Parameters:

Name Type Description Default
x array

array of shape (N, D)

required
y array

array of shape (M, D)

required

Returns:

Type Description
array

Array of shape (N, M)

Source code in mlx_graphs/utils/array_ops.py
def pairwise_distances(x: mx.array, y: mx.array) -> mx.array:
    """
    Compute pairwise distances between points in vectors x and y.

    Args:
        x: array of shape (N, D)
        y: array of shape (M, D)

    Returns:
        Array of shape (N, M)
    """
    assert x.shape[1] == y.shape[1], "Input vectors must have the same dimensionality"
    # Use broadcasting to compute pairwise differences
    expanded_x = mx.expand_dims(x, 1)
    distances = mx.linalg.norm(expanded_x - y, axis=-1)
    return mx.array(distances.tolist())

from mlx_graphs.utils.array_ops import pairwise_distances · mlx_graphs/utils/array_ops.py