[R] Re: matrix exponential: M0
Vicente Canto Casasola
vicented.canto.ext at juntadeandalucia.es
Thu Jan 22 14:34:01 CET 2004
H i, all!
First of all, I'd like to apologize for my poor English. It's for years
I don't use it.
This is a R-version of a function I wrote a long ago for my HP48 calculator.
It works with the binary expression of the power and just need to
duplicate the mem used by X.
Hope this helps.
mtx.exp<-function(X,n)
#Function to calculate the n-th power of a matrix X;
{
phi <- diag(rep(1,length(X[1,])))
pot <- X #This is the first power of the matrix.
while (n > 0)
{
if (n%%2)
{
phi <- phi%*%pot;
}
n <- n%/%2;
pot <- pot %*% pot;
}
return(phi);
}
#Here is some output:
> xx <- matrix(c(1,0,1,1),2,2)
> xx
[,1] [,2]
[1,] 1 1
[2,] 0 1
> mtx.exp(xx,3)
[,1] [,2]
[1,] 1 3
[2,] 0 1
> mtx.exp(xx,4)
[,1] [,2]
[1,] 1 4
[2,] 0 1
> mtx.exp(xx,6)
[,1] [,2]
[1,] 1 6
[2,] 0 1
> mtx.exp(xx,10)
[,1] [,2]
[1,] 1 10
[2,] 0 1
> mtx.exp(xx,1000)
[,1] [,2]
[1,] 1 1000
[2,] 0 1
Vicente D. Canto Casasola
More information about the R-help
mailing list