title: R optim same function for fn and gr
body: <p>I would like to use optim() to optimize a cost function (fn argument), and I will be providing a gradient (gr argument). I can write separate functions for fn and gr. However, they have a lot of code in common and I don't want the optimizer to waste time repeating those calculations. So is it possible to provide one function that computes both the cost and the gradient? If so, what would be the calling syntax to optim()?</p>
  
  <p>As an example, suppose the function I want to minimize is</p>
  
  <pre><code>cost &lt;- function(x) {
      x*exp(x)
  }
  </code></pre>
  
  <p>Obviously, this is not the function I'm trying to minimize. That's too complicated to list here, but the example serves to illustrate the issue. Now, the gradient would be</p>
  
  <pre><code>grad &lt;- function(x) {
      (x+1)*exp(x)
  }
  </code></pre>
  
  <p>So as you can see, the two functions, if called separately, would repeat some of the work (in this case, the exponential function). However, since optim() takes two separate arguments (fn and gr), it appears there is no way to avoid this inefficiency, unless there is a way to define a function like</p>
  
  <pre><code>costAndGrad &lt;- function(x) {
      ex &lt;- exp(x)
      list(cost=x*ex, grad=(x+1)*ex)
  }
  </code></pre>
  
  <p>and then pass that function to optim(), which would need to know how to extract the cost and gradient.</p>
  
  <p>Hope that explains the problem. Like I said my function is much more complicated, but the idea is the same: there is considerable code that goes into both calculations (cost and gradient), which I don't want to repeat unnecessarily.</p>
  
  <p>By the way, I am an R novice, so there might be something simple that I'm missing!</p>
  
  <p>Thanks very much</p>