calc_invref | R Documentation |
Calculate the inverse of a square matrix in place, without copying the data in memory.
calc_invref(matrixv)
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.) |
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.
No return value.
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.