matpow | R Documentation |
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 |
a square |
k |
an integer, |
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))
.
Based on an R-help posting of Vicente Canto Casasola, and Vincent Goulet's C implementation in actuar.
%*%
for matrix multiplication.
A <- cbind(1, 2 * diag(3)[,-1])
A
A %^% 2
stopifnot(identical(A, A %^% 1),
A %^% 2 == A %*% A)
## also for complex number matrix Z :
Z <- A + 2i*A
Z %^% 2
stopifnot(identical(Z, Z %^% 1),
Z %^% 2 == Z %*% Z)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.