calc_eigenp: Calculate the partial eigen decomposition of a dense...

View source: R/RcppExports.R

calc_eigenpR Documentation

Calculate the partial eigen decomposition of a dense symmetric matrix using RcppArmadillo.

Description

Calculate the partial eigen decomposition of a dense symmetric matrix using RcppArmadillo.

Usage

calc_eigenp(matrixv, neigen)

Arguments

matrixv

A square matrix.

neigen

An integer equal to the number of eigenvalues to be calculated.

Details

The function calc_eigenp() calculates the partial eigen decomposition (the lowest order principal components, with the largest eigenvalues) of a dense matrix using RcppArmadillo. It calls the internal Armadillo eigen solver SymEigsSolver in the namespace arma::newarp to calculate the partial eigen decomposition.

The eigen solver SymEigsSolver uses the Implicitly Restarted Lanczos Method (IRLM) which was adapted from the ARPACK library. The eigen solver SymEigsSolver was implemented by Yixuan Qiu.

The function arma::eigs_sym() also calculates the partial eigen decomposition using the eigen solver SymEigsSolver, but it only works for sparse matrices which are not standard R matrices.

For matrices smaller than 100 rows, the function calc_eigenp() is slower than the function calc_eigen() which calculates the full eigen decomposition. But it's faster for very large matrices.

Value

A list with two elements: a vector of eigenvalues (named "values"), and a matrix of eigenvectors (named "vectors").

Examples

## Not run: 
# Create random positive semi-definite matrix
matrixv <- matrix(runif(100), nc=10)
matrixv <- t(matrixv) %*% matrixv
# Calculate the partial eigen decomposition
neigen <- 5
eigenp <- HighFreq::calc_eigenp(matrixv, neigen)
# Calculate the eigen decomposition using RcppArmadillo
eigenval <- numeric(10) # Allocate eigen values
eigenvec <- matrix(numeric(100), nc=10) # Allocate eigen vectors
HighFreq::calc_eigen(matrixv, eigenval, eigenvec)
# Compare the eigen decompositions
all.equal(eigenp$values[1:neigen], eigenval[1:neigen])
all.equal(abs(eigenp$vectors), abs(eigenvec[, 1:neigen]))
# Compare the speed of partial versus full decomposition
summary(microbenchmark(
  partial=HighFreq::calc_eigenp(matrixv, neigen),
  full=HighFreq::calc_eigen(matrixv, eigenval, eigenvec),
  times=10))[, c(1, 4, 5)]  # end microbenchmark summary

## End(Not run)


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