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