powered by
Compute 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^k
x %^% k
x %*% x %*% ... %*% x
a square matrix.
matrix
an integer, \(k \ge 0\).
A matrix of the same dimension as x.
x
Argument \(k\) is coerced to integer using as.integer.
as.integer
The algorithm uses \(O(log_2(k))\) matrix multiplications.
%*% for matrix multiplication.
%*%
# NOT RUN { A <- cbind(1, 2 * diag(3)[,-1]) A A %^% 2 stopifnot(identical(A, A %^% 1), A %^% 2 == A %*% A) # }
Run the code above in your browser using DataLab