Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
LMcovar.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// This code initially comes from MINPACK whose original authors are:
5// Copyright Jorge More - Argonne National Laboratory
6// Copyright Burt Garbow - Argonne National Laboratory
7// Copyright Ken Hillstrom - Argonne National Laboratory
8//
9// This Source Code Form is subject to the terms of the Minpack license
10// (a BSD-like license) described in the accompanying CopyrightMINPACK.txt file.
11// SPDX-License-Identifier: MPL-2.0 AND LicenseRef-MINPACK
12
13#ifndef EIGEN_LMCOVAR_H
14#define EIGEN_LMCOVAR_H
15
16// IWYU pragma: private
17#include "./InternalHeaderCheck.h"
18
19namespace Eigen {
20
21namespace internal {
22
23template <typename Scalar>
24void covar(Matrix<Scalar, Dynamic, Dynamic>& r, const VectorXi& ipvt,
25 Scalar tol = numext::sqrt(NumTraits<Scalar>::epsilon())) {
26 using std::abs;
27 /* Local variables */
28 Index i, j, k, l, ii, jj;
29 bool sing;
30 Scalar temp;
31
32 /* Function Body */
33 const Index n = r.cols();
34 const Scalar tolr = tol * abs(r(0, 0));
35 Matrix<Scalar, Dynamic, 1> wa(n);
36 eigen_assert(ipvt.size() == n);
37
38 /* form the inverse of r in the full upper triangle of r. */
39 l = -1;
40 for (k = 0; k < n; ++k)
41 if (abs(r(k, k)) > tolr) {
42 r(k, k) = 1. / r(k, k);
43 for (j = 0; j <= k - 1; ++j) {
44 temp = r(k, k) * r(j, k);
45 r(j, k) = 0.;
46 r.col(k).head(j + 1) -= r.col(j).head(j + 1) * temp;
47 }
48 l = k;
49 }
50
51 /* form the full upper triangle of the inverse of (r transpose)*r */
52 /* in the full upper triangle of r. */
53 for (k = 0; k <= l; ++k) {
54 for (j = 0; j <= k - 1; ++j) r.col(j).head(j + 1) += r.col(k).head(j + 1) * r(j, k);
55 r.col(k).head(k + 1) *= r(k, k);
56 }
57
58 /* form the full lower triangle of the covariance matrix */
59 /* in the strict lower triangle of r and in wa. */
60 for (j = 0; j < n; ++j) {
61 jj = ipvt[j];
62 sing = j > l;
63 for (i = 0; i <= j; ++i) {
64 if (sing) r(i, j) = 0.;
65 ii = ipvt[i];
66 if (ii > jj) r(ii, jj) = r(i, j);
67 if (ii < jj) r(jj, ii) = r(i, j);
68 }
69 wa[jj] = r(j, j);
70 }
71
72 /* symmetrize the covariance matrix in r. */
73 r.topLeftCorner(n, n).template triangularView<StrictlyUpper>() = r.topLeftCorner(n, n).transpose();
74 r.diagonal() = wa;
75}
76
77} // end namespace internal
78
79} // end namespace Eigen
80
81#endif // EIGEN_LMCOVAR_H
Matrix< int, Dynamic, 1 > VectorXi
Namespace containing all symbols from the Eigen library.