我正在开发一个需要一些非常快速的矩阵乘法的包,所以希望使用RcppEigen. 出于各种原因,尽管与多维数组的需要有关,但我需要将类 Eigen::MatrixXd 的创建对象转换为类 Rcpp::NumericMatrix。
我尝试反转 RcppEigen::FastLm.cpp 中列出的步骤,但这似乎不起作用
例如而不是使用
const Map<MatrixXd> X(as<Map<MatrixXd> >(Xs));
我试过
Rcpp:NumericMatrix X(as<Rcpp::NumericMatrix>(Xs));
其中 Xs 是 Eigen::MatrixXd 类的矩阵,但这似乎不起作用:“错误:没有匹配函数调用 'as' return Rcpp::asRcpp::NumericMatrix(z);”
如果这根本不可能,我可以尝试另一个方向。
基本上我需要在 R 语言中做的是
a = matrix(1, nrow = 10, ncol = 10)
b = array(0, c(10,10,10))
b[,,1] = a
Run Code Online (Sandbox Code Playgroud)
给出一个更清晰的起始示例
我将如何将 MatrixXd 类的对象存储在 NumericMatrix 类的对象中?
a = matrix(1, nrow = 10, ncol = 10)
b = array(0, c(10,10,10))
b[,,1] = a
Run Code Online (Sandbox Code Playgroud)
感谢发布代码!它使一切变得更容易。我只是重新排列了你最微小的代码。
关键的变化是“显式地”通过RcppEigen辅助函数从特征表示返回到 a SEXP,然后实例化矩阵。有时......编译器需要一点点推动。
#include <Rcpp.h>
#include <RcppEigen.h>
// [[Rcpp::depends(RcppEigen)]]
// [[Rcpp::export]]
Rcpp::NumericMatrix sample_problem() {
Eigen::MatrixXd x(2, 2), y(2, 2);
x << 1,1,2,2;
y << 3,3,4,4;
// do some eigen matrix multiplication
Eigen::MatrixXd z = x * y;
// what I'd like to be able to do somehow:
// store the results of the eigen object z in
// a NumericMatrix w
// w = z;
SEXP s = Rcpp::wrap(z);
Rcpp::NumericMatrix w(s);
return w;
}
/*** R
sample_problem()
*/
Run Code Online (Sandbox Code Playgroud)
R> sourceCpp("demo.cpp)
R> sample_problem()
[,1] [,2]
[1,] 7 7
[2,] 14 14
R>
Run Code Online (Sandbox Code Playgroud)
随着g++-9我需要-Wno-ignored-attributes或我得到屏幕和警告屏幕......