Files
eigen/unsupported/Eigen/src/NonLinearOptimization/qrsolv.h

86 lines
3.1 KiB
C
Raw Normal View History

2010-01-28 04:19:39 +01:00
// TODO : once qrsolv2 is removed, use ColPivHouseholderQR or PermutationMatrix instead of ipvt
2009-11-26 04:29:51 +01:00
template <typename Scalar>
void ei_qrsolv(
Matrix< Scalar, Dynamic, Dynamic > &s,
// TODO : use a PermutationMatrix once ei_lmpar is no more:
2010-01-25 07:08:08 +01:00
const VectorXi &ipvt,
2009-11-26 04:29:51 +01:00
const Matrix< Scalar, Dynamic, 1 > &diag,
const Matrix< Scalar, Dynamic, 1 > &qtb,
Matrix< Scalar, Dynamic, 1 > &x,
Matrix< Scalar, Dynamic, 1 > &sdiag)
2009-11-26 04:29:51 +01:00
{
typedef DenseIndex Index;
/* Local variables */
Index i, j, k, l;
Scalar temp;
Index n = s.cols();
2009-11-26 04:29:51 +01:00
Matrix< Scalar, Dynamic, 1 > wa(n);
2010-01-28 04:19:39 +01:00
PlanarRotation<Scalar> givens;
/* Function Body */
// the following will only change the lower triangular part of s, including
// the diagonal, though the diagonal is restored afterward
2009-09-14 23:47:44 +02:00
/* copy r and (q transpose)*b to preserve input and initialize s. */
/* in particular, save the diagonal elements of r in x. */
x = s.diagonal();
2009-11-26 07:37:37 +01:00
wa = qtb;
s.topLeftCorner(n,n).template triangularView<StrictlyLower>() = s.topLeftCorner(n,n).transpose();
2009-09-14 23:47:44 +02:00
/* eliminate the diagonal matrix d using a givens rotation. */
2009-11-26 04:29:51 +01:00
for (j = 0; j < n; ++j) {
2009-09-14 23:47:44 +02:00
/* prepare the row of d to be eliminated, locating the */
/* diagonal element using p from the qr factorization. */
l = ipvt[j];
2009-11-26 04:06:40 +01:00
if (diag[l] == 0.)
2009-11-26 07:37:37 +01:00
break;
2010-01-28 04:19:39 +01:00
sdiag.tail(n-j).setZero();
2009-09-14 23:47:44 +02:00
sdiag[j] = diag[l];
/* the transformations to eliminate the row of d */
/* modify only a single element of (q transpose)*b */
/* beyond the first n, which is initially zero. */
2009-11-26 08:30:38 +01:00
Scalar qtbpj = 0.;
2009-11-26 04:29:51 +01:00
for (k = j; k < n; ++k) {
2009-09-14 23:47:44 +02:00
/* determine a givens rotation which eliminates the */
/* appropriate element in the current row of d. */
givens.makeGivens(-s(k,k), sdiag[k]);
2009-09-14 23:47:44 +02:00
/* compute the modified diagonal element of r and */
/* the modified element of ((q transpose)*b,0). */
s(k,k) = givens.c() * s(k,k) + givens.s() * sdiag[k];
2009-11-26 07:37:37 +01:00
temp = givens.c() * wa[k] + givens.s() * qtbpj;
qtbpj = -givens.s() * wa[k] + givens.c() * qtbpj;
2009-09-14 23:47:44 +02:00
wa[k] = temp;
/* accumulate the tranformation in the row of s. */
2009-11-26 04:29:51 +01:00
for (i = k+1; i<n; ++i) {
temp = givens.c() * s(i,k) + givens.s() * sdiag[i];
sdiag[i] = -givens.s() * s(i,k) + givens.c() * sdiag[i];
s(i,k) = temp;
2009-09-14 23:47:44 +02:00
}
}
}
2009-09-14 23:47:44 +02:00
/* solve the triangular system for z. if the system is */
/* singular, then obtain a least squares solution. */
Index nsing;
2010-06-10 10:59:06 +02:00
for(nsing=0; nsing<n && sdiag[nsing]!=0; nsing++) {}
2009-11-26 07:37:37 +01:00
2010-01-28 04:19:39 +01:00
wa.tail(n-nsing).setZero();
s.topLeftCorner(nsing, nsing).transpose().template triangularView<Upper>().solveInPlace(wa.head(nsing));
// restore
sdiag = s.diagonal();
s.diagonal() = x;
2009-09-14 23:47:44 +02:00
/* permute the components of z back to components of x. */
2009-11-26 07:37:37 +01:00
for (j = 0; j < n; ++j) x[ipvt[j]] = wa[j];
2009-11-26 04:06:40 +01:00
}