diff --git a/include/lbfgs.h b/include/lbfgs.h index c41a939..eeb6f02 100644 --- a/include/lbfgs.h +++ b/include/lbfgs.h @@ -74,6 +74,8 @@ enum { LBFGSFALSE = 0, /** True value. */ LBFGSTRUE, + /** */ + LBFGS_ALREADY_MINIMIZED, /** Unknown error. */ LBFGSERR_UNKNOWNERROR = -1024, diff --git a/lib/lbfgs.c b/lib/lbfgs.c index 342c0f5..27af73c 100644 --- a/lib/lbfgs.c +++ b/lib/lbfgs.c @@ -184,7 +184,7 @@ lbfgsfloatval_t* lbfgs_malloc(int n) #if defined(USE_SSE) && (defined(__SSE__) || defined(__SSE2__)) n = round_out_variables(n); #endif/*defined(USE_SSE)*/ - return vecalloc(sizeof(lbfgsfloatval_t) * n); + return (lbfgsfloatval_t*)vecalloc(sizeof(lbfgsfloatval_t) * n); } void lbfgs_free(lbfgsfloatval_t *x) @@ -354,6 +354,15 @@ int lbfgs( } } + /* + Make sure that the gradients are not zero. + */ + vecnorm(&gnorm, g, n); + if (gnorm == 0.) { + ret = LBFGS_ALREADY_MINIMIZED; + goto lbfgs_exit; + } + /* Compute the initial step: step = 1.0 / sqrt(vecdot(d, d, n)) */