[R] A function for raising a matrix to a power?
Paul Gilbert
pgilbert at bank-banque-canada.ca
Mon May 7 23:15:45 CEST 2007
You might try this, from 9/22/2006 with subject line Exponentiate a matrix:
I am getting a bit rusty on some of these things, but I seem to recall
that there is a numerical advantage (speed and/or accuracy?) to
diagonalizing:
> expM <- function(X,e) { v <- La.svd(X); v$u %*% diag(v$d^e) %*% v$vt }
> P <- matrix(c(.3,.7, .7, .3), ncol=2)
> P %*% P %*% P
[,1] [,2]
[1,] 0.468 0.532
[2,] 0.532 0.468
> expM(P,3)
[,1] [,2]
[1,] 0.468 0.532
[2,] 0.532 0.468
I think this also works for non-integer, negative, large, and complex
exponents:
> expM(P, 1.5)
[,1] [,2]
[1,] 0.3735089 0.6264911
[2,] 0.6264911 0.3735089
> expM(P, 1000)
[,1] [,2]
[1,] 0.5 0.5
[2,] 0.5 0.5
> expM(P, -3)
[,1] [,2]
[1,] -7.3125 8.3125
[2,] 8.3125 -7.3125
> expM(P, 3+.5i)
[,1] [,2]
[1,] 0.4713+0.0141531i 0.5287-0.0141531i
[2,] 0.5287-0.0141531i 0.4713+0.0141531i
>
Paul Gilbert
Doran, Harold wrote:
> Suppose I have a square matrix P
>
> P <- matrix(c(.3,.7, .7, .3), ncol=2)
>
> I know that
>
>
>> P * P
>
> Returns the element by element product, whereas
>
>
>
>> P%*%P
>>
>
> Returns the matrix product.
>
> Now, P2 also returns the element by element product. But, is there a
> slick way to write
>
> P %*% P %*% P
>
> Obviously, P3 does not return the result I expect.
>
> Thanks,
> Harold
>
Atte Tenkanen wrote:
> Hi,
>
> Is there a function for raising a matrix to a power? For example if you like to compute A%*%A%*%A, is there an abbreviation similar to A^3?
>
> Atte Tenkanen
>
>> A=rbind(c(1,1),c(-1,-2))
>> A
> [,1] [,2]
> [1,] 1 1
> [2,] -1 -2
>> A^3
> [,1] [,2]
> [1,] 1 1
> [2,] -1 -8
>
> But:
>
>> A%*%A%*%A
> [,1] [,2]
> [1,] 1 2
> [2,] -2 -5
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
====================================================================================
La version française suit le texte anglais.
------------------------------------------------------------------------------------
This email may contain privileged and/or confidential inform...{{dropped}}
More information about the R-help
mailing list