Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
qrsolv.h
1// IWYU pragma: private
2// SPDX-FileCopyrightText: The Eigen Authors
3// SPDX-License-Identifier: MPL-2.0
4
5#ifndef EIGEN_NONLINEAROPTIMIZATION_QRSOLV_H
6#define EIGEN_NONLINEAROPTIMIZATION_QRSOLV_H
7
8#include "./InternalHeaderCheck.h"
9
10namespace Eigen {
11
12namespace internal {
13
14// TODO : once qrsolv2 is removed, use ColPivHouseholderQR or PermutationMatrix instead of ipvt
15template <typename Scalar>
16void qrsolv(Matrix<Scalar, Dynamic, Dynamic> &s,
17 // TODO : use a PermutationMatrix once lmpar is no more:
18 const VectorXi &ipvt, const Matrix<Scalar, Dynamic, 1> &diag, const Matrix<Scalar, Dynamic, 1> &qtb,
19 Matrix<Scalar, Dynamic, 1> &x, Matrix<Scalar, Dynamic, 1> &sdiag)
20
21{
22 typedef DenseIndex Index;
23
24 /* Local variables */
25 Index i, j, k, l;
26 Scalar temp;
27 Index n = s.cols();
28 Matrix<Scalar, Dynamic, 1> wa(n);
29 JacobiRotation<Scalar> givens;
30
31 /* Function Body */
32 // the following will only change the lower triangular part of s, including
33 // the diagonal, though the diagonal is restored afterward
34
35 /* copy r and (q transpose)*b to preserve input and initialize s. */
36 /* in particular, save the diagonal elements of r in x. */
37 x = s.diagonal();
38 wa = qtb;
39
40 s.topLeftCorner(n, n).template triangularView<StrictlyLower>() = s.topLeftCorner(n, n).transpose();
41
42 /* eliminate the diagonal matrix d using a givens rotation. */
43 for (j = 0; j < n; ++j) {
44 /* prepare the row of d to be eliminated, locating the */
45 /* diagonal element using p from the qr factorization. */
46 l = ipvt[j];
47 if (diag[l] == 0.) break;
48 sdiag.tail(n - j).setZero();
49 sdiag[j] = diag[l];
50
51 /* the transformations to eliminate the row of d */
52 /* modify only a single element of (q transpose)*b */
53 /* beyond the first n, which is initially zero. */
54 Scalar qtbpj = 0.;
55 for (k = j; k < n; ++k) {
56 /* determine a givens rotation which eliminates the */
57 /* appropriate element in the current row of d. */
58 givens.makeGivens(-s(k, k), sdiag[k]);
59
60 /* compute the modified diagonal element of r and */
61 /* the modified element of ((q transpose)*b,0). */
62 s(k, k) = givens.c() * s(k, k) + givens.s() * sdiag[k];
63 temp = givens.c() * wa[k] + givens.s() * qtbpj;
64 qtbpj = -givens.s() * wa[k] + givens.c() * qtbpj;
65 wa[k] = temp;
66
67 /* accumulate the transformation in the row of s. */
68 for (i = k + 1; i < n; ++i) {
69 temp = givens.c() * s(i, k) + givens.s() * sdiag[i];
70 sdiag[i] = -givens.s() * s(i, k) + givens.c() * sdiag[i];
71 s(i, k) = temp;
72 }
73 }
74 }
75
76 /* solve the triangular system for z. if the system is */
77 /* singular, then obtain a least squares solution. */
78 Index nsing;
79 for (nsing = 0; nsing < n && sdiag[nsing] != 0; nsing++) {
80 }
81
82 wa.tail(n - nsing).setZero();
83 s.topLeftCorner(nsing, nsing).transpose().template triangularView<Upper>().solveInPlace(wa.head(nsing));
84
85 // restore
86 sdiag = s.diagonal();
87 s.diagonal() = x;
88
89 /* permute the components of z back to components of x. */
90 for (j = 0; j < n; ++j) x[ipvt[j]] = wa[j];
91}
92
93} // end namespace internal
94
95} // end namespace Eigen
96
97#endif // EIGEN_NONLINEAROPTIMIZATION_QRSOLV_H
Matrix< int, Dynamic, 1 > VectorXi
Namespace containing all symbols from the Eigen library.