ext/lbfgsb/lbfgsbext.c in lbfgsb-0.3.1 vs ext/lbfgsb/lbfgsbext.c in lbfgsb-0.3.2

- old
+ new

@@ -6,38 +6,36 @@ VALUE lbfgsb_min_l_bfgs_b(VALUE self, VALUE fnc, VALUE x_val, VALUE jcb, VALUE args, VALUE l_val, VALUE u_val, VALUE nbd_val, VALUE maxcor, VALUE ftol, VALUE gtol, VALUE maxiter, VALUE disp) { - long i; long n_iter; long n_fev; long n_jev; long max_iter = NUM2LONG(maxiter); narray_t* x_nary; narray_t* l_nary; narray_t* u_nary; narray_t* nbd_nary; long n; long m = NUM2LONG(maxcor); - double *x_ptr; - double *l_ptr; - double *u_ptr; - long *nbd_ptr; + double* x_ptr; + double* l_ptr; + double* u_ptr; + long* nbd_ptr; double f; - double *g; + double* g; double factr = NUM2DBL(ftol); double pgtol = NUM2DBL(gtol); double* wa; long* iwa; char task[60]; long iprint = NIL_P(disp) ? -1 : NUM2LONG(disp); char csave[60]; long lsave[4]; long isave[44]; double dsave[29]; - double* g_ptr; VALUE g_val; VALUE fg_arr; VALUE ret; GetNArray(x_val, x_nary); @@ -109,11 +107,11 @@ wa = ALLOC_N(double, (2 * m + 5) * n + 12 * m * m + 12 * m); iwa = ALLOC_N(long, 3 * n); g_val = Qnil; f = 0.0; - for (i = 0; i < n; g[i++] = 0.0); + memset(g, 0, n * sizeof(*g)); strcpy(task, "START"); n_fev = 0; n_jev = 0; for (n_iter = 0; n_iter < max_iter;) { @@ -128,18 +126,17 @@ g_val = rb_ary_entry(fg_arr, 1); } else { f = NUM2DBL(rb_funcall(self, rb_intern("fnc"), 3, fnc, x_val, args)); g_val = rb_funcall(self, rb_intern("jcb"), 3, jcb, x_val, args); } - n_fev += 1; - n_jev += 1; + n_fev++; + n_jev++; if (CLASS_OF(g_val) != numo_cDFloat) g_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, g_val); if (!RTEST(nary_check_contiguous(g_val))) g_val = nary_dup(g_val); - g_ptr = (double*)na_get_pointer_for_read(g_val); - for (i = 0; i < n; i++) g[i] = g_ptr[i]; + memcpy(g, na_get_pointer_for_read(g_val), n * sizeof(*g)); + RB_GC_GUARD(g_val); } else if (strncmp(task, "NEW_X", 5) == 0) { - n_iter += 1; - continue; + n_iter++; } else { break; } }