Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
LMpar.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// This code initially comes from MINPACK whose original authors are:
5// Copyright Jorge More - Argonne National Laboratory
6// Copyright Burt Garbow - Argonne National Laboratory
7// Copyright Ken Hillstrom - Argonne National Laboratory
8//
9// This Source Code Form is subject to the terms of the Minpack license
10// (a BSD-like license) described in the accompanying CopyrightMINPACK.txt file.
11// SPDX-License-Identifier: MPL-2.0 AND LicenseRef-MINPACK
12
13#ifndef EIGEN_LMPAR_H
14#define EIGEN_LMPAR_H
15
16// IWYU pragma: private
17#include "./InternalHeaderCheck.h"
18
19namespace Eigen {
20
21namespace internal {
22
23template <typename QRSolver, typename VectorType>
24void lmpar2(const QRSolver &qr, const VectorType &diag, const VectorType &qtb, typename VectorType::Scalar m_delta,
25 typename VectorType::Scalar &par, VectorType &x)
26
27{
28 using std::abs;
29 using std::sqrt;
30 typedef typename QRSolver::Scalar Scalar;
31 /* Local variables */
32 Index j;
33 Scalar fp;
34 Scalar parc, parl;
35 Index iter;
36 Scalar temp, paru;
37 Scalar gnorm;
38 Scalar dxnorm;
39
40 /* Function Body */
41 const Scalar dwarf = (std::numeric_limits<Scalar>::min)();
42 const Index n = qr.matrixR().cols();
43
44 // Working copy of the leading n-by-n block of the triangular factor; lmqrsolv()
45 // overwrites its strict lower triangle with the eliminated factor read back
46 // below. The rotations fill that triangle in completely, so this is dense even
47 // when the QR is sparse.
48 Matrix<Scalar, Dynamic, Dynamic> s = qr.matrixR().topLeftCorner(n, n);
49
50 eigen_assert(n == diag.size());
51 eigen_assert(n == qtb.size());
52
53 VectorType wa1, wa2;
54
55 /* compute and store in x the gauss-newton direction. if the */
56 /* jacobian is rank-deficient, obtain a least squares solution. */
57
58 const Index rank = qr.rank(); // use a threshold
59 wa1 = qtb;
60 wa1.tail(n - rank).setZero();
61 s.topLeftCorner(rank, rank).template triangularView<Upper>().solveInPlace(wa1.head(rank));
62
63 x = qr.colsPermutation() * wa1;
64
65 /* initialize the iteration counter. */
66 /* evaluate the function at the origin, and test */
67 /* for acceptance of the gauss-newton direction. */
68 iter = 0;
69 wa2 = diag.cwiseProduct(x);
70 dxnorm = wa2.blueNorm();
71 fp = dxnorm - m_delta;
72 if (fp <= Scalar(0.1) * m_delta) {
73 par = 0;
74 return;
75 }
76
77 /* if the jacobian is not rank deficient, the newton */
78 /* step provides a lower bound, parl, for the zero of */
79 /* the function. otherwise set this bound to zero. */
80 parl = 0.;
81 if (rank == n) {
82 wa1 = qr.colsPermutation().inverse() * diag.cwiseProduct(wa2) / dxnorm;
83 s.topLeftCorner(n, n).transpose().template triangularView<Lower>().solveInPlace(wa1);
84 temp = wa1.blueNorm();
85 parl = fp / m_delta / temp / temp;
86 }
87
88 /* calculate an upper bound, paru, for the zero of the function. */
89 for (j = 0; j < n; ++j) wa1[j] = s.col(j).head(j + 1).dot(qtb.head(j + 1)) / diag[qr.colsPermutation().indices()(j)];
90
91 gnorm = wa1.stableNorm();
92 paru = gnorm / m_delta;
93 if (paru == 0.) paru = dwarf / (std::min)(m_delta, Scalar(0.1));
94
95 /* if the input par lies outside of the interval (parl,paru), */
96 /* set par to the closer endpoint. */
97 par = (std::max)(par, parl);
98 par = (std::min)(par, paru);
99 if (par == 0.) par = gnorm / dxnorm;
100
101 /* beginning of an iteration. */
102 while (true) {
103 ++iter;
104
105 /* evaluate the function at the current value of par. */
106 if (par == 0.) par = (std::max)(dwarf, Scalar(.001) * paru); /* Computing MAX */
107 wa1 = sqrt(par) * diag;
108
109 VectorType sdiag(n);
110 lmqrsolv(s, qr.colsPermutation(), wa1, qtb, x, sdiag);
111
112 wa2 = diag.cwiseProduct(x);
113 dxnorm = wa2.blueNorm();
114 temp = fp;
115 fp = dxnorm - m_delta;
116
117 /* if the function is small enough, accept the current value */
118 /* of par. also test for the exceptional cases where parl */
119 /* is zero or the number of iterations has reached 10. */
120 if (abs(fp) <= Scalar(0.1) * m_delta || (parl == 0. && fp <= temp && temp < 0.) || iter == 10) break;
121
122 /* compute the newton correction. */
123 wa1 = qr.colsPermutation().inverse() * diag.cwiseProduct(wa2 / dxnorm);
124 // we could almost use this here, but the diagonal is outside qr, in sdiag[]
125 for (j = 0; j < n; ++j) {
126 wa1[j] /= sdiag[j];
127 temp = wa1[j];
128 for (Index i = j + 1; i < n; ++i) wa1[i] -= s.coeff(i, j) * temp;
129 }
130 temp = wa1.blueNorm();
131 parc = fp / m_delta / temp / temp;
132
133 /* depending on the sign of the function, update parl or paru. */
134 if (fp > 0.) parl = (std::max)(parl, par);
135 if (fp < 0.) paru = (std::min)(paru, par);
136
137 /* compute an improved estimate for par. */
138 par = (std::max)(parl, par + parc);
139 }
140 if (iter == 0) par = 0.;
141 return;
142}
143} // end namespace internal
144
145} // end namespace Eigen
146
147#endif // EIGEN_LMPAR_H
Namespace containing all symbols from the Eigen library.