Re: [R] Exponentiate a matrix

2006-09-22 Thread Ingmar Visser
Out of curiosity and possibly for later use: is there an R-function that
does matrix logarithms?
Best, Ingmar

On 9/21/06 8:58 PM, Dimitrios Rizopoulos
[EMAIL PROTECTED] wrote:

 Quoting Douglas Bates [EMAIL PROTECTED]:
 
 On 9/21/06, Dimitrios Rizopoulos [EMAIL PROTECTED] wrote:
 
 Quoting Duncan Murdoch [EMAIL PROTECTED]:
 
 On 9/21/2006 10:40 AM, 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, P^2 also returns the element by element product. But, is there a
 slick way to write
 
 P %*% P %*% P
 
 Obviously, P^3 does not return the result I expect.
 
 
 I don't think there's anything built in, but it's easy to write your own:
 
 I think there was function mtx.exp() in the Malmig package, but it
 seems that this package has been withdrawn from CRAN. An old version
 appears to exist in:
 
 http://r.meteo.uni.wroc.pl/src/contrib/Descriptions/Malmig.html
 
 Best,
 Dimitris
 
 Is that function for matrix powers or for the exponential of a matrix
 (which is what I initally thought that Harold wanted)?  There is a
 function expm in the Matrix package, patterned on the octave function
 of the same name, the calculates the matrix exponential for a square
 matrix.
 
 this function calculates the n-th power of a matrix, and this is what
 I thought Harold wanted, i.e.,
 
 P %*% P %*% P %*% P
 
 should be equal to
 
 mtx.exp(P, 4)
 
 
 
 
 %^% - function(mat, pow) {
stopifnot(length(pow) == 1, all.equal(pow, round(pow)), nrow(mat) ==
 ncol(mat))
pow - round(pow)
if (pow  0) {
  mat - solve(mat)
  pow - abs(pow)
}
result - diag(nrow(mat))
while (pow  0) {
  result - result %*% mat
  pow - pow - 1
}
result
 }
 
 Now P %^% 3 will give you the matrix cube.
 
 Duncan Murdoch
 
 __
 R-help@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.
 
 
 
 
 
 Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
 
 __
 R-help@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.
 
 
 __
 R-help@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.
 
 
 
 
 
 Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
 
 __
 R-help@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.

-- 
Ingmar Visser
Department of Psychology, University of Amsterdam
Roetersstraat 15, 1018 WB Amsterdam
The Netherlands
http://users.fmg.uva.nl/ivisser/
tel: +31-20-5256735

__
R-help@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.


Re: [R] Exponentiate a matrix

2006-09-22 Thread Paul Gilbert
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, P^2 also returns the element by element product. But, is there a
slick way to write

P %*% P %*% P

Obviously, P^3 does not return the result I expect.

Thanks,
Harold






   [[alternative HTML version deleted]]

__
R-help@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}}

__
R-help@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.


[R] Exponentiate a matrix

2006-09-21 Thread Doran, Harold
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, P^2 also returns the element by element product. But, is there a
slick way to write

P %*% P %*% P

Obviously, P^3 does not return the result I expect.

Thanks,
Harold






[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Exponentiate a matrix

2006-09-21 Thread Duncan Murdoch
On 9/21/2006 10:40 AM, 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, P^2 also returns the element by element product. But, is there a
 slick way to write
 
 P %*% P %*% P
 
 Obviously, P^3 does not return the result I expect.


I don't think there's anything built in, but it's easy to write your own:

%^% - function(mat, pow) {
   stopifnot(length(pow) == 1, all.equal(pow, round(pow)), nrow(mat) == 
ncol(mat))
   pow - round(pow)
   if (pow  0) {
 mat - solve(mat)
 pow - abs(pow)
   }
   result - diag(nrow(mat))
   while (pow  0) {
 result - result %*% mat
 pow - pow - 1
   }
   result
}

Now P %^% 3 will give you the matrix cube.

Duncan Murdoch

__
R-help@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.


Re: [R] Exponentiate a matrix

2006-09-21 Thread Dimitrios Rizopoulos

Quoting Duncan Murdoch [EMAIL PROTECTED]:

 On 9/21/2006 10:40 AM, 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, P^2 also returns the element by element product. But, is there a
 slick way to write

 P %*% P %*% P

 Obviously, P^3 does not return the result I expect.


 I don't think there's anything built in, but it's easy to write your own:

I think there was function mtx.exp() in the Malmig package, but it  
seems that this package has been withdrawn from CRAN. An old version  
appears to exist in:

http://r.meteo.uni.wroc.pl/src/contrib/Descriptions/Malmig.html

Best,
Dimitris


 %^% - function(mat, pow) {
stopifnot(length(pow) == 1, all.equal(pow, round(pow)), nrow(mat) ==
 ncol(mat))
pow - round(pow)
if (pow  0) {
  mat - solve(mat)
  pow - abs(pow)
}
result - diag(nrow(mat))
while (pow  0) {
  result - result %*% mat
  pow - pow - 1
}
result
 }

 Now P %^% 3 will give you the matrix cube.

 Duncan Murdoch

 __
 R-help@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.





Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

__
R-help@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.


Re: [R] Exponentiate a matrix

2006-09-21 Thread Andreas Hary
There is also mexp in the Matrix package and MatrixExp in the msm package.

Andreas



- Original Message - 
From: Dimitrios Rizopoulos [EMAIL PROTECTED]
To: Duncan Murdoch [EMAIL PROTECTED]
Cc: Doran, Harold [EMAIL PROTECTED]; r-help@stat.math.ethz.ch
Sent: Thursday, September 21, 2006 4:26 PM
Subject: Re: [R] Exponentiate a matrix



 Quoting Duncan Murdoch [EMAIL PROTECTED]:

 On 9/21/2006 10:40 AM, 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, P^2 also returns the element by element product. But, is there a
 slick way to write

 P %*% P %*% P

 Obviously, P^3 does not return the result I expect.


 I don't think there's anything built in, but it's easy to write your own:

 I think there was function mtx.exp() in the Malmig package, but it
 seems that this package has been withdrawn from CRAN. An old version
 appears to exist in:

 http://r.meteo.uni.wroc.pl/src/contrib/Descriptions/Malmig.html

 Best,
 Dimitris


 %^% - function(mat, pow) {
stopifnot(length(pow) == 1, all.equal(pow, round(pow)), nrow(mat) ==
 ncol(mat))
pow - round(pow)
if (pow  0) {
  mat - solve(mat)
  pow - abs(pow)
}
result - diag(nrow(mat))
while (pow  0) {
  result - result %*% mat
  pow - pow - 1
}
result
 }

 Now P %^% 3 will give you the matrix cube.

 Duncan Murdoch

 __
 R-help@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.





 Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

 __
 R-help@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.


__
R-help@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.


Re: [R] Exponentiate a matrix

2006-09-21 Thread Douglas Bates
On 9/21/06, Dimitrios Rizopoulos [EMAIL PROTECTED] wrote:

 Quoting Duncan Murdoch [EMAIL PROTECTED]:

  On 9/21/2006 10:40 AM, 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, P^2 also returns the element by element product. But, is there a
  slick way to write
 
  P %*% P %*% P
 
  Obviously, P^3 does not return the result I expect.
 
 
  I don't think there's anything built in, but it's easy to write your own:

 I think there was function mtx.exp() in the Malmig package, but it
 seems that this package has been withdrawn from CRAN. An old version
 appears to exist in:

 http://r.meteo.uni.wroc.pl/src/contrib/Descriptions/Malmig.html

 Best,
 Dimitris

Is that function for matrix powers or for the exponential of a matrix
(which is what I initally thought that Harold wanted)?  There is a
function expm in the Matrix package, patterned on the octave function
of the same name, the calculates the matrix exponential for a square
matrix.



  %^% - function(mat, pow) {
 stopifnot(length(pow) == 1, all.equal(pow, round(pow)), nrow(mat) ==
  ncol(mat))
 pow - round(pow)
 if (pow  0) {
   mat - solve(mat)
   pow - abs(pow)
 }
 result - diag(nrow(mat))
 while (pow  0) {
   result - result %*% mat
   pow - pow - 1
 }
 result
  }
 
  Now P %^% 3 will give you the matrix cube.
 
  Duncan Murdoch
 
  __
  R-help@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.
 
 



 Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

 __
 R-help@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.


__
R-help@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.


Re: [R] Exponentiate a matrix

2006-09-21 Thread Dimitrios Rizopoulos
Quoting Douglas Bates [EMAIL PROTECTED]:

 On 9/21/06, Dimitrios Rizopoulos [EMAIL PROTECTED] wrote:

 Quoting Duncan Murdoch [EMAIL PROTECTED]:

  On 9/21/2006 10:40 AM, 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, P^2 also returns the element by element product. But, is there a
  slick way to write
 
  P %*% P %*% P
 
  Obviously, P^3 does not return the result I expect.
 
 
  I don't think there's anything built in, but it's easy to write your own:

 I think there was function mtx.exp() in the Malmig package, but it
 seems that this package has been withdrawn from CRAN. An old version
 appears to exist in:

 http://r.meteo.uni.wroc.pl/src/contrib/Descriptions/Malmig.html

 Best,
 Dimitris

 Is that function for matrix powers or for the exponential of a matrix
 (which is what I initally thought that Harold wanted)?  There is a
 function expm in the Matrix package, patterned on the octave function
 of the same name, the calculates the matrix exponential for a square
 matrix.

this function calculates the n-th power of a matrix, and this is what  
I thought Harold wanted, i.e.,

P %*% P %*% P %*% P

should be equal to

mtx.exp(P, 4)




  %^% - function(mat, pow) {
 stopifnot(length(pow) == 1, all.equal(pow, round(pow)), nrow(mat) ==
  ncol(mat))
 pow - round(pow)
 if (pow  0) {
   mat - solve(mat)
   pow - abs(pow)
 }
 result - diag(nrow(mat))
 while (pow  0) {
   result - result %*% mat
   pow - pow - 1
 }
 result
  }
 
  Now P %^% 3 will give you the matrix cube.
 
  Duncan Murdoch
 
  __
  R-help@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.
 
 



 Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

 __
 R-help@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.


 __
 R-help@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.





Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

__
R-help@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.