CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
invertPosDefMatrix.h
Go to the documentation of this file.
1 #ifndef DataFormat_Math_invertPosDefMatrix_H
2 #define DataFormat_Math_invertPosDefMatrix_H
3 
4 #include "Math/SMatrix.h"
6 
7 template<typename T,unsigned int N>
8 inline bool invertPosDefMatrix(ROOT::Math::SMatrix<T,N,N,ROOT::Math::MatRepSym<T,N> > & m) {
9 
11  if (!decomp) {
12  return m.Invert();
13  } else
14  decomp.Invert(m);
15  return true;
16 
17 }
18 
19 template<typename T,unsigned int N>
20 inline bool invertPosDefMatrix(ROOT::Math::SMatrix<T,N,N,ROOT::Math::MatRepSym<T,N> > const & mIn,
21  ROOT::Math::SMatrix<T,N,N,ROOT::Math::MatRepSym<T,N> > & mOut) {
22 
24  if (!decomp) {
25  mOut=mIn;
26  return mOut.Invert();
27  } else
28  decomp.Invert(mOut);
29  return true;
30 
31 }
32 
33 // here for a test
34 #if defined(__SSE3__)
35 #include <pmmintrin.h>
36 
37 namespace mathSSE {
38  struct M2 {
39  // the matrix is shuffed
40  struct M {double m00,m01,m11,m10;};
41  union {
42  double m[4];
43  __m128d r[2];
44  M mm;
45  };
46 
47 
48  // load shuffled
49  inline M2(double i00, double i01, double i10, double i11) {
50  mm.m00=i00; mm.m01=i01; mm.m11=i11; mm.m10=i10; }
51 
52  double & operator[](int i) { return m[i];}
53  __m128d & r0() { return r[0]; }
54  __m128d & r1() { return r[1]; }
55 
56  double operator[](int i) const { return m[i];}
57  __m128d const & r0() const { return r[0]; }
58  __m128d const & r1() const { return r[1]; }
59 
60 
61  // assume second row is already shuffled
62  inline bool invert() {
63  __m128d tmp = r1();
64  // mult and sub
65  __m128d det = _mm_mul_pd(r0(),tmp);
66  __m128d det2 = _mm_shuffle_pd(det,det,1);
67  // det and -det
68  det = _mm_sub_pd(det,det2);
69  // m0 /det, m1/-det -> m3, m2
70  r1() = _mm_div_pd(r0(),det);
71  // m3/det, m2/-det -> m0 m1
72  r0() = _mm_div_pd(tmp,det);
73  double d; _mm_store_sd(&d,det);
74  return !(0.==d);
75  }
76 
77  } __attribute__ ((aligned (16))) ;
78 }
79 
80 template<>
81 inline bool invertPosDefMatrix<double,2>(ROOT::Math::SMatrix<double,2,2,ROOT::Math::MatRepSym<double,2> > & m) {
82  mathSSE::M2 mm(m.Array()[0], m.Array()[1], m.Array()[1], m.Array()[2]);
83 
84  bool ok = mm.invert();
85  if (ok) {
86  m.Array()[0] = mm[0];
87  m.Array()[1] = mm[1];
88  m.Array()[2] = mm[2];
89  }
90  return ok;
91 }
92 
93 template<>
94 inline bool invertPosDefMatrix<double,2>(ROOT::Math::SMatrix<double,2,2,ROOT::Math::MatRepSym<double,2> > const & mIn,
95  ROOT::Math::SMatrix<double,2,2,ROOT::Math::MatRepSym<double,2> > & mOut) {
96 
97  mathSSE::M2 mm(mIn.Array()[0], mIn.Array()[1], mIn.Array()[1], mIn.Array()[2]);
98 
99  bool ok = mm.invert();
100  mOut.Array()[0] = mm[0];
101  mOut.Array()[1] = mm[1];
102  mOut.Array()[2] = mm[2];
103 
104  return ok;
105 }
106 
107 #endif
108 
109 
110 #endif
int i
Definition: DBlmapReader.cc:9
bool Invert(M &m) const
place the inverse into m
class to compute the Cholesky decomposition of a matrix
bool invertPosDefMatrix(ROOT::Math::SMatrix< T, N, N, ROOT::Math::MatRepSym< T, N > > &m)
struct mathSSE::Rot3 __attribute__
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
long double T