t121 at gmx.net
2009-Oct-29 19:31 UTC
[R] deriv() to take vector of expressions as 1st arg?
The deriv() function takes an 'expression' as its first argument). I was
wondering if the this function can take an array or a vector of
expressions as its first argument. Aside, I saw how to give a vector
argument to the second argument.
like to have something like:
deriv(c(~x^2+y^3, ~x^5+y^6), c("x","y"))
the documentation for this function talks about being able to "returns
the function values with a gradient attribute containing the gradient
matrix[!!!]". and I indeed want a matrix of expressions to be returned.
In effect, I want to get the Jacobian.
Thank you, if you have some hints on this.
Tom
Gabor Grothendieck
2009-Oct-29 22:35 UTC
[R] deriv() to take vector of expressions as 1st arg?
Try this:
deriv(expression(x^2+y^3, x^5+y^6), c("x","y"))
On Thu, Oct 29, 2009 at 3:31 PM, <t121 at gmx.net>
wrote:> The deriv() function takes an 'expression' as its first argument).
I was
> wondering if the this function can take an array or a vector of
> expressions as its first argument. Aside, I saw how to give a vector
> argument to the second argument.
>
> like to have something like:
> deriv(c(~x^2+y^3, ~x^5+y^6), c("x","y"))
>
> the documentation for this function talks about being able to "returns
> the function values with a gradient attribute containing the gradient
> matrix[!!!]". and I indeed want a matrix of expressions to be
returned.
>
> In effect, I want to get the Jacobian.
>
> Thank you, if you have some hints on this.
>
> Tom
>
> ______________________________________________
> R-help at 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.
>
On 30/10/2009, at 11:35 AM, Gabor Grothendieck wrote:> Try this: > > deriv(expression(x^2+y^3, x^5+y^6), c("x","y"))Did *you* try it Gabor? I did just now and it returns only the gradient of the first component: > deriv(expression(x^2+y^3, x^5+y^6), c("x","y")) expression({ .value <- x^2 + y^3 .grad <- array(0, c(length(.value), 2L), list(NULL, c("x", "y"))) .grad[, "x"] <- 2 * x .grad[, "y"] <- 3 * y^2 attr(.value, "gradient") <- .grad .value }) cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
Gabor Grothendieck
2009-Oct-29 23:29 UTC
[R] deriv() to take vector of expressions as 1st arg?
OK. Try this:
sapply(expression(x^2+y^3, x^5+y^6), deriv, c("x", "y"))
On Thu, Oct 29, 2009 at 7:11 PM, Rolf Turner <r.turner at auckland.ac.nz>
wrote:>
> On 30/10/2009, at 11:35 AM, Gabor Grothendieck wrote:
>
>> Try this:
>>
>> deriv(expression(x^2+y^3, x^5+y^6), c("x","y"))
>
> Did *you* try it Gabor? ?I did just now and it returns only
> the gradient of the first component:
>
> ?> deriv(expression(x^2+y^3, x^5+y^6), c("x","y"))
> expression({
> ? ?.value <- x^2 + y^3
> ? ?.grad <- array(0, c(length(.value), 2L), list(NULL, c("x",
> ? ? ? ?"y")))
> ? ?.grad[, "x"] <- 2 * x
> ? ?.grad[, "y"] <- 3 * y^2
> ? ?attr(.value, "gradient") <- .grad
> ? ?.value
> })
>
> ? ? ? ?cheers,
>
> ? ? ? ? ? ? ? ?Rolf Turner
>
> ######################################################################
> Attention: This e-mail message is privileged and confidential. If you are
> not the intended recipient please delete the message and notify the sender.
> Any views or opinions presented are solely those of the author.
>
> This e-mail has been scanned and cleared by MailMarshal
> www.marshalsoftware.com
> ######################################################################
>
t121 at gmx.net
2009-Oct-30 16:46 UTC
[R] deriv() to take vector of expressions as 1st arg?
Thanks a lot for the hints. The sapply method may work for me. But how can I extract just the ".grad" expression from the return value of the deriv function? (and secondly store in a matrix of expressions?) Thanks again.