calc_eigen: Calculate the eigen decomposition of a square, symmetric...

View source: R/RcppExports.R

calc_eigenR Documentation

Calculate the eigen decomposition of a square, symmetric matrix using RcppArmadillo.

Description

Calculate the eigen decomposition of a square, symmetric matrix using RcppArmadillo.

Usage

calc_eigen(matrixv, eigenval, eigenvec)

Arguments

matrixv

A square, symmetric matrix.

eigenval

A vector of eigen values.

eigenvec

A matrix of eigen vectors.

Details

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.

Value

Void (no return value - passes the eigen values and eigen vectors by reference).

Examples

## 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)


algoquant/HighFreq documentation built on Feb. 9, 2024, 8:15 p.m.