mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
some more eigenization
This commit is contained in:
@@ -186,7 +186,6 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveInit(
|
||||
njev = 0;
|
||||
|
||||
/* check the input parameters for errors. */
|
||||
|
||||
if (n <= 0 || parameters.xtol < 0. || parameters.maxfev <= 0 || parameters.factor <= 0. )
|
||||
return ImproperInputParameters;
|
||||
if (mode == 2)
|
||||
@@ -196,14 +195,12 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveInit(
|
||||
|
||||
/* evaluate the function at the starting point */
|
||||
/* and calculate its norm. */
|
||||
|
||||
nfev = 1;
|
||||
if ( functor(x, fvec) < 0)
|
||||
return UserAksed;
|
||||
fnorm = fvec.stableNorm();
|
||||
|
||||
/* initialize iteration counter and monitors. */
|
||||
|
||||
iter = 1;
|
||||
ncsuc = 0;
|
||||
ncfail = 0;
|
||||
@@ -224,7 +221,6 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveOneStep(
|
||||
jeval = true;
|
||||
|
||||
/* calculate the jacobian matrix. */
|
||||
|
||||
if ( functor.df(x, fjac) < 0)
|
||||
return UserAksed;
|
||||
++njev;
|
||||
@@ -235,11 +231,8 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveOneStep(
|
||||
/* to the norms of the columns of the initial jacobian. */
|
||||
if (iter == 1) {
|
||||
if (mode != 2)
|
||||
for (j = 0; j < n; ++j) {
|
||||
diag[j] = wa2[j];
|
||||
if (wa2[j] == 0.)
|
||||
diag[j] = 1.;
|
||||
}
|
||||
for (j = 0; j < n; ++j)
|
||||
diag[j] = (wa2[j]==0.) ? 1. : wa2[j];
|
||||
|
||||
/* on the first iteration, calculate the norm of the scaled x */
|
||||
/* and initialize the step bound delta. */
|
||||
@@ -260,7 +253,6 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveOneStep(
|
||||
for(int ii=0; ii< fjac.cols(); ii++) fjac.col(ii).segment(ii+1, fjac.rows()-ii-1) *= fjac(ii,ii); // rescale vectors
|
||||
|
||||
/* form (q transpose)*fvec and store in qtf. */
|
||||
|
||||
qtf = fvec;
|
||||
for (j = 0; j < n; ++j)
|
||||
if (fjac(j,j) != 0.) {
|
||||
@@ -273,76 +265,54 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveOneStep(
|
||||
}
|
||||
|
||||
/* copy the triangular factor of the qr factorization into r. */
|
||||
|
||||
R = qrfac.matrixQR();
|
||||
sing = false;
|
||||
for (j = 0; j < n; ++j)
|
||||
if (wa1[j] == 0.) sing = true;
|
||||
sing = wa1.cwiseAbs().minCoeff()==0.;
|
||||
|
||||
/* accumulate the orthogonal factor in fjac. */
|
||||
ei_qform<Scalar>(n, n, fjac.data(), fjac.rows(), wa1.data());
|
||||
|
||||
/* rescale if necessary. */
|
||||
|
||||
/* Computing MAX */
|
||||
if (mode != 2)
|
||||
diag = diag.cwiseMax(wa2);
|
||||
|
||||
/* beginning of the inner loop. */
|
||||
|
||||
while (true) {
|
||||
|
||||
/* determine the direction p. */
|
||||
|
||||
ei_dogleg<Scalar>(R, diag, qtf, delta, wa1);
|
||||
|
||||
/* store the direction p and x + p. calculate the norm of p. */
|
||||
|
||||
wa1 = -wa1;
|
||||
wa2 = x + wa1;
|
||||
wa3 = diag.cwiseProduct(wa1);
|
||||
pnorm = wa3.stableNorm();
|
||||
|
||||
/* on the first iteration, adjust the initial step bound. */
|
||||
|
||||
if (iter == 1)
|
||||
delta = std::min(delta,pnorm);
|
||||
|
||||
/* evaluate the function at x + p and calculate its norm. */
|
||||
|
||||
if ( functor(wa2, wa4) < 0)
|
||||
return UserAksed;
|
||||
++nfev;
|
||||
fnorm1 = wa4.stableNorm();
|
||||
|
||||
/* compute the scaled actual reduction. */
|
||||
|
||||
actred = -1.;
|
||||
if (fnorm1 < fnorm) /* Computing 2nd power */
|
||||
actred = 1. - ei_abs2(fnorm1 / fnorm);
|
||||
|
||||
/* compute the scaled predicted reduction. */
|
||||
|
||||
for (i = 0; i < n; ++i) {
|
||||
sum = 0.;
|
||||
for (j = i; j < n; ++j)
|
||||
sum += R(i,j) * wa1[j];
|
||||
wa3[i] = qtf[i] + sum;
|
||||
}
|
||||
wa3 = R.template triangularView<Upper>()*wa1 + qtf;
|
||||
temp = wa3.stableNorm();
|
||||
prered = 0.;
|
||||
if (temp < fnorm) /* Computing 2nd power */
|
||||
prered = 1. - ei_abs2(temp / fnorm);
|
||||
|
||||
/* compute the ratio of the actual to the predicted */
|
||||
/* reduction. */
|
||||
|
||||
/* compute the ratio of the actual to the predicted reduction. */
|
||||
ratio = 0.;
|
||||
if (prered > 0.)
|
||||
ratio = actred / prered;
|
||||
|
||||
/* update the step bound. */
|
||||
|
||||
if (ratio < Scalar(.1)) {
|
||||
ncsuc = 0;
|
||||
++ncfail;
|
||||
@@ -350,7 +320,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveOneStep(
|
||||
} else {
|
||||
ncfail = 0;
|
||||
++ncsuc;
|
||||
if (ratio >= Scalar(.5) || ncsuc > 1) /* Computing MAX */
|
||||
if (ratio >= Scalar(.5) || ncsuc > 1)
|
||||
delta = std::max(delta, pnorm / Scalar(.5));
|
||||
if (ei_abs(ratio - 1.) <= Scalar(.1)) {
|
||||
delta = pnorm / Scalar(.5);
|
||||
@@ -358,7 +328,6 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveOneStep(
|
||||
}
|
||||
|
||||
/* test for successful iteration. */
|
||||
|
||||
if (ratio >= Scalar(1e-4)) {
|
||||
/* successful iteration. update x, fvec, and their norms. */
|
||||
x = wa2;
|
||||
@@ -370,7 +339,6 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveOneStep(
|
||||
}
|
||||
|
||||
/* determine the progress of the iteration. */
|
||||
|
||||
++nslow1;
|
||||
if (actred >= Scalar(.001))
|
||||
nslow1 = 0;
|
||||
@@ -380,12 +348,10 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveOneStep(
|
||||
nslow2 = 0;
|
||||
|
||||
/* test for convergence. */
|
||||
|
||||
if (delta <= parameters.xtol * xnorm || fnorm == 0.)
|
||||
return RelativeErrorTooSmall;
|
||||
|
||||
/* tests for termination and stringent tolerances. */
|
||||
|
||||
if (nfev >= parameters.maxfev)
|
||||
return TooManyFunctionEvaluation;
|
||||
if (Scalar(.1) * std::max(Scalar(.1) * delta, pnorm) <= epsilon<Scalar>() * xnorm)
|
||||
@@ -396,37 +362,27 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveOneStep(
|
||||
return NotMakingProgressIterations;
|
||||
|
||||
/* criterion for recalculating jacobian. */
|
||||
|
||||
if (ncfail == 2)
|
||||
break; // leave inner loop and go for the next outer loop iteration
|
||||
|
||||
/* calculate the rank one modification to the jacobian */
|
||||
/* and update qtf if necessary. */
|
||||
|
||||
for (j = 0; j < n; ++j) {
|
||||
sum = wa4.dot(fjac.col(j));
|
||||
wa2[j] = (sum - wa3[j]) / pnorm;
|
||||
wa1[j] = diag[j] * (diag[j] * wa1[j] / pnorm);
|
||||
if (ratio >= Scalar(1e-4))
|
||||
qtf[j] = sum;
|
||||
}
|
||||
wa1 = diag.cwiseProduct( diag.cwiseProduct(wa1)/pnorm );
|
||||
wa2 = fjac.transpose() * wa4;
|
||||
if (ratio >= Scalar(1e-4))
|
||||
qtf = wa2;
|
||||
wa2 = (wa2-wa3)/pnorm;
|
||||
|
||||
/* compute the qr factorization of the updated jacobian. */
|
||||
|
||||
ei_r1updt<Scalar>(n, n, R, wa1.data(), wa2.data(), wa3.data(), &sing);
|
||||
ei_r1mpyq<Scalar>(n, n, fjac.data(), fjac.rows(), wa2.data(), wa3.data());
|
||||
ei_r1mpyq<Scalar>(1, n, qtf.data(), 1, wa2.data(), wa3.data());
|
||||
|
||||
/* end of the inner loop. */
|
||||
|
||||
jeval = false;
|
||||
}
|
||||
/* end of the outer loop. */
|
||||
|
||||
return Running;
|
||||
}
|
||||
|
||||
|
||||
template<typename FunctorType, typename Scalar>
|
||||
typename HybridNonLinearSolver<FunctorType,Scalar>::Status
|
||||
HybridNonLinearSolver<FunctorType,Scalar>::solve(
|
||||
@@ -493,7 +449,6 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffInit(
|
||||
njev = 0;
|
||||
|
||||
/* check the input parameters for errors. */
|
||||
|
||||
if (n <= 0 || parameters.xtol < 0. || parameters.maxfev <= 0 || parameters.nb_of_subdiagonals< 0 || parameters.nb_of_superdiagonals< 0 || parameters.factor <= 0. )
|
||||
return ImproperInputParameters;
|
||||
if (mode == 2)
|
||||
@@ -503,14 +458,12 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffInit(
|
||||
|
||||
/* evaluate the function at the starting point */
|
||||
/* and calculate its norm. */
|
||||
|
||||
nfev = 1;
|
||||
if ( functor(x, fvec) < 0)
|
||||
return UserAksed;
|
||||
fnorm = fvec.stableNorm();
|
||||
|
||||
/* initialize iteration counter and monitors. */
|
||||
|
||||
iter = 1;
|
||||
ncsuc = 0;
|
||||
ncfail = 0;
|
||||
@@ -544,11 +497,8 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffOneStep(
|
||||
/* to the norms of the columns of the initial jacobian. */
|
||||
if (iter == 1) {
|
||||
if (mode != 2)
|
||||
for (j = 0; j < n; ++j) {
|
||||
diag[j] = wa2[j];
|
||||
if (wa2[j] == 0.)
|
||||
diag[j] = 1.;
|
||||
}
|
||||
for (j = 0; j < n; ++j)
|
||||
diag[j] = (wa2[j]==0.) ? 1. : wa2[j];
|
||||
|
||||
/* on the first iteration, calculate the norm of the scaled x */
|
||||
/* and initialize the step bound delta. */
|
||||
@@ -569,7 +519,6 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffOneStep(
|
||||
for(int ii=0; ii< fjac.cols(); ii++) fjac.col(ii).segment(ii+1, fjac.rows()-ii-1) *= fjac(ii,ii); // rescale vectors
|
||||
|
||||
/* form (q transpose)*fvec and store in qtf. */
|
||||
|
||||
qtf = fvec;
|
||||
for (j = 0; j < n; ++j)
|
||||
if (fjac(j,j) != 0.) {
|
||||
@@ -583,74 +532,53 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffOneStep(
|
||||
|
||||
/* copy the triangular factor of the qr factorization into r. */
|
||||
R = qrfac.matrixQR();
|
||||
sing = false;
|
||||
for (j = 0; j < n; ++j)
|
||||
if (wa1[j] == 0.) sing = true;
|
||||
sing = wa1.cwiseAbs().minCoeff()==0.;
|
||||
|
||||
/* accumulate the orthogonal factor in fjac. */
|
||||
ei_qform<Scalar>(n, n, fjac.data(), fjac.rows(), wa1.data());
|
||||
|
||||
/* rescale if necessary. */
|
||||
|
||||
/* Computing MAX */
|
||||
if (mode != 2)
|
||||
diag = diag.cwiseMax(wa2);
|
||||
|
||||
/* beginning of the inner loop. */
|
||||
|
||||
while (true) {
|
||||
|
||||
/* determine the direction p. */
|
||||
|
||||
ei_dogleg<Scalar>(R, diag, qtf, delta, wa1);
|
||||
|
||||
/* store the direction p and x + p. calculate the norm of p. */
|
||||
|
||||
wa1 = -wa1;
|
||||
wa2 = x + wa1;
|
||||
wa3 = diag.cwiseProduct(wa1);
|
||||
pnorm = wa3.stableNorm();
|
||||
|
||||
/* on the first iteration, adjust the initial step bound. */
|
||||
|
||||
if (iter == 1)
|
||||
delta = std::min(delta,pnorm);
|
||||
|
||||
/* evaluate the function at x + p and calculate its norm. */
|
||||
|
||||
if ( functor(wa2, wa4) < 0)
|
||||
return UserAksed;
|
||||
++nfev;
|
||||
fnorm1 = wa4.stableNorm();
|
||||
|
||||
/* compute the scaled actual reduction. */
|
||||
|
||||
actred = -1.;
|
||||
if (fnorm1 < fnorm) /* Computing 2nd power */
|
||||
actred = 1. - ei_abs2(fnorm1 / fnorm);
|
||||
|
||||
/* compute the scaled predicted reduction. */
|
||||
|
||||
for (i = 0; i < n; ++i) {
|
||||
sum = 0.;
|
||||
for (j = i; j < n; ++j)
|
||||
sum += R(i,j) * wa1[j];
|
||||
wa3[i] = qtf[i] + sum;
|
||||
}
|
||||
wa3 = R.template triangularView<Upper>()*wa1 + qtf;
|
||||
temp = wa3.stableNorm();
|
||||
prered = 0.;
|
||||
if (temp < fnorm) /* Computing 2nd power */
|
||||
prered = 1. - ei_abs2(temp / fnorm);
|
||||
|
||||
/* compute the ratio of the actual to the predicted */
|
||||
/* reduction. */
|
||||
|
||||
/* compute the ratio of the actual to the predicted reduction. */
|
||||
ratio = 0.;
|
||||
if (prered > 0.)
|
||||
ratio = actred / prered;
|
||||
|
||||
/* update the step bound. */
|
||||
|
||||
if (ratio < Scalar(.1)) {
|
||||
ncsuc = 0;
|
||||
++ncfail;
|
||||
@@ -658,7 +586,7 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffOneStep(
|
||||
} else {
|
||||
ncfail = 0;
|
||||
++ncsuc;
|
||||
if (ratio >= Scalar(.5) || ncsuc > 1) /* Computing MAX */
|
||||
if (ratio >= Scalar(.5) || ncsuc > 1)
|
||||
delta = std::max(delta, pnorm / Scalar(.5));
|
||||
if (ei_abs(ratio - 1.) <= Scalar(.1)) {
|
||||
delta = pnorm / Scalar(.5);
|
||||
@@ -666,7 +594,6 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffOneStep(
|
||||
}
|
||||
|
||||
/* test for successful iteration. */
|
||||
|
||||
if (ratio >= Scalar(1e-4)) {
|
||||
/* successful iteration. update x, fvec, and their norms. */
|
||||
x = wa2;
|
||||
@@ -678,7 +605,6 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffOneStep(
|
||||
}
|
||||
|
||||
/* determine the progress of the iteration. */
|
||||
|
||||
++nslow1;
|
||||
if (actred >= Scalar(.001))
|
||||
nslow1 = 0;
|
||||
@@ -688,12 +614,10 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffOneStep(
|
||||
nslow2 = 0;
|
||||
|
||||
/* test for convergence. */
|
||||
|
||||
if (delta <= parameters.xtol * xnorm || fnorm == 0.)
|
||||
return RelativeErrorTooSmall;
|
||||
|
||||
/* tests for termination and stringent tolerances. */
|
||||
|
||||
if (nfev >= parameters.maxfev)
|
||||
return TooManyFunctionEvaluation;
|
||||
if (Scalar(.1) * std::max(Scalar(.1) * delta, pnorm) <= epsilon<Scalar>() * xnorm)
|
||||
@@ -703,35 +627,25 @@ HybridNonLinearSolver<FunctorType,Scalar>::solveNumericalDiffOneStep(
|
||||
if (nslow1 == 10)
|
||||
return NotMakingProgressIterations;
|
||||
|
||||
/* criterion for recalculating jacobian approximation */
|
||||
/* by forward differences. */
|
||||
|
||||
/* criterion for recalculating jacobian. */
|
||||
if (ncfail == 2)
|
||||
break; // leave inner loop and go for the next outer loop iteration
|
||||
|
||||
/* calculate the rank one modification to the jacobian */
|
||||
/* and update qtf if necessary. */
|
||||
|
||||
for (j = 0; j < n; ++j) {
|
||||
sum = wa4.dot(fjac.col(j));
|
||||
wa2[j] = (sum - wa3[j]) / pnorm;
|
||||
wa1[j] = diag[j] * (diag[j] * wa1[j] / pnorm);
|
||||
if (ratio >= Scalar(1e-4))
|
||||
qtf[j] = sum;
|
||||
}
|
||||
wa1 = diag.cwiseProduct( diag.cwiseProduct(wa1)/pnorm );
|
||||
wa2 = fjac.transpose() * wa4;
|
||||
if (ratio >= Scalar(1e-4))
|
||||
qtf = wa2;
|
||||
wa2 = (wa2-wa3)/pnorm;
|
||||
|
||||
/* compute the qr factorization of the updated jacobian. */
|
||||
|
||||
ei_r1updt<Scalar>(n, n, R, wa1.data(), wa2.data(), wa3.data(), &sing);
|
||||
ei_r1mpyq<Scalar>(n, n, fjac.data(), fjac.rows(), wa2.data(), wa3.data());
|
||||
ei_r1mpyq<Scalar>(1, n, qtf.data(), 1, wa2.data(), wa3.data());
|
||||
|
||||
/* end of the inner loop. */
|
||||
|
||||
jeval = false;
|
||||
}
|
||||
/* end of the outer loop. */
|
||||
|
||||
return Running;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user