#include using namespace Rcpp; //' fast euclidean distance matrix computation //' //' @param x matrix with sample rows which the distanc matrix is computed (to use with vectors, use \code{as.matrix(x)}) //' @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; } //' double center a symmetric matrix //' //' @param x symmetric matrix //' @param normalize boolean. If \code{TRUE} the matrix will be normalized to mean 1. //' @keywords internal // [[Rcpp::export]] NumericMatrix doubleCenterSymMat(const NumericMatrix & x, bool & normalize) { int i, j; int N = x.nrow(); NumericVector colmeans(N); NumericMatrix out(N, N); double fullmean; double tmp; for (i=0; i