https://github.com/cran/multivariance
Raw File
Tip revision: 4d37ecce6b3f6a5469d23eeba965d7de2f5ca70f authored by Björn Böttcher on 13 September 2018, 14:20:06 UTC
version 1.2.0
Tip revision: 4d37ecc
multivariance.cpp
#include <Rcpp.h>
using namespace Rcpp;

//' fast euclidean distance matrix computation
//'
//' @param x the vector for which the distanc matrix is computed
//' @examples
//' #require(microbenchmark)
//' #x = rnorm(100)
//' #microbenchmark(fastdist(as.matrix(x)),as.matrix(dist(x)))
//' @export
// [[Rcpp::export]]
NumericMatrix fastdist (const NumericMatrix & x){
  unsigned int outrows = x.nrow(), i = 0, j = 0;
  double d;
  Rcpp::NumericMatrix out(outrows,outrows);

  for (i = 0; i < outrows - 1; i++){
    Rcpp::NumericVector v1 = x.row(i);
    for (j = i + 1; j < outrows ; j ++){
      d = sqrt(sum(pow(v1-x.row(j), 2.0)));
      out(j,i)=d;
      out(i,j)=d;
    }
  }

  return out;
}

/*** R
*/
back to top