Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
covar.h
1// IWYU pragma: private
2// SPDX-FileCopyrightText: The Eigen Authors
3// SPDX-License-Identifier: MPL-2.0
4
5#ifndef EIGEN_NONLINEAROPTIMIZATION_COVAR_H
6#define EIGEN_NONLINEAROPTIMIZATION_COVAR_H
7
8#include "./InternalHeaderCheck.h"
9
10namespace Eigen {
11
12namespace internal {
13
14template <typename Scalar>
15void covar(Matrix<Scalar, Dynamic, Dynamic> &r, const VectorXi &ipvt,
16 Scalar tol = numext::sqrt(NumTraits<Scalar>::epsilon())) {
17 using std::abs;
18 typedef DenseIndex Index;
19
20 /* Local variables */
21 Index i, j, k, l, ii, jj;
22 bool sing;
23 Scalar temp;
24
25 /* Function Body */
26 const Index n = r.cols();
27 const Scalar tolr = tol * abs(r(0, 0));
28 Matrix<Scalar, Dynamic, 1> wa(n);
29 eigen_assert(ipvt.size() == n);
30
31 /* form the inverse of r in the full upper triangle of r. */
32 l = -1;
33 for (k = 0; k < n; ++k)
34 if (abs(r(k, k)) > tolr) {
35 r(k, k) = 1. / r(k, k);
36 for (j = 0; j <= k - 1; ++j) {
37 temp = r(k, k) * r(j, k);
38 r(j, k) = 0.;
39 r.col(k).head(j + 1) -= r.col(j).head(j + 1) * temp;
40 }
41 l = k;
42 }
43
44 /* form the full upper triangle of the inverse of (r transpose)*r */
45 /* in the full upper triangle of r. */
46 for (k = 0; k <= l; ++k) {
47 for (j = 0; j <= k - 1; ++j) r.col(j).head(j + 1) += r.col(k).head(j + 1) * r(j, k);
48 r.col(k).head(k + 1) *= r(k, k);
49 }
50
51 /* form the full lower triangle of the covariance matrix */
52 /* in the strict lower triangle of r and in wa. */
53 for (j = 0; j < n; ++j) {
54 jj = ipvt[j];
55 sing = j > l;
56 for (i = 0; i <= j; ++i) {
57 if (sing) r(i, j) = 0.;
58 ii = ipvt[i];
59 if (ii > jj) r(ii, jj) = r(i, j);
60 if (ii < jj) r(jj, ii) = r(i, j);
61 }
62 wa[jj] = r(j, j);
63 }
64
65 /* symmetrize the covariance matrix in r. */
66 r.topLeftCorner(n, n).template triangularView<StrictlyUpper>() = r.topLeftCorner(n, n).transpose();
67 r.diagonal() = wa;
68}
69
70} // end namespace internal
71
72} // end namespace Eigen
73
74#endif // EIGEN_NONLINEAROPTIMIZATION_COVAR_H
Matrix< int, Dynamic, 1 > VectorXi
Namespace containing all symbols from the Eigen library.