diff --git a/inst/include/internal/dists.hpp b/inst/include/internal/dists.hpp index 2a22993..18a845d 100644 --- a/inst/include/internal/dists.hpp +++ b/inst/include/internal/dists.hpp @@ -77,9 +77,9 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - DistanceType result = Dist::euclidean(x, y); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); + DistanceType result = Dist::euclidean(y, x); return Square ? result : std::sqrt(result); } @@ -117,9 +117,9 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - DistanceType result = Dist::manhattan(x, y); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); + DistanceType result = Dist::manhattan(y, x); return result; } @@ -157,9 +157,9 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - DistanceType result = Dist::euclidean(x, y); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); + DistanceType result = Dist::euclidean(y, x); return Square ? result * 0.5 : std::sqrt(result) * (1.0 / std::sqrt(2.0)); } @@ -239,8 +239,8 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); Col result = abs(x - y); return result[result.index_max()]; } @@ -279,8 +279,8 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); Col result = abs(x - y); return result[result.index_min()]; } @@ -319,9 +319,9 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - return Dist::manhattan(x, y) * 0.5; + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); + return Dist::manhattan(y, x) * 0.5; } template @@ -358,9 +358,9 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - return dot(x, y) / (sqrt(sum(square(x))) * sqrt(sum(square(y)))); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); + return dot(y, x) / (sqrt(sum(square(x))) * sqrt(sum(square(y)))); } template @@ -397,9 +397,9 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - return Dist::manhattan(x, y) * (1.0 / size); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); + return Dist::manhattan(y, x) * (1.0 / size); } template @@ -436,9 +436,9 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - return sum(abs(x - y) / (x + y)); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); + return sum(abs(x - y) / (y + x)); } template @@ -475,8 +475,8 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); return sum(abs(x - y) / (abs(x) + abs(y))); } @@ -514,15 +514,11 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - // Col x(const_cast(a), size, false); - // const Col y = data_source.col(b_idx); - // return sum((x - y) % (log(x) - log(y))); - DistanceType res = DistanceType(); for (size_t i = 0; i < size; ++i) { - DistanceType x = a[i], y = data_source.kdtree_get_pt(b_idx, i); - DistanceType v = (x - y) * (std::log(x) - std::log(y)); + DistanceType y = a[i], x = data_source.kdtree_get_pt(b_idx, i); + DistanceType v = (y - x) * (std::log(y) - std::log(x)); if (std::isfinite(v)) { res += v; @@ -568,8 +564,8 @@ namespace Rnanoflann DistanceType res = DistanceType(); for (size_t i = 0; i < size; ++i) { - DistanceType x = a[i], y = data_source.kdtree_get_pt(b_idx, i); - DistanceType v = (x + y) * (std::log(2) - std::log(x + y)) + x * std::log(x) + y * std::log(y); + DistanceType y = a[i], x = data_source.kdtree_get_pt(b_idx, i); + DistanceType v = (y + x) * (std::log(2) - std::log(y + x)) + y * std::log(y) + x * std::log(x); if (v > 0 and !R_IsNA(v)) { res += v; @@ -612,27 +608,18 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - // DistanceType res = DistanceType(); - // for (size_t i = 0; i < size; ++i) - // { - // DistanceType x = a[i], y = data_source.kdtree_get_pt(b_idx, i); - // DistanceType v = (x / y) - (std::log(x / y)) - 1.0; - // // if (std::isfinite(v)) - // // { - // res += v; - // // } - // } - - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - Col log_x(size,fill::none); - Col log_y(size,fill::none); + DistanceType res = DistanceType(); for (size_t i = 0; i < size; ++i) { - log_x[i] = std::log(x[i]); - log_y[i] = std::log(y[i]); + DistanceType y = a[i], x = data_source.kdtree_get_pt(b_idx, i); + DistanceType v = (x / y) - (std::log(x) - std::log(y)) - 1.0; // Not symmetric so x/y is not y/x + if (std::isfinite(v)) + { + res += v; + } } - return sum((x / y) - (log_x - log_y) - 1.0); + + return res; } template @@ -669,9 +656,9 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - return -log(Coeff::bhattacharyya(x, y)); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); + return -log(Coeff::bhattacharyya(y, x)); } template @@ -708,9 +695,9 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - return std::sqrt(2.0 - 2.0 * Coeff::bhattacharyya(x, y)); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); + return std::sqrt(2.0 - 2.0 * Coeff::bhattacharyya(y, x)); } template @@ -747,9 +734,9 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - return Dist::manhattan(x, y) / sum_with>(x, y); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); + return Dist::manhattan(y, x) / sum_with>(y, x); } template @@ -786,9 +773,9 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - return Dist::manhattan(x, y) / sum_with>(x, y); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); + return Dist::manhattan(y, x) / sum_with>(y, x); } template @@ -825,9 +812,9 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - return sum(abs(x - y) / elems(x, y)); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); + return sum(abs(y - x) / elems(y, x)); } template @@ -864,9 +851,9 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - return 1.0 - sum_with>(x, y) / sum(x + y); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); + return 1.0 - sum_with>(y, x) / sum(y + x); } template @@ -903,9 +890,9 @@ namespace Rnanoflann DistanceType evalMetric( const T *a, const IndexType b_idx, size_t size) const { - Col x(const_cast(a), size, false); - const Col y = data_source.col(b_idx); - return 2.0 * dot(x, y) / sum(x + y); + Col y(const_cast(a), size, false); + const Col x = data_source.col(b_idx); + return 2.0 * dot(y, x) / sum(y + x); } template