Java Mailing List Archive

http://www.r-help.com/

Home » Home (12/2007) » R Help for Statistical Computing »

Re: [R] A function for raising a matrix to a power?

Paul Gilbert

2007-05-07

Replies:

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@(protected)
> 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}}

______________________________________________
R-help@(protected)
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.
©2008 r-help.com - Jax Systems, LLC, U.S.A.