Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the determinant of a matrix using RcppEigen

Tags:

c++

r

matrix

rcpp

I am brand new to Rcpp. I am trying to using the R package RcppEigen to get the determinant of a matrix. The following code is saved in a file and I use sourceCpp to use it. There is no compilation error when I use sourceCpp. When using getDeterminant(A) in R, A is a matrix. It always complains the following error.

"Error: could not find function "getDeterminant""

However, the getEigenValues works well.

I appreciate a lot if anybody is happy to help me with this. Thanks a lot!

#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();
}
like image 842
Crystal Avatar asked Sep 28 '22 20:09

Crystal


1 Answers

You are missing a : in the second Rcpp Attributes tag: Rcpp::export is the form the regular expression looks for.

If you add it, the functions becomes accessible:

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> 
like image 119
Dirk Eddelbuettel Avatar answered Oct 05 '22 07:10

Dirk Eddelbuettel