Clustering
C++20 header-only: DBSCAN, HDBSCAN, k-means.
Loading...
Searching...
No Matches
clustering::math::distance Namespace Reference

Namespaces

namespace  detail

Classes

struct  SqEuclideanTag
 Tag selecting the squared Euclidean metric. More...
struct  CosineTag
 Tag selecting the cosine distance metric (1 - cos(angle)). More...
struct  ManhattanTag
 Tag selecting the Manhattan (L1) metric: sum of absolute differences. More...

Functions

template<class T, Layout LA, Layout LB>
tag_invoke (const detail::PointwiseSqFn &, SqEuclideanTag, const NDArray< T, 1, LA > &a, const NDArray< T, 1, LB > &b) noexcept
 Squared Euclidean distance between two rank-1 NDArrays.
template<class T, Layout LA, Layout LB>
tag_invoke (const detail::PointwiseSqFn &, ManhattanTag, const NDArray< T, 1, LA > &a, const NDArray< T, 1, LB > &b) noexcept
 Manhattan (L1) distance between two rank-1 NDArrays.
template<class T, Layout LA, Layout LB>
tag_invoke (const detail::PointwiseSqFn &, CosineTag, const NDArray< T, 1, LA > &a, const NDArray< T, 1, LB > &b) noexcept
 Cosine distance between two rank-1 NDArrays.

Variables

constexpr detail::PointwiseSqFn pointwiseSq {}
 Customization-point object for pairwise squared distances between two rank-1 arrays.

Function Documentation

◆ tag_invoke() [1/3]

template<class T, Layout LA, Layout LB>
T clustering::math::distance::tag_invoke ( const detail::PointwiseSqFn & ,
CosineTag ,
const NDArray< T, 1, LA > & a,
const NDArray< T, 1, LB > & b )
noexcept

Cosine distance between two rank-1 NDArrays.

Default overload of pointwiseSq for CosineTag. Computes 1 - dot(a,b) / (||a||*||b||) with a single head-to-tail pass that accumulates the dot product and both squared norms; the combined sqrt on na*nb keeps the arithmetic to one square root per call. A zero-norm operand makes the cosine undefined: by convention this returns T{1} (maximum dissimilarity), picked so neighbor-search code paths see a large but finite distance instead of NaN.

Template Parameters
TElement type; float or double per the NDArray invariant.
LALayout tag of a.
LBLayout tag of b.
Parameters
aLeft-hand operand; must have the same length as b.
bRight-hand operand.
Returns
Cosine distance as a T in [0, 2], or T{1} when either operand has zero norm.

Definition at line 165 of file distance.h.

◆ tag_invoke() [2/3]

template<class T, Layout LA, Layout LB>
T clustering::math::distance::tag_invoke ( const detail::PointwiseSqFn & ,
ManhattanTag ,
const NDArray< T, 1, LA > & a,
const NDArray< T, 1, LB > & b )
noexcept

Manhattan (L1) distance between two rank-1 NDArrays.

Default overload of pointwiseSq for ManhattanTag. Sums |a(i) - b(i)| head-to-tail; the summation order is deterministic for equal inputs regardless of layout. A zero-length operand returns T{0} without indexing storage. The absolute value is selected via a ternary on the signed difference, which avoids the std::abs / abs overload-set ambiguity and stays noexcept for the T in {float, double} substrate.

Template Parameters
TElement type; float or double per the NDArray invariant.
LALayout tag of a.
LBLayout tag of b.
Parameters
aLeft-hand operand; must have the same length as b.
bRight-hand operand.
Returns
Sum of absolute differences as a T.

Definition at line 135 of file distance.h.

◆ tag_invoke() [3/3]

template<class T, Layout LA, Layout LB>
T clustering::math::distance::tag_invoke ( const detail::PointwiseSqFn & ,
SqEuclideanTag ,
const NDArray< T, 1, LA > & a,
const NDArray< T, 1, LB > & b )
noexcept

Squared Euclidean distance between two rank-1 NDArrays.

Default overload of pointwiseSq for SqEuclideanTag. Sums (a(i) - b(i))^2 from i = 0 head-to-tail; the summation order matches the scalar kd-tree path and is deterministic for equal inputs regardless of layout. A zero-length operand returns T{0} without indexing storage.

When CLUSTERING_USE_AVX2 is defined, T is float, n is at least 8, and both operands are 32-byte aligned, dispatches to detail::sqEuclideanAvx2F32. The 8-lane gate mirrors index/kdtree.h: below 8 dims the horizontal-sum epilogue is pure tax. The alignment check guards against unaligned Borrowed / strided views reaching _mm256_load_ps, which is undefined behavior on misaligned inputs.

Template Parameters
TElement type; float or double per the NDArray invariant.
LALayout tag of a.
LBLayout tag of b.
Parameters
aLeft-hand operand; must have the same length as b.
bRight-hand operand.
Returns
Sum of squared differences as a T.

Definition at line 92 of file distance.h.

Variable Documentation

◆ pointwiseSq

detail::PointwiseSqFn clustering::math::distance::pointwiseSq {}
inlineconstexpr

Customization-point object for pairwise squared distances between two rank-1 arrays.

pointwiseSq(tag, a, b) dispatches through an unqualified tag_invoke lookup, so the metric implementation travels with the tag's namespace. Users extend by defining a tag_invoke overload taking the CPO and their own tag; library-provided overloads live in clustering::math::distance and are found via ADL on the tag parameter.

The return type and noexcept qualification are inherited from the selected overload; the existing squared-Euclidean fast path in the kd-tree is noexcept, and the default overload below preserves that guarantee.

Definition at line 68 of file distance.h.