[R] expm()

Douglas Bates bates at stat.wisc.edu
Tue Mar 6 14:49:47 CET 2007


On 3/6/07, Laura Hill <lhill07 at qub.ac.uk> wrote:
> Can the expm function be used to calculate the exponential of a matrix where
> the matrix is multiplied by a vector in a data frame?

I don't quite understand the question.  The exponential of a matrix is
only defined for square matrices.

> For example
>
>
> for (i in 1:length(y)){
>
>     expmN<-expm(Q*y[i])
>
>     Q<-Matrix(c(1, 2, 3, 4), 2, 2)
>
>     return(expmN)
>
>     }

I think what you are trying to do here is to calculate the exponential
of Q first and then multiply it by the columns of y (although I don't
understand why the loop runs to length(y) and you use y[i] - if y is a
numeric vector then y[i] is a scalar).

> library(Matrix)
Loading required package: lattice
> Q <- Matrix(c(1,2,3,4), 2, 2)
> Q
2 x 2 Matrix of class "dgeMatrix"
     [,1] [,2]
[1,]    1    3
[2,]    2    4
> expm(Q)
2 x 2 Matrix of class "dgeMatrix"
         [,1]     [,2]
[1,] 51.96896 112.1048
[2,] 74.73656 164.0738
>

If you then want to multiply expm(Q) by the columns of a 2 by k matrix
Y you can use

expm(Q) %*% Y

> Sorry I am new to R and I have been finding it difficult to get information
> on calculating Matrix exponentials in R.
>
> Many thanks in advance
>
> Laura
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list