【发布时间】:2016-03-22 16:48:11
【问题描述】:
我正在尝试在 RStudio 中编写以下 C++ 代码。
// [[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 [默认启用]'
有人有建议吗?
提前感谢您的帮助。
【问题讨论】: