wrapper for hybrj1

This commit is contained in:
Thomas Capricelli 2009-08-10 16:32:45 +02:00
parent 1d53ce8d48
commit 4a26baa718
2 changed files with 74 additions and 64 deletions

View File

@ -89,6 +89,26 @@ int ei_hybrd(
); );
} }
template<typename Functor, typename Scalar>
int ei_hybrj1(
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x,
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &fvec,
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &fjac,
Scalar tol = Eigen::ei_sqrt(Eigen::machine_epsilon<Scalar>())
)
{
int n = x.size();
int lwa = (n*(3*n+13))/2;
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > wa(lwa);
int ldfjac = n;
fvec.resize(n);
fjac.resize(ldfjac, n);
return hybrj1(Functor::f, 0, n, x.data(), fvec.data(), fjac.data(), ldfjac, tol, wa.data(), lwa);
}
template<typename Functor, typename Scalar> template<typename Functor, typename Scalar>
int ei_hybrj( int ei_hybrj(
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x, Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x,

View File

@ -274,7 +274,8 @@ void testLmder()
// VERIFY_IS_APPROX( covfac*fjac.corner<n,n>(TopLeft) , cov_ref); // VERIFY_IS_APPROX( covfac*fjac.corner<n,n>(TopLeft) , cov_ref);
} }
int fcn_hybrj1(void * /*p*/, int n, const double *x, double *fvec, double *fjac, int ldfjac, struct hybrj1_functor {
static int f(void * /*p*/, int n, const double *x, double *fvec, double *fjac, int ldfjac,
int iflag) int iflag)
{ {
/* subroutine fcn for hybrj1 example. */ /* subroutine fcn for hybrj1 example. */
@ -309,47 +310,38 @@ int fcn_hybrj1(void * /*p*/, int n, const double *x, double *fvec, double *fjac,
} }
return 0; return 0;
} }
};
void testHybrj1() void testHybrj1()
{ {
int j, n, ldfjac, info, lwa; const int n=9;
double tol, fnorm; int info;
double x[9], fvec[9], fjac[9*9], wa[99]; Eigen::VectorXd x(n), fvec, diag(n);
Eigen::MatrixXd fjac;
n = 9; /* the following starting values provide a rough fit. */
x.setConstant(n, -1.);
/* the following starting values provide a rough solution. */ // do the computation
info = ei_hybrj1<hybrj1_functor, double>(x,fvec, fjac);
for (j=1; j<=9; j++) // check return value
{ VERIFY( 1 == info);
x[j-1] = -1.;
}
ldfjac = 9; // check norm
lwa = 99; VERIFY_IS_APPROX(fvec.norm(), 1.192636e-08);
/* set tol to the square root of the machine precision. */
/* unless high solutions are required, */
/* this is the recommended setting. */
tol = sqrt(dpmpar(1)); // check x
VectorXd x_ref(n);
info = hybrj1(fcn_hybrj1, 0, n, x, fvec, fjac, ldfjac, tol, wa, lwa); x_ref <<
fnorm = enorm(n, fvec);
VERIFY_IS_APPROX(fnorm, 1.192636e-08);
VERIFY(info==1);
double x_ref[] = {
-0.5706545, -0.6816283, -0.7017325, -0.5706545, -0.6816283, -0.7017325,
-0.7042129, -0.701369, -0.6918656, -0.7042129, -0.701369, -0.6918656,
-0.665792, -0.5960342, -0.4164121 -0.665792, -0.5960342, -0.4164121;
}; VERIFY_IS_APPROX(x, x_ref);
for (j=1; j<=n; j++) VERIFY_IS_APPROX(x[j-1], x_ref[j-1]);
} }
struct hybrj_functor { struct hybrj_functor {
static int f(void * /*p*/, int n, const double *x, double *fvec, double *fjac, int ldfjac, static int f(void * /*p*/, int n, const double *x, double *fvec, double *fjac, int ldfjac,
int iflag) int iflag)
@ -401,7 +393,6 @@ void testHybrj()
int info, nfev, njev, mode; int info, nfev, njev, mode;
Eigen::VectorXd x(n), fvec, diag(n), R, qtf; Eigen::VectorXd x(n), fvec, diag(n), R, qtf;
Eigen::MatrixXd fjac; Eigen::MatrixXd fjac;
VectorXi ipvt;
/* the following starting values provide a rough fit. */ /* the following starting values provide a rough fit. */
x.setConstant(n, -1.); x.setConstant(n, -1.);
@ -508,7 +499,6 @@ void testHybrd()
int info, nfev, ml, mu, mode; int info, nfev, ml, mu, mode;
Eigen::VectorXd x(n), fvec, diag(n), R, qtf; Eigen::VectorXd x(n), fvec, diag(n), R, qtf;
Eigen::MatrixXd fjac; Eigen::MatrixXd fjac;
VectorXi ipvt;
/* the following starting values provide a rough fit. */ /* the following starting values provide a rough fit. */
x.setConstant(n, -1.); x.setConstant(n, -1.);