# Compute the power of a matrix in R

(1) I am looking for a package for computing the power of a matrix. If you have some good recommendation please let me know.

(2) I searched on the internet and followed what some said to install a package called "Malmig" in R but after selecting the mirror site, it failed:

In install.packages("Malmig") : package ‘Malmig’ is not available

Some idea why?

Thanks!

• It sounds like the Malmig package is not available on official CRAN package releases, so you're better looking for a source version (that has to be compiled) or another mirror. – chl Nov 8 '10 at 16:53
• Malmig is on the CRAN archive: cran.r-project.org/src/contrib/Archive/Malmig. You would need to compile it yourself in order to install it. – Rob Hyndman Nov 8 '10 at 22:30

Package expm provides the matrix %^% number operator notation for its function matpow():

> library(expm)
> mat <- matrix(1:9, nrow=3)
> mat %^% 2
[,1] [,2] [,3]
[1,]   30   66  102
[2,]   36   81  126
[3,]   42   96  150

# check
> mat %*% mat
[,1] [,2] [,3]
[1,]   30   66  102
[2,]   36   81  126
[3,]   42   96  150


There's also sqrtm() for taking roots and expm() for matrix exponential.

There is the following code you can write:

library(Biodem) png(filename="images/mtx.exp_%03d.png" ,width=480, height=480)

### ** Examples

test<-matrix(c(1:16), 4,4) pow.test<-mtx.exp(test,10) pow.test

for more details:http://rgm2.lab.nig.ac.jp/RGM2/R_man-2.9.0/library/Biodem/man/mtx.exp.html

Or... you could use my own package, 'powerplus' (function 'Matpow'), that allows you to raise any diagonalizable matrix to any power (even complex). For non-diagonalizable matrices, you have the same capabilities as package expm (incidentally, I use it in Matpow's code). To the best of my knowledge, it currently is the most comprehensive R package that exists to deal with matrix exponentiation. Version 3.0 extends capabilities to (some) non-diagonalizable matrices too.

• Please register &/or merge your accounts (you can find information on how to do this in the My Account section of our help center), then you will be able to edit your own answer. – gung - Reinstate Monica Aug 21 '16 at 20:18

You can also use the matrixcalc package, which also provides other matrix operations:

> require(matrixcalc)
> mat <- matrix(1:9, nrow=3)
> matrix.power(mat,2)
[,1] [,2] [,3]
[1,]   30   66  102
[2,]   36   81  126
[3,]   42   96  150
> mat%*%mat
[,1] [,2] [,3]
[1,]   30   66  102
[2,]   36   81  126
[3,]   42   96  150