14 #ifndef EIGEN_LMONESTEP_H
15 #define EIGEN_LMONESTEP_H
19 template<
typename FunctorType>
20 LevenbergMarquardtSpace::Status
21 LevenbergMarquardt<FunctorType>::minimizeOneStep(FVectorType &x)
25 RealScalar temp, temp1,temp2;
27 RealScalar pnorm, xnorm, fnorm1, actred, dirder, prered;
28 eigen_assert(x.size()==n);
30 temp = 0.0; xnorm = 0.0;
32 Index df_ret = m_functor.df(x, m_fjac);
34 return LevenbergMarquardtSpace::UserAsked;
41 for (
int j = 0; j < x.size(); ++j)
42 m_wa2(j) = m_fjac.col(j).blueNorm();
43 QRSolver qrfac(m_fjac);
44 if(qrfac.info() != Success) {
45 m_info = NumericalIssue;
46 return LevenbergMarquardtSpace::ImproperInputParameters;
49 m_rfactor = qrfac.matrixR();
50 m_permutation = (qrfac.colsPermutation());
55 if (!m_useExternalScaling)
56 for (Index j = 0; j < n; ++j)
57 m_diag[j] = (m_wa2[j]==0.)? 1. : m_wa2[j];
61 xnorm = m_diag.cwiseProduct(x).stableNorm();
62 m_delta = m_factor * xnorm;
70 m_wa4 = qrfac.matrixQ().adjoint() * m_fvec;
71 m_qtf = m_wa4.head(n);
76 for (Index j = 0; j < n; ++j)
77 if (m_wa2[m_permutation.indices()[j]] != 0.)
78 m_gnorm = (std::max)(m_gnorm, abs( m_rfactor.col(j).head(j+1).dot(m_qtf.head(j+1)/m_fnorm) / m_wa2[m_permutation.indices()[j]]));
81 if (m_gnorm <= m_gtol) {
83 return LevenbergMarquardtSpace::CosinusTooSmall;
87 if (!m_useExternalScaling)
88 m_diag = m_diag.cwiseMax(m_wa2);
92 internal::lmpar2(qrfac, m_diag, m_qtf, m_delta, m_par, m_wa1);
97 pnorm = m_diag.cwiseProduct(m_wa1).stableNorm();
101 m_delta = (std::min)(m_delta,pnorm);
104 if ( m_functor(m_wa2, m_wa4) < 0)
105 return LevenbergMarquardtSpace::UserAsked;
107 fnorm1 = m_wa4.stableNorm();
111 if (Scalar(.1) * fnorm1 < m_fnorm)
112 actred = 1. - numext::abs2(fnorm1 / m_fnorm);
116 m_wa3 = m_rfactor.template triangularView<Upper>() * (m_permutation.inverse() *m_wa1);
117 temp1 = numext::abs2(m_wa3.stableNorm() / m_fnorm);
118 temp2 = numext::abs2(sqrt(m_par) * pnorm / m_fnorm);
119 prered = temp1 + temp2 / Scalar(.5);
120 dirder = -(temp1 + temp2);
126 ratio = actred / prered;
129 if (ratio <= Scalar(.25)) {
131 temp = RealScalar(.5);
133 temp = RealScalar(.5) * dirder / (dirder + RealScalar(.5) * actred);
134 if (RealScalar(.1) * fnorm1 >= m_fnorm || temp < RealScalar(.1))
137 m_delta = temp * (std::min)(m_delta, pnorm / RealScalar(.1));
139 }
else if (!(m_par != 0. && ratio < RealScalar(.75))) {
140 m_delta = pnorm / RealScalar(.5);
141 m_par = RealScalar(.5) * m_par;
145 if (ratio >= RealScalar(1e-4)) {
148 m_wa2 = m_diag.cwiseProduct(x);
150 xnorm = m_wa2.stableNorm();
156 if (abs(actred) <= m_ftol && prered <= m_ftol && Scalar(.5) * ratio <= 1. && m_delta <= m_xtol * xnorm)
159 return LevenbergMarquardtSpace::RelativeErrorAndReductionTooSmall;
161 if (abs(actred) <= m_ftol && prered <= m_ftol && Scalar(.5) * ratio <= 1.)
164 return LevenbergMarquardtSpace::RelativeReductionTooSmall;
166 if (m_delta <= m_xtol * xnorm)
169 return LevenbergMarquardtSpace::RelativeErrorTooSmall;
173 if (m_nfev >= m_maxfev)
175 m_info = NoConvergence;
176 return LevenbergMarquardtSpace::TooManyFunctionEvaluation;
178 if (abs(actred) <= NumTraits<Scalar>::epsilon() && prered <= NumTraits<Scalar>::epsilon() && Scalar(.5) * ratio <= 1.)
181 return LevenbergMarquardtSpace::FtolTooSmall;
183 if (m_delta <= NumTraits<Scalar>::epsilon() * xnorm)
186 return LevenbergMarquardtSpace::XtolTooSmall;
188 if (m_gnorm <= NumTraits<Scalar>::epsilon())
191 return LevenbergMarquardtSpace::GtolTooSmall;
194 }
while (ratio < Scalar(1e-4));
196 return LevenbergMarquardtSpace::Running;
202 #endif // EIGEN_LMONESTEP_H