Re: [R] A function for raising a matrix to a power?

From: Ron E. VanNimwegen <>
Date: Sun, 06 May 2007 09:48:09 -0500

> Hi,
> Is there a function for raising a matrix to a power? For example if
> you like to compute A%*%A%*%A, is there an abbreviation similar to A3?
> Atte Tenkanen
Hi Atte,

I was looking for a similar operator, because R uses scalar products when raising a matrix to a power with "^". There might be something more elegant, but this little loop function will do what you need for a matrix "mat" raised to a power "pow":

mp <- function(mat,pow){
ans <- mat
for ( i in 1:(pow-1)){
ans <- mat%*%ans

Then, for your example:
> > A=rbind(c(1,1),c(-1,-2))
> > mp(A,3)
> [,1] [,2]
> [1,] 1 2
> [2,] -2 -5


Ron E. VanNimwegen
Ph.D. Candidate, Division of Biology (EEB)
Kansas Cooperative Fish & Wildlife Research Unit
205 Leasure Hall
Kansas State University
Manhattan, KS 66506-3501

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Sun 06 May 2007 - 14:54:38 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 07 May 2007 - 13:31:07 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.