elementary operations, like taking a power, act elementwise on vectors and matrices. You may use a spectral decomposition to compute powers of a matrix - or a for loop if you are interested in small integer powers. HTH Giovanni -- __________________________________________________ [ ] [ Giovanni Petris GPetris at uark.edu ] [ Department of Mathematical Sciences ] [ University of Arkansas - Fayetteville, AR 72701 ] [ Ph: (479) 575-6324, 575-8630 (fax) ] [ http://definetti.uark.edu/~gpetris/ ] [__________________________________________________]> Date: Tue, 20 Jan 2004 16:40:07 +0000 > From: Federico Calboli <f.calboli at ucl.ac.uk> > Sender: r-help-bounces at stat.math.ethz.ch > Organization: > Precedence: list > > Dear All, > > I would like to ask why the zeroeth power of a matrix gives me a matrix > of ones rather than the identity matrix: > > > D<-rbind(c(0,0,0),c(0,0,0),c(0,0,0)) > > D<-as.matrix(D) > > D > [,1] [,2] [,3] > [1,] 0 0 0 > [2,] 0 0 0 > [3,] 0 0 0 > > > D^0 > [,1] [,2] [,3] > [1,] 1 1 1 > [2,] 1 1 1 > [3,] 1 1 1 > > I would have expected the identity matrix here. > > I find the same result with every other square matrix I used. > BTW, I am using R 1.8.1 on Linux Mandrake 9.1 > > Cheers, > > Federico Calboli > -- > > > > ================================> > Federico C. F. Calboli > > PLEASE NOTE NEW ADDRESS > > Dipartimento di Biologia > Via Selmi 3 > 40126 Bologna > Italy > > tel (+39) 051 209 4187 > fax (+39) 051 251 208 > > f.calboli at ucl.ac.uk > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >
Dear Federico, The common arithmetic operators such as ^ operate on the elements of matrices (or vectors or arrays). Similarly, * gives the element-wise product and not the matrix product. I hope that this helps, John At 04:40 PM 1/20/2004 +0000, Federico Calboli wrote:>Dear All, > >I would like to ask why the zeroeth power of a matrix gives me a matrix >of ones rather than the identity matrix: > > > D<-rbind(c(0,0,0),c(0,0,0),c(0,0,0)) > > D<-as.matrix(D) > > D > [,1] [,2] [,3] >[1,] 0 0 0 >[2,] 0 0 0 >[3,] 0 0 0 > > > D^0 > [,1] [,2] [,3] >[1,] 1 1 1 >[2,] 1 1 1 >[3,] 1 1 1 > >I would have expected the identity matrix here. > >I find the same result with every other square matrix I used. >BTW, I am using R 1.8.1 on Linux Mandrake 9.1 > >Cheers, > >Federico Calboli____________________________ John Fox Department of Sociology McMaster University email: jfox at mcmaster.ca web: http://www.socsci.mcmaster.ca/jfox
> -----Original Message----- > From: Federico Calboli > Sent: Tuesday, January 20, 2004 5:40 PM > To: r-help > Subject: [R] matrix exponential: M^0 > > I would like to ask why the zeroeth power of a matrix gives me a matrix > of ones rather than the identity matrix: > > > D<-rbind(c(0,0,0),c(0,0,0),c(0,0,0)) > > D<-as.matrix(D) > > D > [,1] [,2] [,3] > [1,] 0 0 0 > [2,] 0 0 0 > [3,] 0 0 0 > > > D^0 > [,1] [,2] [,3] > [1,] 1 1 1 > [2,] 1 1 1 > [3,] 1 1 1 > > I would have expected the identity matrix here. > > I find the same result with every other square matrix I used.[Dietrich Trenkler] M^0 means appying ^0 to each element of M. Matrix multiplication can be achieved by A%*%B. In this way A%*%A is not the same as A^2. Dietrich
Dear All, I would like to ask why the zeroeth power of a matrix gives me a matrix of ones rather than the identity matrix:> D<-rbind(c(0,0,0),c(0,0,0),c(0,0,0)) > D<-as.matrix(D) > D[,1] [,2] [,3] [1,] 0 0 0 [2,] 0 0 0 [3,] 0 0 0> D^0[,1] [,2] [,3] [1,] 1 1 1 [2,] 1 1 1 [3,] 1 1 1 I would have expected the identity matrix here. I find the same result with every other square matrix I used. BTW, I am using R 1.8.1 on Linux Mandrake 9.1 Cheers, Federico Calboli -- ================================ Federico C. F. Calboli PLEASE NOTE NEW ADDRESS Dipartimento di Biologia Via Selmi 3 40126 Bologna Italy tel (+39) 051 209 4187 fax (+39) 051 251 208 f.calboli at ucl.ac.uk
Dear All, Thanks for all the help. I tried to implement Stephane Dray's suggestion and Erin Hodgess function with the following matrices:> A[,1] [,2] [1,] 2 1 [2,] 1 3> P[,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 2 3 4> D[,1] [,2] [,3] [1,] 0 0 0 [2,] 0 0 0 [3,] 0 0 0 But I run in a number of troubles, probably my fault. I also tried MatrixExp from the library msm, but I failed to understand how to use it. anyway, as my linear algebra is too poor for any intelligent decomposition and whatnot, and I wanted a function, I went for the brute force approach. mtx.exp<-function(X,n){ if (n==0) { phi<-diag(rep(1,length(X[1,]))) return(phi) } if (n==1) { phi<-X return(phi) } X1<-X for (i in 2:n) { X<-X%*%X1 } phi<-X return(phi) } I would imagine this approach would be memory inefficient; would it incurr in other problems? Again, many thanks to all for the invaluable help. Regards, Federico Calboli -- ================================ Federico C. F. Calboli PLEASE NOTE NEW ADDRESS Dipartimento di Biologia Via Selmi 3 40126 Bologna Italy tel (+39) 051 209 4187 fax (+39) 051 251 208 f.calboli at ucl.ac.uk