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();
}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With