10#ifdef CLUSTERING_USE_AVX2
11#include "clustering/math/detail/distance_avx2.h"
44 template <
class Tag,
class A,
class B>
45 constexpr auto operator()(Tag tag, A &&a, B &&b)
const
46 noexcept(
noexcept(
tag_invoke(std::declval<const PointwiseSqFn &>(), tag, std::forward<A>(a),
48 ->
decltype(
tag_invoke(std::declval<const PointwiseSqFn &>(), tag, std::forward<A>(a),
49 std::forward<B>(b))) {
50 return tag_invoke(*
this, tag, std::forward<A>(a), std::forward<B>(b));
91template <
class T, Layout LA, Layout LB>
94 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>,
95 "pointwiseSq: integer element types not supported");
96 const std::size_t n = a.dim(0);
97#ifdef CLUSTERING_USE_AVX2
98 if constexpr (std::is_same_v<T, float>) {
103 if (n >= 8 && a.isContiguous() && b.isContiguous() && a.template isAligned<32>() &&
104 b.template isAligned<32>()) {
105 return detail::sqEuclideanAvx2F32(a.template alignedData<32>(), b.template alignedData<32>(),
111 for (std::size_t i = 0; i < n; ++i) {
112 const T diff = a(i) - b(i);
134template <
class T, Layout LA, Layout LB>
137 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>,
138 "pointwiseSq: integer element types not supported");
139 const std::size_t n = a.dim(0);
141 for (std::size_t i = 0; i < n; ++i) {
142 const T diff = a(i) - b(i);
143 sum += diff < T{0} ? -diff : diff;
164template <
class T, Layout LA, Layout LB>
167 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>,
168 "pointwiseSq: integer element types not supported");
169 const std::size_t n = a.dim(0);
173 for (std::size_t i = 0; i < n; ++i) {
180 if (na == T{0} || nb == T{0}) {
183 return T{1} - (dot / std::sqrt(na * nb));
Represents a multidimensional array (NDArray) of a fixed number of dimensions N and element type T.
T 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.
constexpr detail::PointwiseSqFn pointwiseSq
Customization-point object for pairwise squared distances between two rank-1 arrays.
T sum(const NDArray< T, 1, L > &x) noexcept
Naive single-pass sum of a rank-1 array.
Tag selecting the cosine distance metric (1 - cos(angle)).
Tag selecting the Manhattan (L1) metric: sum of absolute differences.
Tag selecting the squared Euclidean metric.