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