I would like to use R to verify complicated derivative calculations. Here is my best attempt so far on a simple function:
f <- expression(a*log(x^2))
df.dx <- deriv(f, 'x')
(df.dx)
df.dx.byHand <- expression(2*a/x) # The derivative of f calculated by hand
(df.dx.byHand)
all.equal(df.dx, df.dx.byHand)
The output of the above is
> (df.dx)
expression({
.expr1 <- x^2
.value <- a * log(.expr1)
.grad <- array(0, c(length(.value), 1L), list(NULL, c("x")))
.grad[, "x"] <- a * (2 * x/.expr1)
attr(.value, "gradient") <- .grad
.value
> (df.dx.byHand)
expression(2 * a/x)
[1] "Component 1: target, current do not match when deparsed"
I have already checked
Explicit formula versus symbolic derivatives in R,
http://stat.ethz.ch/R-manual/R-patched/library/stats/html/deriv.html
Symbolic derivatives and simplification in R
and also
http://stat.ethz.ch/R-manual/R-patched/library/base/html/expression.html
since the core issue seems to be that I don't know how to extract from 'df.dx' the 'grad[, 'x']' part of the expression.
Due to IT issues, neither Sage nor YACAS are available to me.
Many thanks for suggestions!