-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
Of Cengiz Zopluoglu
Sent: Friday, May 18, 2012 11:33 AM
To: r-help at r-project.org
Subject: Re: [R] Help for numericDeriv function
I missed a couple line of codes in the previous e-mail. Here is whole code
again:
load <- matrix(c(3,0,1,4,1,3),nrow=3,ncol=2,byrow=TRUE)
l <- matrix(nrow=nrow(load),ncol=ncol(load))
for(i in 1:nrow(load)) {
for(j in 1:ncol(load)) { l[i,j]=paste("l",i,j,sep="")}}
for(i in 1:nrow(load)){
for(j in 1:ncol(load)){ assign(paste("l",i,j,sep=""),load[i,j]) }}
numericDeriv(quote(l11*l21+l12*l22), c(t(l),recursive=TRUE))
a <- paste(
paste(l[1,1],l[2,1],sep="*"),
paste(l[1,2],l[1,2],sep="*"),
sep="+"
)
numericDeriv(???a???, c(t(l),recursive=TRUE))
Thanks
On Fri, May 18, 2012 at 1:25 PM, Cengiz Zopluo??lu <cen.zop at gmail.com> wrote:
Hi,
I am stuck on something for a couple days, I am almost about to give up.
This looks simple, but I can't figure out. I hope I can get some help here.
I am trying to do some symbolic and numerical derivations. Let me explain
the problem. Let's say, I have a matrix as follows:
load <- matrix(c(3,0,1,4,1,3),nrow=3,ncol=2,byrow=TRUE)
load
[,1] [,2]
[1,] 3 0
[2,] 1 4
[3,] 1 3
I will create objects for each element of the matrix, and assign the
matrix elements to these objects. These objects will be also an element of
another matrix.
l <- matrix(nrow=nrow(load),ncol=ncol(load))
for(i in 1:nrow(load)) {
for(j in 1:ncol(load)) { l[i,j]=paste("l",i,j,sep="")}}
[,1] [,2]
[1,] "l11" "l12"
[2,] "l21" "l22"
[3,] "l31" "l32"
[1] 3
Let's say, I need to take the derivative of (l11*l21+l12*l22) with
respect to l11,l12,l21,l22,l31,l32.
numericDeriv(quote(l11*l21+l12*l22), c(t(l),recursive=TRUE))
[1] 3
attr(,"gradient")
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 4 3 0 0 0
Everything is fine up to this point. I need what I have. But, I don't want
to manipulate the elements in "quote()" manually within the numericDeriv
function. For instance, I want to call these elements from the matrix "l"
as below:
+ paste(l[1,1],l[2,1],sep="*"),
+ paste(l[1,2],l[1,2],sep="*"),
+ sep="+"
+ )
[1] "l11*l21+l12*l12"
Now, "a" is a character. Do you have any idea about how to use the
character value "l11*l21+l12*l12" assigned to object "a" within the
numericDeriv function. So, I can get the same result as above without
manipulating the expression in quote() manually.
Thanks
--
Cengiz Zopluoglu
--
Cengiz Zopluoglu
[[alternative HTML version deleted]]