A colleague asked me the following question the other day. In the following piece of code, how do you extract the gradient:
> x=5
> a = eval(deriv(~ x^3, "x"))
> a
[1] 125
attr(,"gradient")
x
[1,] 75
My answer was
> attr(a, "gradient")[1]
[1] 75
This syntax seems clunky to me. Is there a better way of extracting the gradient?