简体   繁体   English

如何使用Rcpp初始化mappedsparsematrix

[英]how to initialize mappedsparsematrix using Rcpp

I would like to create a sparse matrix using mappedsparsematrix type via Rcpp in R. I choose mappedsparsematrix rather than sparsematrix because I want to use it in R for further computation. 我想通过R中的Rcpp使用mapsparsematrix类型创建一个稀疏矩阵。我选择了Mapsparsematrix而不是sparsematrix,因为我想在R中使用它进行进一步的计算。 Correct me if I am wrong on this point. 如果我在这一点上错了,请指正。

Here is my code in cpp form 这是我的cpp形式的代码

// [[Rcpp::depends(RcppEigen)]]
# include <RcppEigen.h>
# include <Rcpp.h>
# include <math.h>
# include <list>

using namespace Rcpp;
using Eigen::SparseMatrix;
using Eigen::MappedSparseMatrix;
using Eigen::MatrixXd;
using Eigen::VectorXi;
typedef Eigen::VectorXd Vd;
typedef Eigen::VectorXi Vi;
typedef Eigen::MappedSparseMatrix<double> MSpMat;
typedef MSpMat::InnerIterator InIterMat;
typedef List list;
typedef Eigen::Triplet<double> T;


double euclidean_distance(double lon1, double lat1,double lon2,double lat2){
  double s = pow((lon1 - lon2),2) + pow((lat1 - lat2),2);
    double ed = sqrt(s);
    return(ed);
}


// [[Rcpp::export]]
MSpMat mymain(NumericVector lat, NumericVector lon){
   std::list<T> L;

   int length = lat.size();
   int index = 0;
   for (int i = 0; i < length - 1; i++ ){
       for (int j = i+1; j < length; j++){
         double lon1 = lon[i];
         double lon2 = lon[j];
         double lat1 = lat[i];
         double lat2 = lat[j];
         double dist = euclidean_distance(lon1,lat1,lon2,lat2);
         dist = exp(-dist/0.001);
         if (dist > 0.01 ){
            L.push_back(T(index, i, dist));
            L.push_back(T(index, j, -dist)); 
         }

      }
   }
   int nrows = L.size()/2;
   int ncols = length;

   MSpMat D(nrows, ncols);
   D.setFromTriplets(L.begin(), L.end());
   return(D);
}

However, it returns this error when I try to source the cpp file in R. 但是,当我尝试在R中获取cpp文件时,它将返回此错误。

no matching constructor for initialization of "MSpMat"

Can anyone help to fix it? 任何人都可以帮助修复它吗?

Okay, the full error is: 好的,完整的错误是:

temp_eigen.cpp:51:10: error: no matching constructor for initialization of 'MSpMat' (aka 'MappedSparseMatrix') MSpMat D(nrows, ncols); temp_eigen.cpp:51:10:错误:没有匹配的构造函数用于初始化'MSpMat'(aka'MappedSparseMatrix')MSpMat D(nrows,ncols); ^ ~~~~~~~~~~~~ ^ ~~~~~~~~~~~~

note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided class MappedSparseMatrix ^ 注意:候选构造函数(隐式副本构造函数)不可行:需要1个参数,但是提供了2个类MappedSparseMatrix ^

note: candidate constructor not viable: requires 6 arguments, but 2 were provided inline MappedSparseMatrix(Index rows, Index cols, Index nnz, Index* outerIndexPtr, Index* innerIndexPtr, Scalar* valuePtr) 注意:候选构造函数不可行:需要6个参数,但是提供了2个内联MappedSparseMatrix(索引行,索引列,索引nnz,索引* externalIndexPtr,索引* innerIndexPtr,标量* valuePtr)

Fundamentally, the error here is trying to use a data type that requires a pre-existing memory location and defining it as if it does not (eg a two step load). 从根本上讲,这里的错误是尝试使用需要预先存在的内存位置的数据类型,并将其定义为不需要(例如,两步加载)。

Changing: 更改:

MSpMat D(nrows, ncols);

to: 至:

typedef Eigen::SparseMatrix< double > SpMat;
SpMat D(nrows, ncols);

yields the desired results. 产生期望的结果。

The alternative is to provide a form of: 另一种方法是提供以下形式:

MSpMat(Index rows, Index cols, Index nnz, 
       Index* outerIndexPtr, Index* innerIndexPtr,
       Scalar* valuePtr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM