g4tools  5.4.0
mat
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_mat
5 #define tools_mat
6 
7 #ifdef TOOLS_MEM
8 #include "../mem"
9 #endif
10 
11 #include "MATCOM"
12 
13 //#include <cstring> //memcpy
14 
15 //#define TOOLS_MAT_NEW
16 namespace tools {
17 
18 template <class T,unsigned int D>
19 class mat {
20  static const unsigned int _D2 = D*D;
21  unsigned int dim2() const {return _D2;}
22 #define TOOLS_MAT_CLASS mat
24 #undef TOOLS_MAT_CLASS
25 private:
26 #ifdef TOOLS_MEM
27  static const std::string& s_class() {
28  static const std::string s_v("tools::mat");
29  return s_v;
30  }
31 #endif
32 public:
33  mat(
34 #ifdef TOOLS_MEM
35  bool a_inc = true
36 #endif
37  ) {
38 #ifdef TOOLS_MEM
39  if(a_inc) mem::increment(s_class().c_str());
40 #endif
41 #ifdef TOOLS_MAT_NEW
42  m_vec = new T[D*D];
43 #endif
44  for(unsigned int i=0;i<_D2;i++) m_vec[i] = zero();
45  }
46  virtual ~mat() {
47 #ifdef TOOLS_MAT_NEW
48  delete [] m_vec;
49 #endif
50 #ifdef TOOLS_MEM
51  mem::decrement(s_class().c_str());
52 #endif
53  }
54 public:
55  mat(const mat& a_from) {
56 #ifdef TOOLS_MEM
57  mem::increment(s_class().c_str());
58 #endif
59 #ifdef TOOLS_MAT_NEW
60  m_vec = new T[D*D];
61 #endif
62  _copy(a_from.m_vec);
63  }
64  mat& operator=(const mat& a_from){
65  if(&a_from==this) return *this;
66  _copy(a_from.m_vec);
67  return *this;
68  }
69 public:
70  mat(const T a_v[]){
71 #ifdef TOOLS_MEM
72  mem::increment(s_class().c_str());
73 #endif
74 #ifdef TOOLS_MAT_NEW
75  m_vec = new T[D*D];
76 #endif
77  _copy(a_v);
78  }
79 public:
80  unsigned int dimension() const {return D;}
81 protected:
82 #ifdef TOOLS_MAT_NEW
83  T* m_vec;
84 #else
85  T m_vec[D*D];
86 #endif
87 
88 private:static void check_instantiation() {mat<float,2> dummy;}
89 };
90 
91 template <class T>
92 class nmat {
93  unsigned int dim2() const {return m_D2;}
94 #define TOOLS_MAT_CLASS nmat
96 #undef TOOLS_MAT_CLASS
97 private:
98 #ifdef TOOLS_MEM
99  static const std::string& s_class() {
100  static const std::string s_v("tools::nmat");
101  return s_v;
102  }
103 #endif
104 public:
105  nmat(unsigned int a_D):m_D(a_D),m_D2(a_D*a_D),m_vec(0) {
106 #ifdef TOOLS_MEM
107  mem::increment(s_class().c_str());
108 #endif
109  m_vec = new T[m_D2];
110  for(unsigned int i=0;i<m_D2;i++) m_vec[i] = zero();
111  }
112  virtual ~nmat() {
113  delete [] m_vec;
114 #ifdef TOOLS_MEM
115  mem::decrement(s_class().c_str());
116 #endif
117  }
118 public:
119  nmat(const nmat& a_from)
120  :m_D(a_from.m_D),m_D2(a_from.m_D2),m_vec(0)
121  {
122 #ifdef TOOLS_MEM
123  mem::increment(s_class().c_str());
124 #endif
125  m_vec = new T[m_D2];
126  _copy(a_from.m_vec);
127  }
128  nmat& operator=(const nmat& a_from){
129  if(&a_from==this) return *this;
130  if(a_from.m_D!=m_D) {
131  m_D = a_from.m_D;
132  m_D2 = a_from.m_D2;
133  delete [] m_vec;
134  m_vec = new T[m_D2];
135  }
136  _copy(a_from.m_vec);
137  return *this;
138  }
139 public:
140  nmat(unsigned int a_D,const T a_v[])
141  :m_D(a_D),m_D2(a_D*a_D),m_vec(0)
142  {
143 #ifdef TOOLS_MEM
144  mem::increment(s_class().c_str());
145 #endif
146  m_vec = new T[m_D2];
147  _copy(a_v);
148  }
149 public:
150  unsigned int dimension() const {return m_D;}
151 protected:
152  unsigned int m_D;
153  unsigned int m_D2;
154  T* m_vec;
155 private:static void check_instantiation() {nmat<float> dummy(2);}
156 };
157 
158 template <class T,unsigned int D>
159 inline nmat<T> copy(const mat<T,D>& a_from) {
160  unsigned int D2 = D*D;
161  nmat<T> v(D);
162  for(unsigned int i=0;i<D2;i++) v[i] = a_from[i];
163  return v;
164 }
165 
166 template <class VECTOR>
167 inline void multiply(VECTOR& a_vec,const typename VECTOR::value_type& a_mat) {
168  //typedef typename VECTOR::size_type sz_t;
169  //sz_t number = a_vec.size();
170  //for(sz_t index=0;index<number;index++) a_vec[index] *= a_mat;
171  typedef typename VECTOR::iterator it_t;
172  for(it_t it=a_vec.begin();it!=a_vec.end();++it) *it *= a_mat;
173 }
174 
175 template <class VECTOR>
176 inline void multiply(VECTOR& a_vec,const typename VECTOR::value_type::elem_t& a_value) {
177  typedef typename VECTOR::iterator it_t;
178  for(it_t it=a_vec.begin();it!=a_vec.end();++it) (*it).multiply(a_value);
179 }
180 
184 
185 template <class MAT>
186 inline void conjugate(MAT& a_m,typename MAT::elem_t (*a_conj)(const typename MAT::elem_t&)) {
187  typedef typename MAT::elem_t T;
188  T* pos = const_cast<T*>(a_m.data());
189  unsigned int D2 = a_m.dimension()*a_m.dimension();
190  for(unsigned int i=0;i<D2;i++,pos++) {
191  *pos = a_conj(*pos); //T = std::complex<>
192  }
193 }
194 
195 template <class MAT>
196 inline bool is_real(MAT& a_m,typename MAT::elem_t::value_type (*a_imag)(const typename MAT::elem_t&)) {
197  typedef typename MAT::elem_t T;
198  T* pos = const_cast<T*>(a_m.data());
199  unsigned int D2 = a_m.dimension()*a_m.dimension();
200  for(unsigned int i=0;i<D2;i++,pos++) {if(a_imag(*pos)) return false;}
201  return true;
202 }
203 
204 template <class MAT,class PREC>
205 inline bool is_real_prec(MAT& a_m,typename MAT::elem_t::value_type (*a_imag)(const typename MAT::elem_t&),
206  const PREC& a_prec,PREC(*a_fabs)(const typename MAT::elem_t::value_type&)) {\
207  typedef typename MAT::elem_t T;
208  T* pos = const_cast<T*>(a_m.data());
209  unsigned int D2 = a_m.dimension()*a_m.dimension();
210  for(unsigned int i=0;i<D2;i++,pos++) {if(a_fabs(a_imag(*pos))>=a_prec) return false;}
211  return true;
212 }
213 
214 template <class MAT>
215 inline bool is_imag(MAT& a_m,typename MAT::elem_t::value_type (*a_real)(const typename MAT::elem_t&)) {
216  typedef typename MAT::elem_t T;
217  T* pos = const_cast<T*>(a_m.data());
218  unsigned int D2 = a_m.dimension()*a_m.dimension();
219  for(unsigned int i=0;i<D2;i++,pos++) {if(a_real(*pos)) return false;}
220  return true;
221 }
222 
223 template <class CMAT,class RMAT>
224 inline bool to_real(const CMAT& a_c,RMAT& a_r,typename CMAT::elem_t::value_type (*a_real)(const typename CMAT::elem_t&)) {
225  if(a_r.dimension()!=a_c.dimension()) return false;
226  typedef typename CMAT::elem_t CT;
227  const CT* cpos = a_c.data();
228  typedef typename RMAT::elem_t RT;
229  RT* rpos = const_cast<RT*>(a_r.data());
230  unsigned int D2 = a_c.dimension()*a_c.dimension();
231  for(unsigned int i=0;i<D2;i++,cpos++,rpos++) *rpos = a_real(*cpos);
232  return true;
233 }
234 
235 template <class RMAT,class CMAT>
236 inline bool to_complex(const RMAT& a_r,CMAT& a_c) {
237  if(a_c.dimension()!=a_r.dimension()) return false;
238  typedef typename RMAT::elem_t RT;
239  const RT* rpos = a_r.data();
240  typedef typename CMAT::elem_t CT;
241  CT* cpos = const_cast<CT*>(a_c.data());
242  unsigned int D2 = a_r.dimension()*a_r.dimension();
243  for(unsigned int i=0;i<D2;i++,rpos++,cpos++) *cpos = CT(*rpos);
244  return true;
245 }
246 
247 template <class MAT>
248 inline void dagger(MAT& a_m,typename MAT::elem_t (*a_conj)(const typename MAT::elem_t&)) {
249  conjugate<MAT>(a_m,a_conj);
250  a_m.transpose();
251 }
252 
253 template <class CMAT,class RMAT>
254 inline bool decomplex(const CMAT& a_c,RMAT& a_r,
255  typename CMAT::elem_t::value_type (*a_real)(const typename CMAT::elem_t&),
256  typename CMAT::elem_t::value_type (*a_imag)(const typename CMAT::elem_t&)) {
257  // CMAT = X+iY
258  // RMAT = | X Y |
259  // | -Y X |
260  typedef typename CMAT::elem_t CT; //std::complex<double>
261  typedef typename RMAT::elem_t RT; //double
262 
263  unsigned int cdim = a_c.dimension();
264  if(a_r.dimension()!=2*cdim) {a_r.set_zero();return false;}
265  RT value;unsigned int r,c;
266  for(r=0;r<cdim;r++) {
267  for(c=0;c<cdim;c++) {
268  const CT& cvalue = a_c.value(r,c);
269  value = a_real(cvalue);
270  a_r.set_value(r,c,value);
271  a_r.set_value(r+cdim,c+cdim,value);
272  value = a_imag(cvalue);
273  a_r.set_value(r,c+cdim,value);
274  a_r.set_value(r+cdim,c,-value);
275  }
276  }
277  return true;
278 }
279 
280 template <class VEC_CMAT,class VEC_RMAT>
281 inline bool decomplex(
282  const VEC_CMAT& a_vc,VEC_RMAT& a_vr
283 ,typename VEC_CMAT::value_type::elem_t::value_type (*a_real)(const typename VEC_CMAT::value_type::elem_t&)
284 ,typename VEC_CMAT::value_type::elem_t::value_type (*a_imag)(const typename VEC_CMAT::value_type::elem_t&)
285 ) {
286  // CMAT = X+iY
287  // RMAT = | X Y |
288  // | -Y X |
289  typedef typename VEC_CMAT::size_type sz_t;
290  sz_t number = a_vc.size();
291  a_vr.resize(number);
292  for(sz_t index=0;index<number;index++) {
293  if(!decomplex(a_vc[index],a_vr[index],a_real,a_imag)) {
294  a_vr.clear();
295  return false;
296  }
297  }
298  return true;
299 }
300 
304 
305 //for sf, mf :
306 //template <class T,unsigned int D>
307 //inline const T* get_data(const mat<T,D>& a_v) {return a_v.data();}
308 
309 template <class MAT>
310 inline MAT commutator(const MAT& a1,const MAT& a2) {
311  return a1*a2-a2*a1;
312 }
313 
314 template <class MAT>
315 inline MAT anticommutator(const MAT& a1,const MAT& a2) {
316  return a1*a2+a2*a1;
317 }
318 
319 template <class MAT>
320 inline void commutator(const MAT& a1,const MAT& a2,MAT& a_tmp,MAT& a_res) {
321  a_res = a1;
322  a_res *= a2;
323  a_tmp = a2;
324  a_tmp *= a1;
325  a_res -= a_tmp;
326 }
327 
328 template <class MAT,class T>
329 inline void commutator(const MAT& a1,const MAT& a2,MAT& a_tmp,T a_vtmp[],MAT& a_res) {
330  a_res = a1;
331  a_res.mul_mtx(a2,a_vtmp);
332  a_tmp = a2;
333  a_tmp.mul_mtx(a1,a_vtmp);
334  a_res -= a_tmp;
335 }
336 
337 template <class MAT>
338 inline void anticommutator(const MAT& a1,const MAT& a2,MAT& a_tmp,MAT& a_res) {
339  a_res = a1;
340  a_res *= a2;
341  a_tmp = a2;
342  a_tmp *= a1;
343  a_res += a_tmp;
344 }
345 
346 template <class MAT,class T>
347 inline void anticommutator(const MAT& a1,const MAT& a2,MAT& a_tmp,T a_vtmp[],MAT& a_res) {
348  a_res = a1;
349  a_res.mul_mtx(a2,a_vtmp);
350  a_tmp = a2;
351  a_tmp.mul_mtx(a1,a_vtmp);
352  a_res += a_tmp;
353 }
354 
355 template <class T,unsigned int D>
356 inline bool commutator_equal(const mat<T,D>& a_1,const mat<T,D>& a_2,const mat<T,D>& a_c,const T& a_prec) {
357  unsigned int order = D;
358  const T* p1 = a_1.data();
359  const T* p2 = a_2.data();
360  const T* pc = a_c.data();
361  for(unsigned int r=0;r<order;r++) {
362  for(unsigned int c=0;c<order;c++) {
363  T _12 = T();
364  {for(unsigned int i=0;i<order;i++) {
365  _12 += (*(p1+r+i*order)) * (*(p2+i+c*order));
366  }}
367  T _21 = T();
368  {for(unsigned int i=0;i<order;i++) {
369  _21 += (*(p2+r+i*order)) * (*(p1+i+c*order));
370  }}
371  T diff = (_12-_21) - *(pc+r+c*order);
372  if(diff<T()) diff *= T(-1);
373  if(diff>=a_prec) return false;
374  }
375  }
376  return true;
377 }
378 
379 template <class T,unsigned int D>
380 inline bool anticommutator_equal(const mat<T,D>& a_1,const mat<T,D>& a_2,const mat<T,D>& a_c,const T& a_prec) {
381  unsigned int order = D;
382  const T* p1 = a_1.data();
383  const T* p2 = a_2.data();
384  const T* pc = a_c.data();
385  for(unsigned int r=0;r<order;r++) {
386  for(unsigned int c=0;c<order;c++) {
387  T _12 = T();
388  {for(unsigned int i=0;i<order;i++) {
389  _12 += (*(p1+r+i*order)) * (*(p2+i+c*order));
390  }}
391  T _21 = T();
392  {for(unsigned int i=0;i<order;i++) {
393  _21 += (*(p2+r+i*order)) * (*(p1+i+c*order));
394  }}
395  T diff = (_12+_21) - *(pc+r+c*order);
396  if(diff<T()) diff *= T(-1);
397  if(diff>=a_prec) return false;
398  }
399  }
400  return true;
401 }
402 
403 template <class T,unsigned int D>
404 inline mat<T,D> operator-(const mat<T,D>& a1,const mat<T,D>& a2) {
405  mat<T,D> res(a1);
406  res -= a2;
407  return res;
408 }
409 template <class T,unsigned int D>
410 inline mat<T,D> operator+(const mat<T,D>& a1,const mat<T,D>& a2) {
411  mat<T,D> res(a1);
412  res += a2;
413  return res;
414 }
415 template <class T,unsigned int D>
416 inline mat<T,D> operator*(const mat<T,D>& a1,const mat<T,D>& a2) {
417  mat<T,D> res(a1);
418  res *= a2;
419  return res;
420 }
421 template <class T,unsigned int D>
422 inline mat<T,D> operator*(const T& a_fac,const mat<T,D>& a_m) {
423  mat<T,D> res(a_m);
424  res *= a_fac;
425  return res;
426 }
427 /*
428 template <class T,unsigned int D>
429 inline mat<T,D> operator*(const mat<T,D>& a_m,const T& a_fac) {
430  mat<T,D> res(a_m);
431  res *= a_fac;
432  return res;
433 }
434 */
435 
436 template <class T>
437 inline nmat<T> operator-(const nmat<T>& a1,const nmat<T>& a2) {
438  nmat<T> res(a1);
439  res -= a2;
440  return res;
441 }
442 template <class T>
443 inline nmat<T> operator+(const nmat<T>& a1,const nmat<T>& a2) {
444  nmat<T> res(a1);
445  res += a2;
446  return res;
447 }
448 template <class T>
449 inline nmat<T> operator*(const nmat<T>& a1,const nmat<T>& a2) {
450  nmat<T> res(a1);
451  res *= a2;
452  return res;
453 }
454 template <class T>
455 inline nmat<T> operator*(const T& a_fac,const nmat<T>& a_m) {
456  nmat<T> res(a_m);
457  res *= a_fac;
458  return res;
459 }
460 /*
461 template <class T>
462 inline nmat<T> operator*(const nmat<T>& a_m,const T& a_fac) {
463  nmat<T> res(a_m);
464  res *= a_fac;
465  return res;
466 }
467 */
468 
469 template <class MAT,class REAL>
470 inline bool mat_fabs(const MAT& a_in,MAT& a_ou,REAL(*a_fabs)(const typename MAT::elem_t&)) {
471  if(a_in.dimension()!=a_ou.dimension()) {a_ou.set_zero();return false;}
472  typedef typename MAT::elem_t T;
473  T* in_pos = const_cast<T*>(a_in.data());
474  T* ou_pos = const_cast<T*>(a_ou.data());
475  unsigned int D2 = a_in.dimension()*a_in.dimension();
476  for(unsigned int i=0;i<D2;i++,in_pos++,ou_pos++) {*ou_pos = a_fabs(*in_pos);}
477  return true;
478 }
479 
480 }
481 
482 /*
483 #include <cstdarg>
484 
485 namespace tools {
486 
487 template <class MAT>
488 inline void matrix_va_set(MAT& a_m,size_t a_dummy,...) {
489  typedef typename MAT::elem_t T;
490  va_list args;
491  va_start(args,a_dummy);
492  unsigned int D = a_m.dimension();
493  for(unsigned int r=0;r<D;r++) {
494  for(unsigned int c=0;c<D;c++) {
495  a_m.set_value(r,c,va_arg(args,T));
496  }
497  }
498  va_end(args);
499 }
500 
501 }
502 */
503 
504 namespace tools {
505 
506 #define TOOLS_MELEM const typename MAT::elem_t&
507 
511 template <class MAT>
512 inline void matrix_set(MAT& a_m
513 ,TOOLS_MELEM a_00,TOOLS_MELEM a_01
514 ,TOOLS_MELEM a_10,TOOLS_MELEM a_11
515 ){
516  //a_<R><C>
517  //vec[R + C * 2];
518  typename MAT::elem_t* vec = const_cast<typename MAT::elem_t*>(a_m.data());
519  vec[0] = a_00;vec[2] = a_01;
520  vec[1] = a_10;vec[3] = a_11;
521 }
522 
526 template <class MAT>
527 inline void matrix_set(MAT& a_m
528 ,TOOLS_MELEM a_00,TOOLS_MELEM a_01,TOOLS_MELEM a_02 //1 row
529 ,TOOLS_MELEM a_10,TOOLS_MELEM a_11,TOOLS_MELEM a_12 //2 row
530 ,TOOLS_MELEM a_20,TOOLS_MELEM a_21,TOOLS_MELEM a_22 //3 row
531 ){
532  //a_<R><C>
533  //vec[R + C * 3];
534  typename MAT::elem_t* vec = const_cast<typename MAT::elem_t*>(a_m.data());
535  vec[0] = a_00;vec[3] = a_01;vec[6] = a_02;
536  vec[1] = a_10;vec[4] = a_11;vec[7] = a_12;
537  vec[2] = a_20;vec[5] = a_21;vec[8] = a_22;
538 }
539 
543 template <class MAT>
544 inline void matrix_set(MAT& a_m
545 ,TOOLS_MELEM a_00,TOOLS_MELEM a_01,TOOLS_MELEM a_02,TOOLS_MELEM a_03 //1 row
546 ,TOOLS_MELEM a_10,TOOLS_MELEM a_11,TOOLS_MELEM a_12,TOOLS_MELEM a_13 //2 row
547 ,TOOLS_MELEM a_20,TOOLS_MELEM a_21,TOOLS_MELEM a_22,TOOLS_MELEM a_23 //3 row
548 ,TOOLS_MELEM a_30,TOOLS_MELEM a_31,TOOLS_MELEM a_32,TOOLS_MELEM a_33 //4 row
549 ){
550  //a_<R><C>
551  //vec[R + C * 4];
552  typename MAT::elem_t* vec = const_cast<typename MAT::elem_t*>(a_m.data());
553  vec[0] = a_00;vec[4] = a_01;vec[ 8] = a_02;vec[12] = a_03;
554  vec[1] = a_10;vec[5] = a_11;vec[ 9] = a_12;vec[13] = a_13;
555  vec[2] = a_20;vec[6] = a_21;vec[10] = a_22;vec[14] = a_23;
556  vec[3] = a_30;vec[7] = a_31;vec[11] = a_32;vec[15] = a_33;
557 }
558 
562 template <class MAT>
563 inline void matrix_set(MAT& a_m
564 ,TOOLS_MELEM a_00,TOOLS_MELEM a_01,TOOLS_MELEM a_02,TOOLS_MELEM a_03,TOOLS_MELEM a_04 //1 row
565 ,TOOLS_MELEM a_10,TOOLS_MELEM a_11,TOOLS_MELEM a_12,TOOLS_MELEM a_13,TOOLS_MELEM a_14 //2 row
566 ,TOOLS_MELEM a_20,TOOLS_MELEM a_21,TOOLS_MELEM a_22,TOOLS_MELEM a_23,TOOLS_MELEM a_24 //3 row
567 ,TOOLS_MELEM a_30,TOOLS_MELEM a_31,TOOLS_MELEM a_32,TOOLS_MELEM a_33,TOOLS_MELEM a_34 //4 row
568 ,TOOLS_MELEM a_40,TOOLS_MELEM a_41,TOOLS_MELEM a_42,TOOLS_MELEM a_43,TOOLS_MELEM a_44 //5 row
569 ){
570  //a_<R><C>
571  //vec[R + C * 5];
572  typename MAT::elem_t* vec = const_cast<typename MAT::elem_t*>(a_m.data());
573  vec[0] = a_00;vec[5] = a_01;vec[10] = a_02;vec[15] = a_03;vec[20] = a_04;
574  vec[1] = a_10;vec[6] = a_11;vec[11] = a_12;vec[16] = a_13;vec[21] = a_14;
575  vec[2] = a_20;vec[7] = a_21;vec[12] = a_22;vec[17] = a_23;vec[22] = a_24;
576  vec[3] = a_30;vec[8] = a_31;vec[13] = a_32;vec[18] = a_33;vec[23] = a_34;
577  vec[4] = a_40;vec[9] = a_41;vec[14] = a_42;vec[19] = a_43;vec[24] = a_44;
578 }
579 
583 template <class MAT>
584 inline void matrix_set(MAT& a_m
585 ,TOOLS_MELEM a_00,TOOLS_MELEM a_01,TOOLS_MELEM a_02,TOOLS_MELEM a_03,TOOLS_MELEM a_04,TOOLS_MELEM a_05 //1 row
586 ,TOOLS_MELEM a_10,TOOLS_MELEM a_11,TOOLS_MELEM a_12,TOOLS_MELEM a_13,TOOLS_MELEM a_14,TOOLS_MELEM a_15 //2 row
587 ,TOOLS_MELEM a_20,TOOLS_MELEM a_21,TOOLS_MELEM a_22,TOOLS_MELEM a_23,TOOLS_MELEM a_24,TOOLS_MELEM a_25 //3 row
588 ,TOOLS_MELEM a_30,TOOLS_MELEM a_31,TOOLS_MELEM a_32,TOOLS_MELEM a_33,TOOLS_MELEM a_34,TOOLS_MELEM a_35 //4 row
589 ,TOOLS_MELEM a_40,TOOLS_MELEM a_41,TOOLS_MELEM a_42,TOOLS_MELEM a_43,TOOLS_MELEM a_44,TOOLS_MELEM a_45 //5 row
590 ,TOOLS_MELEM a_50,TOOLS_MELEM a_51,TOOLS_MELEM a_52,TOOLS_MELEM a_53,TOOLS_MELEM a_54,TOOLS_MELEM a_55 //6 row
591 ){
592  //a_<R><C>
593  //vec[R + C * 6];
594  typename MAT::elem_t* vec = const_cast<typename MAT::elem_t*>(a_m.data());
595  vec[0] = a_00;vec[ 6] = a_01;vec[12] = a_02;vec[18] = a_03;vec[24] = a_04;vec[30] = a_05;
596  vec[1] = a_10;vec[ 7] = a_11;vec[13] = a_12;vec[19] = a_13;vec[25] = a_14;vec[31] = a_15;
597  vec[2] = a_20;vec[ 8] = a_21;vec[14] = a_22;vec[20] = a_23;vec[26] = a_24;vec[32] = a_25;
598  vec[3] = a_30;vec[ 9] = a_31;vec[15] = a_32;vec[21] = a_33;vec[27] = a_34;vec[33] = a_35;
599  vec[4] = a_40;vec[10] = a_41;vec[16] = a_42;vec[22] = a_43;vec[28] = a_44;vec[34] = a_45;
600  vec[5] = a_50;vec[11] = a_51;vec[17] = a_52;vec[23] = a_53;vec[29] = a_54;vec[35] = a_55;
601 }
602 
606 template <class MAT>
607 inline void matrix_set(MAT& a_m
608 ,TOOLS_MELEM a_00,TOOLS_MELEM a_01,TOOLS_MELEM a_02,TOOLS_MELEM a_03,TOOLS_MELEM a_04,TOOLS_MELEM a_05,TOOLS_MELEM a_06,TOOLS_MELEM a_07,TOOLS_MELEM a_08,TOOLS_MELEM a_09 //1 row
609 ,TOOLS_MELEM a_10,TOOLS_MELEM a_11,TOOLS_MELEM a_12,TOOLS_MELEM a_13,TOOLS_MELEM a_14,TOOLS_MELEM a_15,TOOLS_MELEM a_16,TOOLS_MELEM a_17,TOOLS_MELEM a_18,TOOLS_MELEM a_19 //2 row
610 ,TOOLS_MELEM a_20,TOOLS_MELEM a_21,TOOLS_MELEM a_22,TOOLS_MELEM a_23,TOOLS_MELEM a_24,TOOLS_MELEM a_25,TOOLS_MELEM a_26,TOOLS_MELEM a_27,TOOLS_MELEM a_28,TOOLS_MELEM a_29 //3 row
611 ,TOOLS_MELEM a_30,TOOLS_MELEM a_31,TOOLS_MELEM a_32,TOOLS_MELEM a_33,TOOLS_MELEM a_34,TOOLS_MELEM a_35,TOOLS_MELEM a_36,TOOLS_MELEM a_37,TOOLS_MELEM a_38,TOOLS_MELEM a_39 //4 row
612 ,TOOLS_MELEM a_40,TOOLS_MELEM a_41,TOOLS_MELEM a_42,TOOLS_MELEM a_43,TOOLS_MELEM a_44,TOOLS_MELEM a_45,TOOLS_MELEM a_46,TOOLS_MELEM a_47,TOOLS_MELEM a_48,TOOLS_MELEM a_49 //5 row
613 ,TOOLS_MELEM a_50,TOOLS_MELEM a_51,TOOLS_MELEM a_52,TOOLS_MELEM a_53,TOOLS_MELEM a_54,TOOLS_MELEM a_55,TOOLS_MELEM a_56,TOOLS_MELEM a_57,TOOLS_MELEM a_58,TOOLS_MELEM a_59 //6 row
614 ,TOOLS_MELEM a_60,TOOLS_MELEM a_61,TOOLS_MELEM a_62,TOOLS_MELEM a_63,TOOLS_MELEM a_64,TOOLS_MELEM a_65,TOOLS_MELEM a_66,TOOLS_MELEM a_67,TOOLS_MELEM a_68,TOOLS_MELEM a_69 //7 row
615 ,TOOLS_MELEM a_70,TOOLS_MELEM a_71,TOOLS_MELEM a_72,TOOLS_MELEM a_73,TOOLS_MELEM a_74,TOOLS_MELEM a_75,TOOLS_MELEM a_76,TOOLS_MELEM a_77,TOOLS_MELEM a_78,TOOLS_MELEM a_79 //8 row
616 ,TOOLS_MELEM a_80,TOOLS_MELEM a_81,TOOLS_MELEM a_82,TOOLS_MELEM a_83,TOOLS_MELEM a_84,TOOLS_MELEM a_85,TOOLS_MELEM a_86,TOOLS_MELEM a_87,TOOLS_MELEM a_88,TOOLS_MELEM a_89 //9 row
617 ,TOOLS_MELEM a_90,TOOLS_MELEM a_91,TOOLS_MELEM a_92,TOOLS_MELEM a_93,TOOLS_MELEM a_94,TOOLS_MELEM a_95,TOOLS_MELEM a_96,TOOLS_MELEM a_97,TOOLS_MELEM a_98,TOOLS_MELEM a_99 //10 row
618 
619 ){
620  //a_<R><C>
621  //vec[R + C * 10];
622  typename MAT::elem_t* vec = const_cast<typename MAT::elem_t*>(a_m.data());
623  vec[0] = a_00;vec[10] = a_01;vec[20] = a_02;vec[30] = a_03;vec[40] = a_04;vec[50] = a_05;vec[60] = a_06;vec[70] = a_07;vec[80] = a_08;vec[90] = a_09;
624  vec[1] = a_10;vec[11] = a_11;vec[21] = a_12;vec[31] = a_13;vec[41] = a_14;vec[51] = a_15;vec[61] = a_16;vec[71] = a_17;vec[81] = a_18;vec[91] = a_19;
625  vec[2] = a_20;vec[12] = a_21;vec[22] = a_22;vec[32] = a_23;vec[42] = a_24;vec[52] = a_25;vec[62] = a_26;vec[72] = a_27;vec[82] = a_28;vec[92] = a_29;
626  vec[3] = a_30;vec[13] = a_31;vec[23] = a_32;vec[33] = a_33;vec[43] = a_34;vec[53] = a_35;vec[63] = a_36;vec[73] = a_37;vec[83] = a_38;vec[93] = a_39;
627  vec[4] = a_40;vec[14] = a_41;vec[24] = a_42;vec[34] = a_43;vec[44] = a_44;vec[54] = a_45;vec[64] = a_46;vec[74] = a_47;vec[84] = a_48;vec[94] = a_49;
628  vec[5] = a_50;vec[15] = a_51;vec[25] = a_52;vec[35] = a_53;vec[45] = a_54;vec[55] = a_55;vec[65] = a_56;vec[75] = a_57;vec[85] = a_58;vec[95] = a_59;
629  vec[6] = a_60;vec[16] = a_61;vec[26] = a_62;vec[36] = a_63;vec[46] = a_64;vec[56] = a_65;vec[66] = a_66;vec[76] = a_67;vec[86] = a_68;vec[96] = a_69;
630  vec[7] = a_70;vec[17] = a_71;vec[27] = a_72;vec[37] = a_73;vec[47] = a_74;vec[57] = a_75;vec[67] = a_76;vec[77] = a_77;vec[87] = a_78;vec[97] = a_79;
631  vec[8] = a_80;vec[18] = a_81;vec[28] = a_82;vec[38] = a_83;vec[48] = a_84;vec[58] = a_85;vec[68] = a_86;vec[78] = a_87;vec[88] = a_88;vec[98] = a_89;
632  vec[9] = a_90;vec[19] = a_91;vec[29] = a_92;vec[39] = a_93;vec[49] = a_94;vec[59] = a_95;vec[69] = a_96;vec[79] = a_97;vec[89] = a_98;vec[99] = a_99;
633 }
634 
635 #undef TOOLS_MELEM
636 
637 }
638 
642 #include <ostream>
643 
644 namespace tools {
645 
646 //NOTE : print is a Python keyword.
647 template <class MAT>
648 inline void dump(std::ostream& a_out,const std::string& aCMT,const MAT& a_matrix) {
649  if(aCMT.size()) a_out << aCMT << std::endl;
650  unsigned int D = a_matrix.dimension();
651  for(unsigned int r=0;r<D;r++) {
652  for(unsigned int c=0;c<D;c++) {
653  a_out << " " << a_matrix.value(r,c);
654  }
655  a_out << std::endl;
656  }
657 }
658 
659 template <class MAT>
660 inline bool check_invert(const MAT& a_matrix,std::ostream& a_out) {
661  MAT I;I.set_identity();
662  MAT tmp;
663  if(!a_matrix.invert(tmp)) return false;
664  tmp.mul_mtx(a_matrix);
665  if(!tmp.equal(I)) {
666  dump(a_out,"problem with inv of :",a_matrix);
667  return false;
668  }
669  return true;
670 }
671 
672 }
673 
674 #endif
tools::nmat::operator=
nmat & operator=(const nmat &a_from)
Definition: mat:128
tools::nmat::nmat
nmat(unsigned int a_D)
Definition: mat:105
tools::is_real_prec
bool is_real_prec(MAT &a_m, typename MAT::elem_t::value_type(*a_imag)(const typename MAT::elem_t &), const PREC &a_prec, PREC(*a_fabs)(const typename MAT::elem_t::value_type &))
Definition: mat:205
tools::value
Definition: value:18
tools::dagger
void dagger(MAT &a_m, typename MAT::elem_t(*a_conj)(const typename MAT::elem_t &))
Definition: mat:248
MATCOM
tools::mat::~mat
virtual ~mat()
Definition: mat:46
tools::mat::m_vec
T m_vec[D *D]
Definition: mat:85
tools::nmat::m_D2
unsigned int m_D2
Definition: mat:153
tools::operator*
array< T > operator*(const T &a_fac, const array< T > &a_m)
Definition: array:617
tools::is_imag
bool is_imag(MAT &a_m, typename MAT::elem_t::value_type(*a_real)(const typename MAT::elem_t &))
Definition: mat:215
tools::decomplex
bool decomplex(const CMAT &a_c, RMAT &a_r, typename CMAT::elem_t::value_type(*a_real)(const typename CMAT::elem_t &), typename CMAT::elem_t::value_type(*a_imag)(const typename CMAT::elem_t &))
Definition: mat:254
tools::commutator_equal
bool commutator_equal(const mat< T, D > &a_1, const mat< T, D > &a_2, const mat< T, D > &a_c, const T &a_prec)
Definition: mat:356
tools::mat::mat
mat(const T a_v[])
Definition: mat:70
tools::nmat::dimension
unsigned int dimension() const
Definition: mat:150
tools::nmat::m_vec
T * m_vec
Definition: mat:154
tools::nmat::m_D
unsigned int m_D
Definition: mat:152
tools::dump
void dump(std::ostream &a_out, const tools::array< T > &a_array, const std::string &a_title)
Definition: array:519
TOOLS_MELEM
#define TOOLS_MELEM
Definition: mat:506
tools::copy
bool copy(T *&a_v, I a_n, const T *a_from)
Definition: carray:30
tools::mat::mat
mat()
Definition: mat:33
tools::mat_fabs
bool mat_fabs(const MAT &a_in, MAT &a_ou, REAL(*a_fabs)(const typename MAT::elem_t &))
Definition: mat:470
tools::is_real
bool is_real(MAT &a_m, typename MAT::elem_t::value_type(*a_imag)(const typename MAT::elem_t &))
Definition: mat:196
tools::commutator
MAT commutator(const MAT &a1, const MAT &a2)
Definition: mat:310
tools::operator-
array< T > operator-(const array< T > &a1, const array< T > &a2)
Definition: array:611
tools::mat::mat
mat(const mat &a_from)
Definition: mat:55
tools::to_complex
bool to_complex(const RMAT &a_r, CMAT &a_c)
Definition: mat:236
tools::mat::dimension
unsigned int dimension() const
Definition: mat:80
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::multiply
void multiply(VECTOR &a_vec, const typename VECTOR::value_type &a_mat)
Definition: mat:167
tools::diff
void diff(std::ostream &a_out, const array< T > &aA, const array< T > &aB, T a_epsilon)
Definition: array:529
tools::nmat
Definition: mat:92
tools::matrix_set
void matrix_set(MAT &a_m, TOOLS_MELEM a_00, TOOLS_MELEM a_01, TOOLS_MELEM a_10, TOOLS_MELEM a_11)
specific D=2 ///////////////////////////////
Definition: mat:512
tools::mat
Definition: mat:19
TOOLS_MATCOM
#define TOOLS_MATCOM
Definition: MATCOM:56
tools::nmat::nmat
nmat(unsigned int a_D, const T a_v[])
Definition: mat:140
tools::operator+
array< T > operator+(const array< T > &a1, const array< T > &a2)
Definition: array:605
tools::anticommutator
MAT anticommutator(const MAT &a1, const MAT &a2)
Definition: mat:315
tools::anticommutator_equal
bool anticommutator_equal(const mat< T, D > &a_1, const mat< T, D > &a_2, const mat< T, D > &a_c, const T &a_prec)
Definition: mat:380
tools::mat::operator=
mat & operator=(const mat &a_from)
Definition: mat:64
tools::to_real
bool to_real(const CMAT &a_c, RMAT &a_r, typename CMAT::elem_t::value_type(*a_real)(const typename CMAT::elem_t &))
Definition: mat:224
tools::value::value
value()
Definition: value:79
tools::nmat::~nmat
virtual ~nmat()
Definition: mat:112
tools::nmat::nmat
nmat(const nmat &a_from)
Definition: mat:119
tools::check_invert
bool check_invert(const MAT &a_matrix, std::ostream &a_out)
Definition: mat:660
tools::conjugate
void conjugate(MAT &a_m, typename MAT::elem_t(*a_conj)(const typename MAT::elem_t &))
related to complex numbers : //////////////////////////////////////////////////////
Definition: mat:186