title: Reduce time of computing partial derivatives
body: <p>I am computing the first and second order partial derivatives of a function (using numdifftools) with three unknown values to find values where my function has a minimum. This is the code:</p>
  
  <pre><code>def gaussian(x, mu, sig):
      return np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))
  
  def partial_function(f___,input,pos,value):
      tmp  = input[pos]
      input[pos] = value
      ret = f___(*input)
      input[pos] = tmp
      return ret
  
  def derivative(f,input):
      # f - function I want to derive
      # input - points where I want to calculate the derivative
      first = np.empty(len(input))
      second = np.empty(len(input))
      for i in range(len(input)):
          fg = lambda x:partial_function(f,input,i,x)
          first[i] = nd.Derivative(fg)(input[i])                 #first deriv
          second[i] = nd.Derivative(nd.Derivative(fg))(input[i]) #second deriv
      deriv = np.vstack((first,second))
      return deriv
  </code></pre>
  
  <p>The function I want to minimize is something like this:</p>
  
  <pre><code>func = lambda w,m,s: sum((gamma_[k,:]-w*gaussian(x,m,s))**2)
  </code></pre>
  
  <p>Where <code>gamma_[k,:]</code> is a known vector.</p>
  
  <p>I call the derivative method like this:</p>
  
  <p><code>d = derivative(func,param0)</code></p>
  
  <p>where <code>param0</code> are the initial guesses (a 1x3 vector in this case)</p>
  
  <p>The problem is that I run the optimization algorithm several times and time per iteration is huge, due to the derivate computation. Which approach can I take in order to reduce the time to compute these derivatives?</p>