Re: [R] Help for numericDeriv function

2012-05-18 Thread William Dunlap
You could use parse(text=a)[[1]]:
   > numericDeriv(parse(text=a)[[1]], c(t(l),recursive=TRUE))
  [1] 3
  attr(,"gradient")
   [,1] [,2] [,3] [,4] [,5] [,6]
  [1,]103000

or construct your expression directly:
  > lnames <- array(lapply(l, as.name), dim=dim(l))
  > aa <- call("+", call("*", lnames[[1,1]], lnames[[2,1]]), call("*", 
lnames[[1,2]], lnames[[1,2]]))
  > numericDeriv(aa, c(t(l),recursive=TRUE))
  [1] 3
  attr(,"gradient")
   [,1] [,2] [,3] [,4] [,5] [,6]
  [1,]103000

The results are identical.  The latter method is a bit more work to set up but
works no matter what the names in the expression look like.  The former
fails if the names contain spaces or other things that are not allowed in
R names.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
> Behalf
> Of Cengiz Zopluoglu
> Sent: Friday, May 18, 2012 11:33 AM
> To: r-help@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  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,]30
> > [2,]14
> > [3,]13
> >
> > 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="")}}
> >
> > > l
> >  [,1]  [,2]
> > [1,] "l11" "l12"
> > [2,] "l21" "l22"
> > [3,] "l31" "l32"
> >
> > > l11
> > [1] 3
> > > l12
> > [1] 0
> > > l21
> > [1] 1
> > > l22
> > [1] 4
> > > l31
> > [1] 1
> > > 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,]143000
> >
> > 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:
> >
> > > a <- paste(
> > + paste(l[1,1],l[2,1],sep="*"),
> > + paste(l[1,2],l[1,2],sep="*"),
> > + sep="+"
> > + )
> > > a
> > [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]]

__
R-help@r-project.org 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.


Re: [R] Help for numericDeriv function

2012-05-18 Thread Cengiz Zopluoğlu
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  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,]30
> [2,]14
> [3,]13
>
> 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="")}}
>
> > l
>  [,1]  [,2]
> [1,] "l11" "l12"
> [2,] "l21" "l22"
> [3,] "l31" "l32"
>
> > l11
> [1] 3
> > l12
> [1] 0
> > l21
> [1] 1
> > l22
> [1] 4
> > l31
> [1] 1
> > 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,]143000
>
> 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:
>
> > a <- paste(
> + paste(l[1,1],l[2,1],sep="*"),
> + paste(l[1,2],l[1,2],sep="*"),
> + sep="+"
> + )
> > a
> [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]]

__
R-help@r-project.org 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.


[R] Help for numericDeriv function

2012-05-18 Thread Cengiz Zopluoğlu
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,]30
[2,]14
[3,]13

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="")}}

> l
 [,1]  [,2]
[1,] "l11" "l12"
[2,] "l21" "l22"
[3,] "l31" "l32"

> l11
[1] 3
> l12
[1] 0
> l21
[1] 1
> l22
[1] 4
> l31
[1] 1
> 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,]143000

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:

> a <- paste(
+ paste(l[1,1],l[2,1],sep="*"),
+ paste(l[1,2],l[1,2],sep="*"),
+ sep="+"
+ )
> a
[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

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.