3 // [[Rcpp::depends(BH, bigmemory)]]
4 #include <bigmemory/MatrixAccessor.hpp>
11 //' computeMedoidsIndices
13 //' For each column of the 'series' matrix input, search for the closest medoid
14 //' (euclidian distance) and store its index
16 //' @param pMedoids External pointer (a big.matrix 'address' slot in R)
17 //' @param series (reference) series, a matrix of size Lxn
19 //' @return An integer vector of the closest medoids indices, for each (column) serie
21 IntegerVector
computeMedoidsIndices(SEXP pMedoids
, NumericMatrix series
)
23 // Turn SEXP external pointer into BigMatrix (description) object
24 XPtr
<BigMatrix
> pMed(pMedoids
);
25 // medoids: access to the content of the BigMatrix object
26 MatrixAccessor
<double> medoids
= MatrixAccessor
<double>(*pMed
);
28 int nb_series
= series
.ncol(),
31 IntegerVector
mi(nb_series
);
33 for (int i
=0; i
<nb_series
; i
++) //column index in series
35 // In R: mi[i] <- which.min( rowSums( sweep(medoids, 2, series[i,], '-')^2 ) )
36 // In C(++), computations must be unrolled
38 double best_dist
= DBL_MAX
;
39 for (int j
=0; j
<K
; j
++) //column index in medoids
42 for (int k
=0; k
<L
; k
++) //common row index (medoids, series)
44 // Accessing values for a big matrix is a bit weird; see Rcpp gallery/bigmemory
45 double delta
= series(k
,i
) - *(medoids
[j
]+k
);
46 dist_ij
+= delta
* delta
;
48 if (dist_ij
< best_dist
)
50 mi
[i
] = j
+1; //R indices start at 1