mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-06-04 18:54:00 +08:00
hybrd : wrapper + eigenize test
This commit is contained in:
parent
953c37f8e5
commit
ec2b9f90a3
@ -41,6 +41,55 @@ 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_hybrd(
|
||||||
|
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x,
|
||||||
|
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &fvec,
|
||||||
|
int &nfev,
|
||||||
|
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 nb_of_subdiagonals = -1,
|
||||||
|
int nb_of_superdiagonals = -1,
|
||||||
|
int maxfev = 2000,
|
||||||
|
Scalar factor = Scalar(100.),
|
||||||
|
Scalar xtol = Eigen::ei_sqrt(Eigen::machine_epsilon<Scalar>()),
|
||||||
|
Scalar epsfcn = Scalar(0.),
|
||||||
|
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);
|
||||||
|
|
||||||
|
|
||||||
|
if (nb_of_subdiagonals<0) nb_of_subdiagonals = n-1;
|
||||||
|
if (nb_of_superdiagonals<0) nb_of_superdiagonals = n-1;
|
||||||
|
fvec.resize(n);
|
||||||
|
qtf.resize(n);
|
||||||
|
R.resize(lr);
|
||||||
|
int ldfjac = n;
|
||||||
|
fjac.resize(ldfjac, n);
|
||||||
|
return hybrd(
|
||||||
|
Functor::f, 0,
|
||||||
|
n, x.data(), fvec.data(),
|
||||||
|
xtol, maxfev,
|
||||||
|
nb_of_subdiagonals, nb_of_superdiagonals,
|
||||||
|
epsfcn,
|
||||||
|
diag.data(), mode,
|
||||||
|
factor,
|
||||||
|
nprint,
|
||||||
|
&nfev,
|
||||||
|
fjac.data(), ldfjac,
|
||||||
|
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(
|
||||||
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x,
|
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &x,
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
//
|
//
|
||||||
// Copyright (C) 2009 Thomas Capricelli <orzel@freehackers.org>
|
// Copyright (C) 2009 Thomas Capricelli <orzel@freehackers.org>
|
||||||
|
|
||||||
|
//#include <stdio.h>
|
||||||
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include <unsupported/Eigen/NonLinear>
|
#include <unsupported/Eigen/NonLinear>
|
||||||
@ -489,83 +490,66 @@ void testHybrd1()
|
|||||||
VERIFY_IS_APPROX(x, x_ref);
|
VERIFY_IS_APPROX(x, x_ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
int fcn_hybrd(void * /*p*/, int n, const double *x, double *fvec, int iflag)
|
struct hybrd_functor {
|
||||||
{
|
static int f(void * /*p*/, int n, const double *x, double *fvec, int iflag)
|
||||||
/* subroutine fcn for hybrd example. */
|
|
||||||
|
|
||||||
int k;
|
|
||||||
double one=1, temp, temp1, temp2, three=3, two=2, zero=0;
|
|
||||||
|
|
||||||
if (iflag == 0)
|
|
||||||
{
|
{
|
||||||
/* insert print statements here when nprint is positive. */
|
/* subroutine fcn for hybrd example. */
|
||||||
return 0;
|
|
||||||
|
int k;
|
||||||
|
double one=1, temp, temp1, temp2, three=3, two=2, zero=0;
|
||||||
|
|
||||||
|
if (iflag == 0)
|
||||||
|
{
|
||||||
|
/* insert print statements here when nprint is positive. */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
for (k=1; k<=n; k++)
|
||||||
|
{
|
||||||
|
|
||||||
|
temp = (three - two*x[k-1])*x[k-1];
|
||||||
|
temp1 = zero;
|
||||||
|
if (k != 1) temp1 = x[k-1-1];
|
||||||
|
temp2 = zero;
|
||||||
|
if (k != n) temp2 = x[k+1-1];
|
||||||
|
fvec[k-1] = temp - temp1 - two*temp2 + one;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
for (k=1; k<=n; k++)
|
};
|
||||||
{
|
|
||||||
|
|
||||||
temp = (three - two*x[k-1])*x[k-1];
|
|
||||||
temp1 = zero;
|
|
||||||
if (k != 1) temp1 = x[k-1-1];
|
|
||||||
temp2 = zero;
|
|
||||||
if (k != n) temp2 = x[k+1-1];
|
|
||||||
fvec[k-1] = temp - temp1 - two*temp2 + one;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void testHybrd()
|
void testHybrd()
|
||||||
{
|
{
|
||||||
int j, n, maxfev, ml, mu, mode, nprint, info, nfev, ldfjac, lr;
|
const int n=9;
|
||||||
double xtol, epsfcn, factor, fnorm;
|
int info, nfev, ml, mu, mode;
|
||||||
double x[9], fvec[9], diag[9], fjac[9*9], r[45], qtf[9],
|
Eigen::VectorXd x(n), fvec, diag(n), R, qtf;
|
||||||
wa1[9], wa2[9], wa3[9], wa4[9];
|
Eigen::MatrixXd fjac;
|
||||||
|
VectorXi ipvt;
|
||||||
|
|
||||||
n = 9;
|
/* the following starting values provide a rough fit. */
|
||||||
|
x.setConstant(n, -1.);
|
||||||
|
|
||||||
/* 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 = 2000;
|
|
||||||
ml = 1;
|
ml = 1;
|
||||||
mu = 1;
|
mu = 1;
|
||||||
epsfcn = 0.;
|
|
||||||
mode = 2;
|
mode = 2;
|
||||||
for (j=1; j<=9; j++)
|
diag.setConstant(n, 1.);
|
||||||
{
|
|
||||||
diag[j-1] = 1.;
|
|
||||||
}
|
|
||||||
|
|
||||||
factor = 1.e2;
|
// do the computation
|
||||||
nprint = 0;
|
info = ei_hybrd<hybrd_functor, double>(x,fvec, nfev, fjac, R, qtf, diag, mode, ml, mu);
|
||||||
|
|
||||||
info = hybrd(fcn_hybrd, 0, n, x, fvec, xtol, maxfev, ml, mu, epsfcn,
|
// check return value
|
||||||
diag, mode, factor, nprint, &nfev,
|
VERIFY( 1 == info);
|
||||||
fjac, ldfjac, r, lr, qtf, wa1, wa2, wa3, wa4);
|
|
||||||
fnorm = enorm(n, fvec);
|
|
||||||
|
|
||||||
VERIFY_IS_APPROX(fnorm, 1.192636e-08);
|
|
||||||
VERIFY(nfev==14);
|
VERIFY(nfev==14);
|
||||||
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]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int fcn_lmstr1(void * /*p*/, int /*m*/, int /*n*/, const double *x, double *fvec, double *fjrow, int iflag)
|
int fcn_lmstr1(void * /*p*/, int /*m*/, int /*n*/, const double *x, double *fvec, double *fjrow, int iflag)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user