* use eigen object for callbacks for hybrd and lmdif

* use Functor instead of argument for ei_fdjac*()
This commit is contained in:
Thomas Capricelli 2009-08-23 04:06:16 +02:00
parent f2fcbb0207
commit 878f15b8a5
6 changed files with 24 additions and 23 deletions

View File

@ -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/qrsolv.h"
#include "src/NonLinear/r1updt.h" #include "src/NonLinear/r1updt.h"
#include "src/NonLinear/r1mpyq.h" #include "src/NonLinear/r1mpyq.h"

View File

@ -1,6 +1,6 @@
template <typename Scalar> template<typename Functor, typename Scalar>
int ei_fdjac1(minpack_func_nn fcn, int ei_fdjac1(
Matrix< Scalar, Dynamic, 1 > &x, Matrix< Scalar, Dynamic, 1 > &x,
Matrix< Scalar, Dynamic, 1 > &fvec, Matrix< Scalar, Dynamic, 1 > &fvec,
Matrix< Scalar, Dynamic, Dynamic > &fjac, Matrix< Scalar, Dynamic, Dynamic > &fjac,
@ -34,7 +34,7 @@ int ei_fdjac1(minpack_func_nn fcn,
if (h == 0.) if (h == 0.)
h = eps; h = eps;
x[j] = temp + h; x[j] = temp + h;
iflag = (*fcn)(n, x.data(), wa1.data(), 1); iflag = Functor::f(x, wa1, 1);
if (iflag < 0) if (iflag < 0)
goto L30; goto L30;
x[j] = temp; x[j] = temp;
@ -59,7 +59,7 @@ L40:
x[j] = wa2[j] + h; x[j] = wa2[j] + h;
/* L60: */ /* L60: */
} }
iflag = (*fcn)(n, x.data(), wa1.data(), 1); iflag = Functor::f(x, wa1, 1);
if (iflag < 0) { if (iflag < 0) {
/* goto L100; */ /* goto L100; */
return iflag; return iflag;

View File

@ -1,6 +1,6 @@
template <typename Scalar> template<typename Functor, typename Scalar>
int ei_fdjac2(minpack_func_mn fcn, int ei_fdjac2(
Matrix< Scalar, Dynamic, 1 > &x, Matrix< Scalar, Dynamic, 1 > &x,
Matrix< Scalar, Dynamic, 1 > &fvec, Matrix< Scalar, Dynamic, 1 > &fvec,
Matrix< Scalar, Dynamic, Dynamic > &fjac, Matrix< Scalar, Dynamic, Dynamic > &fjac,
@ -26,7 +26,7 @@ int ei_fdjac2(minpack_func_mn fcn,
h = eps; h = eps;
} }
x[j] = temp + h; x[j] = temp + h;
iflag = (*fcn)(m, n, x.data(), wa.data(), 1); iflag = Functor::f(x, wa, 1);
if (iflag < 0) { if (iflag < 0) {
/* goto L30; */ /* goto L30; */
return iflag; return iflag;

View File

@ -75,7 +75,7 @@ L20:
/* evaluate the function at the starting point */ /* evaluate the function at the starting point */
/* and calculate its norm. */ /* and calculate its norm. */
iflag = Functor::f(n, x.data(), fvec.data(), 1); iflag = Functor::f(x, fvec, 1);
nfev = 1; nfev = 1;
if (iflag < 0) { if (iflag < 0) {
goto L300; goto L300;
@ -103,7 +103,7 @@ L30:
/* calculate the jacobian matrix. */ /* calculate the jacobian matrix. */
iflag = ei_fdjac1<Scalar>(Functor::f, x, fvec, fjac, iflag = ei_fdjac1<Functor,Scalar>(x, fvec, fjac,
nb_of_subdiagonals, nb_of_superdiagonals, epsfcn, wa1, wa2); nb_of_subdiagonals, nb_of_superdiagonals, epsfcn, wa1, wa2);
nfev += msum; nfev += msum;
if (iflag < 0) { if (iflag < 0) {
@ -215,7 +215,7 @@ L180:
} }
iflag = 0; iflag = 0;
if ((iter - 1) % nprint == 0) { if ((iter - 1) % nprint == 0) {
iflag = Functor::f(n, x.data(), fvec.data(), 0); iflag = Functor::f(x, fvec, 0);
} }
if (iflag < 0) { if (iflag < 0) {
goto L300; goto L300;
@ -244,7 +244,7 @@ L190:
/* evaluate the function at x + p and calculate its norm. */ /* 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; ++nfev;
if (iflag < 0) { if (iflag < 0) {
goto L300; goto L300;
@ -404,7 +404,7 @@ L300:
info = iflag; info = iflag;
} }
if (nprint > 0) { if (nprint > 0) {
Functor::f(n, x.data(), fvec.data(), 0); iflag = Functor::f(x, fvec, 0);
} }
return info; return info;

View File

@ -66,7 +66,7 @@ L20:
/* evaluate the function at the starting point */ /* evaluate the function at the starting point */
/* and calculate its norm. */ /* and calculate its norm. */
iflag = Functor::f(m, n, x.data(), fvec.data(), 1); iflag = Functor::f(x, fvec, 1);
nfev = 1; nfev = 1;
if (iflag < 0) { if (iflag < 0) {
goto L300; goto L300;
@ -84,7 +84,7 @@ L30:
/* calculate the jacobian matrix. */ /* calculate the jacobian matrix. */
iflag = ei_fdjac2<Scalar>(Functor::f, x, fvec, fjac, epsfcn, wa4); iflag = ei_fdjac2<Functor,Scalar>(x, fvec, fjac, epsfcn, wa4);
nfev += n; nfev += n;
if (iflag < 0) { if (iflag < 0) {
goto L300; goto L300;
@ -97,7 +97,7 @@ L30:
} }
iflag = 0; iflag = 0;
if ((iter - 1) % nprint == 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) { if (iflag < 0) {
goto L300; goto L300;
@ -239,7 +239,7 @@ L200:
/* evaluate the function at x + p and calculate its norm. */ /* 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; ++nfev;
if (iflag < 0) { if (iflag < 0) {
goto L300; goto L300;
@ -380,7 +380,7 @@ L300:
} }
iflag = 0; iflag = 0;
if (nprint > 0) { if (nprint > 0) {
iflag = Functor::f(m, n, x.data(), fvec.data(), 0); iflag = Functor::f(x, fvec, 0);
} }
return info; return info;

View File

@ -322,12 +322,15 @@ void testHybrj()
} }
struct hybrd_functor { 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. */ /* subroutine fcn for hybrd1 example. */
int k; int k;
double one=1, temp, temp1, temp2, three=3, two=2, zero=0; 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++) for (k=0; k < n; k++)
{ {
@ -495,7 +498,7 @@ void testLmstr()
} }
struct lmdif_functor { 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 */ /* 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, 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}; 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++) for (i=0; i<15; i++)
{ {
tmp1 = i+1; tmp1 = i+1;