![]() |
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
Powers of transition matrix in the Markov processIn 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.
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
Using repeated Matrix multiplicationSuppose 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.
Output: [,1] [,2]
[1,] 1 4
[2,] 9 16 Using expm libraryThe 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.
Let us calculate A³ using %^% operator
Output: [,1] [,2]
[1,] 1 2
[2,] 3 4
[,1] [,2]
[1,] 37 54
[2,] 81 118 |
Reffered: https://www.geeksforgeeks.org
R Programs |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |