NumericMatrix returns a matrix of all zeros?
I'm trying to implement a simple code in Rcpp that calculates and
populates the entries of a distance matrix. The problem is that the Rcpp
code (below) returns a matrix D with all elements having a value of zero.
This issue does not seem to be addressed anywhere in the forums - I'd
appreciate some advice!
src_d_err_c <- '
using namespace Rcpp;
double d_err_c(NumericVector cx, NumericVector csx, NumericVector cy,
NumericVector csy) {
using namespace Rcpp;
NumericVector d = (cx - cy)*(cx - cy) / csx;
double s = std::accumulate(d.begin(), d.end(), 0.0);
return s;
}'
src_d_mat = '
using namespace Rcpp;
// input
Rcpp::NumericMatrix cX(X);
Rcpp::NumericMatrix cY(Y);
Rcpp::NumericMatrix cSX(SX);
Rcpp::NumericMatrix cSY(SY);
int N1 = cX.nrow();
int N2 = cY.nrow();
NumericMatrix D(N1, N2);
NumericVector v(N1);
for (int x = 0; x++; x<N1){
v[x] = x;
for (int y = 0; y++; y<N2) {
D(x,y) = d_err_c(cX(x,_), cSX(x,_), cY(y,_), cSY(y,_));
};
};
return wrap(v);'
fun <- cxxfunction(signature(X = "numeric", SX = "numeric",
Y = "numeric", SY = "numeric"),
body = src_d_mat, includes = src_d_err_c,
plugin = "Rcpp")
No comments:
Post a Comment