1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace Eigen {
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace internal {
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate <typename Scalar>
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathvoid r1updt(
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        Matrix< Scalar, Dynamic, Dynamic > &s,
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        const Matrix< Scalar, Dynamic, 1> &u,
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        std::vector<JacobiRotation<Scalar> > &v_givens,
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        std::vector<JacobiRotation<Scalar> > &w_givens,
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        Matrix< Scalar, Dynamic, 1> &v,
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        Matrix< Scalar, Dynamic, 1> &w,
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        bool *sing)
14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef DenseIndex Index;
16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    const JacobiRotation<Scalar> IdentityRotation = JacobiRotation<Scalar>(1,0);
17c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /* Local variables */
19c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    const Index m = s.rows();
20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    const Index n = s.cols();
21c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Index i, j=1;
22c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Scalar temp;
23c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    JacobiRotation<Scalar> givens;
24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
25c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    // r1updt had a broader usecase, but we dont use it here. And, more
26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    // importantly, we can not test it.
277faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    eigen_assert(m==n);
287faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    eigen_assert(u.size()==m);
297faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    eigen_assert(v.size()==n);
307faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    eigen_assert(w.size()==n);
31c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
32c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /* move the nontrivial part of the last column of s into w. */
33c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    w[n-1] = s(n-1,n-1);
34c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
35c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /* rotate the vector v into a multiple of the n-th unit vector */
36c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /* in such a way that a spike is introduced into w. */
37c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    for (j=n-2; j>=0; --j) {
38c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        w[j] = 0.;
39c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        if (v[j] != 0.) {
40c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            /* determine a givens rotation which eliminates the */
41c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            /* j-th element of v. */
42c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            givens.makeGivens(-v[n-1], v[j]);
43c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
44c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            /* apply the transformation to v and store the information */
45c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            /* necessary to recover the givens rotation. */
46c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            v[n-1] = givens.s() * v[j] + givens.c() * v[n-1];
47c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            v_givens[j] = givens;
48c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
49c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            /* apply the transformation to s and extend the spike in w. */
50c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            for (i = j; i < m; ++i) {
51c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                temp = givens.c() * s(j,i) - givens.s() * w[i];
52c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                w[i] = givens.s() * s(j,i) + givens.c() * w[i];
53c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                s(j,i) = temp;
54c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            }
55c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        } else
56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            v_givens[j] = IdentityRotation;
57c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
58c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
59c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /* add the spike from the rank 1 update to w. */
60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    w += v[n-1] * u;
61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
62c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /* eliminate the spike. */
63c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    *sing = false;
64c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    for (j = 0; j < n-1; ++j) {
65c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        if (w[j] != 0.) {
66c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            /* determine a givens rotation which eliminates the */
67c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            /* j-th element of the spike. */
68c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            givens.makeGivens(-s(j,j), w[j]);
69c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
70c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            /* apply the transformation to s and reduce the spike in w. */
71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            for (i = j; i < m; ++i) {
72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                temp = givens.c() * s(j,i) + givens.s() * w[i];
73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                w[i] = -givens.s() * s(j,i) + givens.c() * w[i];
74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                s(j,i) = temp;
75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            }
76c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
77c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            /* store the information necessary to recover the */
78c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            /* givens rotation. */
79c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            w_givens[j] = givens;
80c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        } else
81c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            v_givens[j] = IdentityRotation;
82c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
83c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        /* test for zero diagonal elements in the output s. */
84c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        if (s(j,j) == 0.) {
85c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath            *sing = true;
86c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        }
87c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
88c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /* move w back into the last column of the output s. */
89c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    s(n-1,n-1) = w[n-1];
90c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
91c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    if (s(j,j) == 0.) {
92c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        *sing = true;
93c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
94c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return;
95c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
96c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
97c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace internal
98c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
99c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace Eigen
100