2009-08-22 06:40:22 +02:00
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
template <typename Scalar>
|
2009-08-25 20:01:30 +02:00
|
|
|
void ei_r1updt(int m, int n, Scalar *s, int /* ls */, const Scalar *u, Scalar *v, Scalar *w, bool *sing)
|
2009-08-22 06:40:22 +02:00
|
|
|
{
|
|
|
|
|
/* Local variables */
|
2009-08-22 07:14:17 +02:00
|
|
|
int i, j, l, jj, nm1;
|
2009-08-22 06:40:22 +02:00
|
|
|
Scalar tan__;
|
|
|
|
|
int nmj;
|
2009-08-22 07:31:14 +02:00
|
|
|
Scalar cos__, sin__, tau, temp, cotan;
|
2009-08-22 06:40:22 +02:00
|
|
|
|
|
|
|
|
/* Parameter adjustments */
|
|
|
|
|
--w;
|
|
|
|
|
--u;
|
|
|
|
|
--v;
|
|
|
|
|
--s;
|
|
|
|
|
|
|
|
|
|
/* Function Body */
|
2009-08-22 07:31:14 +02:00
|
|
|
const Scalar giant = std::numeric_limits<Scalar>::max();
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
/* initialize the diagonal element pointer. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
|
|
|
|
jj = n * ((m << 1) - n + 1) / 2 - (m - n);
|
|
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
/* move the nontrivial part of the last column of s into w. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
|
|
|
|
l = jj;
|
2009-08-22 07:14:17 +02:00
|
|
|
for (i = n; i <= m; ++i) {
|
2009-09-14 23:47:44 +02:00
|
|
|
w[i] = s[l];
|
|
|
|
|
++l;
|
|
|
|
|
/* L10: */
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
/* rotate the vector v into a multiple of the n-th unit vector */
|
|
|
|
|
/* in such a way that a spike is introduced into w. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
|
|
|
|
nm1 = n - 1;
|
|
|
|
|
if (nm1 < 1) {
|
2009-09-14 23:47:44 +02:00
|
|
|
goto L70;
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
2009-08-22 07:14:17 +02:00
|
|
|
for (nmj = 1; nmj <= nm1; ++nmj) {
|
2009-09-14 23:47:44 +02:00
|
|
|
j = n - nmj;
|
|
|
|
|
jj -= m - j + 1;
|
|
|
|
|
w[j] = 0.;
|
|
|
|
|
if (v[j] == 0.) {
|
|
|
|
|
goto L50;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* determine a givens rotation which eliminates the */
|
|
|
|
|
/* j-th element of v. */
|
|
|
|
|
|
|
|
|
|
if (ei_abs(v[n]) >= ei_abs(v[j]))
|
|
|
|
|
goto L20;
|
|
|
|
|
cotan = v[n] / v[j];
|
|
|
|
|
/* Computing 2nd power */
|
|
|
|
|
sin__ = Scalar(.5) / ei_sqrt(Scalar(0.25) + Scalar(0.25) * ei_abs2(cotan));
|
|
|
|
|
cos__ = sin__ * cotan;
|
|
|
|
|
tau = 1.;
|
|
|
|
|
if (ei_abs(cos__) * giant > 1.) {
|
|
|
|
|
tau = 1. / cos__;
|
|
|
|
|
}
|
|
|
|
|
goto L30;
|
2009-08-22 06:40:22 +02:00
|
|
|
L20:
|
2009-09-14 23:47:44 +02:00
|
|
|
tan__ = v[j] / v[n];
|
|
|
|
|
/* Computing 2nd power */
|
|
|
|
|
cos__ = Scalar(.5) / ei_sqrt(Scalar(0.25) + Scalar(0.25) * ei_abs2(tan__));
|
|
|
|
|
sin__ = cos__ * tan__;
|
|
|
|
|
tau = sin__;
|
2009-08-22 06:40:22 +02:00
|
|
|
L30:
|
|
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
/* apply the transformation to v and store the information */
|
|
|
|
|
/* necessary to recover the givens rotation. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
v[n] = sin__ * v[j] + cos__ * v[n];
|
|
|
|
|
v[j] = tau;
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
/* apply the transformation to s and extend the spike in w. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
l = jj;
|
|
|
|
|
for (i = j; i <= m; ++i) {
|
|
|
|
|
temp = cos__ * s[l] - sin__ * w[i];
|
|
|
|
|
w[i] = sin__ * s[l] + cos__ * w[i];
|
|
|
|
|
s[l] = temp;
|
|
|
|
|
++l;
|
|
|
|
|
/* L40: */
|
|
|
|
|
}
|
2009-08-22 06:40:22 +02:00
|
|
|
L50:
|
2009-09-14 23:47:44 +02:00
|
|
|
/* L60: */
|
|
|
|
|
;
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
L70:
|
|
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
/* add the spike from the rank 1 update to w. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-08-22 07:14:17 +02:00
|
|
|
for (i = 1; i <= m; ++i) {
|
2009-09-14 23:47:44 +02:00
|
|
|
w[i] += v[n] * u[i];
|
|
|
|
|
/* L80: */
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
/* eliminate the spike. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
|
|
|
|
*sing = false;
|
|
|
|
|
if (nm1 < 1) {
|
2009-09-14 23:47:44 +02:00
|
|
|
goto L140;
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
2009-08-22 07:14:17 +02:00
|
|
|
for (j = 1; j <= nm1; ++j) {
|
2009-09-14 23:47:44 +02:00
|
|
|
if (w[j] == 0.) {
|
|
|
|
|
goto L120;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* determine a givens rotation which eliminates the */
|
|
|
|
|
/* j-th element of the spike. */
|
|
|
|
|
|
|
|
|
|
if (ei_abs(s[jj]) >= ei_abs(w[j]))
|
|
|
|
|
goto L90;
|
|
|
|
|
cotan = s[jj] / w[j];
|
|
|
|
|
/* Computing 2nd power */
|
|
|
|
|
sin__ = Scalar(.5) / ei_sqrt(Scalar(0.25) + Scalar(0.25) * ei_abs2(cotan));
|
|
|
|
|
cos__ = sin__ * cotan;
|
|
|
|
|
tau = 1.;
|
|
|
|
|
if (ei_abs(cos__) * giant > 1.) {
|
|
|
|
|
tau = 1. / cos__;
|
|
|
|
|
}
|
|
|
|
|
goto L100;
|
2009-08-22 06:40:22 +02:00
|
|
|
L90:
|
2009-09-14 23:47:44 +02:00
|
|
|
tan__ = w[j] / s[jj];
|
|
|
|
|
/* Computing 2nd power */
|
|
|
|
|
cos__ = Scalar(.5) / ei_sqrt(Scalar(0.25) + Scalar(0.25) * ei_abs2(tan__));
|
|
|
|
|
sin__ = cos__ * tan__;
|
|
|
|
|
tau = sin__;
|
2009-08-22 06:40:22 +02:00
|
|
|
L100:
|
|
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
/* apply the transformation to s and reduce the spike in w. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
l = jj;
|
|
|
|
|
for (i = j; i <= m; ++i) {
|
|
|
|
|
temp = cos__ * s[l] + sin__ * w[i];
|
|
|
|
|
w[i] = -sin__ * s[l] + cos__ * w[i];
|
|
|
|
|
s[l] = temp;
|
|
|
|
|
++l;
|
|
|
|
|
/* L110: */
|
|
|
|
|
}
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
/* store the information necessary to recover the */
|
|
|
|
|
/* givens rotation. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
w[j] = tau;
|
2009-08-22 06:40:22 +02:00
|
|
|
L120:
|
|
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
/* test for zero diagonal elements in the output s. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
if (s[jj] == 0.) {
|
|
|
|
|
*sing = true;
|
|
|
|
|
}
|
|
|
|
|
jj += m - j + 1;
|
|
|
|
|
/* L130: */
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
L140:
|
|
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
/* move w back into the last column of the output s. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
|
|
|
|
l = jj;
|
2009-08-22 07:14:17 +02:00
|
|
|
for (i = n; i <= m; ++i) {
|
2009-09-14 23:47:44 +02:00
|
|
|
s[l] = w[i];
|
|
|
|
|
++l;
|
|
|
|
|
/* L150: */
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
if (s[jj] == 0.) {
|
2009-09-14 23:47:44 +02:00
|
|
|
*sing = true;
|
2009-08-22 06:40:22 +02:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
|
2009-09-14 23:47:44 +02:00
|
|
|
/* last card of subroutine r1updt. */
|
2009-08-22 06:40:22 +02:00
|
|
|
|
|
|
|
|
} /* r1updt_ */
|
|
|
|
|
|