Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DMatrix2x2.h
Go to the documentation of this file.
1 #include <align_16.h>
2 
3 #ifndef USE_SSE2
4 
5 // Matrix class without SIMD instructions
6 
7 class DMatrix2x2{
8  public:
10  mA[0][0]=mA[0][1]=mA[1][0]=mA[1][1]=0.;
11  }
13  DMatrix2x2(double d11, double d12, double d21,
14  double d22){
15  mA[0][0]=d11;
16  mA[0][1]=d12;
17  mA[1][0]=d21;
18  mA[1][1]=d22;
19  }
20  // Access by row and column
21  double &operator() (int row, int col){
22  return mA[row][col];
23  }
24  double operator() (int row, int col) const{
25  return mA[row][col];
26  }
27  // Assignment operator
29  mA[0][0]=m1(0,0);
30  mA[1][0]=m1(1,0);
31  mA[0][1]=m1(0,1);
32  mA[1][1]=m1(1,1);
33  return *this;
34  }
35 
36  // unary minus
38  return DMatrix2x2(-mA[0][0],-mA[0][1],-mA[1][0],-mA[1][1]);
39  }
40  // Matrix multiplication: (2x2) x (2x2)
42  return DMatrix2x2(mA[0][0]*m2(0,0)+mA[0][1]*m2(1,0),
43  mA[0][0]*m2(0,1)+mA[0][1]*m2(1,1),
44  mA[1][0]*m2(0,0)+mA[1][1]*m2(1,0),
45  mA[1][0]*m2(0,1)+mA[1][1]*m2(1,1));
46  }
47  // Matrix multiplication: (2x2) x (2x1)
49  return DMatrix2x1(mA[0][0]*m2(0)+mA[0][1]*m2(1),
50  mA[1][0]*m2(0)+mA[1][1]*m2(1));
51 
52  }
53  // Matrix addition
55  return DMatrix2x2(mA[0][0]+m2(0,0),mA[0][1]+m2(0,1),mA[1][0]+m2(1,0),
56  mA[1][1]+m2(1,1));
57  }
58  // Matrix subtraction
60  return DMatrix2x2(mA[0][0]-m2(0,0),mA[0][1]-m2(0,1),mA[1][0]-m2(1,0),
61  mA[1][1]-m2(1,1));
62  }
63  // Matrix inversion
65  double one_over_det=1./(mA[0][0]*mA[1][1]-mA[0][1]*mA[1][0]);
66  return DMatrix2x2(one_over_det*mA[1][1],-one_over_det*mA[0][1],
67  -one_over_det*mA[1][0],one_over_det*mA[0][0]);
68 
69  }
70 
71  // Compute the determinant
72  double Determinant(){
73  return mA[0][0]*mA[1][1]-mA[0][1]*mA[1][0];
74  }
75 
76 
77  //Compute the chi2 contribution for a pair of hits with residual R and covariance "this"
78  double Chi2(const DMatrix2x1 &R) const{
79  return ( (R(0)*R(0)*mA[1][1]-R(0)*R(1)*(mA[0][1]+mA[1][0])
80  +R(1)*R(1)*mA[0][0])/
81  (mA[0][0]*mA[1][1]-mA[0][1]*mA[1][0]));
82  }
83 
84  void Print(){
85  cout << "DMatrix2x2:" <<endl;
86  cout << " | 0 | 1 |" <<endl;
87  cout << "----------------------------------" <<endl;
88  for (unsigned int i=0;i<2;i++){
89  cout <<" "<<i<<" |"<< setw(11)<<setprecision(4) << mA[i][0] <<" "
90  << setw(11)<<setprecision(4)<< mA[i][1]<< endl;
91  }
92  }
93 
94 
95  private:
96  double mA[2][2];
97 
98 };
99 
100 #else
101 
102 // Matrix class with SIMD instructions
103 
104 class DMatrix2x2{
105  public:
106  DMatrix2x2()
107  : mA( ALIGNED_16_BLOCK_PTR(union dvec, 2, mA) )
108  {
109  mA[0].v=_mm_setzero_pd();
110  mA[1].v=_mm_setzero_pd();
111  }
112  DMatrix2x2(double d11, double d12, double d21,
113  double d22)
114  : mA( ALIGNED_16_BLOCK_PTR(union dvec, 2, mA) )
115  {
116  mA[0].v=_mm_setr_pd(d11,d21);
117  mA[1].v=_mm_setr_pd(d12,d22);
118  }
119  DMatrix2x2(__m128d v1, __m128d v2)
120  : mA( ALIGNED_16_BLOCK_PTR(union dvec, 2, mA) )
121  {
122  mA[0].v=v1;
123  mA[1].v=v2;
124  }
125  DMatrix2x2(const DMatrix2x2& dm)
126  : mA( ALIGNED_16_BLOCK_PTR(union dvec, 2, mA) )
127  {
128  mA[0].v=dm.mA[0].v;
129  mA[1].v=dm.mA[1].v;
130  }
131  ~DMatrix2x2(){};
132 
133  __m128d GetV(int col) const{
134  return mA[col].v;
135  }
136 
137  // Access by row and column
138  double &operator() (int row, int col){
139  return mA[col].d[row];
140  }
141  double operator() (int row, int col) const{
142  return mA[col].d[row];
143  }
144 
145  // Assignment operator
146  DMatrix2x2 &operator=(const DMatrix2x2 &m1){
147  //mA[0].v=m1.GetV(0);
148  //mA[1].v=m1.GetV(1);
149  mA[0].v=m1.mA[0].v;
150  mA[1].v=m1.mA[1].v;
151  return *this;
152  }
153 
154  // unary minus
155  DMatrix2x2 operator-() const{
156  ALIGNED_16_BLOCK_WITH_PTR(__m128d, 1, p);
157  __m128d &zero=p[0];
158  zero=_mm_setzero_pd();
159  return DMatrix2x2(_mm_sub_pd(zero,mA[0].v),
160  _mm_sub_pd(zero,mA[1].v));
161  }
162  // Matrix multiplication: (2x2) x (2x2)
163  DMatrix2x2 operator*(const DMatrix2x2 &m2){
164  return
165  DMatrix2x2(_mm_add_pd(_mm_mul_pd(mA[0].v,
166  _mm_set1_pd(m2(0,0))),
167  _mm_mul_pd(mA[1].v,
168  _mm_set1_pd(m2(1,0)))),
169  _mm_add_pd(_mm_mul_pd(mA[0].v,
170  _mm_set1_pd(m2(0,1))),
171  _mm_mul_pd(mA[1].v,
172  _mm_set1_pd(m2(1,1)))));
173  }
174  // Matrix multiplication: (2x2) x (2x1)
175  DMatrix2x1 operator*(const DMatrix2x1 &m2){
176  return DMatrix2x1(_mm_add_pd(_mm_mul_pd(GetV(0),_mm_set1_pd(m2(0))),
177  _mm_mul_pd(GetV(1),_mm_set1_pd(m2(1)))));
178  }
179 
180  // Matrix addition
181  DMatrix2x2 operator+(const DMatrix2x2 &m2){
182  return DMatrix2x2(_mm_add_pd(mA[0].v,m2.mA[0].v),
183  _mm_add_pd(mA[1].v,m2.mA[1].v));
184  }
185  // matrix subtraction
186  DMatrix2x2 operator-(const DMatrix2x2 &m2){
187  return DMatrix2x2(_mm_sub_pd(mA[0].v,m2.mA[0].v),
188  _mm_sub_pd(mA[1].v,m2.mA[1].v));
189  }
190 
191  // Matrix inversion
192  DMatrix2x2 Invert(){
193  ALIGNED_16_BLOCK_WITH_PTR(__m128d, 1, p);
194  __m128d &scale=p[0];
195  scale=_mm_set1_pd(1./(mA[0].d[0]*mA[1].d[1]-mA[1].d[0]*mA[0].d[1]));
196  return DMatrix2x2( _mm_mul_pd(scale,_mm_setr_pd(mA[1].d[1],-mA[0].d[1])),
197  _mm_mul_pd(scale,_mm_setr_pd(-mA[1].d[0],mA[0].d[0])));
198 
199  }
200  // Compute the determinant
201  double Determinant(){
202  return mA[0].d[0]*mA[1].d[1]-mA[0].d[1]*mA[1].d[0];
203  }
204 
205 
206  //Compute the chi2 contribution for a pair of hits with residual R and covariance "this"
207  double Chi2(const DMatrix2x1 &R) const{
208  return ( (R(0)*R(0)*mA[1].d[1]-R(0)*R(1)*(mA[0].d[1]+mA[1].d[0])
209  +R(1)*R(1)*mA[0].d[0])/
210  (mA[0].d[0]*mA[1].d[1]-mA[0].d[1]*mA[1].d[0]));
211  }
212 
213  void Print(){
214  cout << "DMatrix2x2:" <<endl;
215  cout << " | 0 | 1 |" <<endl;
216  cout << "----------------------------------" <<endl;
217  for (unsigned int i=0;i<2;i++){
218  cout <<" "<<i<<" |"<< setw(11)<<setprecision(4) << mA[0].d[i] <<" "
219  << setw(11)<<setprecision(4)<< mA[1].d[i]<< endl;
220  }
221  }
222 
223  // private:
224  union dvec{
225  __m128d v;
226  double d[2];
227  };
228  ALIGNED_16_BLOCK(union dvec, 2, mA)
229 };
230 #endif
DMatrix2x2 Invert()
Definition: DMatrix2x2.h:64
DMatrix2x2()
Definition: DMatrix2x2.h:9
#define ALIGNED_16_BLOCK_PTR(TYPE, NUM, PTR)
Definition: align_16.h:24
DMatrix2x2(double d11, double d12, double d21, double d22)
Definition: DMatrix2x2.h:13
#define ALIGNED_16_BLOCK_WITH_PTR(TYPE, NUM, PTR)
Definition: align_16.h:27
DMatrix2x2 operator-() const
Definition: DMatrix2x2.h:37
double mA[2][2]
Definition: DMatrix2x2.h:96
double Determinant()
Definition: DMatrix2x2.h:72
double Chi2(const DMatrix2x1 &R) const
Definition: DMatrix2x2.h:78
double & operator()(int row, int col)
Definition: DMatrix2x2.h:21
DMatrix2x2 operator*(const DMatrix2x2 &m2)
Definition: DMatrix2x2.h:41
DMatrix2x2 operator+(const DMatrix2x2 &m2)
Definition: DMatrix2x2.h:54
DMatrix2x2 operator-(const DMatrix2x2 &m2)
Definition: DMatrix2x2.h:59
DMatrix2x1 operator*(const DMatrix2x1 &m2)
Definition: DMatrix2x2.h:48
DMatrix2x2 & operator=(const DMatrix2x2 &m1)
Definition: DMatrix2x2.h:28
#define ALIGNED_16_BLOCK(TYPE, NUM, PTR)
Definition: align_16.h:19
void Print()
Definition: DMatrix2x2.h:84