calc_eigen | R Documentation |
RcppArmadillo
.Calculate the eigen decomposition of a square, symmetric matrix using
RcppArmadillo
.
calc_eigen(matrixv, eigenval, eigenvec)
matrixv |
A square, symmetric matrix. |
eigenval |
A vector of eigen values. |
eigenvec |
A matrix of eigen vectors. |
The function calc_eigen()
calculates the eigen decomposition of a
square, symmetric matrix using RcppArmadillo
. It calls the
Armadillo
function arma::eig_sym()
to calculate the eigen
decomposition.
For small matrices, the function calc_eigen()
is several times
faster than the R
function eigen()
, since
calc_eigen()
has no overhead in R
code. But for large
matrices, they are about the same, since both call C++
code.
Void (no return value - passes the eigen values and eigen vectors by reference).
## Not run:
# Create random positive semi-definite matrix
matrixv <- matrix(runif(25), nc=5)
matrixv <- t(matrixv) %*% matrixv
# Calculate the eigen decomposition using RcppArmadillo
eigenval <- numeric(5) # Allocate eigen values
eigenvec <- matrix(numeric(25), nc=5) # Allocate eigen vectors
HighFreq::calc_eigen(matrixv, eigenval, eigenvec)
# Calculate the eigen decomposition using R
eigenr <- eigen(matrixv)
# Compare the eigen decompositions
all.equal(eigenr$values, drop(eigenval))
all.equal(abs(eigenr$vectors), abs(eigenvec))
# Compare the speed of Rcpp with R code
summary(microbenchmark(
Rcpp=HighFreq::calc_eigen(matrixv, eigenval, eigenvec),
Rcode=eigen(matrixv),
times=10))[, c(1, 4, 5)] # end microbenchmark summary
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.