如何使用 RcppEigen 获取矩阵的行列式

how to get the determinant of a matrix using RcppEigen

我是 Rcpp 的新手。我正在尝试使用 R 包 RcppEigen 来获取矩阵的行列式。以下代码保存在一个文件中,我使用sourceCpp来使用它。我用sourceCpp的时候没有编译错误。当在 R 中使用 getDeterminant(A) 时,A 是一个矩阵。它总是抱怨以下错误。

"Error: could not find function "getDeterminant""

但是,getEigenValues 效果很好。

如果有人愿意帮助我,我将不胜感激。 非常感谢!

#include <RcppEigen.h>

// [[Rcpp::depends(RcppEigen)]]

 using Eigen::Map;                 // 'maps' rather than copies 
 using Eigen::MatrixXd;                  // variable size matrix, double  precision
 using Eigen::VectorXd;                  // variable size vector, double precision
 using Eigen::SelfAdjointEigenSolver;    // one of the eigenvalue solvers
 using Eigen::MatrixXi;
 using Eigen::MatrixBase;
 // [[Rcpp::export]]
 VectorXd getEigenValues(Map<MatrixXd> M) {
     SelfAdjointEigenSolver<MatrixXd> es(M);
     return es.eigenvalues();
 }

// [[Rcpp:export]]
double getDeterminant(Map<MatrixXd> AA){
     return  AA.determinant();
}

您在第二个 Rcpp 属性标记中缺少 :Rcpp::export 是正则表达式查找的形式。

如果添加它,功能将变得可访问:

R> Rcpp::sourceCpp("/tmp/crystal.cpp")
R> M <- matrix(1:9,3,3)*1.0
R> getEigenValues(M)
[1] 2.80689e-16 6.99265e-01 1.43007e+01
R> getDeterminant(M)
[1] 0
R>