Eigen-Contrib  5.0.1-dev+master
 
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 using Index = DenseIndex;
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 // MINPACK's test: the rank ends at the first negligible pivot, and a NaN pivot propagates.
35 if (abs(r(k, k)) <= tolr) break;
36 r(k, k) = 1. / r(k, k);
37 for (j = 0; j <= k - 1; ++j) {
38 temp = r(k, k) * r(j, k);
39 r(j, k) = 0.;
40 r.col(k).head(j + 1) -= r.col(j).head(j + 1) * temp;
41 }
42 l = k;
43 }
44
45 /* form the full upper triangle of the inverse of (r transpose)*r */
46 /* in the full upper triangle of r. */
47 for (k = 0; k <= l; ++k) {
48 for (j = 0; j <= k - 1; ++j) r.col(j).head(j + 1) += r.col(k).head(j + 1) * r(j, k);
49 r.col(k).head(k + 1) *= r(k, k);
50 }
51
52 /* form the full lower triangle of the covariance matrix */
53 /* in the strict lower triangle of r and in wa. */
54 for (j = 0; j < n; ++j) {
55 jj = ipvt[j];
56 sing = j > l;
57 for (i = 0; i <= j; ++i) {
58 if (sing) r(i, j) = 0.;
59 ii = ipvt[i];
60 if (ii > jj) r(ii, jj) = r(i, j);
61 if (ii < jj) r(jj, ii) = r(i, j);
62 }
63 wa[jj] = r(j, j);
64 }
65
66 /* symmetrize the covariance matrix in r. */
67 r.topLeftCorner(n, n).template triangularView<StrictlyUpper>() = r.topLeftCorner(n, n).transpose();
68 r.diagonal() = wa;
69}
70
71} // end namespace internal
72
73} // end namespace Eigen
74
75#endif // EIGEN_NONLINEAROPTIMIZATION_COVAR_H
Matrix< int, Dynamic, 1 > VectorXi
Namespace containing all symbols from the Eigen library.