hmmer-3.3/easel/esl_minimizer.c
/* Return the negative gradient at a point, determined numerically.
 */
Return the negative gradient at a point, determined numerically.
Return the negative gradient at a point determined numerically .
static void
numeric_derivative(ESL_MIN_CFG *cfg, double *x, int n, 
		   double (*func)(double *, int, void*),
		   void *prm, double *dx, ESL_MIN_DAT *dat)
{
  double  relstep = cfg ? cfg->deriv_step : eslMIN_DERIV_STEP;
  double *u       = cfg ? cfg->u          : NULL;
  int    i;
  double delta;
  double f1, f2;
  double tmp;
  for (i = 0; i < n; i++)
    {
      if (u) delta = fabs(u[i] * relstep);
      else   delta = fabs(relstep);
      tmp = x[i]; 
      x[i] = tmp + delta;
      f1  = (*func)(x, n, prm);
      x[i] = tmp - delta;
      f2  = (*func)(x, n, prm);
      x[i] = tmp;
      dx[i] = (-0.5 * (f1-f2)) / delta;
      if (dat) dat->nfunc += 2;
      ESL_DASSERT1((! isnan(dx[i])));
    }
}
static void numeric_derivative ( ESL_MIN_CFG * cfg , double * x , int n , double ( * func ) ( double * , int , void * ) , void * prm , double * dx , ESL_MIN_DAT * dat ) { double relstep = cfg ? cfg -> deriv_step : eslMIN_DERIV_STEP ; double * u = cfg ? cfg -> u : NULL ; int i ; double delta ; double f1 , f2 ; double tmp ; for ( i = 0 ; i < n ; i ++ ) { if ( u ) delta = fabs ( u [ i ] * relstep ) ; else delta = fabs ( relstep ) ; tmp = x [ i ] ; x [ i ] = tmp + delta ; f1 = ( * func ) ( x , n , prm ) ; x [ i ] = tmp - delta ; f2 = ( * func ) ( x , n , prm ) ; x [ i ] = tmp ; dx [ i ] = ( -0.5 * ( f1 - f2 ) ) / delta ; if ( dat ) dat -> nfunc += 2 ; ESL_DASSERT1 ( ( ! isnan ( dx [ i ] ) ) ) ; } }
Return the negative gradient at a point, determined numerically.
Return the negative gradient at a point determined numerically .