npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
truncatedInverseSqrt.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_TRUNCATEDINVERSESQRT_HH_
2 #define NPSTAT_TRUNCATEDINVERSESQRT_HH_
3 
4 /*!
5 // \file truncatedInverseSqrt.hh
6 //
7 // \brief Utility for truncating square roots of symmetric
8 // positive-semidefinite matrices
9 //
10 // Author: I. Volobouev
11 //
12 // July 2018
13 */
14 
15 #include "npstat/nm/Matrix.hh"
16 
17 namespace npstat {
18  /**
19  // The following function constructs an inverse square root of
20  // a symmetric positive semidefinite matrix (given by the "covmat"
21  // argument) by keeping only a certain number of eigenvectors
22  // corresponding to the largest eigenvalues. The number of
23  // eigenvectors to keep is given by the "nEigenvectorsToKeep"
24  // argument. The "result" matrix will have each _row_ set to a kept
25  // eigenvector multiplied by the inverse square root of the
26  // corresponding eigenvalue.
27  //
28  // If the egenvalue to keep is 0 or negative, it will be
29  // converted into the product of the largest eigenvalue times
30  // "eigenvaluePrecision". "eigenvaluePrecision" argument must
31  // not be negative.
32  //
33  // The function returns the ratio of the sum of the rejected
34  // eigenvalues of the "covmat" matrix to the total sum of its
35  // eigenvalues. In this calculation, all negative eigenvalues
36  // are assumed to be due to round-off, so they are converted to 0.
37  //
38  // std::invalid_argument will be thrown in case something is
39  // wrong with the arguments (e.g., the input matrix is not square)
40  // and if the largest eigenvalue is not positive.
41  //
42  // Intended for use in linear least squares problems with
43  // degenerate covariance matrices when the "proper" number
44  // of degrees of freedom is known in advance.
45  //
46  // If desired, the function can return the number of eigenvalues
47  // affected by the "eigenvaluePrecision" cutoff.
48  */
50  unsigned nEigenvectorsToKeep,
51  double eigenvaluePrecision,
52  Matrix<lapack_double>* result,
53  unsigned* numEigenvaluesAdjusted = 0,
54  EigenMethod m = EIGEN_SIMPLE);
55 
56 #ifdef SWIG
57  inline Matrix<double> truncatedInverseSqrt_2(const Matrix<double>& covmat,
58  unsigned nEigenvectorsToKeep,
59  double eigenvaluePrecision,
60  double* eigenRatio,
61  int* numEigenvaluesAdjusted)
62  {
63  Matrix<lapack_double> result;
64  unsigned u = 0;
65  *eigenRatio = truncatedInverseSqrt(covmat, nEigenvectorsToKeep,
66  eigenvaluePrecision, &result, &u);
67  *numEigenvaluesAdjusted = u;
68  return result;
69  }
70 #endif // SWIG
71 }
72 
73 #endif // NPSTAT_TRUNCATEDINVERSESQRT_HH_
Template matrix class.
Definition: AbsArrayProjector.hh:14
double truncatedInverseSqrt(const Matrix< lapack_double > &covmat, unsigned nEigenvectorsToKeep, double eigenvaluePrecision, Matrix< lapack_double > *result, unsigned *numEigenvaluesAdjusted=0, EigenMethod m=EIGEN_SIMPLE)
EigenMethod
Definition: EigenMethod.hh:27