mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-09-22 14:23:18 +08:00
wrapper for hybrj
This commit is contained in:
parent
120235deef
commit
1d53ce8d48
@ -89,6 +89,47 @@ int ei_hybrd(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Functor, typename Scalar>
|
||||||
|
int ei_hybrj(
|
||||||
|
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x,
|
||||||
|
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &fvec,
|
||||||
|
int &nfev,
|
||||||
|
int &njev,
|
||||||
|
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &fjac,
|
||||||
|
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &R,
|
||||||
|
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &qtf,
|
||||||
|
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &diag,
|
||||||
|
int mode=1,
|
||||||
|
int maxfev = 1000,
|
||||||
|
Scalar factor = Scalar(100.),
|
||||||
|
Scalar xtol = Eigen::ei_sqrt(Eigen::machine_epsilon<Scalar>()),
|
||||||
|
int nprint=0
|
||||||
|
)
|
||||||
|
{
|
||||||
|
int n = x.size();
|
||||||
|
int lr = (n*(n+1))/2;
|
||||||
|
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > wa1(n), wa2(n), wa3(n), wa4(n);
|
||||||
|
|
||||||
|
fvec.resize(n);
|
||||||
|
qtf.resize(n);
|
||||||
|
R.resize(lr);
|
||||||
|
int ldfjac = n;
|
||||||
|
fjac.resize(ldfjac, n);
|
||||||
|
return hybrj (
|
||||||
|
Functor::f, 0,
|
||||||
|
n, x.data(), fvec.data(),
|
||||||
|
fjac.data(), ldfjac,
|
||||||
|
xtol, maxfev,
|
||||||
|
diag.data(), mode,
|
||||||
|
factor,
|
||||||
|
nprint,
|
||||||
|
&nfev,
|
||||||
|
&njev,
|
||||||
|
R.data(), lr,
|
||||||
|
qtf.data(),
|
||||||
|
wa1.data(), wa2.data(), wa3.data(), wa4.data()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Functor, typename Scalar>
|
template<typename Functor, typename Scalar>
|
||||||
int ei_lmder1(
|
int ei_lmder1(
|
||||||
|
@ -349,7 +349,9 @@ void testHybrj1()
|
|||||||
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_hybrj(void * /*p*/, int n, const double *x, double *fvec, double *fjac, int ldfjac,
|
|
||||||
|
struct hybrj_functor {
|
||||||
|
static int f(void * /*p*/, int n, const double *x, double *fvec, double *fjac, int ldfjac,
|
||||||
int iflag)
|
int iflag)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -391,58 +393,42 @@ int fcn_hybrj(void * /*p*/, int n, const double *x, double *fvec, double *fjac,
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void testHybrj()
|
void testHybrj()
|
||||||
{
|
{
|
||||||
|
const int n=9;
|
||||||
|
int info, nfev, njev, mode;
|
||||||
|
Eigen::VectorXd x(n), fvec, diag(n), R, qtf;
|
||||||
|
Eigen::MatrixXd fjac;
|
||||||
|
VectorXi ipvt;
|
||||||
|
|
||||||
int j, n, ldfjac, maxfev, mode, nprint, info, nfev, njev, lr;
|
/* the following starting values provide a rough fit. */
|
||||||
double xtol, factor, fnorm;
|
x.setConstant(n, -1.);
|
||||||
double x[9], fvec[9], fjac[9*9], diag[9], r[45], qtf[9],
|
|
||||||
wa1[9], wa2[9], wa3[9], wa4[9];
|
|
||||||
|
|
||||||
n = 9;
|
|
||||||
|
|
||||||
/* the following starting values provide a rough solution. */
|
|
||||||
|
|
||||||
for (j=1; j<=9; j++)
|
|
||||||
{
|
|
||||||
x[j-1] = -1.;
|
|
||||||
}
|
|
||||||
|
|
||||||
ldfjac = 9;
|
|
||||||
lr = 45;
|
|
||||||
|
|
||||||
/* set xtol to the square root of the machine precision. */
|
|
||||||
/* unless high solutions are required, */
|
|
||||||
/* this is the recommended setting. */
|
|
||||||
|
|
||||||
xtol = sqrt(dpmpar(1));
|
|
||||||
|
|
||||||
maxfev = 1000;
|
|
||||||
mode = 2;
|
mode = 2;
|
||||||
for (j=1; j<=9; j++)
|
diag.setConstant(n, 1.);
|
||||||
{
|
|
||||||
diag[j-1] = 1.;
|
|
||||||
}
|
|
||||||
factor = 1.e2;
|
|
||||||
nprint = 0;
|
|
||||||
|
|
||||||
info = hybrj(fcn_hybrj, 0, n, x, fvec, fjac, ldfjac, xtol, maxfev, diag,
|
// do the computation
|
||||||
mode, factor, nprint, &nfev, &njev, r, lr, qtf,
|
info = ei_hybrj<hybrj_functor, double>(x,fvec, nfev, njev, fjac, R, qtf, diag, mode);
|
||||||
wa1, wa2, wa3, wa4);
|
|
||||||
fnorm = enorm(n, fvec);
|
|
||||||
|
|
||||||
VERIFY_IS_APPROX(fnorm, 1.192636e-08);
|
// check return value
|
||||||
|
VERIFY( 1 == info);
|
||||||
VERIFY(nfev==11);
|
VERIFY(nfev==11);
|
||||||
VERIFY(njev==1);
|
VERIFY(njev==1);
|
||||||
VERIFY(info==1);
|
|
||||||
|
|
||||||
double x_ref[] = {
|
// check norm
|
||||||
|
VERIFY_IS_APPROX(fvec.norm(), 1.192636e-08);
|
||||||
|
|
||||||
|
|
||||||
|
// check x
|
||||||
|
VectorXd x_ref(n);
|
||||||
|
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 hybrd1_functor {
|
struct hybrd1_functor {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user