title: Equality of symbolic derivatives in R
body: <p>I would like to use R to verify complicated derivative calculations. Here is my best attempt so far on a simple function:</p>
  
  <pre><code>f &lt;- expression(a*log(x^2))
  df.dx &lt;- deriv(f, 'x')
  (df.dx)
  df.dx.byHand &lt;- expression(2*a/x) # The derivative of f calculated by hand
  (df.dx.byHand)
  all.equal(df.dx, df.dx.byHand) 
  </code></pre>
  
  <p>The output of the above is</p>
  
  <pre><code>&gt; (df.dx)
   expression({
  .expr1 &lt;- x^2
  .value &lt;- a * log(.expr1)
  .grad &lt;- array(0, c(length(.value), 1L), list(NULL, c("x")))
  .grad[, "x"] &lt;- a * (2 * x/.expr1)
  attr(.value, "gradient") &lt;- .grad
  .value
  
  &gt; (df.dx.byHand)
  expression(2 * a/x)
  
  [1] "Component 1: target, current do not match when deparsed"
  </code></pre>
  
  <p>I have already checked</p>
  
  <p><a href="https://stackoverflow.com/questions/8779362/explicit-formula-versus-symbolic-derivatives-in-r?rq=1">Explicit formula versus symbolic derivatives in R</a>, </p>
  
  <p><a href="http://stat.ethz.ch/R-manual/R-patched/library/stats/html/deriv.html" rel="nofollow noreferrer">http://stat.ethz.ch/R-manual/R-patched/library/stats/html/deriv.html</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7311996/symbolic-derivatives-and-simplification-in-r">Symbolic derivatives and simplification in R</a></p>
  
  <p>and also</p>
  
  <p><a href="http://stat.ethz.ch/R-manual/R-patched/library/base/html/expression.html" rel="nofollow noreferrer">http://stat.ethz.ch/R-manual/R-patched/library/base/html/expression.html</a></p>
  
  <p>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.</p>
  
  <p>Due to IT issues, neither Sage nor YACAS are available to me.</p>
  
  <p>Many thanks for suggestions!</p>