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