Image Component Library (ICL)
DynMatrix.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLMath/src/ICLMath/DynMatrix.h **
10 ** Module : ICLMath **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/Macros.h>
34 #include <ICLUtils/Exception.h>
35 
36 #include <iterator>
37 #include <algorithm>
38 #include <numeric>
39 #include <functional>
40 #include <vector>
41 #include <cmath>
42 
43 #ifdef ICL_HAVE_IPP
44 #include <ipp.h>
45 #endif
46 
47 // Intel Math Kernel Library
48 #ifdef ICL_HAVE_MKL
49 #include "mkl_cblas.h"
50 #endif
51 
52 namespace icl{
53  namespace math{
54 
57  InvalidMatrixDimensionException(const std::string &msg):utils::ICLException(msg){}
58  };
59 
62  IncompatibleMatrixDimensionException(const std::string &msg):utils::ICLException(msg){}
63  };
64 
67  InvalidIndexException(const std::string &msg):utils::ICLException(msg){}
68  };
69 
72  SingularMatrixException(const std::string &msg):utils::ICLException(msg){}
73  };
74 
76 
80  template<class T>
81  struct DynMatrix{
82 
84  class DynMatrixColumn;
87  DynMatrix(const DynMatrixColumn &column);
89 
91  inline DynMatrix():m_rows(0),m_cols(0),m_data(0),m_ownData(true){}
92 
94  inline DynMatrix(unsigned int cols,unsigned int rows,const T &initValue=0) :
96  if(!dim()) throw InvalidMatrixDimensionException("matrix dimensions must be > 0");
97  m_data = new T[cols*rows];
98  std::fill(begin(),end(),initValue);
99  }
100 
102 
104  inline DynMatrix(unsigned int cols,unsigned int rows, T *data, bool deepCopy=true) :
105  m_rows(rows),m_cols(cols),m_ownData(deepCopy){
106  if(!dim()) throw InvalidMatrixDimensionException("matrix dimensions must be > 0");
107  if(deepCopy){
108  m_data = new T[dim()];
109  std::copy(data,data+dim(),begin());
110  }else{
111  m_data = data;
112  }
113  }
114 
116  inline DynMatrix(unsigned int cols,unsigned int rows,const T *data) :
117  m_rows(rows),m_cols(cols),m_ownData(true){
118  if(!dim()) throw InvalidMatrixDimensionException("matrix dimensions must be > 0");
119  m_data = new T[dim()];
120  std::copy(data,data+dim(),begin());
121  }
122 
124  inline DynMatrix(const DynMatrix &other):
125  m_rows(other.m_rows),m_cols(other.m_cols),m_data(dim() ? new T[dim()] : 0),m_ownData(true){
126  std::copy(other.begin(),other.end(),begin());
127  }
128 
130 
131  inline DynMatrix(const std::string &filename):m_rows(0),m_cols(0),m_data(0),m_ownData(true){
132  *this = loadCSV(filename);
133  }
134 
136 
140  static DynMatrix<T> loadCSV(const std::string &filename) ;
141 
143 
144  void saveCSV(const std::string &filename) ;
145 
147  inline bool isNull() const { return !m_data; }
148 
150  inline ~DynMatrix(){
151  if(m_data && m_ownData) delete [] m_data;
152  }
153 
155 
159  inline DynMatrix &operator=(const DynMatrix &other){
160  if(!m_data && !other.m_ownData){
161  m_data = other.m_data;
162  m_ownData = false;
163  m_rows = other.m_rows;
164  m_cols = other.m_cols;
165  }else{
166  if(dim() != other.dim()){
167  delete[] m_data;
168  m_data = other.dim() ? new T[other.dim()] : 0;
169  }
170  m_cols = other.m_cols;
171  m_rows = other.m_rows;
172 
173  std::copy(other.begin(),other.end(),begin());
174  }
175  return *this;
176  }
177 
179  inline void setBounds(unsigned int cols, unsigned int rows, bool holdContent=false, const T &initializer=0) {
180  if((int)cols == m_cols && (int)rows==m_rows) return;
181  if(cols*rows == 0) throw InvalidMatrixDimensionException("matrix dimensions must be > 0");
182  DynMatrix M(cols,rows,initializer);
183  if(holdContent){
184  unsigned int min_cols = iclMin(cols,(unsigned int)m_cols);
185  unsigned int min_rows = iclMin(rows,(unsigned int)m_rows);
186  for(unsigned int i=0;i<min_cols;++i){
187  for(unsigned int j=0;j<min_rows;++j){
188  M(i,j) = (*this)(i,j);
189  }
190  }
191  }
192  m_cols = cols;
193  m_rows = rows;
194  if(m_data && m_ownData) delete [] m_data;
195  m_data = M.begin();
196  m_ownData = true;
197  M.set_data(0);
198  }
199 
201  inline bool isSimilar(const DynMatrix &other, T tollerance=T(0.0001)) const{
202  if(other.cols() != cols() || other.rows() != rows()) return false;
203  for(unsigned int i=0;i<dim();++i){
204  T diff = m_data[i] - other.m_data[i];
205  if((diff>0?diff:-diff) > tollerance) return false;
206  }
207  return true;
208  }
209 
211  inline bool operator==(const DynMatrix &other) const{
212  if(other.cols() != cols() || other.rows() != rows()) return false;
213  for(unsigned int i=0;i<dim();++i){
214  if(m_data[i] != other.m_data[i]) return false;
215  }
216  return true;
217  }
218 
220  inline bool operator!=(const DynMatrix &other) const{
221  if(other.cols() != cols() || other.rows() != rows()) return false;
222  for(unsigned int i=0;i<dim();++i){
223  if(m_data[i] != other.m_data[i]) return true;
224  }
225  return false;
226  }
227 
228 
230  inline DynMatrix operator*(T f) const{
231  DynMatrix dst(cols(),rows());
232  return mult(f,dst);
233  }
234 
236  inline DynMatrix &mult(T f, DynMatrix &dst) const{
237  dst.setBounds(cols(),rows());
238  std::transform(begin(),end(),dst.begin(),std::bind2nd(std::multiplies<T>(),f));
239  return dst;
240  }
241 
243  inline DynMatrix &operator*=(T f){
244  std::transform(begin(),end(),begin(),std::bind2nd(std::multiplies<T>(),f));
245  return *this;
246  }
247 
249  inline DynMatrix operator/(T f) const{
250  return this->operator*(1/f);
251  }
252 
254  inline DynMatrix &operator/=(T f){
255  return this->operator*=(1/f);
256  }
257 
259  inline DynMatrix &mult(const DynMatrix &m, DynMatrix &dst) const {
260  if( cols() != m.rows() ) throw IncompatibleMatrixDimensionException("A*B : cols(A) must be rows(B)");
261  dst.setBounds(m.cols(),rows());
262  for(unsigned int c=0;c<dst.cols();++c){
263  for(unsigned int r=0;r<dst.rows();++r){
264  dst(c,r) = std::inner_product(row_begin(r),row_end(r),m.col_begin(c),T(0));
265  }
266  }
267  return dst;
268  }
269 
271  inline DynMatrix &elementwise_mult(const DynMatrix &m, DynMatrix &dst) const {
272  if((m.cols() != cols()) || (m.rows() != rows())) throw IncompatibleMatrixDimensionException("A.*B dimension mismatch");
273  dst.setBounds(cols(),rows());
274  for(unsigned int i=0;i<dim();++i){
275  dst[i] = m_data[i] * m[i];
276  }
277  return dst;
278  }
279 
281  inline DynMatrix elementwise_mult(const DynMatrix &m) const {
282  DynMatrix dst(cols(),rows());
283  return elementwise_mult(m,dst);
284  }
285 
287  inline DynMatrix &elementwise_div(const DynMatrix &m, DynMatrix &dst) const {
288  if((m.cols() != cols()) || (m.rows() != rows())) throw IncompatibleMatrixDimensionException("A./B dimension mismatch");
289  dst.setBounds(cols(),rows());
290  for(int i=0;i<dim();++i){
291  dst[i] = m_data[i] / m[i];
292  }
293  return dst;
294  }
295 
297  inline DynMatrix elementwise_div(const DynMatrix &m) const {
298  DynMatrix dst(cols(),rows());
299  return elementwise_div(m,dst);
300  }
301 
302 
303 
304 
306  inline DynMatrix operator*(const DynMatrix &m) const {
307  DynMatrix d(m.cols(),rows());
308  return mult(m,d);
309  }
310 
312  inline DynMatrix &operator*=(const DynMatrix &m) {
313  return *this=((*this)*m);
314  }
315 
317  inline DynMatrix operator/(const DynMatrix &m) const {
318  return this->operator*(m.inv());
319  }
320 
322  inline DynMatrix &operator/=(const DynMatrix &m) const {
323  return *this = this->operator*(m.inv());
324  }
325 
327  inline DynMatrix operator+(const T &t) const{
328  DynMatrix d(cols(),rows());
329  std::transform(begin(),end(),d.begin(),std::bind2nd(std::plus<T>(),t));
330  return d;
331  }
332 
334  inline DynMatrix operator-(const T &t) const{
335  DynMatrix d(cols(),rows());
336  std::transform(begin(),end(),d.begin(),std::bind2nd(std::minus<T>(),t));
337  return d;
338  }
339 
341  inline DynMatrix &operator+=(const T &t){
342  std::transform(begin(),end(),begin(),std::bind2nd(std::plus<T>(),t));
343  return *this;
344  }
345 
347  inline DynMatrix &operator-=(const T &t){
348  std::transform(begin(),end(),begin(),std::bind2nd(std::minus<T>(),t));
349  return *this;
350  }
351 
353  inline DynMatrix operator+(const DynMatrix &m) const {
354  if(cols() != m.cols() || rows() != m.rows()) throw IncompatibleMatrixDimensionException("A+B size(A) must be size(B)");
355  DynMatrix d(cols(),rows());
356  std::transform(begin(),end(),m.begin(),d.begin(),std::plus<T>());
357  return d;
358  }
359 
361  inline DynMatrix operator-(const DynMatrix &m) const {
362  if(cols() != m.cols() || rows() != m.rows()) throw IncompatibleMatrixDimensionException("A+B size(A) must be size(B)");
363  DynMatrix d(cols(),rows());
364  std::transform(begin(),end(),m.begin(),d.begin(),std::minus<T>());
365  return d;
366  }
367 
369  inline DynMatrix &operator+=(const DynMatrix &m) {
370  if(cols() != m.cols() || rows() != m.rows()) throw IncompatibleMatrixDimensionException("A+B size(A) must be size(B)");
371  std::transform(begin(),end(),m.begin(),begin(),std::plus<T>());
372  return *this;
373  }
374 
376  inline DynMatrix &operator-=(const DynMatrix &m) {
377  if(cols() != m.cols() || rows() != m.rows()) throw IncompatibleMatrixDimensionException("A+B size(A) must be size(B)");
378  std::transform(begin(),end(),m.begin(),begin(),std::minus<T>());
379  return *this;
380  }
381 
383  inline T &operator()(unsigned int col,unsigned int row){
384  #ifdef DYN_MATRIX_INDEX_CHECK
385  if((int)col >= m_cols || (int)row >= m_rows) ERROR_LOG("access to "<<m_cols<<'x'<<m_rows<<"-matrix index (" << col << "," << row << ")");
386  #endif
387  return m_data[col+cols()*row];
388  }
389 
391  inline const T &operator() (unsigned int col,unsigned int row) const{
392  #ifdef DYN_MATRIX_INDEX_CHECK
393  if((int)col >= m_cols || (int)row >= m_rows) ERROR_LOG("access to "<<m_cols<<'x'<<m_rows<<"-matrix index (" << col << "," << row << ")");
394  #endif
395  return m_data[col+cols()*row];
396  }
397 
399  inline T &at(unsigned int col,unsigned int row) {
400  if(col>=cols() || row >= rows()) throw InvalidIndexException("row or col index too large");
401  return m_data[col+cols()*row];
402  }
403 
405  inline const T &at(unsigned int col,unsigned int row) const {
406  return const_cast<DynMatrix*>(this)->at(col,row);
407  }
408 
409 
410 
412  inline T &operator[](unsigned int idx) {
413  idx_check(idx);
414  if(idx >= dim()) ERROR_LOG("access to "<<m_cols<<'x'<<m_rows<<"-matrix index [" << idx<< "]");
415 
416  return m_data[idx];
417 
418  }
419 
420 
422  inline const T &operator[](unsigned int idx) const {
423  idx_check(idx);
424  return m_data[idx];
425  }
426 
428  inline T norm(double l=2) const{
429  double accu = 0;
430  for(unsigned int i=0;i<dim();++i){
431  accu += ::pow(double(m_data[i]),l);
432  }
433  return ::pow(double(accu),1.0/l);
434  }
435 
437  private:
438  static T diff_power_two(const T&a, const T&b){
439  T d = a-b;
440  return d*d;
441  }
442  public:
445  inline T sqrDistanceTo(const DynMatrix &other) const {
447  ICLASSERT_THROW(dim() == other.dim(), InvalidMatrixDimensionException("DynMatrix::sqrDistanceTo: dimension missmatch"));
448  return std::inner_product(begin(),end(),other.begin(),T(0), std::plus<T>(), diff_power_two);
449  }
450 
452  inline T distanceTo(const DynMatrix &other) const {
453  ICLASSERT_THROW(dim() == other.dim(), InvalidMatrixDimensionException("DynMatrix::distanceTo: dimension missmatch"));
454  return ::sqrt( distanceTo(other) );
455  }
456 
457 
459  typedef T* iterator;
460 
462  typedef const T* const_iterator;
463 
465  typedef T* row_iterator;
466 
468  typedef const T* const_row_iterator;
469 
471  unsigned int rows() const { return m_rows; }
472 
474  unsigned int cols() const { return m_cols; }
475 
477  T *data() { return m_data; }
478 
480  const T *data() const { return m_data; }
481 
483  unsigned int dim() const { return m_rows*m_cols; }
484 
486  int stride0() const { return sizeof(T) * dim(); }
487 
489  int stride1() const { return sizeof(T) * cols(); }
490 
492  int stride2() const { return sizeof(T); }
493 
495  struct col_iterator : public std::iterator<std::random_access_iterator_tag,T>{
496  typedef unsigned int difference_type;
497  mutable T *p;
498  unsigned int stride;
499  inline col_iterator(T *col_begin,unsigned int stride):p(col_begin),stride(stride){}
500 
501 
504  p+=stride;
505  return *this;
506  }
508  inline const col_iterator &operator++() const{
509  p+=stride;
510  return *this;
511  }
514  col_iterator tmp = *this;
515  ++(*this);
516  return tmp;
517  }
519  inline const col_iterator operator++(int) const{
520  col_iterator tmp = *this;
521  ++(*this);
522  return tmp;
523  }
524 
527  p-=stride;
528  return *this;
529  }
530 
532  inline const col_iterator &operator--() const{
533  p-=stride;
534  return *this;
535  }
536 
539  col_iterator tmp = *this;
540  --(*this);
541  return tmp;
542  }
543 
545  inline const col_iterator operator--(int) const{
546  col_iterator tmp = *this;
547  --(*this);
548  return tmp;
549  }
550 
553  p += n * stride;
554  return *this;
555  }
556 
558  inline const col_iterator &operator+=(difference_type n) const{
559  p += n * stride;
560  return *this;
561  }
562 
563 
566  p -= n * stride;
567  return *this;
568  }
569 
571  inline const col_iterator &operator-=(difference_type n) const{
572  p -= n * stride;
573  return *this;
574  }
575 
576 
579  col_iterator tmp = *this;
580  tmp+=n;
581  return tmp;
582  }
583 
585  inline const col_iterator operator+(difference_type n) const{
586  col_iterator tmp = *this;
587  tmp+=n;
588  return tmp;
589  }
590 
593  col_iterator tmp = *this;
594  tmp-=n;
595  return tmp;
596  }
597 
599  inline const col_iterator operator-(difference_type n) const {
600  col_iterator tmp = *this;
601  tmp-=n;
602  return tmp;
603  }
604 
605  inline difference_type operator-(const col_iterator &other) const{
606  return (p-other.p)/stride;
607  }
608 
609 
611  inline T &operator*(){
612  return *p;
613  }
614 
616  inline T operator*() const{
617  return *p;
618  }
619 
621  inline bool operator==(const col_iterator &i) const{ return p == i.p; }
622 
624  inline bool operator!=(const col_iterator &i) const{ return p != i.p; }
625 
627  inline bool operator<(const col_iterator &i) const{ return p < i.p; }
628 
630  inline bool operator<=(const col_iterator &i) const{ return p <= i.p; }
631 
633  inline bool operator>=(const col_iterator &i) const{ return p >= i.p; }
634 
636  inline bool operator>(const col_iterator &i) const{ return p > i.p; }
637  };
638 
640  typedef const col_iterator const_col_iterator;
641 
644  public:
645  #ifdef DYN_MATRIX_INDEX_CHECK
646  #define DYN_MATRIX_COLUMN_CHECK(C,E) if(C) ERROR_LOG(E)
647  #else
648  #define DYN_MATRIX_COLUMN_CHECK(C,E)
649  #endif
650  DynMatrix<T> *matrix;
652 
654  unsigned int column;
655 
657  inline DynMatrixColumn(const DynMatrix<T> *matrix, unsigned int column):
658  matrix(const_cast<DynMatrix<T>*>(matrix)),column(column){
659  DYN_MATRIX_COLUMN_CHECK(column >= matrix->cols(),"invalid column index");
660  }
661 
663  inline DynMatrixColumn(const DynMatrix<T> &matrix):
664  matrix(const_cast<DynMatrix<T>*>(&matrix)),column(0){
665  DYN_MATRIX_COLUMN_CHECK(matrix->cols() != 1,"source matrix must have exactly ONE column");
666  }
669  matrix(c.matrix),column(c.column){}
670 
672  inline col_iterator begin() { return matrix->col_begin(column); }
673 
675  inline col_iterator end() { return matrix->col_end(column); }
676 
678  inline const col_iterator begin() const { return matrix->col_begin(column); }
679 
681  inline const col_iterator end() const { return matrix->col_end(column); }
682 
684  inline unsigned int dim() const { return matrix->rows(); }
685 
688  DYN_MATRIX_COLUMN_CHECK(dim() != c.dim(),"dimension missmatch");
689  std::copy(c.begin(),c.end(),begin());
690  return *this;
691  }
692 
694  inline DynMatrixColumn &operator=(const DynMatrix &src){
695  DYN_MATRIX_COLUMN_CHECK(dim() != src.dim(),"dimension missmatch");
696  std::copy(src.begin(),src.end(),begin());
697  return *this;
698  }
699 
702  DYN_MATRIX_COLUMN_CHECK(dim() != c.dim(),"dimension missmatch");
703  std::transform(c.begin(),c.end(),begin(),begin(),std::plus<T>());
704  return *this;
705  }
706 
709  DYN_MATRIX_COLUMN_CHECK(dim() != c.dim(),"dimension missmatch");
710  std::transform(c.begin(),c.end(),begin(),begin(),std::minus<T>());
711  return *this;
712  }
713 
716  DYN_MATRIX_COLUMN_CHECK(dim() != m.dim(),"dimension missmatch");
717  std::transform(m.begin(),m.end(),begin(),begin(),std::plus<T>());
718  return *this;
719  }
722  DYN_MATRIX_COLUMN_CHECK(dim() != m.dim(),"dimension missmatch");
723  std::transform(m.begin(),m.end(),begin(),begin(),std::minus<T>());
724  return *this;
725  }
726 
728  inline DynMatrixColumn &operator*=(const T&val){
729  std::for_each(begin(),end(),std::bind2nd(std::multiplies<T>(),val));
730  return *this;
731  }
733  inline DynMatrixColumn &operator/=(const T&val){
734  std::for_each(begin(),end(),std::bind2nd(std::divides<T>(),val));
735  return *this;
736  }
737 
738  };
739 
740  inline DynMatrix &operator=(const DynMatrixColumn &col){
741  DYN_MATRIX_COLUMN_CHECK(dim() != col.dim(),"dimension missmatch");
742  std::copy(col.begin(),col.end(),begin());
743  return *this;
744  }
745 
746  #undef DYN_MATRIX_COLUMN_CHECK
747 
748 
749 
751  inline iterator begin() { return m_data; }
752 
754  inline iterator end() { return m_data+dim(); }
755 
757  inline const_iterator begin() const { return m_data; }
758 
760  inline const_iterator end() const { return m_data+dim(); }
761 
763  inline col_iterator col_begin(unsigned int col) {
764  col_check(col);
765  return col_iterator(m_data+col,cols());
766  }
767 
769  inline col_iterator col_end(unsigned int col) {
770  col_check(col);
771  return col_iterator(m_data+col+dim(),cols());
772  }
773 
775  inline const_col_iterator col_begin(unsigned int col) const {
776  col_check(col);
777  return col_iterator(m_data+col,cols());
778  }
779 
781  inline const_col_iterator col_end(unsigned int col) const {
782  col_check(col);
783  return col_iterator(m_data+col+dim(),cols());
784  }
785 
787  inline row_iterator row_begin(unsigned int row) {
788  row_check(row);
789  return m_data+row*cols();
790  }
791 
793  inline row_iterator row_end(unsigned int row) {
794  row_check(row);
795  return m_data+(row+1)*cols();
796  }
797 
799  inline const_row_iterator row_begin(unsigned int row) const {
800  row_check(row);
801  return m_data+row*cols();
802  }
803 
805  inline const_row_iterator row_end(unsigned int row) const {
806  row_check(row);
807  return m_data+(row+1)*cols();
808  }
809 
811  inline DynMatrix row(int row){
812  row_check(row);
813  return DynMatrix(m_cols,1,row_begin(row),false);
814  }
815 
817  inline const DynMatrix row(int row) const{
818  row_check(row);
819  return DynMatrix(m_cols,1,const_cast<T*>(row_begin(row)),false);
820  }
821 
823  inline DynMatrixColumn col(int col){
824  return DynMatrixColumn(this,col);
825  }
826 
827  inline const DynMatrixColumn col(int col) const{
828  return DynMatrixColumn(this,col);
829  }
830 
832  void decompose_QR(DynMatrix &Q, DynMatrix &R) const
833  ;
834 
836  void decompose_RQ(DynMatrix &R, DynMatrix &Q) const
837  ;
838 
840 
842  void decompose_LU(DynMatrix &L, DynMatrix &U, T zeroThreshold = T(1E-16)) const;
843 
846 
849 
851 
898  DynMatrix solve(const DynMatrix &b, const std::string &method = "lu", T zeroThreshold = T(1E-16));
899 
900 
902  DynMatrix inv() const ;
903 
905 
918  void eigen(DynMatrix &eigenvectors, DynMatrix &eigenvalues) const ;
919 
921 
928  void svd(DynMatrix &U, DynMatrix &S, DynMatrix &V) const ;
929 
931 
955  DynMatrix pinv(bool useSVD = false, T zeroThreshold = T(1E-16)) const
956  ;
957 
959 
965  DynMatrix big_matrix_pinv(T zeroThreshold = T(1E-16)) const
966  ;
967 
968  #ifdef ICL_HAVE_MKL
969  typedef void(*GESDD)(const char*, const int*, const int*, T*, const int*, T*, T*, const int*, T*, const int*, T*, const int*, int*, int*);
970  typedef void(*CBLAS_GEMM)(CBLAS_ORDER,CBLAS_TRANSPOSE,CBLAS_TRANSPOSE,int,int,int,T,const T*,int,const T*,int,T,T*,int);
971  DynMatrix big_matrix_pinv(T zeroThreshold, GESDD gesdd, CBLAS_GEMM cblas_gemm) const
972  ;
973  #endif
974 
976  T det() const ;
977 
979  inline DynMatrix transp() const{
980  DynMatrix d(rows(),cols());
981  for(unsigned int x=0;x<cols();++x){
982  for(unsigned int y=0;y<rows();++y){
983  d(y,x) = (*this)(x,y);
984  }
985  }
986  return d;
987  }
988 
990 
991  inline const DynMatrix<T> shallowTransposed() const{
992  return DynMatrix<T>(m_rows,m_cols,const_cast<T*>(m_data),false);
993  }
994 
997  return DynMatrix<T>(m_rows,m_cols,const_cast<T*>(m_data),false);
998  }
999 
1001 
1010  inline void reshape(int newCols, int newRows) {
1011  if((cols() * rows()) != (newCols * newRows)){
1012  throw InvalidMatrixDimensionException("DynMatrix<T>::reshape: source dimension and destination dimension differs!");
1013  }
1014  m_cols = newCols;
1015  m_rows = newRows;
1016  }
1017 
1019 
1021  return std::inner_product(begin(),end(),other.begin(),T(0));
1022  }
1023 
1024 
1026 
1029  DynMatrix<T> dot(const DynMatrix<T> &M) const {
1030  return this->transp() * M;
1031  }
1032 
1033 
1037  DynMatrix<T> d(1,rows());
1038  for(int i=0;i<rows();++i){
1039  d[i] = (*this)(i,i);
1040  }
1041  return d;
1042  }
1043 
1045  T trace() const{
1046  ICLASSERT_RETURN_VAL(cols()==rows(),0);
1047  double accu = 0;
1048  for(unsigned int i=0;i<dim();i+=cols()+1){
1049  accu += m_data[i];
1050  }
1051  return accu;
1052  }
1053 
1055  static DynMatrix<T> cross(const DynMatrix<T> &x, const DynMatrix<T> &y){
1056  if(x.cols()==1 && y.cols()==1 && x.rows()==3 && y.rows()==3){
1057  DynMatrix<T> r(1,x.rows());
1058  r(0,0) = x(0,1)*y(0,2)-x(0,2)*y(0,1);
1059  r(0,1) = x(0,2)*y(0,0)-x(0,0)*y(0,2);
1060  r(0,2) = x(0,0)*y(0,1)-x(0,1)*y(0,0);
1061  return r;
1062  }else{
1063  ICLASSERT_RETURN_VAL(x.rows() == 3 && y.rows() == 3,DynMatrix<T>());
1064  return DynMatrix<T>();
1065  }
1066  }
1067 
1069  T cond(const double p=2) const {
1070  if(cols() == 3 && rows() == 3){
1071  DynMatrix<T> M_inv = (*this).inv();
1072  return (*this).norm(p) * M_inv.norm(p);
1073  } else {
1074  DynMatrix<T> U,S,V;
1075  (*this).svd(U,S,V);
1076  if(S[S.rows()-1]){
1077  return S[0]/S[S.rows()-1];
1078  } else {
1079  return S[0];
1080  }
1081  }
1082  }
1083 
1085  inline T *set_data(T *newData){
1086  T *old_data = m_data;
1087  m_data = newData;
1088  return old_data;
1089  }
1090 
1092  static inline DynMatrix id(unsigned int dim) {
1093  DynMatrix M(dim,dim);
1094  for(unsigned int i=0;i<dim;++i) M(i,i) = 1;
1095  return M;
1096  }
1097 
1098  private:
1099  inline void row_check(unsigned int row) const{
1100  #ifdef DYN_MATRIX_INDEX_CHECK
1101  if((int)row >= m_rows) ERROR_LOG("access to row index " << row << " on a "<<m_cols<<'x'<<m_rows<<"-matrix");
1102  #else
1103  (void)row;
1104  #endif
1105  }
1106  inline void col_check(unsigned int col) const{
1107  #ifdef DYN_MATRIX_INDEX_CHECK
1108  if((int)col >= m_cols) ERROR_LOG("access to column index " << col << " on a "<<m_cols<<'x'<<m_rows<<"-matrix");
1109  #else
1110  (void)col;
1111  #endif
1112  }
1113  inline void idx_check(unsigned int col, unsigned int row) const{
1114  col_check(col);
1115  row_check(row);
1116  }
1117 
1118  inline void idx_check(unsigned int idx) const{
1119  #ifdef DYN_MATRIX_INDEX_CHECK
1120  if(idx >= dim()) ERROR_LOG("access to linear index " << idx << " on a "<<m_cols<<'x'<<m_rows<<"-matrix");
1121  #else
1122  (void)idx;
1123  #endif
1124  }
1125 
1126  int m_rows;
1127  int m_cols;
1130  };
1131 
1133  template<class T>
1135  DynMatrix<T>::DynMatrix(const typename DynMatrix<T>::DynMatrixColumn &column) :
1136  m_rows(column.dim()),m_cols(1),m_data(new T[column.dim()]),m_ownData(true){
1137  std::copy(column.begin(),column.end(),begin());
1138  }
1141  template<class T> ICLMath_IMP
1143  std::ostream &operator<<(std::ostream &s, const DynMatrix<T> &m);
1144 
1146  template<class T> ICLMath_IMP
1147  std::istream &operator>>(std::istream &s, DynMatrix<T> &m);
1148 
1149 
1150 #ifdef ICL_HAVE_IPP
1151 
1152  template<>
1153  inline float DynMatrix<float>::sqrDistanceTo(const DynMatrix<float> &other) const {
1154  ICLASSERT_THROW(dim() == other.dim(), InvalidMatrixDimensionException("DynMatrix::sqrDistanceTo: dimension missmatch"));
1155  float norm = 0 ;
1156  ippsNormDiff_L2_32f(begin(), other.begin(), dim(), &norm);
1157  return norm*norm;
1158  }
1159 
1160  template<>
1161  inline double DynMatrix<double>::sqrDistanceTo(const DynMatrix<double> &other) const {
1162  ICLASSERT_THROW(dim() == other.dim(), InvalidMatrixDimensionException("DynMatrix::sqrDistanceTo: dimension missmatch"));
1163  double norm = 0 ;
1164  ippsNormDiff_L2_64f(begin(), other.begin(), dim(), &norm);
1165  return norm*norm;
1166  }
1167 
1168  template<>
1169  inline float DynMatrix<float>::distanceTo(const DynMatrix<float> &other) const {
1170  ICLASSERT_THROW(dim() == other.dim(), InvalidMatrixDimensionException("DynMatrix::distanceTo: dimension missmatch"));
1171  float norm = 0 ;
1172  ippsNormDiff_L2_32f(begin(), other.begin(), dim(), &norm);
1173  return norm;
1174  }
1175 
1176  template<>
1177  inline double DynMatrix<double>::distanceTo(const DynMatrix<double> &other) const {
1178  ICLASSERT_THROW(dim() == other.dim(), InvalidMatrixDimensionException("DynMatrix::distanceTo: dimension missmatch"));
1179  double norm = 0 ;
1180  ippsNormDiff_L2_64f(begin(), other.begin(), dim(), &norm);
1181  return norm;
1182  }
1183 
1184 #define DYN_MATRIX_MULT_SPECIALIZE(IPPT) \
1185  template<> \
1186  inline DynMatrix<Ipp##IPPT> &DynMatrix<Ipp##IPPT>::mult( \
1187  const DynMatrix<Ipp##IPPT> &m, DynMatrix<Ipp##IPPT> &dst) const \
1188  { \
1189  if(cols() != m.rows() ) throw IncompatibleMatrixDimensionException("A*B : cols(A) must be row(B)"); \
1190  dst.setBounds(m.cols(),rows()); \
1191  ippmMul_mm_##IPPT(data(),sizeof(Ipp##IPPT)*cols(),sizeof(Ipp##IPPT),cols(),rows(), \
1192  m.data(),sizeof(Ipp##IPPT)*m.cols(),sizeof(Ipp##IPPT),m.cols(),m.rows(), \
1193  dst.data(),m.cols()*sizeof(Ipp##IPPT),sizeof(Ipp##IPPT)); \
1194  return dst; \
1195  }
1196 
1197  DYN_MATRIX_MULT_SPECIALIZE(32f)
1198  DYN_MATRIX_MULT_SPECIALIZE(64f)
1199 #undef DYN_MATRIX_MULT_SPECIALIZE
1200 
1201 
1202 #define DYN_MATRIX_ELEMENT_WISE_DIV_SPECIALIZE(IPPT) \
1203  template<> \
1204  inline DynMatrix<Ipp##IPPT> &DynMatrix<Ipp##IPPT>::elementwise_div( \
1205  const DynMatrix<Ipp##IPPT> &m, DynMatrix<Ipp##IPPT> &dst) const \
1206  { \
1207  if((m.cols() != cols()) || (m.rows() != rows())){ \
1208  throw IncompatibleMatrixDimensionException("A./B dimension mismatch"); \
1209  } \
1210  dst.setBounds(cols(),rows()); \
1211  ippsDiv_##IPPT(data(),m.data(),dst.data(),dim()); \
1212  return dst; \
1213  }
1214 
1215  DYN_MATRIX_ELEMENT_WISE_DIV_SPECIALIZE(32f)
1216  DYN_MATRIX_ELEMENT_WISE_DIV_SPECIALIZE(64f)
1217 
1218 #undef DYN_MATRIX_ELEMENT_WISE_DIV_SPECIALIZE
1219 
1220 
1221 
1222 
1223 
1224 
1225 #define DYN_MATRIX_MULT_BY_CONSTANT(IPPT) \
1226  template<> \
1227  inline DynMatrix<Ipp##IPPT> &DynMatrix<Ipp##IPPT>::mult( \
1228  Ipp##IPPT f, DynMatrix<Ipp##IPPT> &dst) const{ \
1229  dst.setBounds(cols(),rows()); \
1230  ippsMulC_##IPPT(data(),f, dst.data(),dim()); \
1231  return dst; \
1232  }
1233 
1234  DYN_MATRIX_MULT_BY_CONSTANT(32f)
1235  DYN_MATRIX_MULT_BY_CONSTANT(64f)
1236 
1237 #undef DYN_MATRIX_MULT_BY_CONSTANT
1238 
1239 #define DYN_MATRIX_NORM_SPECIALZE(T,IPPT) \
1240  template<> \
1241  inline T DynMatrix<T> ::norm(double l) const{ \
1242  if(l==1){ \
1243  T val; \
1244  ippsNorm_L1_##IPPT(m_data,dim(),&val); \
1245  return val; \
1246  }else if(l==2){ \
1247  T val; \
1248  ippsNorm_L2_##IPPT(m_data,dim(),&val); \
1249  return val; \
1250  } \
1251  double accu = 0; \
1252  for(unsigned int i=0;i<dim();++i){ \
1253  accu += ::pow(double(m_data[i]),l); \
1254  } \
1255  return ::pow(accu,1.0/l); \
1256  }
1257 
1258  DYN_MATRIX_NORM_SPECIALZE(float,32f)
1259  // DYN_MATRIX_NORM_SPECIALZE(double,64f)
1260 
1261 #undef DYN_MATRIX_NORM_SPECIALZE
1262 
1265 #endif // ICL_HAVE_IPP
1266 
1268 
1269  template<class T>
1270  inline DynMatrix<T> operator,(const DynMatrix<T> &left, const DynMatrix<T> &right){
1271  int w = left.cols() + right.cols();
1272  int h = iclMax(left.rows(),right.rows());
1273  DynMatrix<T> result(w,h,float(0));
1274  for(unsigned int y=0;y<left.rows();++y){
1275  std::copy(left.row_begin(y), left.row_end(y), result.row_begin(y));
1276  }
1277  for(unsigned int y=0;y<right.rows();++y){
1278  std::copy(right.row_begin(y), right.row_end(y), result.row_begin(y) + left.cols());
1279  }
1280  return result;
1281  }
1282 
1284 
1285  template<class T>
1286  inline DynMatrix<T> operator%(const DynMatrix<T> &top, const DynMatrix<T> &bottom){
1287  int w = iclMax(top.cols(),bottom.cols());
1288  int h = top.rows() + bottom.rows();
1289  DynMatrix<T> result(w,h,float(0));
1290  for(unsigned int y=0;y<top.rows();++y){
1291  std::copy(top.row_begin(y), top.row_end(y), result.row_begin(y));
1292  }
1293  for(unsigned int y=0;y<bottom.rows();++y){
1294  std::copy(bottom.row_begin(y), bottom.row_end(y), result.row_begin(y+top.rows()));
1295  }
1296  return result;
1297  }
1298  } // namespace math
1299 }
DynMatrix pinv(bool useSVD=false, T zeroThreshold=T(1E-16)) const
calculates the Moore-Penrose pseudo-inverse (only implemented for icl32f and icl64f)
DynMatrix< T > dot(const DynMatrix< T > &M) const
returns the inner product of two matrices (i.e. dot-product)
Definition: DynMatrix.h:1029
col_iterator(T *col_begin, unsigned int stride)
Definition: DynMatrix.h:499
ICLQt_API ImgQ sqrt(const ImgQ &image)
calls sqrt( each pixel)
void idx_check(unsigned int col, unsigned int row) const
Definition: DynMatrix.h:1113
const_row_iterator row_end(unsigned int row) const
returns an iterator of a certains row's end (const)
Definition: DynMatrix.h:805
T * set_data(T *newData)
sets new data internally and returns old data pointer (for experts only!)
Definition: DynMatrix.h:1085
void(* GESDD)(const char *, const int *, const int *, T *, const int *, T *, T *, const int *, T *, const int *, T *, const int *, int *, int *)
Definition: DynMatrix.h:969
void decompose_LU(DynMatrix &L, DynMatrix &U, T zeroThreshold=T(1E-16)) const
applies LU-decomposition (without using partial pivoting) (only for icl32f and icl64f)
bool operator==(const DynMatrix &other) const
elementwise comparison (==)
Definition: DynMatrix.h:211
const col_iterator & operator-=(difference_type n) const
jump backward next n elements (inplace) (const)
Definition: DynMatrix.h:571
const col_iterator operator--(int) const
postfix decrement operator (const)
Definition: DynMatrix.h:545
undocument this line if you encounter any issues!
Definition: Any.h:37
DynMatrix elementwise_mult(const DynMatrix &m) const
Elementwise matrix multiplication (without destination matrix) [IPP-Supported].
Definition: DynMatrix.h:281
const T * data() const
internal data pointer (const)
Definition: DynMatrix.h:480
col_iterator & operator+=(difference_type n)
jump next n elements (inplace)
Definition: DynMatrix.h:552
void decompose_QR(DynMatrix &Q, DynMatrix &R) const
applies QR-decomposition using stabilized Gram-Schmidt orthonormalization (only for icl32f and icl64f...
DynMatrix & operator=(const DynMatrix &other)
Assignment operator (using deep/shallow-copy)
Definition: DynMatrix.h:159
void col_check(unsigned int col) const
Definition: DynMatrix.h:1106
ICLQt_API core::Img< T > norm(const core::Img< T > &image)
normalize an images range to [0,255]
const T & at(unsigned int col, unsigned int row) const
element access with index check (const)
Definition: DynMatrix.h:405
#define ICLMath_API
Definition: CompatMacros.h:173
DynMatrix & elementwise_div(const DynMatrix &m, DynMatrix &dst) const
Elementwise division (in source destination fashion) [IPP-Supported].
Definition: DynMatrix.h:287
DynMatrix(unsigned int cols, unsigned int rows, const T *data)
Create a matrix with given data (const version: deepCopy only)
Definition: DynMatrix.h:116
Special linear algebra exception type .
Definition: DynMatrix.h:71
const DynMatrixColumn col(int col) const
Definition: DynMatrix.h:827
DynMatrix elementwise_div(const DynMatrix &m) const
Elementwise matrix multiplication (without destination matrix) [IPP-Supported].
Definition: DynMatrix.h:297
DynMatrixColumn & operator=(const DynMatrix &src)
assigne dyn matrix to matrix columns
Definition: DynMatrix.h:694
const T * const_row_iterator
complex const_row_iterator type
Definition: DynMatrix.h:468
DynMatrix solve(const DynMatrix &b, const std::string &method="lu", T zeroThreshold=T(1E-16))
solves Mx=b for M=*this (only for icl32f and icl64f)
DynMatrixColumn & operator-=(const DynMatrix &m)
operator -= for DynMatrices
Definition: DynMatrix.h:721
T det() const
matrix determinant (only for icl32f and icl64f)
const col_iterator & operator++() const
prefix increment operator (const)
Definition: DynMatrix.h:508
DynMatrixColumn(const DynMatrix< T > &matrix)
Create from source matrix (only works if matrix has only single column = column-vector)
Definition: DynMatrix.h:663
DynMatrix & operator+=(const DynMatrix &m)
Matrix addition (inplace)
Definition: DynMatrix.h:369
void(* CBLAS_GEMM)(CBLAS_ORDER, CBLAS_TRANSPOSE, CBLAS_TRANSPOSE, int, int, int, T, const T *, int, const T *, int, T, T *, int)
Definition: DynMatrix.h:970
col_iterator operator-(difference_type n)
jump backward next n elements
Definition: DynMatrix.h:592
DynMatrix operator *(T f) const
Multiply elements with scalar.
Definition: DynMatrix.h:230
Highly flexible and optimized matrix class implementation.
Definition: DynMatrix.h:81
DynMatrix operator/(T f) const
Device elements by scalar.
Definition: DynMatrix.h:249
DynMatrix inv() const
invert the matrix (only for icl32f and icl64f)
col_iterator operator+(difference_type n)
jump next n elements
Definition: DynMatrix.h:578
const DynMatrix< T > shallowTransposed()
returns a shallow transposed copy of this matrix (dimensions are swapped, data is not re-aranged)
Definition: DynMatrix.h:996
col_iterator col_begin(unsigned int col)
returns an iterator running through a certain matrix column
Definition: DynMatrix.h:763
int m_rows
Definition: DynMatrix.h:1126
const col_iterator & operator--() const
prefix decrement operator (const)
Definition: DynMatrix.h:532
difference_type operator-(const col_iterator &other) const
Definition: DynMatrix.h:605
iterator begin()
returns an iterator to the begin of internal data array
Definition: DynMatrix.h:751
T trace() const
computes the sum of all diagonal elements
Definition: DynMatrix.h:1045
DynMatrix(unsigned int cols, unsigned int rows, const T &initValue=0)
Create a dyn matrix with given dimensions (and optional initialValue)
Definition: DynMatrix.h:94
DynMatrix< T > operator,(const DynMatrix< T > &left, const DynMatrix< T > &right)
vertical concatenation of matrices
Definition: DynMatrix.h:1270
DynMatrix operator+(const T &t) const
adds a scalar to each element
Definition: DynMatrix.h:327
DynMatrix operator-(const DynMatrix &m) const
Matrix substraction.
Definition: DynMatrix.h:361
ICLMath_IMP std::ostream & operator<<(std::ostream &s, const DynMatrix< T > &m)
ostream operator implemented for uchar, short, int, float and double matrices
const col_iterator operator++(int) const
postfix increment operator (const)
Definition: DynMatrix.h:519
#define DYN_MATRIX_COLUMN_CHECK(C, E)
Definition: DynMatrix.h:648
const_col_iterator col_begin(unsigned int col) const
returns an iterator running through a certain matrix column (const)
Definition: DynMatrix.h:775
#define iclMin(A, B)
Definition: Macros.h:204
bool operator!=(const DynMatrix &other) const
elementwise comparison (!=)
Definition: DynMatrix.h:220
const col_iterator begin() const
returns column begin (const)
Definition: DynMatrix.h:678
unsigned int column
referenced column in matrix
Definition: DynMatrix.h:654
DynMatrix< T > operator%(const DynMatrix< T > &top, const DynMatrix< T > &bottom)
horizontal concatenation of matrices
Definition: DynMatrix.h:1286
unsigned int dim() const
returns column length (matrix->rows())
Definition: DynMatrix.h:684
col_iterator col_end(unsigned int col)
returns an iterator end of a certain matrix column
Definition: DynMatrix.h:769
T * iterator
default iterator type (just a data-pointer)
Definition: DynMatrix.h:459
T * row_iterator
comples row_iterator type
Definition: DynMatrix.h:465
DynMatrix()
Default empty constructor creates a null-matrix.
Definition: DynMatrix.h:91
Special linear algebra exception type .
Definition: DynMatrix.h:66
DynMatrix & operator *=(T f)
Multiply elements with scalar (inplace)
Definition: DynMatrix.h:243
T & operator()(unsigned int col, unsigned int row)
element access operator (x,y)-access index begin 0!
Definition: DynMatrix.h:383
IncompatibleMatrixDimensionException(const std::string &msg)
Definition: DynMatrix.h:62
DynMatrix(const DynMatrix &other)
Default copy constructor.
Definition: DynMatrix.h:124
DynMatrix< T > diag() const
returns diagonal-elements as column-vector
Definition: DynMatrix.h:1035
Special linear algebra exception type .
Definition: DynMatrix.h:56
DynMatrix & elementwise_mult(const DynMatrix &m, DynMatrix &dst) const
Elementwise matrix multiplication (in source destination fashion) [IPP-Supported].
Definition: DynMatrix.h:271
DynMatrix solve_lower_triangular(const DynMatrix &b) const
solves Mx=b for M=*this (only if M is a squared lower triangular matrix) (only for icl32f and icl64f)
DynMatrix & operator/=(T f)
Device elements by scalar (inplace)
Definition: DynMatrix.h:254
void svd(DynMatrix &U, DynMatrix &S, DynMatrix &V) const
Computes Singular Value Decomposition of a matrix - decomposes A into USV'.
void saveCSV(const std::string &filename)
writes the current matrix to a csv file
SingularMatrixException(const std::string &msg)
Definition: DynMatrix.h:72
Special linear algebra exception type .
Definition: DynMatrix.h:61
col_iterator operator--(int)
postfix decrement operator
Definition: DynMatrix.h:538
void decompose_RQ(DynMatrix &R, DynMatrix &Q) const
applies RQ-decomposition (by exploiting implemnetation of QR-decomposition) (only for icl32f,...
DynMatrix row(int row)
Extracts a shallow copied matrix row.
Definition: DynMatrix.h:811
DynMatrixColumn(const DynMatrixColumn &c)
Shallow copy from another matrix column reference.
Definition: DynMatrix.h:668
T & operator *()
Dereference operator.
Definition: DynMatrix.h:611
unsigned int rows() const
height of the matrix (number of rows)
Definition: DynMatrix.h:471
DynMatrix & operator=(const DynMatrixColumn &col)
Definition: DynMatrix.h:740
DynMatrix & operator-=(const T &t)
substacts a scalar from each element (inplace)
Definition: DynMatrix.h:347
#define ICLASSERT_RETURN_VAL(X, VALUE)
Definition: Macros.h:148
ICLQt_API void fill(float r, float g=-1, float b=-1, float alpha=255)
sets the current fill color to given r,g,b,alpha value
unsigned int difference_type
Definition: DynMatrix.h:496
DynMatrix & operator/=(const DynMatrix &m) const
inplace matrix devision (calling this/m.inv()) (inplace)
Definition: DynMatrix.h:322
int stride2() const
returns sizeof (T)
Definition: DynMatrix.h:492
bool operator>=(const col_iterator &i) const
comparison operator >=
Definition: DynMatrix.h:633
unsigned int cols() const
width of the matrix (number of columns)
Definition: DynMatrix.h:474
DynMatrixColumn & operator-=(const DynMatrixColumn &c)
operator += for other columns
Definition: DynMatrix.h:708
DynMatrixColumn & operator/=(const T &val)
operator /= for scalars
Definition: DynMatrix.h:733
DynMatrix & mult(const DynMatrix &m, DynMatrix &dst) const
Matrix multiplication (in source destination fashion) [IPP-Supported].
Definition: DynMatrix.h:259
DynMatrix transp() const
matrix transposed
Definition: DynMatrix.h:979
col_iterator & operator--()
prefix decrement operator
Definition: DynMatrix.h:526
T norm(double l=2) const
applies an L_l norm on the matrix elements (all elements are treated as vector)
Definition: DynMatrix.h:428
#define iclMax(A, B)
Definition: Macros.h:207
ICLMath_IMP std::istream & operator>>(std::istream &s, DynMatrix< T > &m)
istream operator implemented for uchar, short, int, float and double matrices
const_iterator end() const
returns an iterator to the end of internal data array (const)
Definition: DynMatrix.h:760
DynMatrixColumn(const DynMatrix< T > *matrix, unsigned int column)
create from source matrix and column index
Definition: DynMatrix.h:657
static DynMatrix< T > cross(const DynMatrix< T > &x, const DynMatrix< T > &y)
computes the cross product
Definition: DynMatrix.h:1055
static DynMatrix< T > loadCSV(const std::string &filename)
loads a dynmatrix from given CSV file
const col_iterator operator+(difference_type n) const
jump next n elements (const)
Definition: DynMatrix.h:585
unsigned int dim() const
matrix dimension (width*height) or (cols*rows)
Definition: DynMatrix.h:483
bool isNull() const
returns with this matrix has a valid data pointer
Definition: DynMatrix.h:147
const_row_iterator row_begin(unsigned int row) const
returns an iterator running through a certain matrix row (const)
Definition: DynMatrix.h:799
iterator end()
returns an iterator to the end of internal data array
Definition: DynMatrix.h:754
col_iterator begin()
returns column begin
Definition: DynMatrix.h:672
bool operator<=(const col_iterator &i) const
comparison operator <=
Definition: DynMatrix.h:630
DynMatrix big_matrix_pinv(T zeroThreshold=T(1E-16)) const
calculates the Moore-Penrose pseudo-inverse (specialized for big matrices)
T * m_data
Definition: DynMatrix.h:1128
void row_check(unsigned int row) const
Definition: DynMatrix.h:1099
T sqrDistanceTo(const DynMatrix &other) const
returns the squared distance of the inner data vectors (linearly interpreted) (IPP accelerated)
Definition: DynMatrix.h:446
bool operator>(const col_iterator &i) const
comparison operator >
Definition: DynMatrix.h:636
bool operator!=(const col_iterator &i) const
comparison operator !=
Definition: DynMatrix.h:624
~DynMatrix()
Destructor (deletes data if no wrapped shallowly)
Definition: DynMatrix.h:150
#define ERROR_LOG(x)
Definition: Macros.h:111
DynMatrix operator-(const T &t) const
substacts a scalar from each element
Definition: DynMatrix.h:334
const T * const_iterator
dafault const_iterator type (just a data-pointer)
Definition: DynMatrix.h:462
const col_iterator operator-(difference_type n) const
jump backward next n elements (const)
Definition: DynMatrix.h:599
bool operator<(const col_iterator &i) const
comparison operator <
Definition: DynMatrix.h:627
void idx_check(unsigned int idx) const
Definition: DynMatrix.h:1118
unsigned int stride
Definition: DynMatrix.h:498
void reshape(int newCols, int newRows)
resets the matrix dimensions without changing the content
Definition: DynMatrix.h:1010
DynMatrix & mult(T f, DynMatrix &dst) const
Multiply elements with scalar (in source destination fashion)
Definition: DynMatrix.h:236
void eigen(DynMatrix &eigenvectors, DynMatrix &eigenvalues) const
Extracts the matrix's eigenvalues and eigenvectors.
DynMatrixColumn & operator=(const DynMatrixColumn &c)
assignment by another column
Definition: DynMatrix.h:687
DynMatrix operator/(const DynMatrix &m) const
inplace matrix devision (calling this/m.inv()) [IPP-Supported]
Definition: DynMatrix.h:317
void setBounds(unsigned int cols, unsigned int rows, bool holdContent=false, const T &initializer=0)
resets matrix dimensions
Definition: DynMatrix.h:179
T cond(const double p=2) const
computes the condition of a matrix
Definition: DynMatrix.h:1069
static DynMatrix id(unsigned int dim)
creates a dim-D identity Matrix
Definition: DynMatrix.h:1092
row_iterator row_begin(unsigned int row)
returns an iterator running through a certain matrix row
Definition: DynMatrix.h:787
#define ICLMath_IMP
Definition: CompatMacros.h:172
bool m_ownData
Definition: DynMatrix.h:1129
T * p
Definition: DynMatrix.h:497
int stride0() const
returns sizeof (T)*dim()
Definition: DynMatrix.h:486
col_iterator & operator-=(difference_type n)
jump backward next n elements (inplace)
Definition: DynMatrix.h:565
Base class for Exception handling in the ICL.
Definition: Exception.h:42
DynMatrix & operator+=(const T &t)
adds a scalar to each element (inplace)
Definition: DynMatrix.h:341
DynMatrix(const std::string &filename)
creates a new DynMatrix from given csv filename
Definition: DynMatrix.h:131
DynMatrix operator+(const DynMatrix &m) const
Matrix addition.
Definition: DynMatrix.h:353
DynMatrix & operator-=(const DynMatrix &m)
Matrix substraction (inplace)
Definition: DynMatrix.h:376
int stride1() const
returns sizeof(T)*cols()
Definition: DynMatrix.h:489
const T & operator[](unsigned int idx) const
linear access to actual data array (const)
Definition: DynMatrix.h:422
const_iterator begin() const
returns an iterator to the begin of internal data array (const)
Definition: DynMatrix.h:757
InvalidMatrixDimensionException(const std::string &msg)
Definition: DynMatrix.h:57
int m_cols
Definition: DynMatrix.h:1127
const_col_iterator col_end(unsigned int col) const
returns an iterator end of a certain matrix column (const)
Definition: DynMatrix.h:781
const col_iterator & operator+=(difference_type n) const
jump next n elements (inplace) (const)
Definition: DynMatrix.h:558
DynMatrixColumn & operator+=(const DynMatrix &m)
operator += for DynMatrices
Definition: DynMatrix.h:715
void copy(const T *src, const T *srcEnd, T *dst)
moves data from source to destination array (no casting possible)
Definition: CoreFunctions.h:216
DynMatrixColumn col(int col)
Extracts a shallow copied matrix column.
Definition: DynMatrix.h:823
const DynMatrix< T > shallowTransposed() const
returns a shallow transposed copy of this matrix (dimensions are swapped, data is not re-aranged) (co...
Definition: DynMatrix.h:991
col_iterator end()
returns column end
Definition: DynMatrix.h:675
const DynMatrix row(int row) const
Extracts a shallow copied matrix row (const)
Definition: DynMatrix.h:817
DynMatrixColumn & operator+=(const DynMatrixColumn &c)
operator += for other columns
Definition: DynMatrix.h:701
T distanceTo(const DynMatrix &other) const
returns the distance of the inner data vectors (linearly interpreted) (IPP accelerated)
Definition: DynMatrix.h:452
Internal column iterator struct (using height-stride)
Definition: DynMatrix.h:495
T element_wise_inner_product(const DynMatrix< T > &other) const
inner product of data pointers (not matrix-mulitiplication)
Definition: DynMatrix.h:1020
bool operator==(const col_iterator &i) const
comparison operator ==
Definition: DynMatrix.h:621
col_iterator & operator++()
prefix increment operator
Definition: DynMatrix.h:503
#define ICLASSERT_THROW(X, OBJ)
Definition: Macros.h:155
DynMatrix solve_upper_triangular(const DynMatrix &b) const
solves Mx=b for M=*this (only if M is a squared upper triangular matrix) (only for icl32f and icl64f)
col_iterator operator++(int)
postfix increment operator
Definition: DynMatrix.h:513
const col_iterator end() const
returns column end (const)
Definition: DynMatrix.h:681
DynMatrix(unsigned int cols, unsigned int rows, T *data, bool deepCopy=true)
Create a matrix with given data.
Definition: DynMatrix.h:104
const col_iterator const_col_iterator
const column iterator typedef
Definition: DynMatrix.h:640
T & operator[](unsigned int idx)
linear access to actual data array
Definition: DynMatrix.h:412
T * data()
internal data pointer
Definition: DynMatrix.h:477
InvalidIndexException(const std::string &msg)
Definition: DynMatrix.h:67
T & at(unsigned int col, unsigned int row)
element access with index check
Definition: DynMatrix.h:399
row_iterator row_end(unsigned int row)
returns an iterator of a certains row's end
Definition: DynMatrix.h:793
Internally used Utility structure referencing a matrix column shallowly.
Definition: DynMatrix.h:643
bool isSimilar(const DynMatrix &other, T tollerance=T(0.0001)) const
tests weather a matrix is enough similar to another matrix
Definition: DynMatrix.h:201