From 878f15b8a56ba1c19a6b44fbd5955ac702e46a06 Mon Sep 17 00:00:00 2001 From: Thomas Capricelli Date: Sun, 23 Aug 2009 04:06:16 +0200 Subject: [PATCH] * use eigen object for callbacks for hybrd and lmdif * use Functor instead of argument for ei_fdjac*() --- unsupported/Eigen/NonLinear | 4 ---- unsupported/Eigen/src/NonLinear/fdjac1.h | 8 ++++---- unsupported/Eigen/src/NonLinear/fdjac2.h | 6 +++--- unsupported/Eigen/src/NonLinear/hybrd.h | 10 +++++----- unsupported/Eigen/src/NonLinear/lmdif.h | 10 +++++----- unsupported/test/NonLinear.cpp | 9 +++++++-- 6 files changed, 24 insertions(+), 23 deletions(-) diff --git a/unsupported/Eigen/NonLinear b/unsupported/Eigen/NonLinear index f9dd63db8..333604aea 100644 --- a/unsupported/Eigen/NonLinear +++ b/unsupported/Eigen/NonLinear @@ -36,10 +36,6 @@ namespace Eigen { */ //@{ -/* Declarations for minpack */ -typedef int (*minpack_func_nn)(int n, const double *x, double *fvec, int iflag ); -typedef int (*minpack_func_mn)(int m, int n, const double *x, double *fvec, int iflag ); - #include "src/NonLinear/qrsolv.h" #include "src/NonLinear/r1updt.h" #include "src/NonLinear/r1mpyq.h" diff --git a/unsupported/Eigen/src/NonLinear/fdjac1.h b/unsupported/Eigen/src/NonLinear/fdjac1.h index 9388bd347..2fd0696f7 100644 --- a/unsupported/Eigen/src/NonLinear/fdjac1.h +++ b/unsupported/Eigen/src/NonLinear/fdjac1.h @@ -1,6 +1,6 @@ -template -int ei_fdjac1(minpack_func_nn fcn, +template +int ei_fdjac1( Matrix< Scalar, Dynamic, 1 > &x, Matrix< Scalar, Dynamic, 1 > &fvec, Matrix< Scalar, Dynamic, Dynamic > &fjac, @@ -34,7 +34,7 @@ int ei_fdjac1(minpack_func_nn fcn, if (h == 0.) h = eps; x[j] = temp + h; - iflag = (*fcn)(n, x.data(), wa1.data(), 1); + iflag = Functor::f(x, wa1, 1); if (iflag < 0) goto L30; x[j] = temp; @@ -59,7 +59,7 @@ L40: x[j] = wa2[j] + h; /* L60: */ } - iflag = (*fcn)(n, x.data(), wa1.data(), 1); + iflag = Functor::f(x, wa1, 1); if (iflag < 0) { /* goto L100; */ return iflag; diff --git a/unsupported/Eigen/src/NonLinear/fdjac2.h b/unsupported/Eigen/src/NonLinear/fdjac2.h index c8cd74b20..2a4b9734b 100644 --- a/unsupported/Eigen/src/NonLinear/fdjac2.h +++ b/unsupported/Eigen/src/NonLinear/fdjac2.h @@ -1,6 +1,6 @@ -template -int ei_fdjac2(minpack_func_mn fcn, +template +int ei_fdjac2( Matrix< Scalar, Dynamic, 1 > &x, Matrix< Scalar, Dynamic, 1 > &fvec, Matrix< Scalar, Dynamic, Dynamic > &fjac, @@ -26,7 +26,7 @@ int ei_fdjac2(minpack_func_mn fcn, h = eps; } x[j] = temp + h; - iflag = (*fcn)(m, n, x.data(), wa.data(), 1); + iflag = Functor::f(x, wa, 1); if (iflag < 0) { /* goto L30; */ return iflag; diff --git a/unsupported/Eigen/src/NonLinear/hybrd.h b/unsupported/Eigen/src/NonLinear/hybrd.h index b3cf51727..772a57b67 100644 --- a/unsupported/Eigen/src/NonLinear/hybrd.h +++ b/unsupported/Eigen/src/NonLinear/hybrd.h @@ -75,7 +75,7 @@ L20: /* evaluate the function at the starting point */ /* and calculate its norm. */ - iflag = Functor::f(n, x.data(), fvec.data(), 1); + iflag = Functor::f(x, fvec, 1); nfev = 1; if (iflag < 0) { goto L300; @@ -103,7 +103,7 @@ L30: /* calculate the jacobian matrix. */ - iflag = ei_fdjac1(Functor::f, x, fvec, fjac, + iflag = ei_fdjac1(x, fvec, fjac, nb_of_subdiagonals, nb_of_superdiagonals, epsfcn, wa1, wa2); nfev += msum; if (iflag < 0) { @@ -215,7 +215,7 @@ L180: } iflag = 0; if ((iter - 1) % nprint == 0) { - iflag = Functor::f(n, x.data(), fvec.data(), 0); + iflag = Functor::f(x, fvec, 0); } if (iflag < 0) { goto L300; @@ -244,7 +244,7 @@ L190: /* evaluate the function at x + p and calculate its norm. */ - iflag = Functor::f(n, wa2.data(), wa4.data(), 1); + iflag = Functor::f(wa2, wa4, 0); ++nfev; if (iflag < 0) { goto L300; @@ -404,7 +404,7 @@ L300: info = iflag; } if (nprint > 0) { - Functor::f(n, x.data(), fvec.data(), 0); + iflag = Functor::f(x, fvec, 0); } return info; diff --git a/unsupported/Eigen/src/NonLinear/lmdif.h b/unsupported/Eigen/src/NonLinear/lmdif.h index ae50edd58..afcc57fdf 100644 --- a/unsupported/Eigen/src/NonLinear/lmdif.h +++ b/unsupported/Eigen/src/NonLinear/lmdif.h @@ -66,7 +66,7 @@ L20: /* evaluate the function at the starting point */ /* and calculate its norm. */ - iflag = Functor::f(m, n, x.data(), fvec.data(), 1); + iflag = Functor::f(x, fvec, 1); nfev = 1; if (iflag < 0) { goto L300; @@ -84,7 +84,7 @@ L30: /* calculate the jacobian matrix. */ - iflag = ei_fdjac2(Functor::f, x, fvec, fjac, epsfcn, wa4); + iflag = ei_fdjac2(x, fvec, fjac, epsfcn, wa4); nfev += n; if (iflag < 0) { goto L300; @@ -97,7 +97,7 @@ L30: } iflag = 0; if ((iter - 1) % nprint == 0) { - iflag = Functor::f(m, n, x.data(), fvec.data(), 0); + iflag = Functor::f(x, fvec, 0); } if (iflag < 0) { goto L300; @@ -239,7 +239,7 @@ L200: /* evaluate the function at x + p and calculate its norm. */ - iflag = Functor::f(m, n, wa2.data(), wa4.data(), 1); + iflag = Functor::f(wa2, wa4, 1); ++nfev; if (iflag < 0) { goto L300; @@ -380,7 +380,7 @@ L300: } iflag = 0; if (nprint > 0) { - iflag = Functor::f(m, n, x.data(), fvec.data(), 0); + iflag = Functor::f(x, fvec, 0); } return info; diff --git a/unsupported/test/NonLinear.cpp b/unsupported/test/NonLinear.cpp index 31b44e5e8..73d07b0bb 100644 --- a/unsupported/test/NonLinear.cpp +++ b/unsupported/test/NonLinear.cpp @@ -322,12 +322,15 @@ void testHybrj() } struct hybrd_functor { - static int f(int n, const double *x, double *fvec, int /*iflag*/) + static int f(const VectorXd &x, VectorXd &fvec, int /*iflag*/) { /* subroutine fcn for hybrd1 example. */ int k; double one=1, temp, temp1, temp2, three=3, two=2, zero=0; + const int n = x.size(); + + assert(fvec.size()==n); for (k=0; k < n; k++) { @@ -495,7 +498,7 @@ void testLmstr() } struct lmdif_functor { - static int f(int /*m*/, int /*n*/, const double *x, double *fvec, int /*iflag*/) + static int f(const VectorXd &x, VectorXd &fvec, int /*iflag*/) { /* function fcn for lmdif1 example */ @@ -504,6 +507,8 @@ struct lmdif_functor { double y[15]={1.4e-1,1.8e-1,2.2e-1,2.5e-1,2.9e-1,3.2e-1,3.5e-1,3.9e-1, 3.7e-1,5.8e-1,7.3e-1,9.6e-1,1.34e0,2.1e0,4.39e0}; + assert(x.size()==3); + assert(fvec.size()==15); for (i=0; i<15; i++) { tmp1 = i+1;