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

2007-05-09 Thread Atte Tenkanen
Hello, Thanks for many replys. I tested all the functions presented. I'm a beginner in linear algebra, but now I have a serious question ;-) Here is a matrix A, which determinant is 3, so it is nonsingular. Then there are similar computer runs done with each function proposed. I have

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

2007-05-09 Thread Ravi Varadhan
Atte, Your matrix A is not symmetric, that is why exponentiation using spectral decomposition, expM.sd, does not give you the correct answer. Convert A to a symmetric matrix: A - (A + t(A))/2 then the results will all match. Ravi.

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

2007-05-08 Thread Paul Gilbert
Ravi Varadhan wrote: Paul, Your solution based on SVD does not work Ooops. I really am getting rusty. The idea is based on eigenvalues which, of course, are not always the same as singular values. Paul even for the matrix in your example (the reason it worked for e=3, was that it is an

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

2007-05-07 Thread Michael Dewey
At 15:48 06/05/2007, Ron E. VanNimwegen 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 A3? Atte Tenkanen I may be revealing my ignorance here, but is MatrixExp in the msm package (available

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

2007-05-07 Thread Alberto Vieira Ferreira Monteiro
Michael Dewey wrote: I may be revealing my ignorance here, but is MatrixExp in the msm package (available from CRAN) not relevant here? Never heard about it. Searching with help.search(matrix) didn't show any function that might be used as Matrix^n. Alberto Monteiro

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

2007-05-07 Thread Prof Brian Ripley
On Mon, 7 May 2007, Alberto Vieira Ferreira Monteiro wrote: Michael Dewey wrote: I may be revealing my ignorance here, but is MatrixExp in the msm package (available from CRAN) not relevant here? Never heard about it. Searching with help.search(matrix) didn't show any function that might

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

2007-05-07 Thread Paul Gilbert
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

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

2007-05-07 Thread Ravi Varadhan
Paul, Your solution based on SVD does not work even for the matrix in your example (the reason it worked for e=3, was that it is an odd power and since P is a permutation matrix. It will also work for all odd powers, but not for even powers). However, a spectral decomposition will work for

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

2007-05-07 Thread Alberto Vieira Ferreira Monteiro
Paul Gilbert wrote: 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: (...) I think this also works for non-integer, negative, large, and complex This is diverging into mathematics, maybe

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

2007-05-06 Thread Atte Tenkanen
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,]11 [2,] -1 -2 A^3 [,1] [,2] [1,]11 [2,] -1 -8 But:

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

2007-05-06 Thread Ted Harding
[Apologies: This will probably break the thread, but at the moment I cannot receive mail since my remote mail-server is down, and so am responding to the message as found on the R-help archives; hence this is not a Reply] From: Atte Tenkanen attenka at utu.fi Sun May 6 11:07:07 CEST 2007 Is

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

2007-05-06 Thread Ron E. VanNimwegen
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 A3? Atte Tenkanen Hi Atte, I was looking for a similar operator, because R uses scalar products when raising a matrix to a power with ^. There

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

2007-05-06 Thread Christos Hatzis
Here is a recursive version of the same function: %^% - function(A, n) if(n == 1) A else A %*% (A %^% (n-1)) a - matrix(1:4, 2) a %^% 1 [,1] [,2] [1,]13 [2,]24 a %^% 2 [,1] [,2] [1,]7 15 [2,] 10 22 a %^% 3 [,1] [,2] [1,] 37 81 [2,] 54 118

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

2007-05-06 Thread Ken Knoblauch
See mtx.exp in the Malmig package which is more efficient than a simple recurvsive routine or alternatively, %^% in Lindsey's rmutil package HTH ken 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

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

2007-05-06 Thread Alberto Vieira Ferreira Monteiro
Ron E. VanNimwegen wrote: I was looking for a similar operator, because R uses scalar products when raising a matrix to a power with ^. There might be something more elegant, but this little loop function will do what you need for a matrix mat raised to a power pow: mp - function(mat,pow){