Reputation: 19
I am trying to achieve A^k in R with further assumption that k is not an integer (and possibly less than 1).
It seems like %^%
command does not do the right job. Namely -- A %^% (1.3) == A %^% 1
while using this command.
Upvotes: 1
Views: 2811
Reputation: 331
In case it is still relevant to you, and as already pointed out, my own package called 'powerplus' has a function called 'Matpow' that enables you to raise any diagonalizable matrix to any power (even complex powers after the recent update). Edit: Version 3.0 extends capabilities to (some) non-diagonalizable matrices too.
Upvotes: 1
Reputation: 1
This morning I had the same problem (unfortunately logm only works for matrices for which its log exists... not my case apparently) and after some research I found package matlib (function mpower) and package powerplus (function Matpow). Both accept non-integer powers but matlib has the restriction that the input matrix must be symmetric. So I ended up using Matpow from package powerplus and it did the trick. Hope it helps!
Upvotes: 0
Reputation: 226182
Presumably you're talking about the %^%
operator from the expm
package:
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’.
Note the definition of k
:
k: an integer, k >= 0.
I believe that if you want fractional powers you can do something like:
z <- matrix(c(3,1,1,3),2,2)
expm(1.3*logm(z))
## Note ...
## [,1] [,2]
## [1,] 4.262578 1.800289
## [2,] 1.800289 4.262578
I think this may only work for positive-definite matrices, though.
Upvotes: 2