calc_invref: Calculate the inverse of a square _matrix_ in place, without...

View source: R/RcppExports.R

calc_invrefR Documentation

Calculate the inverse of a square matrix in place, without copying the data in memory.

Description

Calculate the inverse of a square matrix in place, without copying the data in memory.

Usage

calc_invref(matrixv)

Arguments

matrixv

A matrix of data to be inverted. (The argument is interpreted as a pointer to a matrix, and it is overwritten with the inverse matrix.)

Details

The function calc_invref() calculates the inverse of a square matrix in place, without copying the data in memory. It accepts a pointer to the argument matrixv (which is the matrix to be inverted), and it overwrites the old matrix values with the inverse matrix values. It performs the calculation in place, without copying the data in memory, which can significantly increase the computation speed for large matrices.

The function calc_invref() doesn't return a value. The function calc_invref() calls the C++ Armadillo function arma::inv() to calculate the matrix inverse.

Value

No return value.

Examples

## Not run: 
# Calculate a random matrix
matrixv <- matrix(rnorm(100), nc=10)
# Copy matrixv to a matrix in a different memory location
invmat <- matrixv + 0
# Calculate the inverse in place using RcppArmadillo
HighFreq::calc_invref(invmat)
# Multiply the matrix times its inverse
multmat <- matrixv %*% invmat
round(multmat, 4)
# Calculate the sum of the off-diagonal elements
sum(multmat[upper.tri(multmat)])
# Compare RcppArmadillo with R
all.equal(invmat, solve(matrixv))
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
   rcode=solve(matrixv),
   cppcode=calc_invref(matrixv),
   times=10))[, c(1, 4, 5)]

## End(Not run)


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