Itasca C++ Interface
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
matrix.h
Go to the documentation of this file.
1 #pragma once
2 
8 #ifdef __LINUX
9 #include <string.h>
10 #endif
11 
12 #include "symtensor.h"
13 #include <cassert>
14 
21 template <class T,unsigned SX,unsigned SY=SX> class Matrix {
22 private:
24  class Column {
25  public:
27  Column(unsigned x,Matrix<T,SX,SY> &m) : x_(x), m_(m) { }
29  Column(const Column &r) : x_(r.x_), m_(r.m_) { }
31  const T &operator[](unsigned y) const { return m_.get(x_,y); }
33  T &operator[](unsigned y) { return m_.get(x_,y); }
34  private:
35  void operator=(const Column &r); // DISALLOWED
36  unsigned x_;
37  Matrix<T,SX,SY> &m_;
38  };
39  // Using template-meta programming to unroll innermost loop of matrix multiply
40  template <unsigned SZ,unsigned I,unsigned J,unsigned K>
41  class innerLoop {
42  public:
43  static double execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,SZ> &m) {
44  return l.t_[SX-I][SY-K]*m.t_[SY-K][SZ-J] + innerLoop<SZ,I,J,K-1>::execute(l,m);
45  }
46  };
47  template <unsigned SZ,unsigned I,unsigned J> // Termination specialization
48  class innerLoop<SZ,I,J,1> {
49  public:
50  static double execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,SZ> &m) {
51  return l.t_[SX-I][SY-1]*m.t_[SY-1][SZ-J];
52  }
53  };
54  template <unsigned I,unsigned K> // Specialization for column matrix (SZ=1)
55  class innerLoop<1,I,1,K> {
56  public:
57  static double execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,1> &m) {
58  return l.t_[SX-I][SY-K]*m.t_[SY-K] + innerLoop<1,I,1,K-1>::execute(l,m);
59  }
60  };
61  template <unsigned I> // termination specialization for column matrix specialization
62  class innerLoop<1,I,1,1> {
63  public:
64  static double execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,1> &m) {
65  return l.t_[SX-I][SY-1]*m.t_[SY-1];
66  }
67  };
68  // Using template meta-programming to unroll second loop of matrix multiply.
69  template <unsigned SZ,unsigned I,unsigned J>
70  class loop2Multiply {
71  public:
72  static void execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,SZ> &m,Matrix<T,SX,SZ> &ret) {
73  ret.t_[SX-I][SZ-J] = innerLoop<SZ,I,J,SY>::execute(l,m);
74  loop2Multiply<SZ,I,J-1>::execute(l,m,ret);
75  }
76  };
77  template <unsigned SZ,unsigned I> // Termination specialization
78  class loop2Multiply<SZ,I,1> {
79  public:
80  static void execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,SZ> &m,Matrix<T,SX,SZ> &ret) {
81  ret.t_[SX-I][SZ-1] = innerLoop<SZ,I,1,SY>::execute(l,m);
82  }
83  };
84  template <unsigned I> // Column matrix specialization (SZ=1)
85  class loop2Multiply<1,I,1> {
86  public:
87  static void execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,1> &m,Matrix<T,SX,1> &ret) {
88  ret.t_[SX-I] = innerLoop<1,I,1,SY>::execute(l,m);
89  }
90  };
91  // Using template meta-programming to unroll outermost loop of matrix multiply.
92  template <unsigned SZ,unsigned I>
93  class loopMultiply {
94  public:
95  static void execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,SZ> &m,Matrix<T,SX,SZ> &ret) {
96  loop2Multiply<SZ,I,SZ>::execute(l,m,ret);
97  loopMultiply<SZ,I-1>::execute(l,m,ret);
98  }
99  };
100  template <unsigned SZ> // Termination specialization
101  class loopMultiply<SZ,1> {
102  public:
103  static void execute(const Matrix<T,SX,SY> &l,const Matrix<T,SY,SZ> &m,Matrix<T,SX,SZ> &ret) {
104  loop2Multiply<SZ,1,SZ>::execute(l,m,ret);
105  }
106  };
107 
108 public:
110 #ifdef _DEBUG
111  Matrix() { set(initVal<T>()); }
112 #else
113 #pragma warning(push)
114 #pragma warning(disable:26495) // Uninitialize warning
115  Matrix() {} // Uninitialized on purpose
116 #pragma warning(pop)
117 #endif
118  explicit Matrix(const T &t) { set(t); }
121  Matrix(const Matrix<T,SX,SY> &m) { operator=(m); }
123  const Matrix<T,SX,SY> &operator=(const Matrix<T,SX,SY> &m) { memcpy(t_,m.t_,sizeof(T)*SX*SY); return *this; }
124 
126  const T & get(unsigned x,unsigned y) const { assert(x<SX); assert(y<SY); return t_[x][y]; }
128  T & get(unsigned x,unsigned y) { assert(x<SX); assert(y<SY); return t_[x][y]; }
130  const Column operator[](unsigned y) const { return Column(y,*this); }
132  Column operator[](unsigned y) { return Column(y,*this); }
134  const T & operator()(unsigned x,unsigned y) const { return get(x,y); }
136  T & operator()(unsigned x,unsigned y) { return get(x,y); }
137 
139  const Matrix<T,SX,SY> &operator+=(const Matrix<T,SX,SY> &m) { for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) t_[i][j] += m.t_[i][j]; return *this; }
141  const Matrix<T,SX,SY> &operator-=(const Matrix<T,SX,SY> &m) { for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) t_[i][j] -= m.t_[i][j]; return *this; }
143  const Matrix<T,SX,SY> &operator*=(const T &t) { for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) t_[i][j] *= t; return *this; }
145  const Matrix<T,SX,SY> &operator/=(const T &t) { for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) t_[i][j] /= t; return *this; }
146 
148  Matrix<T,SX,SY> operator+(const Matrix<T,SX,SY> &m) const { Matrix<T,SX,SY> ret; for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) ret.t_[i][j] = t_[i][j] + m.t_[i][j]; return ret; }
150  Matrix<T,SX,SY> operator-(const Matrix<T,SX,SY> &m) const { Matrix<T,SX,SY> ret; for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) ret.t_[i][j] = t_[i][j] - m.t_[i][j]; return ret; }
152  Matrix<T,SX,SY> operator*(const T &t) const { Matrix<T,SX,SY> ret; for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) ret.t_[i][j] = t_[i][j] * t; return ret; }
154  Matrix<T,SX,SY> operator/(const T &t) const { Matrix<T,SX,SY> ret; for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) ret.t_[i][j] = t_[i][j] / t; return ret; }
156  template <unsigned SZ>
158  Matrix<T,SX,SZ> ret;
159  // OK - we are using template expansion to do loop unrolling at compile time. Fun!
160  //for (unsigned i=0;i<SX;++i) { // ROWS OF FIRST
161  // for (unsigned j=0;j<SZ;++j) { // COLUMNS OF SECOND
162  // T &val = ret.get(i,j);
163  // val = 0;
164  // for (unsigned k=0;k<SY;++k) // Add up
165  // val += get(i,k)*m.get(k,j);
166  // }
167  //}
168  loopMultiply<SZ,SX>::execute(*this,m,ret);
169  return ret;
170  }
172  //template <class T>
173  //Matrix<T,SX,1> operator*(const Matrix<T,SY,1> &m) const {
174  // Matrix<T,SX,1> ret;
175  // // OK - we are using template expansion to do loop unrolling at compile time. Fun!
176  // //for (unsigned i=0;i<SX;++i) { // ROWS OF FIRST
177  // // T &val = ret.get(i,0);
178  // // val = 0.0;
179  // // for (unsigned k=0;k<SY;++k) // Add up
180  // // val += get(i,k)*m.get(k,0);
181  // //}
182  // loopMultiply<1,SX>::execute(*this,m,ret);
183  // return ret;
184  //}
185 
186  // Block operations
188  template <unsigned BX,unsigned BY,unsigned SX2,unsigned SY2>
189  void addBlock(const Matrix<T,SX2,SY2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
190  addGenBlock<BX,BY,SX2,SY2>(m,iSrc*BX,jSrc*BY,iDst*BX,jDst*BY);
191  }
193  template <unsigned BX,unsigned BY,unsigned SX2,unsigned SY2>
194  void addGenBlock(const Matrix<T,SX2,SY2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
195  for (unsigned i=iSrc,id=iDst;i<(iSrc+BX);i++,id++)
196  for (unsigned j=jSrc,jd=jDst;j<(jSrc+BY);j++,jd++)
197  get(id,jd) += m(i,j);
198  }
199 
201  T maxNorm() const { T ret(0); for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) ret = std::max(ret,std::abs(t_[i][j])); return ret; }
203  Matrix<T,SY,SX> transpose() const { Matrix<T,SY,SX> ret; for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) ret(j,i) = t_[i][j]; return ret; }
205  T trace() const { T tt = t_[0][0]; unsigned len = std::min(SX,SY); for (unsigned i=1; i<len; ++i) tt += t_[i][i]; return tt; }
207  void set(const T &t=T()) { for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SY;++j) t_[i][j] = t; }
208 
210  static Matrix<T,SX,SY> identity() { Matrix<T,SX,SY> ret(0.0); for (unsigned i=0;i<std::min(SX,SY);++i) ret(i,i) = 1.0; return ret; }
211 
212  T t_[SX][SY];
213 };
214 
215 
217 template <class T,unsigned SX> class Matrix<T,SX,1> {
218 public:
220 #ifdef _DEBUG
221  Matrix() { set(initVal<T>()); }
222 #else
223  Matrix() { }
224 #endif
225  explicit Matrix(const T &t) { set(t); }
228  Matrix(const Matrix<T,SX,1> &m) { operator=(m); }
230  const Matrix<T,SX,1> &operator=(const Matrix<T,SX,1> &m) { memcpy(t_,m.t_,sizeof(T)*SX); return *this; }
231 
233  const T & get(unsigned x,unsigned y) const { assert(x<SX); assert(!y); y; return t_[x]; }
235  const T & get(unsigned x) const { assert(x<SX); return t_[x]; }
237  T & get(unsigned x,unsigned y) { assert(x<SX); assert(!y); y; return t_[x]; }
239  T & get(unsigned x) { assert(x<SX); return t_[x]; }
241  const T & operator()(unsigned x,unsigned y) const { y; return get(x,y); }
243  const T & operator()(unsigned x) const { return get(x); }
245  T & operator()(unsigned x,unsigned y) { y; return get(x,y); }
247  T & operator()(unsigned x) { return get(x); }
248 
250  const Matrix<T,SX,1> &operator+=(const Matrix<T,SX,1> &m) { for (unsigned i=0;i<SX;++i) t_[i] += m.t_[i]; return *this; }
252  const Matrix<T,SX,1> &operator-=(const Matrix<T,SX,1> &m) { for (unsigned i=0;i<SX;++i) t_[i] -= m.t_[i]; return *this; }
254  const Matrix<T,SX,1> &operator*=(const T &t) { for (unsigned i=0;i<SX;++i) t_[i] *= t; return *this; }
256  const Matrix<T,SX,1> &operator/=(const T &t) { for (unsigned i=0;i<SX;++i) t_[i] /= t; return *this; }
257 
259  Matrix<T,SX,1> operator+(const Matrix<T,SX,1> &m) const { Matrix<T,SX,1> ret; for (unsigned i=0;i<SX;++i) ret.t_[i] = t_[i] + m.t_[i]; return ret; }
261  Matrix<T,SX,1> operator-(const Matrix<T,SX,1> &m) const { Matrix<T,SX,1> ret; for (unsigned i=0;i<SX;++i) ret.t_[i] = t_[i] - m.t_[i]; return ret; }
263  Matrix<T,SX,1> operator*(const T &t) const { Matrix<T,SX,1> ret; for (unsigned i=0;i<SX;++i) ret.t_[i] = t_[i] * t; return ret; }
265  Matrix<T,SX,1> operator/(const T &t) const { Matrix<T,SX,1> ret; for (unsigned i=0;i<SX;++i) ret.t_[i] = t_[i] / t; return ret; }
267  template <unsigned SZ> Matrix<T,SX,SZ> operator*(const Matrix<T,1,SZ> &m) const {
268  Matrix<T,SX,SZ> ret;
269  for (unsigned i=0;i<SX;++i) // ROWS OF FIRST
270  for (unsigned j=0;j<SZ;++j) // COLUMNS OF SECOND
271  ret.get(i,j) = get(i,0)*m.get(0,j);
272  return ret;
273  }
274 
275  // Block operations
277  template <unsigned BX,unsigned BY,unsigned SX2,unsigned SY2> void addBlock(const Matrix<T,SX2,SY2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
278  addGenBlock<BX,BY,SX2,SY2>(m,iSrc*BX,jSrc*BY,iDst*BX,jDst*BY);
279  }
281  template <unsigned BX,unsigned SX2> void addBlock(const Matrix<T,SX2,1> &m,unsigned iSrc,unsigned iDst) {
282  addGenBlock<BX,SX2>(m,iSrc*BX,iDst*BX);
283  }
285  template <unsigned BX,unsigned BY,unsigned SX2,unsigned SY2> void addGenBlock(const Matrix<T,SX2,SY2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
286  for (unsigned i=iSrc,id=iDst;i<(iSrc+BX);i++,id++)
287  for (unsigned j=jSrc,jd=jDst;j<(jSrc+BY);j++,jd++)
288  get(id,jd) += m(i,j);
289  }
291  template <unsigned BX,unsigned SX2> void addGenBlock(const Matrix<T,SX2,1> &m,unsigned iSrc,unsigned iDst) {
292  for (unsigned i=iSrc,id=iDst;i<(iSrc+BX);i++,id++)
293  get(id) += m(i);
294  }
295 
297  T maxNorm() const { T ret(0); for (unsigned i=0;i<SX;++i) ret = std::max(ret,std::abs(t_[i])); return ret; }
299  Matrix<T,1,SX> transpose() const { Matrix<T,1,SX> ret; for (unsigned i=0;i<SX;++i) ret(0,i) = t_[i]; return ret; }
301  void set(const T &t=T()) { for (unsigned i=0;i<SX;++i) t_[i] = t; }
302 
304  static Matrix<T,SX,1> identity() { Matrix<T,SX,1> ret(0.0); ret.t_[0] = 1.0; return ret; }
305  T t_[SX];
306 };
307 
312 template <class T,unsigned SX> class SymMatrix {
313 private:
315  class Column {
316  public:
318  Column(unsigned x,SymMatrix<T,SX> &m) : x_(x), m_(m) { }
320  Column(const Column &r) : x_(r.x_), m_(r.m_) { }
322  const T &operator[](unsigned y) const { return m_.get(x_,y); }
324  T &operator[](unsigned y) { return m_.get(x_,y); }
325  private:
326  void operator=(const Column &r); // DISALLOWED
327  unsigned x_;
328  SymMatrix<T,SX> &m_;
329  };
330  // Using template-meta programming to unroll innermost loop of matrix multiply
331  template <unsigned SZ,unsigned I,unsigned J,unsigned K>
332  class innerLoop {
333  public:
334  static double execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,SZ> &m) {
335  return l.t_[index(SX-I,SX-K)]*m.t_[SX-K][SZ-J] + innerLoop<SZ,I,J,K-1>::execute(l,m);
336  }
337  };
338  template <unsigned SZ,unsigned I,unsigned J> // Termination specialization
339  class innerLoop<SZ,I,J,1> {
340  public:
341  static double execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,SZ> &m) {
342  return l.t_[index(SX-I,SX-1)]*m.t_[SX-1][SZ-J];
343  }
344  };
345  template <unsigned I,unsigned K> // Specialization for column matrix (SZ=1)
346  class innerLoop<1,I,1,K> {
347  public:
348  static double execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,1> &m) {
349  return l.t_[index(SX-I,SX-K)]*m.t_[SX-K] + innerLoop<1,I,1,K-1>::execute(l,m);
350  }
351  };
352  template <unsigned I> // termination specialization for column matrix specialization
353  class innerLoop<1,I,1,1> {
354  public:
355  static double execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,1> &m) {
356  return l.t_[index(SX-I,SX-1)]*m.t_[SX-1];
357  }
358  };
359  // Using template meta-programming to unroll second loop of matrix multiply.
360  template <unsigned SZ,unsigned I,unsigned J>
361  class loop2Multiply {
362  public:
363  static void execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,SZ> &m,Matrix<T,SX,SZ> &ret) {
364  ret.t_[SX-I][SZ-J] = innerLoop<SZ,I,J,SX>::execute(l,m);
365  loop2Multiply<SZ,I,J-1>::execute(l,m,ret);
366  }
367  };
368  template <unsigned SZ,unsigned I> // Termination specialization
369  class loop2Multiply<SZ,I,1> {
370  public:
371  static void execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,SZ> &m,Matrix<T,SX,SZ> &ret) {
372  ret.t_[SX-I][SZ-1] = innerLoop<SZ,I,1,SX>::execute(l,m);
373  }
374  };
375  template <unsigned I> // Column matrix specialization (SZ=1)
376  class loop2Multiply<1,I,1> {
377  public:
378  static void execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,1> &m,Matrix<T,SX,1> &ret) {
379  ret.t_[SX-I] = innerLoop<1,I,1,SX>::execute(l,m);
380  }
381  };
382  // Using template meta-programming to unroll outermost loop of matrix multiply.
383  template <unsigned SZ,unsigned I>
384  class loopMultiply {
385  public:
386  static void execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,SZ> &m,Matrix<T,SX,SZ> &ret) {
387  loop2Multiply<SZ,I,SZ>::execute(l,m,ret);
388  loopMultiply<SZ,I-1>::execute(l,m,ret);
389  }
390  };
391  template <unsigned SZ> // Termination specialization
392  class loopMultiply<SZ,1> {
393  public:
394  static void execute(const SymMatrix<T,SX> &l,const Matrix<T,SX,SZ> &m,Matrix<T,SX,SZ> &ret) {
395  loop2Multiply<SZ,1,SZ>::execute(l,m,ret);
396  }
397  };
398 
399 
400  // Using template-meta programming to unroll innermost loop of matrix multiply
401  template <unsigned I,unsigned J,unsigned K>
402  class innerLoopS {
403  public:
404  static double execute(const SymMatrix<T,SX> &l,const SymMatrix<T,SX> &m) {
405  return l.t_[index(SX-I,SX-K)]*m.t_[index(SX-K,SX-J)] + innerLoopS<I,J,K-1>::execute(l,m);
406  }
407  };
408 
409  template <unsigned I,unsigned J> // Termination specialization
410  class innerLoopS<I,J,1> {
411  public:
412  static double execute(const SymMatrix<T,SX> &l,const SymMatrix<T,SX> &m) {
413  return l.t_[index(SX-I,SX-1)]*m.t_[index(SX-1,SX-J)];
414  // what is X here? seems undefined??
415  }
416  };
417 
418  // Using template meta-programming to unroll second loop of matrix multiply.
419  template <unsigned I,unsigned J>
420  class loop2MultiplyS {
421  public:
422  static void execute(const SymMatrix<T,SX> &l,const SymMatrix<T,SX> &m,Matrix<T,SX,SX> &ret) {
423  ret.t_[SX-I][SX-J] = innerLoopS<I,J,SX>::execute(l,m);
424  loop2MultiplyS<I,J-1>::execute(l,m,ret);
425  }
426  };
427  template <unsigned I> // Termination specialization
428  class loop2MultiplyS<I,1> {
429  public:
430  static void execute(const SymMatrix<T,SX> &l,const SymMatrix<T,SX> &m,Matrix<T,SX,SX> &ret) {
431  ret.t_[SX-I][SX-1] = innerLoopS<I,1,SX>::execute(l,m);
432  }
433  };
434  // Using template meta-programming to unroll outermost loop of matrix multiply.
435  template <unsigned I>
436  class loopMultiplyS {
437  public:
438  static void execute(const SymMatrix<T,SX> &l,const SymMatrix<T,SX> &m,Matrix<T,SX,SX> &ret) {
439  loop2MultiplyS<I,SX>::execute(l,m,ret);
440  loopMultiplyS<I-1>::execute(l,m,ret);
441  }
442  };
443 #ifndef __LINUX
444  template <> // Termination specialization
445  class loopMultiplyS<1> {
446  public:
447  static void execute(const SymMatrix<T,SX> &l,const SymMatrix<T,SX> &m,Matrix<T,SX,SX> &ret) {
448  loop2MultiplyS<1,SX>::execute(l,m,ret);
449  }
450  };
451 #endif
452 
453 public:
455 #ifdef _DEBUG
456  SymMatrix() { set(initVal<T>()); }
457 #else
458  SymMatrix() { }
459 #endif
460  explicit SymMatrix(const T &t) { set(t); }
463  SymMatrix(const SymMatrix<T,SX> &m) { memcpy(t_,m.t_,sizeof(T)*len_); }
465  const SymMatrix<T,SX> &operator=(const SymMatrix<T,SX> &m) { memcpy(t_,m.t_,sizeof(T)*len_); return *this; }
466 
468  const T & get(unsigned x,unsigned y) const { return t_[index(x,y)]; }
470  T & get(unsigned x,unsigned y) { return t_[index(x,y)]; }
472  const Column operator[](unsigned y) const { return Column(y,*this); }
474  Column operator[](unsigned y) { return Column(y,*this); }
476  const T & operator()(unsigned x,unsigned y) const { return get(x,y); }
478  T & operator()(unsigned x,unsigned y) { return get(x,y); }
479 
481  const SymMatrix<T,SX> &operator+=(const SymMatrix<T,SX> &m) { for (unsigned i=0;i<len_;++i) t_[i] += m.t_[i]; return *this; }
483  const SymMatrix<T,SX> &operator-=(const SymMatrix<T,SX> &m) { for (unsigned i=0;i<len_;++i) t_[i] -= m.t_[i]; return *this; }
485  const SymMatrix<T,SX> &operator*=(const T &t) { for (unsigned i=0;i<len_;++i) t_[i] *= t; return *this; }
487  const SymMatrix<T,SX> &operator/=(const T &t) { for (unsigned i=0;i<len_;++i) t_[i] /= t; return *this; }
488 
490  SymMatrix<T,SX> operator+(const SymMatrix<T,SX> &m) const { SymMatrix<T,SX> ret; for (unsigned i=0;i<len_;++i) ret.t_[i] = t_[i] + m.t_[i]; return ret; }
492  Matrix<T,SX,SX> operator+(const Matrix<T,SX,SX> &m) const { Matrix<T,SX,SX> ret; for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SX;++j) ret(i,j) = get(i,j) + m.get(i,j); return ret; }
494  SymMatrix<T,SX> operator-(const SymMatrix<T,SX> &m) const { SymMatrix<T,SX> ret; for (unsigned i=0;i<len_;++i) ret.t_[i] = t_[i] - m.t_[i]; return ret; }
496  Matrix<T, SX, SX> operator-(const Matrix<T, SX, SX>& m) const { Matrix<T, SX, SX> ret; for (unsigned i = 0; i < SX; ++i) for (unsigned j = 0; j < SX; ++j) ret(i, j) = get(i, j) - m.get(i, j); return ret; }
497 
499  SymMatrix<T,SX> operator*(const T &t) const { SymMatrix<T,SX> ret; for (unsigned i=0;i<len_;++i) ret.t_[i] = t_[i] * t; return ret; }
501  SymMatrix<T,SX> operator/(const T &t) const { SymMatrix<T,SX> ret; for (unsigned i=0;i<len_;++i) ret.t_[i] = t_[i] / t; return ret; }
504  Matrix<T,SX,SX> ret;
505  loopMultiplyS<SX>::execute(*this,m,ret);
506  //for (unsigned i=0;i<SX;++i) { // ROWS OF FIRST
507  // for (unsigned j=0;j<SX;++j) { // COLUMNS OF SECOND
508  // T &val = ret.get(i,j);
509  // val = 0.0;
510  // for (unsigned k=0;k<SX;++k) // Add up
511  // val += get(i,k)*m.get(k,j);
512  // }
513  //}
514  return ret;
515  }
517  template <unsigned SZ> Matrix<T,SX,SZ> operator*(const Matrix<T,SX,SZ> &m) const {
518  Matrix<T,SX,SZ> ret;
519  loopMultiply<SZ,SX>::execute(*this,m,ret);
520  //for (unsigned i=0;i<SX;++i) { // ROWS OF FIRST
521  // for (unsigned j=0;j<SY;++j) { // COLUMNS OF SECOND
522  // T &val = ret.get(i,j);
523  // val = 0.0;
524  // for (unsigned k=0;k<SX;++k) // Add up
525  // val += get(i,k)*m.get(k,j);
526  // }
527  //}
528  return ret;
529  }
531  //template <> Matrix<T,SX,1> operator*(const Matrix<T,SX,1> &m) const {
532  // Matrix<T,SX,1> ret;
533  // for (unsigned i=0;i<SX;++i) { // ROWS OF FIRST
534  // T &val = ret.get(i,0);
535  // val = 0.0;
536  // for (unsigned k=0;k<SX;++k) // Add up
537  // val += get(i,k)*m.get(k,0);
538  // }
539  // return ret;
540  //}
541 
542  // Block operations
544  template <unsigned BX,unsigned BY,unsigned SX2,unsigned SY2> void addBlock(const Matrix<T,SX2,SY2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
545  addGenBlock<BX,BY,SX2,SY2>(m,iSrc*BX,jSrc*BY,iDst*BX,jDst*BY);
546  }
548  template <unsigned BX,unsigned BY,unsigned SX2> void addBlock(const SymMatrix<T,SX2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
549  addGenBlock<BX,BY,SX2>(m,iSrc*BX,jSrc*BY,iDst*BX,jDst*BY);
550  }
552  template <unsigned BX,unsigned BY,unsigned SX2,unsigned SY2> void addGenBlock(const Matrix<T,SX2,SY2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
553  for (unsigned i=iSrc,id=iDst;i<(iSrc+BX);i++,id++) {
554  for (unsigned j=jSrc,jd=jDst;j<(jSrc+BY);j++,jd++) {
555  if (id<=jd)
556  get(id,jd) += m(i,j);
557  }
558  }
559  }
561  template <unsigned BX,unsigned BY,unsigned SX2> void addGenBlock(const SymMatrix<T,SX2> &m,unsigned iSrc,unsigned jSrc,unsigned iDst,unsigned jDst) {
562  for (unsigned i=iSrc,id=iDst;i<(iSrc+BX);i++,id++) {
563  for (unsigned j=jSrc,jd=jDst;j<(jSrc+BY);j++,jd++) {
564  if (id<=jd)
565  get(id,jd) += m(i,j);
566  }
567  }
568  }
569 
571  T maxNorm() const { T ret(0); for (unsigned i=0;i<len_;++i) ret = std::max(std::abs(t_[i]),ret); return ret; }
573  SymMatrix<T,SX> transpose() const { return *this; }
575  T trace() const { T tt = t_[0]; for (unsigned i=1; i<SX; ++i) tt += get(i,i); return tt; }
577  void set(const T &t=T()) { for (unsigned i=0;i<len_;++i) t_[i] = t; }
579  Matrix<T,SX,SX> toMatrix() const { Matrix<T,SX,SX> ret; for (unsigned i=0;i<SX;++i) for (unsigned j=0;j<SX;++j) ret(i,j)=get(i,j); return ret; }
580 
582  static SymMatrix<T,SX> identity() { SymMatrix<T,SX> ret(0.0); for (unsigned i=0;i<SX;++i) ret(i,i) = 1.0; return ret; }
585  SymMatrix<T,SX> ret;
586 #ifdef _DEBUG
587  double maxdif = 0.0;
588 #endif
589  for (unsigned i=0;i<SX;++i) {
590  ret(i,i) = m(i,i);
591  for (unsigned j=i+1;j<SX;++j) {
592  ret(i,j) = (m(i,j) + m(j,i)) * 0.5;
593 #ifdef _DEBUG
594  maxdif = std::max(std::abs(ret(i,j) - m(i,j)),maxdif);
595 #endif
596  }
597  }
598 #ifdef _DEBUG
599  assert(maxdif <= ret.maxNorm()*limits<float>::epsilon());
600 #endif
601  return ret;
602  }
603 
604  static constexpr unsigned len_ = (SX*SX + SX) / 2;
605  static constexpr unsigned indexConst_ = 1 + 2*SX;
606  static constexpr unsigned index(unsigned x,unsigned y) {
607  unsigned x2 = x > y ? y : x;
608  unsigned y2 = x > y ? x : y;
609  unsigned i = (((indexConst_-x2)*x2)/2) + (y2-x2);
610  return i;
611  }
612 
613  T t_[len_];
614 };
615 
616 // MOO NOTE!:: There are optimizations that can be made here!
619 // Using template-meta programming to unroll innermost loop of matrix multiply
620 template <class T,unsigned SX,unsigned SY,unsigned I,unsigned J,unsigned K>
621 class innerLoopMS {
622 public:
623  static double execute(const Matrix<T,SX,SY> &l,const SymMatrix<T,SY> &m) {
624  return l.t_[SX-I][SY-K]*m.t_[m.index(SY-K,SY-J)] + innerLoopMS<T,SX,SY,I,J,K-1>::execute(l,m);
625  }
626 };
627 template <class T,unsigned SX,unsigned SY,unsigned I,unsigned J> // Termination specialization
628 class innerLoopMS<T,SX,SY,I,J,1> {
629 public:
630  static double execute(const Matrix<T,SX,SY> &l,const SymMatrix<T,SY> &m) {
631  return l.t_[SX-I][SY-1]*m.t_[m.index(SY-1,SY-J)];
632  }
633 };
634 // Using template meta-programming to unroll second loop of matrix multiply.
635 template <class T,unsigned SX,unsigned SY,unsigned I,unsigned J>
637 public:
638  static void execute(const Matrix<T,SX,SY> &l,const SymMatrix<T,SY> &m,Matrix<T,SX,SY> &ret) {
639  ret.t_[SX-I][SY-J] = innerLoopMS<T,SX,SY,I,J,SY>::execute(l,m);
641  }
642 };
643 template <class T,unsigned SX,unsigned SY,unsigned I> // Termination specialization
644 class loop2MultiplyMS<T,SX,SY,I,1> {
645 public:
646  static void execute(const Matrix<T,SX,SY> &l,const SymMatrix<T,SY> &m,Matrix<T,SX,SY> &ret) {
647  ret.t_[SX-I][SY-1] = innerLoopMS<T,SX,SY,I,1,SY>::execute(l,m);
648  }
649 };
650 // Using template meta-programming to unroll outermost loop of matrix multiply.
651 template <class T,unsigned SX,unsigned SY,unsigned I>
653 public:
654  static void execute(const Matrix<T,SX,SY> &l,const SymMatrix<T,SY> &m,Matrix<T,SX,SY> &ret) {
657  }
658 };
659 template <class T,unsigned SX,unsigned SY> // Termination specialization
660 class loopMultiplyMS<T,SX,SY,1> {
661 public:
662  static void execute(const Matrix<T,SX,SY> &l,const SymMatrix<T,SY> &m,Matrix<T,SX,SY> &ret) {
664  }
665 };
666 
667 template <class T,unsigned SX,unsigned SY>
669  Matrix<T,SX,SY> ret;
671  //for (unsigned i=0;i<SX;++i) { // ROWS OF FIRST
672  // for (unsigned j=0;j<SY;++j) { // COLUMNS OF SECOND
673  // T &val = ret.get(i,j);
674  // val = 0.0;
675  // for (unsigned k=0;k<SY;++k) // Add up
676  // val += m.get(i,k)*s.get(k,j);
677  // }
678  //}
679  return ret;
680 }
681 
687 template <class T,unsigned S> class VMatrix : public Matrix<T,S,1> {
688 public:
690  VMatrix() : Matrix<T,S,1>() { }
692  explicit VMatrix(const T &t) : Matrix<T,S,1>(t) { }
694  VMatrix(const VMatrix<T,S> &m) : Matrix<T,S,1>(m) { }
696  VMatrix(const Matrix<T,S,1> &m) : Matrix<T,S,1>(m) { }
698  const VMatrix<T,S> &operator=(const VMatrix<T,S> &m) { Matrix<T,S,1>::operator=(m); return *this; }
700  const VMatrix<T,S> &operator=(const Matrix<T,S,1> &m) { Matrix<T,S,1>::operator=(m); return *this; }
701 
703  //const T & get(unsigned x) const { return Matrix<T,S,1>::get(x,0); }
705  //T & get(unsigned x) { return Matrix<T,S,1>::get(x,0); }
707  const T &operator[](unsigned x) const { return this->get(x); }
709  T & operator[](unsigned x) { return this->get(x); }
711  //const T & operator()(unsigned x) const { return get(x); }
713  //T & operator()(unsigned x) { return get(x); }
714 };
715 
719 template <unsigned SX,unsigned SY=SX> class DMatrix : public Matrix<double,SX,SY> {
720 public:
722  DMatrix() : Matrix<double,SX,SY>() { }
724  explicit DMatrix(const double &t) : Matrix<double,SX,SY>(t) { }
726  DMatrix(const DMatrix<SX,SY> &m) : Matrix<double,SX,SY>(m) { }
728  DMatrix(const Matrix<double,SX,SY> &m) : Matrix<double,SX,SY>(m) { }
733 };
734 
738 template <unsigned SX> class DSymMatrix : public SymMatrix<double,SX> {
739 public:
741  DSymMatrix() : SymMatrix<double,SX>() { }
743  explicit DSymMatrix(const double &t) : SymMatrix<double,SX>(t) { }
745  DSymMatrix(const DSymMatrix<SX> &m) : SymMatrix<double,SX>(m) { }
747  DSymMatrix(const SymMatrix<double,SX> &m) : SymMatrix<double,SX>(m) { }
752 };
753 
756 template <unsigned S> class DVMatrix : public VMatrix<double,S> {
757 public:
759  DVMatrix() : VMatrix<double,S>() { }
761  explicit DVMatrix(const double &d) : VMatrix<double,S>(d) { }
763  DVMatrix(const DVMatrix<S> &m) : VMatrix<double,S>(m) { }
765  DVMatrix(const Matrix<double,S,1> &m) : VMatrix<double,S>(m) { }
767  const DVMatrix<S> &operator=(const DVMatrix<S> &m) { VMatrix<double,S>::operator=(m); return *this; }
770 };
771 
772 template <class T,unsigned S>
773 double innerProduct(const VMatrix<T,S> &v1,const VMatrix<T,S> &v2) {
774  double d=0.0;
775  for (UInt i=0;i<S;++i)
776  d += v1(i)*v2(i);
777  return d;
778 }
779 
780 template <class T,unsigned S> Matrix<T,S,S> outerProduct(const VMatrix<T,S> &v1,const VMatrix<T,S> &v2) {
781  Matrix<T,S,S> ret;
782  for (UInt i=0;i<S;++i) {
783  for (UInt j=0;j<S;++j)
784  ret(i,j) = v1(i) * v2(j);
785  }
786  return ret;
787 }
788 
792 template <class T> Matrix<T,3,3> outerProduct(const Vector3<T> &v1,const Vector3<T> &v2) {
793  Matrix<T,3,3> ret;
794  ret.get(0,0) = v1.x() * v2.x();
795  ret.get(0,1) = v1.x() * v2.y();
796  ret.get(0,2) = v1.x() * v2.z();
797  ret.get(1,0) = v1.y() * v2.x();
798  ret.get(1,1) = v1.y() * v2.y();
799  ret.get(1,2) = v1.y() * v2.z();
800  ret.get(2,0) = v1.z() * v2.x();
801  ret.get(2,1) = v1.z() * v2.y();
802  ret.get(2,2) = v1.z() * v2.z();
803  return ret;
804 }
805 
809 template <class T> Matrix<T,2,2> outerProduct(const Vector2<T> &v1,const Vector2<T> &v2) {
810  Matrix<T,2,2> ret;
811  ret.get(0,0) = v1.x() * v2.x();
812  ret.get(0,1) = v1.x() * v2.y();
813  ret.get(1,0) = v1.y() * v2.x();
814  ret.get(1,1) = v1.y() * v2.y();
815  return ret;
816 }
817 
821 template <class T> T determinant(const Matrix<T,3,3> &mat) {
823  T t = mat.get(0,0) * (mat.get(1,1) * mat.get(2,2) - mat.get(2,1) * mat.get(1,2));
824  t -= mat.get(1,0) * (mat.get(0,1) * mat.get(2,2) - mat.get(2,1) * mat.get(0,2));
825  t += mat.get(2,0) * (mat.get(0,1) * mat.get(1,2) - mat.get(1,1) * mat.get(0,2));
826  return t;
827 }
828 
832 template <class T> T determinant(const Matrix<T,2,2> &mat) {
833  return mat.get(0,0) * mat.get(1,1) - mat.get(0,1) * mat.get(1,0);
834 }
835 
839 template <class T> Matrix<T,3,3> inverse(const Matrix<T,3,3> &mat) {
841  // First calculate the adjoint
842  T a11 = mat.get(0,0), a21 = mat.get(1,0), a31 = mat.get(2,0);
843  T a12 = mat.get(0,1), a22 = mat.get(1,1), a32 = mat.get(2,1);
844  T a13 = mat.get(0,2), a23 = mat.get(1,2), a33 = mat.get(2,2);
845 
846  Matrix<T,3,3> rm;
847  rm.get(0,0) = a22*a33 - a32*a23;
848  rm.get(1,0) = -a21*a33 + a31*a23;
849  rm.get(2,0) = a21*a32 - a31*a22;
850  rm.get(0,1) = -a12*a33 + a32*a13;
851  rm.get(1,1) = a11*a33 - a31*a13;
852  rm.get(2,1) = -a11*a32 + a31*a12;
853  rm.get(0,2) = a12*a23 - a22*a13;
854  rm.get(1,2) = -a11*a23 + a21*a13;
855  rm.get(2,2) = a11*a22 - a21*a12;
856  // Divide by the determinant
857  return rm / determinant(mat);
858 }
859 
862 template <class T> VMatrix<T,2> toMatrix(const Vector2<T> &v) {
863  VMatrix<T,2> ret;
864  ret.get(0) = v.x();
865  ret.get(1) = v.y();
866  return ret;
867 }
868 
871 template <class T> VMatrix<T,3> toMatrix(const Vector3<T> &v) {
872  VMatrix<T,3> ret;
873  ret.get(0) = v.x();
874  ret.get(1) = v.y();
875  ret.get(2) = v.z();
876  return ret;
877 }
878 
880 template <class T,unsigned SX> VMatrix<T,SX> toVMatrix(const Vector3<T> &v,unsigned start=0) {
881  VMatrix<T,SX> ret(0.0);
882  ret.get(start) = v.x();
883  ret.get(start+1) = v.y();
884  ret.get(start+2) = v.z();
885  return ret;
886 }
887 
890 template <unsigned SX> DVMatrix<SX> toDVMatrix(const DVect3 &v,unsigned start=0) { return toVMatrix<double,SX>(v,start); }
891 
894 template <class T> Vector2<T> operator*(const Matrix<T,2,2> &m,const Vector2<T> &v) {
895  Vector2<T> ret(v.x()*m.get(0,0) + v.y()*m.get(0,1),
896  v.x()*m.get(1,0) + v.y()*m.get(1,1));
897  return ret;
898 }
899 
902 template <class T> Vector2<T> operator*(const SymMatrix<T,2> &m,const Vector2<T> &v) {
903  Vector2<T> ret(v.x()*m.get(0,0) + v.y()*m.get(0,1),
904  v.x()*m.get(1,0) + v.y()*m.get(1,1));
905  return ret;
906 }
907 
910 template <class T> Vector3<T> operator*(const Matrix<T,3,3> &m,const Vector3<T> &v) {
911  Vector3<T> ret(v.x()*m.get(0,0) + v.y()*m.get(0,1) + v.z()*m.get(0,2),
912  v.x()*m.get(1,0) + v.y()*m.get(1,1) + v.z()*m.get(1,2),
913  v.x()*m.get(2,0) + v.y()*m.get(2,1) + v.z()*m.get(2,2));
914  return ret;
915 }
916 
919 template <class T> Vector3<T> operator*(const SymMatrix<T,3> &m,const Vector3<T> &v) {
920  Vector3<T> ret(v.x()*m.get(0,0) + v.y()*m.get(0,1) + v.z()*m.get(0,2),
921  v.x()*m.get(1,0) + v.y()*m.get(1,1) + v.z()*m.get(1,2),
922  v.x()*m.get(2,0) + v.y()*m.get(2,1) + v.z()*m.get(2,2));
923  return ret;
924 }
925 
929 
933 
938  SymTensor ret(m.get(0,0),
939  m.get(1,1),
940  m.get(2,2),
941  (m.get(0,1)+m.get(1,0))*0.5,
942  (m.get(0,2)+m.get(2,0))*0.5,
943  (m.get(1,2)+m.get(2,1))*0.5);
944  return ret;
945 }
946 
947 inline SymTensor toSymTensor(const DMatrix<2,2> &m) {
948  SymTensor ret(m.get(0,0),
949  m.get(1,1),
950  0.0,
951  (m.get(0,1)+m.get(1,0))*0.5,
952  0.0,
953  0.0);
954  return ret;
955 }
956 
960  SymTensor ret(m.get(0,0),m.get(1,1),m.get(1,2),
961  m.get(0,1),m.get(0,2),m.get(1,2));
962  return ret;
963 }
964 
966 template <class T,unsigned SX> Vector2<T> toVector2(const VMatrix<T,SX> &m,unsigned start=0) { Vector2<T> ret(m(start),m(start+1)); return ret; }
967 
969 template <class T,unsigned SX> Vector3<T> toVector3(const VMatrix<T,SX> &m,unsigned start=0) { Vector3<T> ret(m(start),m(start+1),m(start+2)); return ret; }
970 
972 template <class T> Vector2<T> toVector(const VMatrix<T,2> &m) { Vector2<T> ret(m(0),m(1)); return ret; }
973 
975 template <class T> Vector3<T> toVector(const VMatrix<T,3> &m) { Vector3<T> ret(m(0),m(1),m(2)); return ret; }
976 
978 template <class T,unsigned SY> Vector2<T> columnToVector(const Matrix<T,2,SY> &m,unsigned col) { Vector2<T> ret(m(0,col),m(1,col)); return ret; }
980 template <class T,unsigned SY> Vector3<T> columnToVector(const Matrix<T,3,SY> &m,unsigned col) { Vector3<T> ret(m(0,col),m(1,col),m(2,col)); return ret; }
982 template <class T,unsigned SX> Vector2<T> rowToVector(const Matrix<T,SX,2> &m,unsigned row) { Vector2<T> ret(m(row,0),m(row,1)); return ret; }
984 template <class T,unsigned SX> Vector3<T> rowToVector(const Matrix<T,SX,3> &m,unsigned row) { Vector3<T> ret(m(row,0),m(row,1),m(row,2)); return ret; }
985 
986 template <class T,unsigned SY>
987 void vectorToColumn(Matrix<T,2,SY> &m,const DVect2 &v,unsigned col) { m(0,col) = v.x(); m(1,col) = v.y(); }
988 
989 template <class T,unsigned SY>
990 void vectorToColumn(Matrix<T,3,SY> &m,const DVect3 &v,unsigned col) { m(0,col) = v.x(); m(1,col) = v.y(); m(2,col) = v.z(); }
991 
992 template <class T,unsigned SX>
993 void vectorToRow(Matrix<T,SX,2> &m,const DVect2 &v,unsigned row) { m(row,0) = v.x(); m(row,1) = v.y(); }
994 
995 template <class T,unsigned SX>
996 void vectorToRow(Matrix<T,SX,3> &m,const DVect3 &v,unsigned row) { m(row,0) = v.x(); m(row,1) = v.y(); m(row,2) = v.z(); }
997 
998 template <class T,unsigned SX,unsigned SY>
999 VMatrix<T,SX> column(const Matrix<T,SX,SY> &m,unsigned c) {
1000  VMatrix<T,SX> ret;
1001  for (unsigned i=0;i<SX;++i)
1002  ret(i) = m(i,c);
1003  return ret;
1004 }
1006 // EoF
A Symmetric 2nd order tensor.
A 1-Dimensional version of Matrix, to represent a vector.
Definition: matrix.h:687
const DVMatrix< S > & operator=(const Matrix< double, S, 1 > &m)
Equality operator, for matrix class.
Definition: matrix.h:769
T & operator()(unsigned x, unsigned y)
() operator access to get(x,y)
Definition: matrix.h:136
const Matrix< T, SX, SY > & operator/=(const T &t)
In-place division by a scalar.
Definition: matrix.h:145
Column operator[](unsigned y)
Array access operator returns a Column helper class, which has it's own [] operator to access a colum...
Definition: matrix.h:132
VMatrix()
Default constructor - no data initialization.
Definition: matrix.h:690
Matrix< T, SX, SY > operator/(const T &t) const
Binary scalar division operator.
Definition: matrix.h:154
Matrix< T, 2, 2 > outerProduct(const Vector2< T > &v1, const Vector2< T > &v2)
Creates a 2X2 Matrix from the outer product of two Vector2 types.
Definition: matrix.h:809
const Matrix< T, SX, 1 > & operator-=(const Matrix< T, SX, 1 > &m)
In-place matrix subtraction.
Definition: matrix.h:252
DVMatrix()
Default constructor, no data initialization.
Definition: matrix.h:759
void addBlock(const Matrix< T, SX2, SY2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from block indices (iSrc,...
Definition: matrix.h:277
const Matrix< T, SX, 1 > & operator+=(const Matrix< T, SX, 1 > &m)
In-place matrix addition.
Definition: matrix.h:250
T & get(unsigned x, unsigned y)
Retrieve value at row x column y. Bounds checking is done in a debug compile.
Definition: matrix.h:470
VMatrix(const Matrix< T, S, 1 > &m)
Copy contructor, works on Matrix if SY is 1.
Definition: matrix.h:696
DMatrix is a Matrix that defaults to type double...
Definition: matrix.h:719
Matrix()
Default constructor, does nothing and no initialization.
Definition: matrix.h:115
T & operator[](unsigned x)
1D version of array operator, which currently unfortunately eliminates [x][0] syntax on VMatrix (may ...
Definition: matrix.h:709
VMatrix< T, SX > toVMatrix(const Vector3< T > &v, unsigned start=0)
Converts a Vector3 into a VMatrix of arbitrary size, at an arbitrary starting index.
Definition: matrix.h:880
const T & operator()(unsigned x, unsigned y) const
() operator access to get(x,y)
Definition: matrix.h:476
void addGenBlock(const Matrix< T, SX2, SY2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from normal not block indices...
Definition: matrix.h:285
DMatrix(const double &t)
Explicit contructor, initializes all elements to t.
Definition: matrix.h:724
A specialization of the Matrix class for the case when SY=1.
Definition: matrix.h:217
T trace() const
Return the trace of the matrix or the sum of the diagonal components. Works also if the matrix is not...
Definition: matrix.h:575
SymMatrix< T, SX > transpose() const
Return the transpose of the matrix.
Definition: matrix.h:573
Matrix< T, SX, SX > toMatrix() const
Returns its copy.
Definition: matrix.h:579
T maxNorm() const
Returns the infinity norm of the matrix, or the maximum absolute magnitude of any element.
Definition: matrix.h:571
Vector2< T > toVector(const VMatrix< T, 2 > &m)
Converts a VMatrix to a Vector3, using three elements starting at index start.
Definition: matrix.h:972
const Matrix< T, SX, 1 > & operator/=(const T &t)
In-place division by a scalar.
Definition: matrix.h:256
const Matrix< T, SX, SY > & operator=(const Matrix< T, SX, SY > &m)
Equality operator.
Definition: matrix.h:123
const T & operator()(unsigned x) const
() operator access to const get(x)
Definition: matrix.h:243
Vector2< T > operator *(const Matrix< T, 2, 2 > &m, const Vector2< T > &v)
Definition: matrix.h:894
SymTensor toSymTensor(const DMatrix< 3, 3 > &m)
Definition: matrix.h:937
Matrix< T, SX, SY > operator *(const T &t) const
Binary scalar multiplication operator.
Definition: matrix.h:152
Matrix< T, SY, SX > transpose() const
Return the transpose of the matrix.
Definition: matrix.h:203
DVMatrix(const double &d)
Explicit contructor, initializes all elements to t.
Definition: matrix.h:761
const Column operator[](unsigned y) const
Array access operator returns a Column helper class, which has it's own [] operator to access a colum...
Definition: matrix.h:472
Matrix< T, SX, SX > operator+(const Matrix< T, SX, SX > &m) const
Binary addition operator.
Definition: matrix.h:492
Vector2< T > rowToVector(const Matrix< T, SX, 2 > &m, unsigned row)
returns in a Vector2<t> the row row from matrix Matrix<T,SX,2>
Definition: matrix.h:982
Matrix< T, SX, 1 > operator-(const Matrix< T, SX, 1 > &m) const
Binary subtraction operator.
Definition: matrix.h:261
Vector2< T > toVector2(const VMatrix< T, SX > &m, unsigned start=0)
Converts a VMatrix to a Vector2, using two elements starting at index start.
Definition: matrix.h:966
const T & get(unsigned x, unsigned y) const
Retrieve value at row x column y. Bounds checking is done in a debug compile.
Definition: matrix.h:126
const SymMatrix< T, SX > & operator *=(const T &t)
In-place multiplication by a scalar.
Definition: matrix.h:485
const DMatrix< SX, SY > & operator=(const Matrix< double, SX, SY > &m)
Equality operator, for Matrix.
Definition: matrix.h:732
DVMatrix(const Matrix< double, S, 1 > &m)
Copy constructor, for Matrix class.
Definition: matrix.h:765
debug checked shorthand for std::numeric_limits<T>::
Definition: limit.h:25
unsigned int UInt
unsigned 32 bit
Definition: basedef.h:31
const DMatrix< SX, SY > & operator=(const DMatrix< SX, SY > &m)
Equality operator.
Definition: matrix.h:730
SymMatrix< T, SX > operator/(const T &t) const
Binary scalar division operator for a symetric matrix.
Definition: matrix.h:501
void addBlock(const SymMatrix< T, SX2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from block indices (iSrc,...
Definition: matrix.h:548
Matrix< T, 1, SX > transpose() const
returns the transposed matrix of this matrix
Definition: matrix.h:299
void addBlock(const Matrix< T, SX2, SY2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from block indices (iSrc,...
Definition: matrix.h:189
const T & operator()(unsigned x, unsigned y) const
() operator access to get(x,y)
Definition: matrix.h:134
T maxNorm() const
Returns the infinity norm of the matrix, or the maximum absolute magnitude of any element.
Definition: matrix.h:201
T maxNorm() const
Returns the infinity norm of the matrix, or the maximum absolute magnitude of any element.
Definition: matrix.h:297
SymTensor toSymTensor(const DSymMatrix< 3 > &m)
Definition: matrix.h:959
#define BASE_EXPORT
Definition: basedef.h:21
const DSymMatrix< SX > & operator=(const SymMatrix< double, SX > &m)
Equality operator, for Matrix.
Definition: matrix.h:751
DSymMatrix(const SymMatrix< double, SX > &m)
Copy constructor, for SymMatrix.
Definition: matrix.h:747
void addBlock(const Matrix< T, SX2, SY2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from block indices (iSrc,...
Definition: matrix.h:544
A symmetric 2nd order tensor.
Definition: symtensor.h:19
DSymMatrix(const double &t)
Explicit contructor, initializes all elements to t.
Definition: matrix.h:743
const T & y() const
Y component access.
Definition: vect.h:56
const SymMatrix< T, SX > & operator-=(const SymMatrix< T, SX > &m)
In-place matrix subtraction.
Definition: matrix.h:483
DVMatrix< SX > toDVMatrix(const DVect3 &v, unsigned start=0)
Definition: matrix.h:890
const T & get(unsigned x, unsigned y) const
Retrieve constant value at row x column y. Bounds checking is done in a debug compile.
Definition: matrix.h:233
void addGenBlock(const Matrix< T, SX2, SY2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from normal not block indices...
Definition: matrix.h:194
Matrix< T, SX, SX > operator-(const Matrix< T, SX, SX > &m) const
Binary subtraction operator.
Definition: matrix.h:496
Matrix()
Default constructor, does nothing and no initialization.
Definition: matrix.h:223
Matrix< T, 3, 3 > inverse(const Matrix< T, 3, 3 > &mat)
Returns the inverse of a 3X3 Matrix.
Definition: matrix.h:840
void set(const T &t=T())
Sets all matrix elemnts to T.
Definition: matrix.h:301
static SymMatrix< T, SX > fromMatrix(const Matrix< T, SX, SX > &m)
Assign from full matrix.
Definition: matrix.h:584
2D vector utility class.
Definition: vect.h:31
const DSymMatrix< SX > & operator=(const DSymMatrix< SX > &m)
Equality operator.
Definition: matrix.h:749
T determinant(const Matrix< T, 3, 3 > &mat)
Returns the determinant of a 3X3 Matrix.
Definition: matrix.h:822
DSymMatrix(const DSymMatrix< SX > &m)
Copy constructor.
Definition: matrix.h:745
void set(const T &t=T())
Set all entries in the matrix to t.
Definition: matrix.h:207
const Matrix< T, SX, SY > & operator+=(const Matrix< T, SX, SY > &m)
In-place matrix addition.
Definition: matrix.h:139
const T & operator[](unsigned x) const
1D version of array operator, which currently unfortunately eliminates [x][0] syntax on VMatrix (may ...
Definition: matrix.h:707
const T & z() const
The z-component of the vector.
Definition: vect.h:186
SymMatrix()
Default constructor, does nothing and no initialization.
Definition: matrix.h:458
T & operator()(unsigned x, unsigned y)
() operator access to get(x,y)
Definition: matrix.h:245
Vector2< T > columnToVector(const Matrix< T, 2, SY > &m, unsigned col)
returns in a Vector2<t> the column col from matrix Matrix<T,SX,2>
Definition: matrix.h:978
void addGenBlock(const Matrix< T, SX2, 1 > &m, unsigned iSrc, unsigned iDst)
Adds a block to this matrix from matrix m using the block size BX from normal non block indice iSrc t...
Definition: matrix.h:291
Matrix(const Matrix< T, SX, SY > &m)
Copy constructor.
Definition: matrix.h:121
SymMatrix< T, SX > operator-(const SymMatrix< T, SX > &m) const
Binary subtraction operator for a symetric matrix.
Definition: matrix.h:494
const VMatrix< T, S > & operator=(const VMatrix< T, S > &m)
Equality operator.
Definition: matrix.h:698
const T & get(unsigned x, unsigned y) const
Retrieve value at row x column y. Bounds checking is done in a debug compile.
Definition: matrix.h:468
VMatrix(const VMatrix< T, S > &m)
Copy constructor.
Definition: matrix.h:694
SymMatrix(const SymMatrix< T, SX > &m)
Copy constructor.
Definition: matrix.h:463
T determinant(const Matrix< T, 2, 2 > &mat)
Returns the determinant of a 2X2 Matrix.
Definition: matrix.h:832
Matrix< T, SX, SY > operator+(const Matrix< T, SX, SY > &m) const
Binary addition operator.
Definition: matrix.h:148
Definition: matrix.h:621
VMatrix< T, 3 > toMatrix(const Vector3< T > &v)
Definition: matrix.h:871
DSymMatrix is a SymMatrix that defaults to type double...
Definition: matrix.h:738
DMatrix()
Default constructor, no data initialization.
Definition: matrix.h:722
A template-based matrix class, size fixed at compile time. Defaults to symmetric sized matrix.
Definition: matrix.h:21
const Matrix< T, SX, SY > & operator-=(const Matrix< T, SX, SY > &m)
In-place matrix subtraction.
Definition: matrix.h:141
DVMatrix(const DVMatrix< S > &m)
Copy constructor.
Definition: matrix.h:763
T & operator()(unsigned x)
() operator access to get(x)
Definition: matrix.h:247
VMatrix< T, 2 > toMatrix(const Vector2< T > &v)
Definition: matrix.h:862
const Matrix< T, SX, 1 > & operator=(const Matrix< T, SX, 1 > &m)
Equality operator.
Definition: matrix.h:230
VMatrix(const T &t)
Explicit constructor, all elements initialzed to t.
Definition: matrix.h:692
SymMatrix< T, SX > operator+(const SymMatrix< T, SX > &m) const
Binary addition operator for a symetric matris.
Definition: matrix.h:490
Matrix< T, SX, 1 > operator+(const Matrix< T, SX, 1 > &m) const
Binary addition operator.
Definition: matrix.h:259
A template-based symmetric matrix class, size fixed at compile time. This primitive template-based ma...
Definition: matrix.h:312
Matrix< T, 3, 3 > outerProduct(const Vector3< T > &v1, const Vector3< T > &v2)
Creates a 3X3 Matrix from the outer product of two Vector3 types.
Definition: matrix.h:792
Column operator[](unsigned y)
Array access operator returns a Column helper class, which has it's own [] operator to access a colum...
Definition: matrix.h:474
const Column operator[](unsigned y) const
Array access operator returns a Column helper class, which has it's own [] operator to access a colum...
Definition: matrix.h:130
3D vector utility class.
Definition: vect.h:161
const Matrix< T, SX, SY > & operator *=(const T &t)
In-place multiplication by a scalar.
Definition: matrix.h:143
Matrix< T, SX, 1 > operator/(const T &t) const
Binary scalar division operator.
Definition: matrix.h:265
T & operator()(unsigned x, unsigned y)
() operator access to get(x,y)
Definition: matrix.h:478
const DVMatrix< S > & operator=(const DVMatrix< S > &m)
Equality operator.
Definition: matrix.h:767
BASE_EXPORT DSymMatrix< 3 > toSymMatrix(const SymTensor &s)
Definition: matrix.cpp:16
DVMatrix is a double version of VMatrix.
Definition: matrix.h:756
T trace() const
Return the trace of the matrix or the sum of the diagonal components. Works also if the matrix is not...
Definition: matrix.h:205
T & get(unsigned x, unsigned y)
Retrieve value at row x column y. Bounds checking is done in a debug compile.
Definition: matrix.h:237
Vector3< Double > DVect3
Definition: vect.h:289
const T & x() const
X component access.
Definition: vect.h:54
void addGenBlock(const Matrix< T, SX2, SY2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from normal not block indices...
Definition: matrix.h:552
Vector2< Double > DVect2
Definition: vect.h:282
DMatrix(const Matrix< double, SX, SY > &m)
Copy constructor, for Matrix.
Definition: matrix.h:728
Matrix(const Matrix< T, SX, 1 > &m)
Copy constructor.
Definition: matrix.h:228
void addGenBlock(const SymMatrix< T, SX2 > &m, unsigned iSrc, unsigned jSrc, unsigned iDst, unsigned jDst)
Adds a block to this matrix from matrix m using the block size (BX,BY), from normal not block indices...
Definition: matrix.h:561
static Matrix< T, SX, 1 > identity()
Returns an identity matrix (or as close as you can get if not diagonal).
Definition: matrix.h:304
const SymMatrix< T, SX > & operator+=(const SymMatrix< T, SX > &m)
In-place matrix addition.
Definition: matrix.h:481
Definition: matrix.h:636
Vector3< T > toVector3(const VMatrix< T, SX > &m, unsigned start=0)
Converts a VMatrix to a Vector3, using three elements starting at index start.
Definition: matrix.h:969
const T & get(unsigned x) const
Retrieve constant value at row x. Bounds checking is done in a debug compile.
Definition: matrix.h:235
void set(const T &t=T())
Sets all matrix elements to t.
Definition: matrix.h:577
void addBlock(const Matrix< T, SX2, 1 > &m, unsigned iSrc, unsigned iDst)
Adds a block to this matrix from matrix m using the block size BX from block indice iSrc to indice iD...
Definition: matrix.h:281
DMatrix(const DMatrix< SX, SY > &m)
Copy constructor.
Definition: matrix.h:726
Matrix< T, SX, SY > operator-(const Matrix< T, SX, SY > &m) const
Binary subtraction operator.
Definition: matrix.h:150
static SymMatrix< T, SX > identity()
Returns an identity matrix (or as close as you can get if not diagonal).
Definition: matrix.h:582
T & get(unsigned x, unsigned y)
Retrieve value at row x column y. Bounds checking is done in a debug compile.
Definition: matrix.h:128
static Matrix< T, SX, SY > identity()
Returns an identity matrix (or as close as you can get if not diagonal).
Definition: matrix.h:210
const SymMatrix< T, SX > & operator=(const SymMatrix< T, SX > &m)
Equality operator.
Definition: matrix.h:465
DSymMatrix()
Default constructor, no data initialization.
Definition: matrix.h:741
T & get(unsigned x)
Retrieve value at row x. Bounds checking is done in a debug compile.
Definition: matrix.h:239
const T & y() const
The y-component of the vector.
Definition: vect.h:184
SymMatrix< T, SX > operator *(const T &t) const
Binary scalar multiplication operator for a symetric matrix.
Definition: matrix.h:499
const T & operator()(unsigned x, unsigned y) const
() operator access to const get(x,y)
Definition: matrix.h:241
const T & x() const
The x-component of the vector.
Definition: vect.h:182
Definition: matrix.h:652
const SymMatrix< T, SX > & operator/=(const T &t)
In-place division by a scalar.
Definition: matrix.h:487
const VMatrix< T, S > & operator=(const Matrix< T, S, 1 > &m)
Equality operator, works on Matrix is SY is 1.
Definition: matrix.h:700