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

From: Ron E. VanNimwegen <vanron_at_ksu.edu>
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
}
return(ans)
}

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

Cheers,
Ron

---
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
vanron_at_ksu.edu
---

______________________________________________
R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.