c++ - 为什么我的 Rcpp 代码会给出意外的所有整数值输出?

标签 c++ r matrix distance rcpp

给定一个数据矩阵 X,我想计算 X 的任意两行之间的成对距离矩阵。我有以下代码,它来自稍微调整代码 here .

#include <Rcpp.h>
#include <cmath>
#include <algorithm>

using namespace Rcpp;
// generic function for l1_distance
template <typename InputIterator1, typename InputIterator2>
inline double l1_distance(InputIterator1 begin1, InputIterator1 end1, 
                            InputIterator2 begin2) {
   double rval = 0;
   InputIterator1 it1 = begin1;
   InputIterator2 it2 = begin2;
   while (it1 != end1) {
      double d1 = *it1++;
      double d2 = *it2++;
      rval += abs(d1 - d2);
   }
   return rval;  
}

// [[Rcpp::export]]
NumericMatrix rcpp_l1_distance(NumericMatrix mat) {

   // allocate the matrix we will return
   NumericMatrix rmat(mat.nrow(), mat.nrow());
   for (int i = 0; i < rmat.nrow(); i++) {
      for (int j = 0; j < i; j++) {
         NumericMatrix::Row row1 = mat.row(i);
         NumericMatrix::Row row2 = mat.row(j);
         double d = l1_distance(row1.begin(), row1.end(), row2.begin());
         rmat(i,j) = d;
         rmat(j,i) = d;
      }
   }
   return rmat;
}

问题在于此代码返回所有整数值的矩阵。整数值似乎与我想要的距离值正相关,这使它更加困惑。我还计算了一个成对的 l2 距离矩阵和成对的归一化 l1 距离(将两行之间的 l1 距离除以它们的 l1 范数之和)矩阵,它们的行为都符合预期。

谁能告诉我我哪里错了?

您可以执行以下操作以获得奇怪的结果

library(Rcpp)
sourceCpp("distance.cpp") #the file containing the cpp code above
X = matrix(rnorm(16), 4, 4)
rcpp_l1_distance(X)

提前致谢!

最佳答案

编译你的代码给我这些警告:

> Rcpp::sourceCpp('Desktop/mat.cpp')
mat.cpp:16:15: warning: using integer absolute value function 'abs' when argument is of floating point type [-Wabsolute-value]
      rval += abs(d1 - d2);
              ^
mat.cpp:16:15: note: use function 'std::abs' instead
      rval += abs(d1 - d2);
              ^~~
              std::abs
mat.cpp:16:15: warning: using integer absolute value function 'abs' when argument is of floating point type [-Wabsolute-value]
      rval += abs(d1 - d2);
              ^
mat.cpp:30:18: note: in instantiation of function template specialization 'l1_distance<Rcpp::MatrixRow<14>::iterator, Rcpp::MatrixRow<14>::iterator>' requested here
      double d = l1_distance(row1.begin(), row1.end(), row2.begin());
                 ^
mat.cpp:16:15: note: use function 'std::abs' instead
      rval += abs(d1 - d2);
              ^~~
              std::abs
2 warnings generated.

... 暗示 abs 是整数,参见 this help page , 你可以使用 fabs相反,或者 std::abs ,或者你可以使用 sugar 的 operator-abssum:

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
NumericMatrix rcpp_l1_distance(NumericMatrix mat) {

  // allocate the matrix we will return
  NumericMatrix rmat(mat.nrow(), mat.nrow());
  for (int i = 0; i < rmat.nrow(); i++) {
    NumericMatrix::Row row1 = mat.row(i);

    for (int j = 0; j < i; j++) {
      rmat(j,i) = rmat(i,j) = sum( abs(row1 - mat.row(j) )) ;
    }
  }
  return rmat;
}

关于c++ - 为什么我的 Rcpp 代码会给出意外的所有整数值输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43601844/

相关文章:

python - 使用 R/Python 从 Web 下载数据的非 Selenium 方式

r - 巨大表的 dist(as.matrix)

matlab - 在矩阵 MATLAB 中查找数组

c++ STL设置差异

c++ - 你能通过保证多个线程不会访问同一个内存来避免锁定吗?

c++ - 测量忽略处理器速度的快速代码的性能/吞吐量?

R doParallel foreach 为独立 worker 提供错误处理

algorithm - MPI:混淆算法实现

python - sklearn.decomposition.TruncatedSVD 可以部分应用于矩阵吗?

c++ - 未定义的类,无法从 main 到达我的 header