CMS 3D CMS Logo

invertPosDefMatrix.h
Go to the documentation of this file.
1 #ifndef DataFormat_Math_invertPosDefMatrix_H
2 #define DataFormat_Math_invertPosDefMatrix_H
3 
4 #define SMATRIX_USE_CONSTEXPR
5 #include "Math/SMatrix.h"
6 #include "Math/CholeskyDecomp.h"
7 #include <type_traits>
8 
9 template <typename T, unsigned int N>
10 inline bool invertPosDefMatrix(ROOT::Math::SMatrix<T, N, N, ROOT::Math::MatRepSym<T, N> >& m) {
11  ROOT::Math::CholeskyDecomp<T, N> decomp(m);
12  if (!decomp) {
13  return m.Invert();
14  } else
15  decomp.Invert(m);
16  return true;
17 }
18 
19 template <typename PDM2>
20 void fastInvertPDM2(PDM2& mm) {
21  auto m = mm.Array();
22 
23  constexpr typename std::remove_reference<decltype(m[0])>::type one = 1.;
24  auto c0 = one / m[0];
25  auto c1 = m[1] * m[1] * c0;
26  auto c2 = one / (m[2] - c1);
27 
28  auto li21 = c1 * c0 * c2;
29  m[0] = li21 + c0;
30  m[1] = -m[1] * c0 * c2;
31  m[2] = c2;
32 }
33 
34 template <>
35 inline bool invertPosDefMatrix<double, 1>(ROOT::Math::SMatrix<double, 1, 1, ROOT::Math::MatRepSym<double, 1> >& m) {
36  m(0, 0) = 1. / m(0, 0);
37  return true;
38 }
39 template <>
40 inline bool invertPosDefMatrix<float, 1>(ROOT::Math::SMatrix<float, 1, 1, ROOT::Math::MatRepSym<float, 1> >& m) {
41  m(0, 0) = 1.f / m(0, 0);
42  return true;
43 }
44 
45 template <>
46 inline bool invertPosDefMatrix<double, 2>(ROOT::Math::SMatrix<double, 2, 2, ROOT::Math::MatRepSym<double, 2> >& m) {
48  return true;
49 }
50 template <>
51 inline bool invertPosDefMatrix<float, 2>(ROOT::Math::SMatrix<float, 2, 2, ROOT::Math::MatRepSym<float, 2> >& m) {
53  return true;
54 }
55 
56 template <typename T, unsigned int N>
57 inline bool invertPosDefMatrix(ROOT::Math::SMatrix<T, N, N, ROOT::Math::MatRepSym<T, N> > const& mIn,
58  ROOT::Math::SMatrix<T, N, N, ROOT::Math::MatRepSym<T, N> >& mOut) {
59  ROOT::Math::CholeskyDecomp<T, N> decomp(mIn);
60  if (!decomp) {
61  mOut = mIn;
62  return mOut.Invert();
63  } else
64  decomp.Invert(mOut);
65  return true;
66 }
67 
68 #endif
bool invertPosDefMatrix< double, 1 >(ROOT::Math::SMatrix< double, 1, 1, ROOT::Math::MatRepSym< double, 1 > > &m)
bool invertPosDefMatrix< double, 2 >(ROOT::Math::SMatrix< double, 2, 2, ROOT::Math::MatRepSym< double, 2 > > &m)
bool invertPosDefMatrix(ROOT::Math::SMatrix< T, N, N, ROOT::Math::MatRepSym< T, N > > &m)
#define N
Definition: blowfish.cc:9
bool invertPosDefMatrix< float, 1 >(ROOT::Math::SMatrix< float, 1, 1, ROOT::Math::MatRepSym< float, 1 > > &m)
bool invertPosDefMatrix< float, 2 >(ROOT::Math::SMatrix< float, 2, 2, ROOT::Math::MatRepSym< float, 2 > > &m)
long double T
void fastInvertPDM2(PDM2 &mm)