qrsolv.h
Go to the documentation of this file.
00001 namespace internal {
00002 
00003 // TODO : once qrsolv2 is removed, use ColPivHouseholderQR or PermutationMatrix instead of ipvt
00004 template <typename Scalar>
00005 void qrsolv(
00006         Matrix< Scalar, Dynamic, Dynamic > &s,
00007         // TODO : use a PermutationMatrix once lmpar is no more:
00008         const VectorXi &ipvt,
00009         const Matrix< Scalar, Dynamic, 1 >  &diag,
00010         const Matrix< Scalar, Dynamic, 1 >  &qtb,
00011         Matrix< Scalar, Dynamic, 1 >  &x,
00012         Matrix< Scalar, Dynamic, 1 >  &sdiag)
00013 
00014 {
00015     typedef DenseIndex Index;
00016 
00017     /* Local variables */
00018     Index i, j, k, l;
00019     Scalar temp;
00020     Index n = s.cols();
00021     Matrix< Scalar, Dynamic, 1 >  wa(n);
00022     JacobiRotation<Scalar> givens;
00023 
00024     /* Function Body */
00025     // the following will only change the lower triangular part of s, including
00026     // the diagonal, though the diagonal is restored afterward
00027 
00028     /*     copy r and (q transpose)*b to preserve input and initialize s. */
00029     /*     in particular, save the diagonal elements of r in x. */
00030     x = s.diagonal();
00031     wa = qtb;
00032 
00033     s.topLeftCorner(n,n).template triangularView<StrictlyLower>() = s.topLeftCorner(n,n).transpose();
00034 
00035     /*     eliminate the diagonal matrix d using a givens rotation. */
00036     for (j = 0; j < n; ++j) {
00037 
00038         /*        prepare the row of d to be eliminated, locating the */
00039         /*        diagonal element using p from the qr factorization. */
00040         l = ipvt[j];
00041         if (diag[l] == 0.)
00042             break;
00043         sdiag.tail(n-j).setZero();
00044         sdiag[j] = diag[l];
00045 
00046         /*        the transformations to eliminate the row of d */
00047         /*        modify only a single element of (q transpose)*b */
00048         /*        beyond the first n, which is initially zero. */
00049         Scalar qtbpj = 0.;
00050         for (k = j; k < n; ++k) {
00051             /*           determine a givens rotation which eliminates the */
00052             /*           appropriate element in the current row of d. */
00053             givens.makeGivens(-s(k,k), sdiag[k]);
00054 
00055             /*           compute the modified diagonal element of r and */
00056             /*           the modified element of ((q transpose)*b,0). */
00057             s(k,k) = givens.c() * s(k,k) + givens.s() * sdiag[k];
00058             temp = givens.c() * wa[k] + givens.s() * qtbpj;
00059             qtbpj = -givens.s() * wa[k] + givens.c() * qtbpj;
00060             wa[k] = temp;
00061 
00062             /*           accumulate the tranformation in the row of s. */
00063             for (i = k+1; i<n; ++i) {
00064                 temp = givens.c() * s(i,k) + givens.s() * sdiag[i];
00065                 sdiag[i] = -givens.s() * s(i,k) + givens.c() * sdiag[i];
00066                 s(i,k) = temp;
00067             }
00068         }
00069     }
00070 
00071     /*     solve the triangular system for z. if the system is */
00072     /*     singular, then obtain a least squares solution. */
00073     Index nsing;
00074     for(nsing=0; nsing<n && sdiag[nsing]!=0; nsing++) {}
00075 
00076     wa.tail(n-nsing).setZero();
00077     s.topLeftCorner(nsing, nsing).transpose().template triangularView<Upper>().solveInPlace(wa.head(nsing));
00078 
00079     // restore
00080     sdiag = s.diagonal();
00081     s.diagonal() = x;
00082 
00083     /*     permute the components of z back to components of x. */
00084     for (j = 0; j < n; ++j) x[ipvt[j]] = wa[j];
00085 }
00086 
00087 } // end namespace internal


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:32:16