Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
r1updt.h
1// IWYU pragma: private
2// SPDX-FileCopyrightText: The Eigen Authors
3// SPDX-License-Identifier: MPL-2.0
4
5#ifndef EIGEN_NONLINEAROPTIMIZATION_R1UPDT_H
6#define EIGEN_NONLINEAROPTIMIZATION_R1UPDT_H
7
8#include "./InternalHeaderCheck.h"
9
10namespace Eigen {
11
12namespace internal {
13
14template <typename Scalar>
15void r1updt(Matrix<Scalar, Dynamic, Dynamic> &s, const Matrix<Scalar, Dynamic, 1> &u,
16 std::vector<JacobiRotation<Scalar> > &v_givens, std::vector<JacobiRotation<Scalar> > &w_givens,
17 Matrix<Scalar, Dynamic, 1> &v, Matrix<Scalar, Dynamic, 1> &w, bool *sing) {
18 typedef DenseIndex Index;
19 const JacobiRotation<Scalar> IdentityRotation = JacobiRotation<Scalar>(1, 0);
20
21 /* Local variables */
22 const Index m = s.rows();
23 const Index n = s.cols();
24 Index i, j = 1;
25 Scalar temp;
26 JacobiRotation<Scalar> givens;
27
28 // r1updt had a broader use case, but we don't use it here. And, more
29 // importantly, we can not test it.
30 eigen_assert(m == n);
31 eigen_assert(u.size() == m);
32 eigen_assert(v.size() == n);
33 eigen_assert(w.size() == n);
34
35 /* move the nontrivial part of the last column of s into w. */
36 w[n - 1] = s(n - 1, n - 1);
37
38 /* rotate the vector v into a multiple of the n-th unit vector */
39 /* in such a way that a spike is introduced into w. */
40 for (j = n - 2; j >= 0; --j) {
41 w[j] = 0.;
42 if (v[j] != 0.) {
43 /* determine a givens rotation which eliminates the */
44 /* j-th element of v. */
45 givens.makeGivens(-v[n - 1], v[j]);
46
47 /* apply the transformation to v and store the information */
48 /* necessary to recover the givens rotation. */
49 v[n - 1] = givens.s() * v[j] + givens.c() * v[n - 1];
50 v_givens[j] = givens;
51
52 /* apply the transformation to s and extend the spike in w. */
53 for (i = j; i < m; ++i) {
54 temp = givens.c() * s(j, i) - givens.s() * w[i];
55 w[i] = givens.s() * s(j, i) + givens.c() * w[i];
56 s(j, i) = temp;
57 }
58 } else
59 v_givens[j] = IdentityRotation;
60 }
61
62 /* add the spike from the rank 1 update to w. */
63 w += v[n - 1] * u;
64
65 /* eliminate the spike. */
66 *sing = false;
67 for (j = 0; j < n - 1; ++j) {
68 if (w[j] != 0.) {
69 /* determine a givens rotation which eliminates the */
70 /* j-th element of the spike. */
71 givens.makeGivens(-s(j, j), w[j]);
72
73 /* apply the transformation to s and reduce the spike in w. */
74 for (i = j; i < m; ++i) {
75 temp = givens.c() * s(j, i) + givens.s() * w[i];
76 w[i] = -givens.s() * s(j, i) + givens.c() * w[i];
77 s(j, i) = temp;
78 }
79
80 /* store the information necessary to recover the */
81 /* givens rotation. */
82 w_givens[j] = givens;
83 } else
84 w_givens[j] = IdentityRotation;
85
86 /* test for zero diagonal elements in the output s. */
87 if (s(j, j) == 0.) {
88 *sing = true;
89 }
90 }
91 /* move w back into the last column of the output s. */
92 s(n - 1, n - 1) = w[n - 1];
93
94 if (s(j, j) == 0.) {
95 *sing = true;
96 }
97 return;
98}
99
100} // end namespace internal
101
102} // end namespace Eigen
103
104#endif // EIGEN_NONLINEAROPTIMIZATION_R1UPDT_H
Namespace containing all symbols from the Eigen library.