1

I am currently encountering problems regarding the computation of eigenvalues with Rcpp. When using Rcpp's eig_sym I don't get the same result as with R's eigen, although it is stated at the web page of Armadillo that it ought to deliver the same outcome (e.g. http://gallery.rcpp.org/articles/armadillo-eigenvalues/).

I'll use the following Rcpp function (the same as provided online):

#include <RcppArmadillo.h>

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

// [[Rcpp::export]]
arma::vec getEigenValues(arma::mat M) {
  return arma::eig_sym(M);
}

and after using the following lines of codes:

 library(Rcpp)
 library(RcppArmadillo)
 MM <- matrix(c(0.5055860,0.2093442,-0.1061261,
                -0.3170091,0.5472850,-0.4170188,
                0.29660273,-0.02383499,0.80188728),3,3)

 sourceCpp("./getEigenValues.cpp")

 getEigenValues(MM)
 eigen(MM)$values

I get:

> getEigenValues(MM)
          [,1]
[1,] 0.1410249
[2,] 0.6472190
[3,] 1.0665144
> eigen(MM)$values
[1] 0.6986485+0.2855979i 0.6986485-0.2855979i
[3] 0.4574612+0.0000000i

Where does this incongruence come from? The imaginary part is also completely missing in the Rcpp command, that wouldn't disturb me since I am only interest in the Real part. I hope someone can enlighten me!

Best regards!

Louki
  • 215
  • 1
  • 12

1 Answers1

4

Your matrix is not symmetric, so eig_sym is not correct. You can use the following C++ code with eig_gen.

#include <RcppArmadillo.h>

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

// [[Rcpp::export]]
arma::cx_vec getEigenValues(arma::mat M) {
  return arma::eig_gen(M);
}
J_F
  • 9,956
  • 2
  • 31
  • 55