Trying to compute the power of a matrix in R, I found that package expm
implements the operator %^%.
So x %^% k computes the k-th power of a matrix.
> A<-matrix(c(1,3,0,2,8,4,1,1,1),nrow=3) > A %^% 5 [,1] [,2] [,3] [1,] 6469 18038 2929 [2,] 21837 60902 9889 [3,] 10440 29116 4729
but, to my surprise:
> A [,1] [,2] [,3] [1,] 691 1926 312 [2,] 2331 6502 1056 [3,] 1116 3108 505
somehow the initial matrix A has changed to A %^% 4 !!!
How do you perform the matrix power operation?
Matrix is an arrangement of numbers into rows and columns. Different ways of finding the power of matrix in R programming: By using %^%. By using a power function.
How to find the power of a matrix? To find the power of a matrix, multiply the matrix by itself as many times as the exponent indicates. Therefore, to calculate the power of a matrix, you must first know how to multiply matrices. Otherwise you will not be able to calculate the power of a matrix.
The power of a matrix for a nonnegative integer is defined as the matrix product of copies of , A matrix to the zeroth power is defined to be the identity matrix of the same dimensions, . The matrix inverse is commonly denoted , which should not be interpreted to mean .
It is important to recognize that the power of a matrix is only well defined if the matrix is a square matrix. Furthermore, if 𝐴 is of order 𝑛 × 𝑛 , then this will be the case for 𝐴 , 𝐴 , and so on. Higher powers of a matrix can be calculated with reference to the lower powers of a matrix.
I have fixed that bug in the R-forge sources (of "expm" package), svn rev. 53. --> expm R-forge page For some reason the web page still shows rev.52, so the following may not yet solve your problem (but should within 24 hours):
install.packages("expm", repos="http://R-Forge.R-project.org")
Otherwise, get the svn version directly, and install yourself:
svn checkout svn://svn.r-forge.r-project.org/svnroot/expm
Thanks to "gd047" who alerted me to the problem by e-mail. Note that R-forge also has its own bug tracking facilities.
Martint
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With