Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 79baa1c
Author: Manos Papadakis <[email protected]>
Date:   Tue Dec 5 13:29:46 2023 +0200

    Update nn.Rd

commit 6c1531c
Author: Manos Papadakis <[email protected]>
Date:   Tue Dec 5 13:29:43 2023 +0200

    Update system.Rd

commit ad2c5a7
Author: Manos Papadakis <[email protected]>
Date:   Tue Dec 5 13:29:41 2023 +0200

    Update .Rbuildignore

commit fc9f68f
Author: Manos Papadakis <[email protected]>
Date:   Mon Dec 4 17:20:24 2023 +0200

    Update nn.Rd

commit b686cf8
Author: Manos Papadakis <[email protected]>
Date:   Mon Dec 4 17:16:55 2023 +0200

    Update NEWS.md

commit 6eccaf4
Author: Manos Papadakis <[email protected]>
Date:   Mon Dec 4 17:16:52 2023 +0200

    Update DESCRIPTION

commit a6c50ed
Author: Manos Papadakis <[email protected]>
Date:   Mon Dec 4 17:07:11 2023 +0200

    Update NEWS.md

commit 0273aba
Author: Manos Papadakis <[email protected]>
Date:   Mon Dec 4 17:06:58 2023 +0200

    Update KDTreeArmadilloAdaptor.hpp

commit b46a7c6
Author: Manos Papadakis <[email protected]>
Date:   Mon Dec 4 17:04:58 2023 +0200

    Update helpers.h

commit 8af14f4
Author: Manos Papadakis <[email protected]>
Date:   Thu Nov 23 19:25:23 2023 +0200

    optimize log calculation

commit 226d257
Author: Manos Papadakis <[email protected]>
Date:   Thu Nov 23 19:06:20 2023 +0200

    Update .gitignore

commit f76c078
Author: Manos Papadakis <[email protected]>
Date:   Thu Nov 23 19:05:13 2023 +0200

    optimize jensen_shannon

commit aa1759e
Author: Manos Papadakis <[email protected]>
Date:   Thu Nov 16 13:28:36 2023 +0200

    Update nn.R

commit 470bfd5
Author: Manos Papadakis <[email protected]>
Date:   Wed Nov 15 16:41:38 2023 +0200

    Update RcppExports.cpp

commit 055399c
Author: Manos Papadakis <[email protected]>
Date:   Wed Nov 15 16:41:34 2023 +0200

    Update nn.R

commit 9c9542c
Author: Manos Papadakis <[email protected]>
Date:   Wed Nov 15 16:41:29 2023 +0200

    Update .gitignore

commit 65c7a2f
Author: Manos Papadakis <[email protected]>
Date:   Wed Nov 15 16:41:17 2023 +0200

    upload proj

commit 2049da8
Author: Manos Papadakis <[email protected]>
Date:   Wed Nov 15 15:01:24 2023 +0200

    add equations

commit b8b19fd
Author: Manos Papadakis <[email protected]>
Date:   Wed Nov 15 13:50:39 2023 +0200

    Update .gitignore

commit e352ba3
Author: Manos Papadakis <[email protected]>
Date:   Wed Nov 15 13:46:08 2023 +0200

    fix bugs

    This reverts commit 99a5c98.

commit d976a3e
Merge: f20c937 99a5c98
Author: Manos Papadakis <[email protected]>
Date:   Tue Nov 14 22:04:01 2023 +0200

    Merge branch 'main' into dev

commit f20c937
Author: Manos Papadakis <[email protected]>
Date:   Wed Nov 1 17:07:37 2023 +0200

    Create r.yml (#2)
  • Loading branch information
ManosPapadakis95 committed Dec 6, 2023
1 parent 3691013 commit 070c322
Show file tree
Hide file tree
Showing 12 changed files with 1,384 additions and 131 deletions.
8 changes: 6 additions & 2 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
^.*\.Rproj$
^\.Rproj\.user$
^.RData
^.Rhistory
^docs
^.todo
^.github
^.html
^.gitignore
^.vscode
11 changes: 7 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

*src/.dll
*src/.o
*.dll
*.swp
*.o
*.o.tmp
*\src-i386
*\src-x64
*.dll
*.html
*.RData
*.Rhistory
.Rproj.user
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: Rnanoflann
Type: Package
Title: Extremely Fast Nearest Neighbor Search
Version: 0.0.1
Date: 2023-10-30
Version: 0.0.2
Date: 2023-12-04
Authors@R: c(
person("Manos Papadakis", email = "[email protected]", role = c("aut", "cre", "cph")),
person("Jose Luis Blanco", email = "[email protected]" , role = c("aut","cph")),
Expand Down
19 changes: 19 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ Rnanoflann

</h1>

### **Version 0.0.2**

------------------------------------------------------------------------

> **New**
>
> | Function | What's new! |
> |----------|------------------------------------------|
> | nn | add new metrics with parallel version, "hellinger","canberra","kullback_leibler","jensen_shannon","itakura_saito","bhattacharyya","jeffries_matusita","minimum","maximum","total_variation","sorensen,"cosine","gower","minkowski","soergel","kulczynski","wave_hedges","motyka","harmonic_mean". |
> | nn | fix bug in metric "euclidean". |
>
> **LinkingTo**
>
> | Function | What's new! |
> |----------|------------------------------------------|
> | nn | add new metrics with parallel version, "hellinger","canberra","kullback_leibler","jensen_shannon","itakura_saito","bhattacharyya","jeffries_matusita","minimum","maximum","total_variation","sorensen,"cosine","gower","minkowski","soergel","kulczynski","wave_hedges","motyka","harmonic_mean". |
> | nn | fix bug in metric "euclidean". |

### **Version 0.0.1**

------------------------------------------------------------------------
Expand Down
11 changes: 7 additions & 4 deletions R/nn.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#[export]
nn <- function(data, points = data, k = nrow(data), method = "euclidean", search = "standard", eps = 0.0, square = FALSE,
sorted = FALSE, radius = 0.0, trans = TRUE, leafs = 10, parallel = FALSE, cores = 0){
res <- .Call(`_Rnanoflann_nn`, t(data), t(points), k, method, search, eps, square,
sorted, radius, leafs, parallel, cores)
nn <- function(data, points, k = nrow(data), method = "euclidean", search = "standard",
eps = 0.0, square = FALSE, sorted = FALSE, radius = 0.0, trans = TRUE, leafs = 10L, p = 0.0, parallel = FALSE, cores = 0L) {
if(method == "hellinger"){
data <- sqrt(data)
points <- sqrt(points)
}
res <- .Call(`_Rnanoflann_nn`, t(data), t(points), k, method, search, eps, square, sorted, radius, leafs, p, parallel, cores)
if(trans){
res$indices <- t(res$indices)
res$distances <- t(res$distances)
Expand Down
229 changes: 189 additions & 40 deletions inst/include/internal/KDTreeArmadilloAdaptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
using namespace arma;
using namespace nanoflann;


namespace Rnanoflann {
namespace Rnanoflann
{
// Define an Armadillo KDTreeAdaptor class
template <class MatrixType, class Distance, int DIM = -1>
struct KDTreeArmadilloAdaptor
{
using self_t = KDTreeArmadilloAdaptor<MatrixType, Distance, DIM>;
using num_t = typename MatrixType::elem_type;
using IndexType = uword;
using metric_t = typename Distance::template traits<num_t, self_t>::distance_t;// You can change the distance metric as needed.
using IndexType = uword;

using metric_t = typename Distance::template traits<num_t, self_t>::distance_t; // You can change the distance metric as needed.

using index_t = nanoflann::KDTreeSingleIndexAdaptor<metric_t, self_t, DIM, IndexType>;
index_t* index_;

index_t *index_;

const std::reference_wrapper<const MatrixType> m_data_matrix;
explicit KDTreeArmadilloAdaptor(const uword dimensionality, const std::reference_wrapper<const MatrixType>& mat,const uword leafs = 10)

explicit KDTreeArmadilloAdaptor(const uword dimensionality, const std::reference_wrapper<const MatrixType> &mat, const uword leafs = 10)
: m_data_matrix(mat)
{
const auto dims = mat.get().n_rows; // Assumes column-major Armadillo matrix
Expand All @@ -35,47 +35,51 @@ namespace Rnanoflann {
index_ = new index_t(dims, *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leafs));
index_->buildIndex();
}

~KDTreeArmadilloAdaptor() { delete index_; }
const self_t& derived() const { return *this; }
self_t& derived() { return *this; }

const self_t &derived() const { return *this; }
self_t &derived() { return *this; }

uword kdtree_get_point_count() const
{
return m_data_matrix.get().n_cols;
}

num_t kdtree_get_pt(uword idx, size_t dim) const
{
return m_data_matrix.get()(dim,idx);
return m_data_matrix.get()(dim, idx);
}

colvec col(uword idx) const
{
return m_data_matrix.get().col(idx);
}

template <class BBOX>
bool kdtree_get_bbox(BBOX& bb) const
bool kdtree_get_bbox(BBOX &bb) const
{
return false; // Optional bounding-box computation (not used in this example)
}
};


// Define an Armadillo KDTreeAdaptor class
template <class MatrixType, class Distance, bool Square = false, int DIM = -1>
template <class MatrixType, class Distance, bool Square, int DIM = -1>
struct KDTreeArmadilloAdaptor2
{
using self_t = KDTreeArmadilloAdaptor2<MatrixType, Distance, Square, DIM>;
using num_t = typename MatrixType::elem_type;
using IndexType = uword;
using metric_t = typename Distance::template traits<num_t, self_t, Square>::distance_t;// You can change the distance metric as needed.
using IndexType = uword;

using metric_t = typename Distance::template traits<num_t, self_t, Square>::distance_t; // You can change the distance metric as needed.

using index_t = nanoflann::KDTreeSingleIndexAdaptor<metric_t, self_t, DIM, IndexType>;
index_t* index_;

index_t *index_;

const std::reference_wrapper<const MatrixType> m_data_matrix;
explicit KDTreeArmadilloAdaptor2(const uword dimensionality, const std::reference_wrapper<const MatrixType>& mat,const uword leafs = 10)

explicit KDTreeArmadilloAdaptor2(const uword dimensionality, const std::reference_wrapper<const MatrixType> &mat, const uword leafs = 10)
: m_data_matrix(mat)
{
const auto dims = mat.get().n_rows; // Assumes column-major Armadillo matrix
Expand All @@ -86,24 +90,169 @@ namespace Rnanoflann {
index_ = new index_t(dims, *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leafs));
index_->buildIndex();
}

~KDTreeArmadilloAdaptor2() { delete index_; }
const self_t& derived() const { return *this; }
self_t& derived() { return *this; }

const self_t &derived() const { return *this; }
self_t &derived() { return *this; }

uword kdtree_get_point_count() const
{
return m_data_matrix.get().n_cols;
}


colvec col(uword idx) const
{
return m_data_matrix.get().col(idx);
}

num_t kdtree_get_pt(uword idx, size_t dim) const
{
return m_data_matrix.get()(dim,idx);
return m_data_matrix.get()(dim, idx);
}

template <class BBOX>
bool kdtree_get_bbox(BBOX &bb) const
{
return false; // Optional bounding-box computation (not used in this example)
}

};

template <class MatrixType, class Distance, int DIM = -1>
struct KDTreeArmadilloAdaptor3
{
using self_t = KDTreeArmadilloAdaptor3<MatrixType, Distance, DIM>;
using num_t = typename MatrixType::elem_type;
using IndexType = uword;

using metric_t = typename Distance::template traits<num_t, self_t>::distance_t; // You can change the distance metric as needed.

using index_t = nanoflann::KDTreeSingleIndexAdaptor<metric_t, self_t, DIM, IndexType>;

index_t *index_;

const double p;
const double p_1;

const std::reference_wrapper<const MatrixType> m_data_matrix;

explicit KDTreeArmadilloAdaptor3(const uword dimensionality, const std::reference_wrapper<const MatrixType> &mat, const double p, const uword leafs = 10)
: p(p), p_1(1.0 / p), m_data_matrix(mat)
{
const auto dims = mat.get().n_rows; // Assumes column-major Armadillo matrix
if (static_cast<uword>(dims) != dimensionality)
throw std::runtime_error("Error: 'dimensionality' must match the column count in the data matrix");
if (DIM > 0 && static_cast<int>(dims) != DIM)
throw std::runtime_error("Data set dimensionality does not match the 'DIM' template argument");
index_ = new index_t(dims, *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leafs));
index_->buildIndex();
}

~KDTreeArmadilloAdaptor3() { delete index_; }

const self_t &derived() const { return *this; }
self_t &derived() { return *this; }

uword kdtree_get_point_count() const
{
return m_data_matrix.get().n_cols;
}

num_t kdtree_get_pt(uword idx, size_t dim) const
{
return m_data_matrix.get()(dim, idx);
}

colvec col(uword idx) const
{
return m_data_matrix.get().col(idx);
}

const double getP() const
{
return p;
}

const double getP_1() const
{
return p_1;
}

template <class BBOX>
bool kdtree_get_bbox(BBOX &bb) const
{
return false; // Optional bounding-box computation (not used in this example)
}
};

// Define an Armadillo KDTreeAdaptor class
template <class MatrixType, class Distance, int DIM = -1>
struct KDTreeArmadilloAdaptor4
{
using self_t = KDTreeArmadilloAdaptor4<MatrixType, Distance, DIM>;
using num_t = typename MatrixType::elem_type;
using IndexType = uword;

using metric_t = typename Distance::template traits<num_t, self_t>::distance_t; // You can change the distance metric as needed.

using index_t = nanoflann::KDTreeSingleIndexAdaptor<metric_t, self_t, DIM, IndexType>;

index_t *index_;

const std::reference_wrapper<const MatrixType> m_data_matrix;
const MatrixType xlogx, ylogy;
const num_t *begin_points;

explicit KDTreeArmadilloAdaptor4(const uword dimensionality, const std::reference_wrapper<const MatrixType> &mat, const MatrixType &points, const uword leafs = 10)
: m_data_matrix(mat), xlogx(mat.get() % arma::log(mat.get())), ylogy(points % arma::log(points)), begin_points(points.memptr())
{
const auto dims = mat.get().n_rows; // Assumes column-major Armadillo matrix
if (static_cast<uword>(dims) != dimensionality)
throw std::runtime_error("Error: 'dimensionality' must match the column count in the data matrix");
if (DIM > 0 && static_cast<int>(dims) != DIM)
throw std::runtime_error("Data set dimensionality does not match the 'DIM' template argument");
index_ = new index_t(dims, *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leafs));
index_->buildIndex();
}

~KDTreeArmadilloAdaptor4() { delete index_; }

const self_t &derived() const { return *this; }
self_t &derived() { return *this; }

uword kdtree_get_point_count() const
{
return m_data_matrix.get().n_cols;
}

num_t kdtree_get_pt(uword idx, size_t dim) const
{
return m_data_matrix.get()(dim, idx);
}

colvec col(uword idx) const
{
return m_data_matrix.get().col(idx);
}

colvec col_xlogx(uword idx) const
{
return xlogx.col(idx);
}

colvec col_ylogy(const num_t *a) const
{
auto index = std::floor((a - begin_points) / ylogy.n_rows);
return ylogy.col(index);
}

double log0_5() const
{
return std::log(0.5);
}

template <class BBOX>
bool kdtree_get_bbox(BBOX& bb) const
bool kdtree_get_bbox(BBOX &bb) const
{
return false; // Optional bounding-box computation (not used in this example)
}
Expand Down
Loading

0 comments on commit 070c322

Please sign in to comment.