reduce local variables so that we can split algorithms

This commit is contained in:
Thomas Capricelli 2009-08-25 22:49:05 +02:00
parent be368c33bb
commit baec4f39ab
2 changed files with 61 additions and 87 deletions

View File

@ -59,6 +59,21 @@ public:
int njev; int njev;
private: private:
const FunctorType &functor; const FunctorType &functor;
int n;
Scalar sum;
bool sing;
int iter;
Scalar temp;
Scalar delta;
bool jeval;
int ncsuc;
Scalar ratio;
Scalar fnorm;
Scalar pnorm, xnorm, fnorm1;
int nslow1, nslow2;
int ncfail;
Scalar actred, prered;
Matrix< Scalar, Dynamic, 1 > wa1, wa2, wa3, wa4;
}; };
@ -70,7 +85,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solve(
const Scalar tol const Scalar tol
) )
{ {
const int n = x.size(); n = x.size();
Parameters parameters; Parameters parameters;
/* check the input parameters for errors. */ /* check the input parameters for errors. */
@ -99,9 +114,9 @@ HybridNonLinearSolver<FunctorType,Scalar>::solve(
const int mode const int mode
) )
{ {
const int n = x.size(); n = x.size();
Matrix< Scalar, Dynamic, 1 > wa1(n), wa2(n), wa3(n), wa4(n);
wa1.resize(n); wa2.resize(n); wa3.resize(n); wa4.resize(n);
fvec.resize(n); fvec.resize(n);
qtf.resize(n); qtf.resize(n);
R.resize( (n*(n+1))/2); R.resize( (n*(n+1))/2);
@ -111,22 +126,6 @@ HybridNonLinearSolver<FunctorType,Scalar>::solve(
diag.resize(n); diag.resize(n);
assert( (mode!=2 || diag.size()==n) || "When using mode==2, the caller must provide a valid 'diag'"); assert( (mode!=2 || diag.size()==n) || "When using mode==2, the caller must provide a valid 'diag'");
/* Local variables */
int i, j, l, iwa[1];
Scalar sum;
bool sing;
int iter;
Scalar temp;
Scalar delta;
bool jeval;
int ncsuc;
Scalar ratio;
Scalar fnorm;
Scalar pnorm, xnorm, fnorm1;
int nslow1, nslow2;
int ncfail;
Scalar actred, prered;
/* Function Body */ /* Function Body */
nfev = 0; nfev = 0;
njev = 0; njev = 0;
@ -136,7 +135,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solve(
if (n <= 0 || parameters.xtol < 0. || parameters.maxfev <= 0 || parameters.factor <= 0. ) if (n <= 0 || parameters.xtol < 0. || parameters.maxfev <= 0 || parameters.factor <= 0. )
return ImproperInputParameters; return ImproperInputParameters;
if (mode == 2) if (mode == 2)
for (j = 0; j < n; ++j) for (int j = 0; j < n; ++j)
if (diag[j] <= 0.) if (diag[j] <= 0.)
return ImproperInputParameters; return ImproperInputParameters;
@ -159,6 +158,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solve(
/* beginning of the outer loop. */ /* beginning of the outer loop. */
while (true) { while (true) {
int i, j, l, iwa[1];
jeval = true; jeval = true;
/* calculate the jacobian matrix. */ /* calculate the jacobian matrix. */
@ -380,7 +380,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiff(
const Scalar tol const Scalar tol
) )
{ {
const int n = x.size(); n = x.size();
Parameters parameters; Parameters parameters;
/* check the input parameters for errors. */ /* check the input parameters for errors. */
@ -412,12 +412,12 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiff(
const Scalar epsfcn const Scalar epsfcn
) )
{ {
const int n = x.size(); n = x.size();
Matrix< Scalar, Dynamic, 1 > wa1(n), wa2(n), wa3(n), wa4(n);
if (nb_of_subdiagonals<0) nb_of_subdiagonals = n-1; if (nb_of_subdiagonals<0) nb_of_subdiagonals = n-1;
if (nb_of_superdiagonals<0) nb_of_superdiagonals = n-1; if (nb_of_superdiagonals<0) nb_of_superdiagonals = n-1;
wa1.resize(n); wa2.resize(n); wa3.resize(n); wa4.resize(n);
qtf.resize(n); qtf.resize(n);
R.resize( (n*(n+1))/2); R.resize( (n*(n+1))/2);
fjac.resize(n, n); fjac.resize(n, n);
@ -426,22 +426,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiff(
diag.resize(n); diag.resize(n);
assert( (mode!=2 || diag.size()==n) || "When using mode==2, the caller must provide a valid 'diag'"); assert( (mode!=2 || diag.size()==n) || "When using mode==2, the caller must provide a valid 'diag'");
/* Local variables */
int i, j, l, iwa[1];
Scalar sum;
bool sing;
int iter;
Scalar temp;
int msum; int msum;
Scalar delta;
bool jeval;
int ncsuc;
Scalar ratio;
Scalar fnorm;
Scalar pnorm, xnorm, fnorm1;
int nslow1, nslow2;
int ncfail;
Scalar actred, prered;
/* Function Body */ /* Function Body */
@ -452,7 +437,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiff(
if (n <= 0 || parameters.xtol < 0. || parameters.maxfev <= 0 || nb_of_subdiagonals < 0 || nb_of_superdiagonals < 0 || parameters.factor <= 0. ) if (n <= 0 || parameters.xtol < 0. || parameters.maxfev <= 0 || nb_of_subdiagonals < 0 || nb_of_superdiagonals < 0 || parameters.factor <= 0. )
return ImproperInputParameters; return ImproperInputParameters;
if (mode == 2) if (mode == 2)
for (j = 0; j < n; ++j) for (int j = 0; j < n; ++j)
if (diag[j] <= 0.) if (diag[j] <= 0.)
return ImproperInputParameters; return ImproperInputParameters;
@ -481,6 +466,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiff(
/* beginning of the outer loop. */ /* beginning of the outer loop. */
while (true) { while (true) {
int i, j, l, iwa[1];
jeval = true; jeval = true;
/* calculate the jacobian matrix. */ /* calculate the jacobian matrix. */

View File

@ -77,6 +77,17 @@ public:
int njev; int njev;
private: private:
const FunctorType &functor; const FunctorType &functor;
int n;
int m;
Matrix< Scalar, Dynamic, 1 > wa1, wa2, wa3, wa4;
Scalar par, sum;
int iter;
Scalar temp, temp1, temp2;
Scalar delta;
Scalar ratio;
Scalar fnorm, gnorm;
Scalar pnorm, xnorm, fnorm1, actred, dirder, prered;
}; };
template<typename FunctorType, typename Scalar> template<typename FunctorType, typename Scalar>
@ -86,8 +97,8 @@ LevenbergMarquardt<FunctorType,Scalar>::minimize(
const Scalar tol const Scalar tol
) )
{ {
const int n = x.size(); n = x.size();
const int m = functor.nbOfFunctions(); m = functor.nbOfFunctions();
Parameters parameters; Parameters parameters;
/* check the input parameters for errors. */ /* check the input parameters for errors. */
@ -115,10 +126,11 @@ LevenbergMarquardt<FunctorType,Scalar>::minimize(
const int mode const int mode
) )
{ {
const int n = x.size(); n = x.size();
const int m = functor.nbOfFunctions(); m = functor.nbOfFunctions();
Matrix< Scalar, Dynamic, 1 > wa1(n), wa2(n), wa3(n), wa4;
wa1.resize(n); wa2.resize(n); wa3.resize(n);
wa4.resize(m);
fvec.resize(m); fvec.resize(m);
ipvt.resize(n); ipvt.resize(n);
fjac.resize(m, n); fjac.resize(m, n);
@ -127,16 +139,6 @@ LevenbergMarquardt<FunctorType,Scalar>::minimize(
assert( (mode!=2 || diag.size()==n) || "When using mode==2, the caller must provide a valid 'diag'"); assert( (mode!=2 || diag.size()==n) || "When using mode==2, the caller must provide a valid 'diag'");
qtf.resize(n); qtf.resize(n);
/* Local variables */
int i, j, l;
Scalar par, sum;
int iter;
Scalar temp, temp1, temp2;
Scalar delta;
Scalar ratio;
Scalar fnorm, gnorm;
Scalar pnorm, xnorm, fnorm1, actred, dirder, prered;
/* Function Body */ /* Function Body */
nfev = 0; nfev = 0;
njev = 0; njev = 0;
@ -147,7 +149,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimize(
return ImproperInputParameters; return ImproperInputParameters;
if (mode == 2) if (mode == 2)
for (j = 0; j < n; ++j) for (int j = 0; j < n; ++j)
if (diag[j] <= 0.) if (diag[j] <= 0.)
return ImproperInputParameters; return ImproperInputParameters;
@ -167,6 +169,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimize(
/* beginning of the outer loop. */ /* beginning of the outer loop. */
while (true) { while (true) {
int i, j, l;
/* calculate the jacobian matrix. */ /* calculate the jacobian matrix. */
@ -349,7 +352,6 @@ LevenbergMarquardt<FunctorType,Scalar>::minimize(
} while (ratio < Scalar(1e-4)); } while (ratio < Scalar(1e-4));
/* end of the outer loop. */ /* end of the outer loop. */
} }
assert(false); // should never be reached
} }
@ -360,8 +362,8 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeNumericalDiff(
const Scalar tol const Scalar tol
) )
{ {
const int n = x.size(); n = x.size();
const int m = functor.nbOfFunctions(); m = functor.nbOfFunctions();
Parameters parameters; Parameters parameters;
/* check the input parameters for errors. */ /* check the input parameters for errors. */
@ -389,10 +391,11 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeNumericalDiff(
const Scalar epsfcn const Scalar epsfcn
) )
{ {
const int n = x.size(); n = x.size();
const int m = functor.nbOfFunctions(); m = functor.nbOfFunctions();
Matrix< Scalar, Dynamic, 1 > wa1(n), wa2(n), wa3(n), wa4(m);
wa1.resize(n); wa2.resize(n); wa3.resize(n);
wa4.resize(m);
fvec.resize(m); fvec.resize(m);
ipvt.resize(n); ipvt.resize(n);
fjac.resize(m, n); fjac.resize(m, n);
@ -401,16 +404,6 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeNumericalDiff(
assert( (mode!=2 || diag.size()==n) || "When using mode==2, the caller must provide a valid 'diag'"); assert( (mode!=2 || diag.size()==n) || "When using mode==2, the caller must provide a valid 'diag'");
qtf.resize(n); qtf.resize(n);
/* Local variables */
int i, j, l;
Scalar par, sum;
int iter;
Scalar temp, temp1, temp2;
Scalar delta;
Scalar ratio;
Scalar fnorm, gnorm;
Scalar pnorm, xnorm, fnorm1, actred, dirder, prered;
/* Function Body */ /* Function Body */
nfev = 0; nfev = 0;
@ -419,7 +412,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeNumericalDiff(
if (n <= 0 || m < n || parameters.ftol < 0. || parameters.xtol < 0. || parameters.gtol < 0. || parameters.maxfev <= 0 || parameters.factor <= 0.) if (n <= 0 || m < n || parameters.ftol < 0. || parameters.xtol < 0. || parameters.gtol < 0. || parameters.maxfev <= 0 || parameters.factor <= 0.)
return ImproperInputParameters; return ImproperInputParameters;
if (mode == 2) if (mode == 2)
for (j = 0; j < n; ++j) for (int j = 0; j < n; ++j)
if (diag[j] <= 0.) if (diag[j] <= 0.)
return ImproperInputParameters; return ImproperInputParameters;
@ -439,6 +432,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeNumericalDiff(
/* beginning of the outer loop. */ /* beginning of the outer loop. */
while (true) { while (true) {
int i, j, l;
/* calculate the jacobian matrix. */ /* calculate the jacobian matrix. */
@ -633,8 +627,8 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeOptimumStorage(
const Scalar tol const Scalar tol
) )
{ {
const int n = x.size(); n = x.size();
const int m = functor.nbOfFunctions(); m = functor.nbOfFunctions();
Matrix< Scalar, Dynamic, Dynamic > fjac(m, n); Matrix< Scalar, Dynamic, Dynamic > fjac(m, n);
VectorXi ipvt; VectorXi ipvt;
Parameters parameters; Parameters parameters;
@ -663,10 +657,11 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeOptimumStorage(
const int mode const int mode
) )
{ {
const int n = x.size(); n = x.size();
const int m = functor.nbOfFunctions(); m = functor.nbOfFunctions();
Matrix< Scalar, Dynamic, 1 > wa1(n), wa2(n), wa3(n), wa4(m);
wa1.resize(n); wa2.resize(n); wa3.resize(n);
wa4.resize(m);
fvec.resize(m); fvec.resize(m);
ipvt.resize(n); ipvt.resize(n);
fjac.resize(m, n); fjac.resize(m, n);
@ -676,15 +671,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeOptimumStorage(
qtf.resize(n); qtf.resize(n);
/* Local variables */ /* Local variables */
int i, j, l;
Scalar par, sum;
int iter;
bool sing; bool sing;
Scalar temp, temp1, temp2;
Scalar delta;
Scalar ratio;
Scalar fnorm, gnorm;
Scalar pnorm, xnorm, fnorm1, actred, dirder, prered;
/* Function Body */ /* Function Body */
nfev = 0; nfev = 0;
@ -696,7 +683,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeOptimumStorage(
return ImproperInputParameters; return ImproperInputParameters;
if (mode == 2) if (mode == 2)
for (j = 0; j < n; ++j) for (int j = 0; j < n; ++j)
if (diag[j] <= 0.) if (diag[j] <= 0.)
return ImproperInputParameters; return ImproperInputParameters;
@ -716,6 +703,7 @@ LevenbergMarquardt<FunctorType,Scalar>::minimizeOptimumStorage(
/* beginning of the outer loop. */ /* beginning of the outer loop. */
while (true) { while (true) {
int i, j, l;
/* compute the qr factorization of the jacobian matrix */ /* compute the qr factorization of the jacobian matrix */
/* calculated one row at a time, while simultaneously */ /* calculated one row at a time, while simultaneously */