Ron E. VanNimwegen
2006-Nov-23 17:08 UTC
[R] Partial derivatives of a matrix with respect to a single element
UseRs, We are using projection matrices in demographic analyses, and one technique is to calculate partial derivatives of a matrix with respect to a variable appearing in one or more of its elements. For example, consider the matrix A: | a*b a*c | | d d | If I'm not mistaken, the partial derivative of matrix A w/ respect to variable 'a' is the matrix: | b c | | 0 0 | ... just an element by element first order derivative of the notation, but would like to use R in a manner similar to our method: We are doing this with symbolic notation, variable assignments, substitution, and the 'diff(matrix,variable)' function in Matlab; however, I'm always looking for GNU solutions to old procedure$, if you get my drift. I've read and used the derivative functions in R, and could (probably) write a lengthy routine to duplicate this solution, but if there's a shortcut, I have no delusions about my own laziness and will greatly appreciate your input. R. VanNimwegen Kansas State University Division of Biology vanron at ksu.edu
Gabor Grothendieck
2006-Nov-23 17:28 UTC
[R] Partial derivatives of a matrix with respect to a single element
Try the Ryacas package:> library(Ryacas) > a <- Sym("a"); b <- Sym("b"); c <- Sym("c"); d <- Sym("d") > M <- List(List(a*b, a*c), List(d, d)) > deriv(M, "a")expression(list(list(b, c), list(0, 0))) On 11/23/06, Ron E. VanNimwegen <vanron at ksu.edu> wrote:> UseRs, > > We are using projection matrices in demographic analyses, and one > technique is to calculate partial derivatives of a matrix with respect > to a variable appearing in one or more of its elements. For example, > consider the matrix A: > > | a*b a*c | > | d d | > > > If I'm not mistaken, the partial derivative of matrix A w/ respect to > variable 'a' is the matrix: > > | b c | > | 0 0 | > > ... just an element by element first order derivative of the notation, > but would like to use R in a manner similar to our method: > > We are doing this with symbolic notation, variable assignments, > substitution, and the 'diff(matrix,variable)' function in Matlab; > however, I'm always looking for GNU solutions to old procedure$, if you > get my drift. I've read and used the derivative functions in R, and > could (probably) write a lengthy routine to duplicate this solution, but > if there's a shortcut, I have no delusions about my own laziness and > will greatly appreciate your input. > > R. VanNimwegen > > Kansas State University > Division of Biology > vanron at ksu.edu > > ______________________________________________ > 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. >