1
#include <Rcpp.h>
2
// [[Rcpp::plugins(cpp11)]]
3
// [[Rcpp::depends(RcppEigen)]]
4

5
using namespace Rcpp;
6

7
#include <vector>
8
#include <RcppEigen.h>
9
#include <Rcpp.h>
10

11
// [[Rcpp::export]]
12 4
std::vector<double> rcpp_squared_distance(Rcpp::NumericMatrix x, Rcpp::NumericMatrix y, Rcpp::NumericVector y_weights) {
13
  //// Initialization
14 4
  Eigen::MatrixXd x_MTX(Rcpp::as<Eigen::MatrixXd>(x));
15 4
  Eigen::MatrixXd y_MTX(Rcpp::as<Eigen::MatrixXd>(y));
16
    
17
  //// Preliminary processing
18
  // declare variables
19 4
  std::size_t N_x_INT = x_MTX.rows();
20 4
  std::size_t N_y_INT = y_MTX.rows(); 
21 4
  Eigen::ArrayXd tmp_AXD;
22

23
  //// Main processing
24
  /// calculate wss
25 4
  std::vector<double> d(N_y_INT);
26
  double curr_lowest_DBL;
27 4
  for (std::size_t j=0; j < N_y_INT; ++j) {
28 4
    curr_lowest_DBL = std::numeric_limits<double>::max();
29 4
    for (std::size_t i=0; i < N_x_INT; ++i) {
30 4
      tmp_AXD = x_MTX.row(i) - y_MTX.row(j);
31 4
      curr_lowest_DBL = std::min(tmp_AXD.square().sum() * y_weights[i], curr_lowest_DBL);
32
    }
33 4
    d[j] = curr_lowest_DBL;
34
  }
35

36
  //// Exports
37
  // return distances
38 4
  return (d);
39
}
40

Read our documentation on viewing source code .

Loading