我正在尝试用C++编写下面的RStudio代码。
// [[Rcpp::depends(RcppEigen)]]
#include <RcppEigen.h>
using namespace Rcpp;
// [[Rcpp::export]]
#include <iostream>
#include <cmath>
using Eigen::Dense;
using Eigen::SparseLU;
using Eigen::Sparse;
using Eigen::SparseMatrix;
using namespace std;
Eigen::SparseMatrix<double> getGalMat(int N) {
// Note: N hast to bigger or equal to 5!!!
assert(N >= 5);
typedef Eigen::SparseMatrix<double>::Index index_t;
typedef Eigen::Triplet<double> triplet_t;
std::vector<triplet_t> triplets;
// reserve "minimal" vector size (the number of non-zero entries)
triplets.reserve(5*N - 6);
// N minus 1
int Nm = N - 1;
// set the (off-) diagonals
double diag_m2 = + 16;
double diag_m1 = - 64;
double diag = + 96;
double diag_p1 = - 64;
double diag_p2 = + 16;
// set first and last 2 rows by hand
// A(1 ,:)
triplets.push_back({0 ,0 ,diag }); // A(1,1)
triplets.push_back({0 ,1 ,diag_p1}); // A(1,2)
triplets.push_back({0 ,2 ,diag_p2}); // A(1,3)
// A(2 ,:)
triplets.push_back({1 ,0 ,diag_m1}); // A(2,1)
triplets.push_back({1 ,1 ,diag }); // A(2,2)
triplets.push_back({1 ,2 ,diag_p1}); // A(2,3)
triplets.push_back({1 ,3 ,diag_p2}); // A(2,4)
// A(N-1,:)
triplets.push_back({Nm-1,Nm-3,diag_m2}); // A(N-1,N-3)
triplets.push_back({Nm-1,Nm-2,diag_m1}); // A(N-1,N-2)
triplets.push_back({Nm-1,Nm-1,diag }); // A(N-1,N-1)
triplets.push_back({Nm-1,Nm ,diag_p1}); // A(N-1,N )
// A(N ,:)
triplets.push_back({Nm ,Nm-2,diag_m2}); // A(N,N-2)
triplets.push_back({Nm ,Nm-1,diag_m1}); // A(N,N-1)
triplets.push_back({Nm ,Nm ,diag }); // A(N,N )
// loop over remaining rows
for (int i = 2; i < Nm-1; i++) {
triplets.push_back({i,i-2,diag_m2}); // A(i,i-2)
triplets.push_back({i,i-1,diag_m1}); // A(i,i-1)
triplets.push_back({i,i ,diag }); // A(i,i )
triplets.push_back({i,i+1,diag_p1}); // A(i,i+1)
triplets.push_back({i,i+2,diag_p2}); // A(i,i+2)
}
// let EIGEN build the sparse matrix from our triplets
Eigen::SparseMatrix<double> spMat(N,N);
spMat.setFromTriplets(triplets.begin(), triplets.end());
// return
return spMat;
}在运行它时,我从第41行单词中有一个很长的错误列表,对应于:
triplets.push_back({0 ,0 ,diag }); // A(1,1)获取错误:“扩展初始化程序列表仅在默认启用-std=c++0x或-std=gnu++0x时可用”
有人有什么建议吗?
提前谢谢你的帮助。
发布于 2016-03-27 17:09:39
由于您似乎无法启用C++11,另一种方法是使代码C++98兼容。而不是:
triplets.push_back({0 ,0 ,diag }); // A(1,1)显式使用构造函数:
triplets.push_back(triplet_t(0 ,0 ,diag )); // A(1,1)发布于 2016-03-22 17:18:32
该脚本使用的语法要求比c++98更高的C++标准。要解决这个问题,有两种方法可以解决这个问题。
.Rprofile中设置。这将设置在~/.R/Makevars中常见的编译标志。
Sys.setenv(PKG_CXXFLAGS=“-std=c++11”)https://stackoverflow.com/questions/36160423
复制相似问题