eigenize the test for lmder1, clean functor stuff.

(and check the tests still pass, of course, that's the whole point..)
This commit is contained in:
Thomas Capricelli 2009-08-09 03:54:36 +02:00
parent 5e4cf6cae1
commit a6625f22d4
2 changed files with 94 additions and 89 deletions

View File

@ -28,7 +28,6 @@
#include <cminpack.h> #include <cminpack.h>
template<typename Functor, typename Scalar> template<typename Functor, typename Scalar>
// TODO : fixe Scalar here
int ei_hybrd1( int ei_hybrd1(
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x, Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x,
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &fvec, Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &fvec,
@ -41,5 +40,27 @@ int ei_hybrd1(
return hybrd1(Functor::f, 0, x.size(), x.data(), fvec.data(), tol, wa.data(), lwa); return hybrd1(Functor::f, 0, x.size(), x.data(), fvec.data(), tol, wa.data(), lwa);
} }
template<typename Functor, typename Scalar>
int ei_lmder1(
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x,
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &fvec,
Scalar tol = Eigen::ei_sqrt(Eigen::machine_epsilon<Scalar>())
)
{
int lwa = 5*x.size()+fvec.size();
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > wa(lwa);
VectorXi ipvt(x.size());
int ldfjac = fvec.size();
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > fjac(ldfjac, x.size());
return lmder1 (
Functor::f, 0,
fvec.size(), x.size(), x.data(), fvec.data(),
fjac.data() , ldfjac,
tol,
ipvt.data(),
wa.data(), lwa
);
}
#endif // EIGEN_NONLINEAR_MATHFUNCTIONS_H #endif // EIGEN_NONLINEAR_MATHFUNCTIONS_H

View File

@ -112,8 +112,8 @@ void testChkder()
} }
int fcn_lmder1(void * /*p*/, int /*m*/, int /*n*/, const double *x, double *fvec, double *fjac, struct lmder1_functor {
int ldfjac, int iflag) static int f(void * /*p*/, int /*m*/, int /*n*/, const double *x, double *fvec, double *fjac, int ldfjac, int iflag)
{ {
/* subroutine fcn for lmder1 example. */ /* subroutine fcn for lmder1 example. */
@ -150,43 +150,34 @@ int fcn_lmder1(void * /*p*/, int /*m*/, int /*n*/, const double *x, double *fvec
} }
return 0; return 0;
} }
};
void testLmder1() void testLmder1()
{ {
int j, m, n, ldfjac, info, lwa; int m=15, n=3, info;
int ipvt[3];
double tol, fnorm;
double x[3], fvec[15], fjac[15*3], wa[30];
m = 15; Eigen::VectorXd x(n), fvec(m);
n = 3; Eigen::MatrixXd fjac(m, n);
/* the following starting values provide a rough fit. */ /* the following starting values provide a rough fit. */
x.setConstant(n, 1.);
x[1-1] = 1.; // do the computation
x[2-1] = 1.; info = ei_lmder1<lmder1_functor,double>(x, fvec);
x[3-1] = 1.;
ldfjac = 15; // check return value
lwa = 30; VERIFY( 1 == info);
/* set tol to the square root of the machine precision. */ // check norm
/* unless high solutions are required, */ VERIFY_IS_APPROX(fvec.norm(), 0.09063596);
/* this is the recommended setting. */
tol = sqrt(dpmpar(1)); // check x
VectorXd x_ref(n);
info = lmder1(fcn_lmder1, 0, m, n, x, fvec, fjac, ldfjac, tol, x_ref << 0.08241058, 1.133037, 2.343695;
ipvt, wa, lwa); VERIFY_IS_APPROX(x, x_ref);
fnorm = enorm(m, fvec);
VERIFY_IS_APPROX(fnorm, 0.09063596);
VERIFY(info == 1);
double x_ref[] = {0.08241058, 1.133037, 2.343695 };
for (j=1; j<=n; j++) VERIFY_IS_APPROX(x[j-1], x_ref[j-1]);
} }
int fcn_lmder(void * /*p*/, int /*m*/, int /*n*/, const double *x, double *fvec, double *fjac, int fcn_lmder(void * /*p*/, int /*m*/, int /*n*/, const double *x, double *fvec, double *fjac,
int ldfjac, int iflag) int ldfjac, int iflag)
{ {
@ -464,7 +455,8 @@ void testHybrj()
for (j=1; j<=n; j++) VERIFY_IS_APPROX(x[j-1], x_ref[j-1]); for (j=1; j<=n; j++) VERIFY_IS_APPROX(x[j-1], x_ref[j-1]);
} }
int fcn_hybrd1(void * /*p*/, int n, const double *x, double *fvec, int /*iflag*/) struct hybrd1_functor {
static int f(void * /*p*/, int n, const double *x, double *fvec, int /*iflag*/)
{ {
/* subroutine fcn for hybrd1 example. */ /* subroutine fcn for hybrd1 example. */
@ -482,11 +474,6 @@ int fcn_hybrd1(void * /*p*/, int n, const double *x, double *fvec, int /*iflag*/
} }
return 0; return 0;
} }
struct myfunctor {
static int f(void *p, int n, const double *x, double *fvec, int iflag )
{ return fcn_hybrd1(p,n,x,fvec,iflag) ; }
}; };
void testHybrd1() void testHybrd1()
@ -497,11 +484,8 @@ void testHybrd1()
/* the following starting values provide a rough solution. */ /* the following starting values provide a rough solution. */
x.setConstant(n, -1.); x.setConstant(n, -1.);
/* set tol to the square root of the machine precision. */ // do the computation
/* unless high solutions are required, */ info = ei_hybrd1<hybrd1_functor,double>(x, fvec);
/* this is the recommended setting. */
info = ei_hybrd1<myfunctor,double>(x, fvec);
// check return value // check return value
VERIFY( 1 == info); VERIFY( 1 == info);