Re: [R] Derivative of a Function Expression

2007-09-03 Thread Rory Winston
Hi guys Thanks for all the fantastic suggestions! I didnt realise you could extract the body of a function in that manner. It looks like R always has many ways to solve a particular problem. Cheers Rory On 9/4/07, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > > And if f has brace brackets surr

Re: [R] Derivative of a Function Expression

2007-09-03 Thread Gabor Grothendieck
And if f has brace brackets surrounding the body then do this: f <- function(x) { x*x } deriv(body(f)[[2]], "x", func = TRUE) If you are writing a general function you can do this: e <- if (identical(body(f)[[1]], as.name("{"))) body(f)[[2]] else body(f) deriv(e, "x", func = TRUE) On 9/3/07, G

Re: [R] Derivative of a Function Expression

2007-09-03 Thread Gabor Grothendieck
One improvement. This returns a function directly without having to create a template and filling in its body: deriv(body(f), "x", func = TRUE) On 9/3/07, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > The problem is that brace brackets are not in the derivatives table. > Make sure you don't ha

Re: [R] Derivative of a Function Expression

2007-09-03 Thread Ted Harding
On 03-Sep-07 21:45:40, Alberto Monteiro wrote: > Rory Winston wrote: >> >> I am currently (for pedagogical purposes) writing a simple numerical >> analysis library in R. I have come unstuck when writing a simple >> Newton-Raphson implementation, that looks like this: >> >> f <- function(x) { 2*co

Re: [R] Derivative of a Function Expression

2007-09-03 Thread Gabor Grothendieck
The problem is that brace brackets are not in the derivatives table. Make sure you don't have any. On 9/3/07, Alberto Vieira Ferreira Monteiro <[EMAIL PROTECTED]> wrote: > Gabor Grothendieck wrote: > > > > Actually in thinking about this its pretty easy to do it without Ryacas > > too: > > > > Df

Re: [R] Derivative of a Function Expression

2007-09-03 Thread Alberto Vieira Ferreira Monteiro
Gabor Grothendieck wrote: > > Actually in thinking about this its pretty easy to do it without Ryacas > too: > > Df <- f > body(Df) <- deriv(body(f), "x") > Df > This is weird. f <- function(x) { x^2 + 2*x+1 } Df <- f body(Df) <- deriv(body(f), "x") # error Also: f <- function(x) x^2 + 2 * x + 1

Re: [R] Derivative of a Function Expression

2007-09-03 Thread Gabor Grothendieck
Actually in thinking about this its pretty easy to do it without Ryacas too: Df <- f body(Df) <- deriv(body(f), "x") Df On 9/3/07, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > The Ryacas package can do that (but the function must be one line > and it can't have brace brackets). The first yac

Re: [R] Derivative of a Function Expression

2007-09-03 Thread Gabor Grothendieck
The Ryacas package can do that (but the function must be one line and it can't have brace brackets). The first yacas call below registers f with yacas, then we set up a function to act as a template to hold the derivative and then we set its body calling yacas again to take the derivative. librar

Re: [R] Derivative of a Function Expression

2007-09-03 Thread Alberto Monteiro
Rory Winston wrote: > > I am currently (for pedagogical purposes) writing a simple numerical > analysis library in R. I have come unstuck when writing a simple > Newton-Raphson implementation, that looks like this: > > f <- function(x) { 2*cos(x)^2 + 3*sin(x) + 0.5 } > > root <- newton(f, tol=

[R] Derivative of a Function Expression

2007-09-03 Thread Rory Winston
Hi I am currently (for pedagogical purposes) writing a simple numerical analysis library in R. I have come unstuck when writing a simple Newton-Raphson implementation, that looks like this: f <- function(x) { 2*cos(x)^2 + 3*sin(x) + 0.5 } root <- newton(f, tol=0.0001, N=20, a=1) My issue is c

Re: [R] Derivative of a function

2005-07-05 Thread Martin Maechler
> "Gabor" == Gabor Grothendieck <[EMAIL PROTECTED]> > on Tue, 5 Jul 2005 09:14:20 -0400 writes: Gabor> On 7/4/05, Gabriel Rodrigues Alves Margarido Gabor> <[EMAIL PROTECTED]> wrote: >> Suppose I have a simple function that returns a matrix, such as: >> >> test <- f

Re: [R] Derivative of a function

2005-07-05 Thread Gabor Grothendieck
On 7/5/05, Gabor Grothendieck <[EMAIL PROTECTED]> wrote: > On 7/4/05, Gabriel Rodrigues Alves Margarido > <[EMAIL PROTECTED]> wrote: > > Suppose I have a simple function that returns a matrix, such as: > > > > test <- function(x){ return(matrix(c(x,x^2,x^3,x^4),2,2)) } > > > > so that test returns:

Re: [R] Derivative of a function

2005-07-05 Thread Gabor Grothendieck
On 7/4/05, Gabriel Rodrigues Alves Margarido <[EMAIL PROTECTED]> wrote: > Suppose I have a simple function that returns a matrix, such as: > > test <- function(x){ return(matrix(c(x,x^2,x^3,x^4),2,2)) } > > so that test returns: > [ x x^3 ] > [ x^2x^4 ] > > Is it possible for me to get

Re: [R] Derivative of a function

2005-07-05 Thread Uwe Ligges
Gabriel Rodrigues Alves Margarido wrote: > Suppose I have a simple function that returns a matrix, such as: > > test <- function(x){ return(matrix(c(x,x^2,x^3,x^4),2,2)) } > > so that test returns: > [ x x^3 ] > [ x^2x^4 ] > > Is it possible for me to get the derivative of an expressio

[R] Derivative of a function

2005-07-04 Thread Gabriel Rodrigues Alves Margarido
Suppose I have a simple function that returns a matrix, such as: test <- function(x){ return(matrix(c(x,x^2,x^3,x^4),2,2)) } so that test returns: [ x x^3 ] [ x^2x^4 ] Is it possible for me to get the derivative of an expression such as: c(1,0) %*% test() %*% c(0,1) The vectors are us

[R] Derivative of a function

2005-07-04 Thread Gabriel Rodrigues Alves Margarido
Suppose I have a simple function that returns a matrix, such as: test <- function(x){ return(matrix(c(x,x^2,x^3,x^4),2,2)) } so that test returns: [ x x^3 ] [ x^2x^4 ] Is it possible for me to get the derivative of an expression such as: c(1,0) %*% test() %*% c(0,1) The vectors are us