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>
template<typename Functor, typename Scalar>
// TODO : fixe Scalar here
int ei_hybrd1(
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x,
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);
}
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

View File

@ -112,11 +112,11 @@ void testChkder()
}
int fcn_lmder1(void * /*p*/, int /*m*/, int /*n*/, const double *x, double *fvec, double *fjac,
int ldfjac, int iflag)
{
struct lmder1_functor {
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. */
int i;
double tmp1, tmp2, tmp3, tmp4;
@ -149,44 +149,35 @@ int fcn_lmder1(void * /*p*/, int /*m*/, int /*n*/, const double *x, double *fvec
}
}
return 0;
}
}
};
void testLmder1()
{
int j, m, n, ldfjac, info, lwa;
int ipvt[3];
double tol, fnorm;
double x[3], fvec[15], fjac[15*3], wa[30];
int m=15, n=3, info;
m = 15;
n = 3;
Eigen::VectorXd x(n), fvec(m);
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.;
x[2-1] = 1.;
x[3-1] = 1.;
// do the computation
info = ei_lmder1<lmder1_functor,double>(x, fvec);
ldfjac = 15;
lwa = 30;
// check return value
VERIFY( 1 == info);
/* set tol to the square root of the machine precision. */
/* unless high solutions are required, */
/* this is the recommended setting. */
// check norm
VERIFY_IS_APPROX(fvec.norm(), 0.09063596);
tol = sqrt(dpmpar(1));
info = lmder1(fcn_lmder1, 0, m, n, x, fvec, fjac, ldfjac, tol,
ipvt, wa, lwa);
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]);
// check x
VectorXd x_ref(n);
x_ref << 0.08241058, 1.133037, 2.343695;
VERIFY_IS_APPROX(x, x_ref);
}
int fcn_lmder(void * /*p*/, int /*m*/, int /*n*/, const double *x, double *fvec, double *fjac,
int ldfjac, int iflag)
{
@ -464,9 +455,10 @@ void testHybrj()
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*/)
{
/* subroutine fcn for hybrd1 example. */
struct hybrd1_functor {
static int f(void * /*p*/, int n, const double *x, double *fvec, int /*iflag*/)
{
/* subroutine fcn for hybrd1 example. */
int k;
double one=1, temp, temp1, temp2, three=3, two=2, zero=0;
@ -481,12 +473,7 @@ int fcn_hybrd1(void * /*p*/, int n, const double *x, double *fvec, int /*iflag*/
fvec[k-1] = temp - temp1 - two*temp2 + one;
}
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()
@ -497,11 +484,8 @@ void testHybrd1()
/* the following starting values provide a rough solution. */
x.setConstant(n, -1.);
/* set tol to the square root of the machine precision. */
/* unless high solutions are required, */
/* this is the recommended setting. */
info = ei_hybrd1<myfunctor,double>(x, fvec);
// do the computation
info = ei_hybrd1<hybrd1_functor,double>(x, fvec);
// check return value
VERIFY( 1 == info);