如何使用 Rcpp 初始化 mappsparsematrix
how to initialize mappedsparsematrix using Rcpp
我想在 R 中通过 Rcpp 使用 mappsparsematrix 类型创建一个稀疏矩阵。我选择 mappsparsematrix 而不是 sparsematrix 是因为我想在 R 中使用它进行进一步计算。如果我在这一点上有误,请纠正我。
这是我的 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);
}
但是,当我尝试在 R 中获取 cpp 文件时,它 returns 这个错误
no matching constructor for initialization of "MSpMat"
谁能帮忙解决一下?
好的,完整的错误是:
temp_eigen.cpp:51:10: error: no matching constructor for initialization of '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
^
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)
从根本上说,这里的错误是尝试使用需要预先存在的内存位置的数据类型并将其定义为好像不需要(例如两步加载)。
变化中:
MSpMat D(nrows, ncols);
至:
typedef Eigen::SparseMatrix< double > SpMat;
SpMat D(nrows, ncols);
产生了想要的结果。
另一种方法是提供以下形式:
MSpMat(Index rows, Index cols, Index nnz,
Index* outerIndexPtr, Index* innerIndexPtr,
Scalar* valuePtr)
我想在 R 中通过 Rcpp 使用 mappsparsematrix 类型创建一个稀疏矩阵。我选择 mappsparsematrix 而不是 sparsematrix 是因为我想在 R 中使用它进行进一步计算。如果我在这一点上有误,请纠正我。
这是我的 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);
}
但是,当我尝试在 R 中获取 cpp 文件时,它 returns 这个错误
no matching constructor for initialization of "MSpMat"
谁能帮忙解决一下?
好的,完整的错误是:
temp_eigen.cpp:51:10: error: no matching constructor for initialization of '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 ^
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)
从根本上说,这里的错误是尝试使用需要预先存在的内存位置的数据类型并将其定义为好像不需要(例如两步加载)。
变化中:
MSpMat D(nrows, ncols);
至:
typedef Eigen::SparseMatrix< double > SpMat;
SpMat D(nrows, ncols);
产生了想要的结果。
另一种方法是提供以下形式:
MSpMat(Index rows, Index cols, Index nnz,
Index* outerIndexPtr, Index* innerIndexPtr,
Scalar* valuePtr)