matpow.RdCompute the \(k\)-th power of a matrix. Whereas x^k computes
element wise powers, x %^% k corresponds to \(k -
1\) matrix multiplications, x %*% x %*% ... %*% x.
x %^% ka square matrix, numeric or complex.
an integer, \(k \ge 0\).
Argument \(k\) is coerced to integer using as.integer.
The algorithm uses \(O(log_2(k))\) matrix multiplications.
A matrix of the same dimension as x.
If you think you need x^k for \(k < 0\), then consider
instead solve(x %^% (-k)).
%*% for matrix multiplication.
A <- cbind(1, 2 * diag(3)[,-1])
A
#> [,1] [,2] [,3]
#> [1,] 1 0 0
#> [2,] 1 2 0
#> [3,] 1 0 2
A %^% 2
#> [,1] [,2] [,3]
#> [1,] 1 0 0
#> [2,] 3 4 0
#> [3,] 3 0 4
stopifnot(identical(A, A %^% 1),
A %^% 2 == A %*% A)
## also for complex number matrix Z :
Z <- A + 2i*A
Z %^% 2
#> [,1] [,2] [,3]
#> [1,] -3+ 4i 0+ 0i 0+ 0i
#> [2,] -9+12i -12+16i 0+ 0i
#> [3,] -9+12i 0+ 0i -12+16i
stopifnot(identical(Z, Z %^% 1),
Z %^% 2 == Z %*% Z)