Matrix exponentiation in R
Last Updated :
06 May, 2024
Matrix exponentiation is raising a square matrix to a given exponent. Suppose there is a matrix A raised to the power k and the value of A raised to the power of k is calculated by repeatedly multiplying the matrix A k times. So matrix exponentiation of A to the power 5 is calculated by repeatedly multiplying matrix A to 5 times.
In R Programming Language the value of matrix A^k does not give the correct result because it calculates element-wise exponentiation which is not equal to matrix exponentiation. Let us see how matrix exponentiation is different than element-wise exponentiation.
Matrix Exponentiation vs Element-wise exponentiation
- Matrix Exponentiation: It is calculated by repeated multiplication of the matrices. Suppose we want to calculate A³ then it is calculated by multiplying the matrix 3 times.
- Element-Wise exponentiation: In R studio, this ‘ ^ ‘ operator performs element-wise exponentiation where each element in the matrix is individually exponentiated.
Powers of transition matrix in the Markov process
In a markov process , the system moves from one state to another state and to calculate the nth state of a system, we have a formula Sn = P^n * So where P is the transition matrix and So is the initial state of the system. Here we will calculate the power of the transition matrix P using matrix exponentiation in R.
Let us have a system with initial state So = [ [ 1 ] , [ 0 ] ] and transition matrix, P = [ [ 0.2, 0.6 ] , [0.8 ,0.4 ] ]
Now let us calculate the 4th state of the above system using the formula Sn = P^n * So. Therefore, S4 = P⁴ *. Now let us calculate the value of P⁴ repeated multiplication.
R
#defining the transition matrix
P <- matrix(c(2,4,8,6),byrow=TRUE,nrow=2)
P
#defining the initial state of the system
S0 <- matrix(c(1,0),byrow=TRUE,nrow=2)
S0
#calculating power of transition matrix using repeated multiplication
Sn <- (P %*% P %*% P %*% P) %*% S0
print(Sn)
Output:
[,1] [,2]
[1,] 2 4
[2,] 8 6
[,1]
[1,] 1
[2,] 0
[,1]
[1,] 3344
[2,] 6656
In the above code, there is a transition matrix and initial state matrix. It is calculating the 4th state of the system by calculating P⁴ using repeated multiplication.
P⁴ = P %*% P %*% P %*%P ==> This expression is used to calculate the powers of transition matrix in the above code
Here %*% operator is performing matrix multiplication with P till 4 times and then the S0 is performing matrix multiplication with this result which is giving us the final result.
Matrix exponentiation in R can be done using the below approaches
- Repeated Matrix multiplication
- Using expm library
Using repeated Matrix multiplication
Suppose we need to calculate A³ so this can be done by multiplying A three times.
To perform matrix multiplication in R studio , we need to use ‘ %*% ‘ operator. Therefore, A³ = A %*% A%*% A. Use the below code to calculate A³ using repeated matrix multiplication in R studio.
R
A<-matrix(c(1,2,3,4),byrow=TRUE,nrow=2)
print(A*A)
Output:
[,1] [,2]
[1,] 1 4
[2,] 9 16
Using expm library
The expm library in R provides %^% operator to calculate the power of a matrix without repeated multiplication. We must import it before using it in our code. This expm library also provides sqrtm() and logm() functions.
- sqrtm( ) – This function calculates the square root of a diagonal matrix.
- logm( ) – This function calculates the logarithm of a diagonal matrix.
Let us calculate A³ using %^% operator
R
#importing the expm library
library(expm)
A<-matrix(c(1,2,3,4),byrow=TRUE,nrow=2)
print(A %^% 3)
Output:
[,1] [,2]
[1,] 1 2
[2,] 3 4
[,1] [,2]
[1,] 37 54
[2,] 81 118
Share your thoughts in the comments
Please Login to comment...