Image Component Library (ICL)
FixedMatrix.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/FixedMatrix.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/CompatMacros.h>
34 #include <ICLUtils/Exception.h>
35 #include <ICLUtils/ClippedCast.h>
36 #include <ICLUtils/FixedArray.h>
37 #include <ICLMath/DynMatrix.h>
39 
40 #include <iterator>
41 #include <algorithm>
42 #include <numeric>
43 #include <functional>
44 #include <iostream>
45 #include <vector>
46 #include <cmath>
47 
48 #ifdef ICL_HAVE_IPP
49 #include <ippm.h>
50 #endif
51 
52 namespace icl{
53  namespace math{
54 
58  template<class SrcIterator, class DstIterator, unsigned int N>
59  static void optimized_copy(SrcIterator srcBegin, SrcIterator srcEnd, DstIterator dstBegin){
60  if(N>30){
61  std::copy(srcBegin,srcEnd,dstBegin);
62  }else{
63  while(srcBegin != srcEnd){
64  // *dstBegin++ = *srcBegin++;
65  *dstBegin =
66  *srcBegin;
67 
68  dstBegin++;
69  srcBegin++;
70  }
71  }
72  }
73  };
74 
76  template<class T, unsigned int COLS, unsigned int ROWS> class FixedMatrix;
81 
91  template<class T,unsigned int N, class Iterator>
93 
94  public:
96  Iterator begin;
97 
99  Iterator end;
100 
102  FixedMatrixPart(Iterator begin, Iterator end):begin(begin),end(end){}
103 
105  FixedMatrixPart& operator=(const T &value){
106  std::fill(begin,end,value);
107  return *this;
108  }
110 
112  FixedMatrixBase::optimized_copy<Iterator,Iterator,N>(other.begin,other.end,begin);
113  return *this;
114  }
115 
117 
119  template<class OtherIterator, class OtherT>
121  std::transform(other.begin,other.end,begin,utils::clipped_cast<OtherT,T>);
122  return *this;
123  }
124 
126 
127  template<unsigned int COLS>
129 
131 
132  template<class T2, unsigned int COLS>
134 
135  };
136 
137 
138 
140 
171  template<class T,unsigned int COLS,unsigned int ROWS>
172  class FixedMatrix : public utils::FixedArray<T, COLS*ROWS>, public FixedMatrixBase{
173  public:
174 
176 
177  DynMatrix<T> dyn() { return DynMatrix<T>(COLS,ROWS,begin(),false); }
178 
180  const DynMatrix<T> dyn() const { return DynMatrix<T>(COLS,ROWS,const_cast<T*>(begin()),false); }
181 
183  static const FixedMatrix &null(){
184  static FixedMatrix null_matrix(T(0));
185  return null_matrix;
186  }
187 
189  static const unsigned int DIM = COLS*ROWS;
190 
191  public:
192 
194 
196 
198  explicit FixedMatrix(const T &initValue){
199  std::fill(begin(),end(),initValue);
200  }
201 
203 
207  explicit FixedMatrix(const T *srcdata){
208  FixedMatrixBase::optimized_copy<const T*,T*,DIM>(srcdata,srcdata+dim(),begin());
209  //std::copy(srcdata,srcdata+dim(),begin());
210  }
211 
213 
216  FixedMatrix(const T& v0,const T& v1,const T& v2=0,const T& v3=0,
217  const T& v4=0,const T& v5=0,const T& v6=0,const T& v7=0,
218  const T& v8=0,const T& v9=0,const T& v10=0,const T& v11=0,
219  const T& v12=0,const T& v13=0,const T& v14=0,const T& v15=0){
220  #define C1(N) if(DIM>N) utils::FixedArray<T,COLS*ROWS>::m_data[N]=v##N
221  #define C4(A,B,C,D) C1(A);C1(B);C1(C);C1(D)
222  C4(0,1,2,3);C4(4,5,6,7);C4(8,9,10,11);C4(12,13,14,15);
223  #undef C1
224  #undef C2
225  }
226 
228 
229  template<class OtherIterator>
230  FixedMatrix(OtherIterator begin, OtherIterator end){
231  FixedMatrixBase::optimized_copy<OtherIterator,T*,DIM>(begin,end,this->begin());
232  // std::copy(begin,end,begin());
233  }
234 
235  // Explicit Copy template based constructor (deep copy)
236  FixedMatrix(const FixedMatrix &other){
237  FixedMatrixBase::optimized_copy<const T*,T*,DIM>(other.begin(),other.end(),begin());
238  //std::copy(other.begin(),other.end(),begin());
239  }
240 
241  // Explicit Copy template based constructor (deep copy)
242  template<class otherT>
244  std::transform(other.begin(),other.end(),begin(),utils::clipped_cast<otherT,T>);
245  }
246 
248  template<class Iterator>
250  FixedMatrixBase::optimized_copy<Iterator,T*,DIM>(r.begin,r.end,begin());
251  }
252 
254  template<class otherT, class Iterator>
256  std::transform(r.begin,r.end,begin(),utils::clipped_cast<otherT,T>);
257  }
258 
261  if(this == &other) return *this;
262  FixedMatrixBase::optimized_copy<const T*,T*,DIM>(other.begin(),other.end(),begin());
263  //std::copy(other.begin(),other.end(),begin());
264  return *this;
265  }
266 
268 
269  template<class otherT>
271  if(this == &other) return *this;
272  std::transform(other.begin(),other.end(),begin(),utils::clipped_cast<otherT,T>);
273  return *this;
274  }
275 
277  FixedMatrix &operator=(const T &t){
278  std::fill(begin(),end(),t);
279  return *this;
280  }
281 
283  template<class Iterator>
285  // std::copy(r.begin,r.end,begin());
286  FixedMatrixBase::optimized_copy<Iterator,T*,DIM>(r.begin,r.end,begin());
287  return *this;
288  }
289 
291  template<class otherT, class Iterator>
293  std::transform(r.begin,r.end,begin(),utils::clipped_cast<otherT,T>);
294  return *this;
295  }
296 
298 
303  return this->operator*(m.inv());
304  }
305 
308  return *this = this->operator*(m.inv());
309  }
310 
312  FixedMatrix operator*(T f) const{
313  FixedMatrix d;
314  std::transform(begin(),end(),d.begin(),std::bind2nd(std::multiplies<T>(),f));
315  return d;
316  }
317 
320  std::transform(begin(),end(),begin(),std::bind2nd(std::multiplies<T>(),f));
321  return *this;
322  }
323 
325  FixedMatrix operator/(T f) const{
326  FixedMatrix d;
327  std::transform(begin(),end(),d.begin(),std::bind2nd(std::divides<T>(),f));
328  return d;
329  }
330 
333  std::transform(begin(),end(),begin(),std::bind2nd(std::divides<T>(),f));
334  return *this;
335  }
336 
338  FixedMatrix operator+(const T &t) const{
339  FixedMatrix d;
340  std::transform(begin(),end(),d.begin(),std::bind2nd(std::plus<T>(),t));
341  return d;
342  }
343 
345  FixedMatrix &operator+=(const T &t){
346  std::transform(begin(),end(),begin(),std::bind2nd(std::plus<T>(),t));
347  return *this;
348  }
349 
350 
352  FixedMatrix operator-(const T &t) const{
353  FixedMatrix d;
354  std::transform(begin(),end(),d.begin(),std::bind2nd(std::minus<T>(),t));
355  return d;
356  }
357 
359  FixedMatrix &operator-=(const T &t){
360  std::transform(begin(),end(),begin(),std::bind2nd(std::minus<T>(),t));
361  return *this;
362  }
363 
366  FixedMatrix d;
367  std::transform(begin(),end(),m.begin(),d.begin(),std::plus<T>());
368  return d;
369  }
370 
373  std::transform(begin(),end(),m.begin(),begin(),std::plus<T>());
374  return *this;
375  }
376 
379  FixedMatrix d;
380  std::transform(begin(),end(),m.begin(),d.begin(),std::minus<T>());
381  return d;
382  }
385  std::transform(begin(),end(),m.begin(),begin(),std::minus<T>());
386  return *this;
387  }
388 
390 
393  FixedMatrix cpy(*this);
394  std::transform(cpy.begin(),cpy.end(),cpy.begin(),std::negate<T>());
395  return cpy;
396  }
397 
398 
400  T &operator()(unsigned int col,unsigned int row){
401  return begin()[col+cols()*row];
402  }
403 
405  const T &operator() (unsigned int col,unsigned int row) const{
406  return begin()[col+cols()*row];
407  }
408 
410  T &at(unsigned int col,unsigned int row) {
411  if(col>=cols() || row >= rows()) throw InvalidIndexException("row or col index too large");
412  return begin()[col+cols()*row];
413  }
414 
416  const T &at(unsigned int col,unsigned int row) const {
417  return const_cast<FixedMatrix*>(this)->at(col,row);
418  }
419 
421 
423  T &operator[](unsigned int idx){
424  return begin()[idx];
425  }
427 
429  const T &operator[](unsigned int idx) const{
430  return begin()[idx];
431  }
432 
433 
435  typedef T* iterator;
436 
438  typedef const T* const_iterator;
439 
441  typedef T* row_iterator;
442 
444  typedef const T* const_row_iterator;
445 
447  static unsigned int rows(){ return ROWS; }
448 
450  static unsigned int cols(){ return COLS; }
451 
454 
456  const T *data() const { return utils::FixedArray<T,COLS*ROWS>::m_data; }
457 
459  static unsigned int dim() { return DIM; }
460 
462  struct col_iterator : public std::iterator<std::random_access_iterator_tag,T>{
463 
465  typedef unsigned int difference_type;
466 
468  mutable T *p;
469 
471  static const unsigned int STRIDE = COLS;
472 
475 
478  p+=STRIDE;
479  return *this;
480  }
482  const col_iterator &operator++() const{
483  p+=STRIDE;
484  return *this;
485  }
488  col_iterator tmp = *this;
489  ++(*this);
490  return tmp;
491  }
493  const col_iterator operator++(int) const{
494  col_iterator tmp = *this;
495  ++(*this);
496  return tmp;
497  }
498 
501  p-=STRIDE;
502  return *this;
503  }
504 
506  const col_iterator &operator--() const{
507  p-=STRIDE;
508  return *this;
509  }
510 
513  col_iterator tmp = *this;
514  --(*this);
515  return tmp;
516  }
517 
519  const col_iterator operator--(int) const{
520  col_iterator tmp = *this;
521  --(*this);
522  return tmp;
523  }
524 
527  p += n * STRIDE;
528  return *this;
529  }
530 
533  p += n * STRIDE;
534  return *this;
535  }
536 
537 
540  p -= n * STRIDE;
541  return *this;
542  }
543 
546  p -= n * STRIDE;
547  return *this;
548  }
549 
550 
553  col_iterator tmp = *this;
554  tmp+=n;
555  return tmp;
556  }
557 
560  col_iterator tmp = *this;
561  tmp+=n;
562  return tmp;
563  }
564 
567  col_iterator tmp = *this;
568  tmp-=n;
569  return tmp;
570  }
571 
574  col_iterator tmp = *this;
575  tmp-=n;
576  return tmp;
577  }
578 
581  return (p-i.p)/STRIDE;
582  }
583 
585  T &operator*(){
586  return *p;
587  }
588 
590  const T &operator*() const{
591  return *p;
592  }
593 
595  bool operator==(const col_iterator &i) const{ return p == i.p; }
596 
598  bool operator!=(const col_iterator &i) const{ return p != i.p; }
599 
601  bool operator<(const col_iterator &i) const{ return p < i.p; }
602 
604  bool operator<=(const col_iterator &i) const{ return p <= i.p; }
605 
607  bool operator>=(const col_iterator &i) const{ return p >= i.p; }
608 
610  bool operator>(const col_iterator &i) const{ return p > i.p; }
611 
612  };
613 
614  // const column operator typedef
615  typedef const col_iterator const_col_iterator;
616 
619 
622 
625 
628 
630  col_iterator col_begin(unsigned int col) { return col_iterator(begin()+col); }
631 
633  col_iterator col_end(unsigned int col) { return col_iterator(begin()+col+dim()); }
634 
636  const_col_iterator col_begin(unsigned int col) const { return col_iterator(const_cast<T*>(begin())+col); }
637 
639  const_col_iterator col_end(unsigned int col) const { return col_iterator(const_cast<T*>(begin())+col+dim()); }
640 
642  row_iterator row_begin(unsigned int row) { return begin()+row*cols(); }
643 
645  row_iterator row_end(unsigned int row) { return begin()+(row+1)*cols(); }
646 
648  const_row_iterator row_begin(unsigned int row) const { return begin()+row*cols(); }
649 
651  const_row_iterator row_end(unsigned int row) const { return begin()+(row+1)*cols(); }
652 
654 
666  template<unsigned int MCOLS>
668  for(unsigned int c=0;c<MCOLS;++c){
669  for(unsigned int r=0;r<ROWS;++r){
670  // std::cout << "calling inner_product" << std::endl;
671  dst(c,r) = std::inner_product(m.col_begin(c),m.col_end(c),row_begin(r),T(0));
672  }
673  }
674  }
676 
699  template<unsigned int MCOLS>
702  mult(m,d);
703  return d;
704  }
705 
706 
707 
709 
715  FixedMatrix inv() const {
716  return FixedMatrix(dyn().inv().data());
717  }
718 
720 
723  T det() const {
724  return dyn().det();
725  }
726 
730  for(unsigned int i=0;i<cols();++i){
731  FixedMatrixBase::optimized_copy<const_col_iterator, typename FixedMatrix<T,ROWS,COLS>::row_iterator,DIM>(col_begin(i),col_end(i),d.row_begin(i));
732  // std::copy(col_begin(i),col_end(i),d.row_begin(i));
733  }
734  return d;
735  }
736 
738 
739  template<unsigned int OTHER_COLS>
741  return std::inner_product(begin(),end(),other.begin(),T(0));
742  }
743 
744 
746 
749  template<unsigned int OTHER_COLS>
751  return this->transp() * M;
752  }
753 
755  double cond(const double p=2) const {
756  DynMatrix<T> mat = this->dyn();
757  return mat.cond();
758  }
759 
761  T trace() const{
762  ICLASSERT_RETURN_VAL(COLS==ROWS,0);
763  double accu = 0;
764  for(int i=0;i<DIM;i+=COLS+1){
765  accu += begin()[i];
766  }
767  return accu;
768  }
769 
773  }
774 
778  }
779 
783  }
784 
788  }
789 
791  template<unsigned int X,unsigned int Y,unsigned int WIDTH,unsigned int HEIGHT>
794  MatrixSubRectIterator<T>(begin(),COLS,X,Y,WIDTH,HEIGHT),
795  MatrixSubRectIterator<T>::create_end_iterator(begin(),COLS,X,Y,WIDTH,HEIGHT));
796  }
797 
799  template<unsigned int X,unsigned int Y,unsigned int WIDTH,unsigned int HEIGHT>
801  return const_cast<FixedMatrix<T,COLS,ROWS>*>(this)->part<X,Y,WIDTH,HEIGHT>();
802  }
803 
805 
810  template<unsigned int NEW_WIDTH,unsigned int NEW_HEIGHT>
811  inline FixedMatrix<T,NEW_WIDTH,NEW_HEIGHT> resize(const T &init=T(0)) const {
813  for(unsigned int x=0;x<COLS && x < NEW_WIDTH; ++x){
814  for(unsigned int y=0;y<ROWS && y < NEW_HEIGHT; ++y){
815  M(x,y) = (*this)(x,y);
816  }
817  }
818  return M;
819  }
820 
822 
827  FixedMatrix<T,ROWS,COLS> m(T(0));
828  for(unsigned int i=0;i<ROWS && i<COLS;++i){
829  m(i,i) = 1;
830  }
831  return m;
832  }
833 
835  inline double length(T norm=2) const{
836  double sumSquares = 0;
837  for(unsigned int i=0;i<DIM;++i){
838  sumSquares += ::pow((double)(*this)[i],(double)norm);
839  }
840  return ::pow( sumSquares, 1.0/norm);
841  }
842 
844  inline void normalize(T norm=2){
845  T l = (T)length(norm);
846  if(l) (*this) /= l;
847  }
848 
851  T l = (T)length(norm);
852  return l ? (*this)/l : *this;
853  }
854 
856  template<class otherT>
858  for(unsigned int i=0;i<DIM;++i){
859  if(begin()[i] != m[i]) return false;
860  }
861  return true;
862  }
864  template<class otherT>
866  return !this->operator==(m);
867  }
868 
869 
872  if(ROWS != COLS) throw InvalidMatrixDimensionException("trace is only possible for sqaure matrices");
874  for(unsigned int i=0;i<ROWS;++i){
875  t[i] = (*this)(i,i);
876  }
877  return t;
878  }
879 
881 
883  DynMatrix<T> Qd = Q.dyn(), Rd = R.dyn();
884  dyn().decompose_QR(Qd,Rd);
885  }
886 
888 
890  DynMatrix<T> Rd = R.dyn(), Qd = Q.dyn();
891  dyn().decompose_RQ(Rd,Qd);
892  }
893 
895 
897  DynMatrix<T> Ud = U.dyn(), sd = s.dyn(), Vd = V.dyn();
898  return dyn().svd(Ud,sd,Vd);
899  }
900 
902 
903  FixedMatrix<T,ROWS,COLS> pinv(bool useSVD=0, float zeroThreshold=0.00000000000000001) const {
904  return FixedMatrix<T,ROWS,COLS>(dyn().pinv(useSVD,zeroThreshold).begin());
905  }
906 
908 
921  void eigen(FixedMatrix &eigenvectors, FixedMatrix<T,1,COLS> &eigenvalues) const{
922  if(ROWS != COLS) throw InvalidMatrixDimensionException("eigenvalue decomposition is only possible for sqaure matrices (use svd instead!)");
923  DynMatrix<T> evecs = eigenvectors.dyn(), evals = eigenvalues.dyn();
924  return dyn().eigen(evecs,evals);
925  }
926 
927 
928  };
929 
931 
932  template<class T,unsigned int WIDTH,unsigned int HEIGHT, unsigned int HEIGHT2>
936  for(unsigned int i=0;i<HEIGHT;++i) M.row(i) = a.row(i);
937  for(unsigned int i=0;i<HEIGHT2;++i) M.row(i+HEIGHT) = b.row(i);
938  return M;
939  }
940 
942 
943  template<class T,unsigned int WIDTH,unsigned int HEIGHT, unsigned int WIDTH2>
947  for(unsigned int i=0;i<WIDTH;++i) M.col(i) = a.col(i);
948  for(unsigned int i=0;i<WIDTH2;++i) M.col(i+WIDTH) = b.col(i);
949  return M;
950  }
951 
952 
953 
954 
956 
958  template<class T, unsigned int M_ROWS_AND_COLS,unsigned int V_COLS>
961  return v = (v*m);
962  }
963 
965 
966  template<class T, unsigned int COLS, unsigned int ROWS>
967  inline std::ostream &operator<<(std::ostream &s,const FixedMatrix<T,COLS,ROWS> &m){
968  return s << m.dyn();
969  }
970 
972 
973  template<class T, unsigned int COLS, unsigned int ROWS>
974  inline std::istream &operator>>(std::istream &s,FixedMatrix<T,COLS,ROWS> &m){
975  DynMatrix<T> dyn = m.dyn();
976  return s >> dyn;
977  }
978 
979 
981  template<class T> ICLMath_IMP
983 
985  template<class T> ICLMath_IMP
986  FixedMatrix<T, 3, 3> create_hom_3x3(T angle, T dx = 0, T dy = 0, T v0 = 0, T v1 = 0);
987 
989  template<class T>
992  m(2,0)=dx;
993  m(2,1)=dy;
994  return m;
995  }
996 
997 
999  enum AXES { sxyz, sxyx, sxzy, sxzx, syzx, syzy,
1003  extern ICLMath_API const AXES AXES_DEFAULT; // rxyz
1004 
1006  template<class T> ICLMath_IMP
1007  FixedMatrix<T, 3, 3> create_rot_3D(T axisX, T axisY, T axisZ, T angle);
1008 
1010  template<class T> ICLMath_IMP
1011  FixedMatrix<T,3,3> create_rot_3D (T ai, T aj, T ak, AXES axes=AXES_DEFAULT);
1013  template<class T> ICLMath_IMP
1014  FixedMatrix<T,4,4> create_hom_4x4(T rx, T ry, T rz,
1015  T dx=0, T dy=0, T dz=0,
1016  T v0=0, T v1=0, T v2=0,
1017  AXES axes=AXES_DEFAULT);
1018 
1020  template<class T> ICLMath_IMP
1021  FixedMatrix<T, 4, 4> create_rot_4x4(T axisX, T axisY, T axisZ, T angle);
1022 
1023 
1025  template<class T>
1026  inline FixedMatrix<T, 4, 4> create_hom_4x4_trans(T dx, T dy, T dz){
1028  m(3,0)=dx;
1029  m(3,1)=dy;
1030  m(3,2)=dz;
1031  return m;
1032  }
1033 
1035  template<class T> ICLMath_IMP
1037  AXES axes=AXES_DEFAULT);
1038  template<class T> ICLMath_IMP
1040  AXES axes=AXES_DEFAULT);
1041 
1042 
1044  template<class T,unsigned int N, class Iterator> template<unsigned int COLS>
1046  FixedMatrixBase::optimized_copy<const T*,Iterator,N>(m.begin(),m.end(),begin);
1047  //std::copy(m.begin(),m.end(),begin);
1048  return *this;
1049  }
1050  template<class T,unsigned int N, class Iterator> template<class T2, unsigned int COLS>
1051  inline FixedMatrixPart<T,N,Iterator>& FixedMatrixPart<T,N,Iterator>::operator=(const FixedMatrix<T2,COLS,N/COLS> &m){
1052  std::transform(m.begin(),m.end(),begin,utils::clipped_cast<T2,T>);
1053  return *this;
1054  }
1057  #ifdef ICL_HAVE_IPP
1058  #define OPTIMIZED_MATRIX_MULTIPLICATION(LEFT_COLS,LEFT_ROWS,RIGHT_COLS,TYPE,IPPSUFFIX) \
1059  template<> template<> \
1060  inline void \
1061  FixedMatrix<TYPE,RIGHT_COLS,LEFT_ROWS>::mult \
1062  ( \
1063  const FixedMatrix<TYPE,RIGHT_COLS,LEFT_COLS> &m, \
1064  FixedMatrix<TYPE,RIGHT_COLS,LEFT_ROWS> &dst \
1065  ) const { \
1066  static const unsigned int ST=sizeof(TYPE); \
1067  ippmMul_mm_##IPPSUFFIX(data(),LEFT_COLS*ST,ST,LEFT_COLS,LEFT_ROWS, \
1068  m.data(),RIGHT_COLS*ST,ST,RIGHT_COLS,LEFT_COLS, \
1069  dst.data(),RIGHT_COLS*ST,ST); \
1070  }
1071 
1072  OPTIMIZED_MATRIX_MULTIPLICATION(2,2,2,float,32f);
1073  OPTIMIZED_MATRIX_MULTIPLICATION(3,3,3,float,32f);
1074  OPTIMIZED_MATRIX_MULTIPLICATION(4,4,4,float,32f);
1075 
1076  OPTIMIZED_MATRIX_MULTIPLICATION(2,2,2,double,64f);
1077  OPTIMIZED_MATRIX_MULTIPLICATION(3,3,3,double,64f);
1078  OPTIMIZED_MATRIX_MULTIPLICATION(4,4,4,double,64f);
1079  #undef OPTIMIZED_MATRIX_MULTIPLICATION
1080 
1081 
1082  #endif
1083 
1084  #define USE_OPTIMIZED_INV_AND_DET_FOR_2X2_3X3_AND_4X4_MATRICES
1085 
1086  #ifdef USE_OPTIMIZED_INV_AND_DET_FOR_2X2_3X3_AND_4X4_MATRICES
1087 
1089  // this functions are implemented in iclFixedMatrix.cpp. All templates are
1090  // instantiated for float and double
1091 
1092  template<class T> ICLMath_IMP
1093  void icl_util_get_fixed_4x4_matrix_inv(const T *src, T*dst);
1094  template<class T> ICLMath_IMP
1095  void icl_util_get_fixed_3x3_matrix_inv(const T *src, T*dst);
1096  template<class T> ICLMath_IMP
1097  void icl_util_get_fixed_2x2_matrix_inv(const T *src, T*dst);
1098 
1099  template<class T> ICLMath_IMP
1100  T icl_util_get_fixed_4x4_matrix_det(const T *src);
1101  template<class T> ICLMath_IMP
1102  T icl_util_get_fixed_3x3_matrix_det(const T *src);
1103  template<class T> ICLMath_IMP
1104  T icl_util_get_fixed_2x2_matrix_det(const T *src);
1105 
1106  #define SPECIALISED_MATRIX_INV_AND_DET(D,T) \
1107  template<> \
1108  inline FixedMatrix<T,D,D> FixedMatrix<T,D,D>::inv() const \
1109  { \
1110  FixedMatrix<T,D,D> r; \
1111  icl_util_get_fixed_##D##x##D##_matrix_inv<T>(begin(),r.begin()); \
1112  return r; \
1113  } \
1114  template<> \
1115  inline T FixedMatrix<T,D,D>::det() const \
1116  { \
1117  return icl_util_get_fixed_##D##x##D##_matrix_det<T>(begin()); \
1118  }
1119 
1120 
1121  SPECIALISED_MATRIX_INV_AND_DET(2,float);
1122  SPECIALISED_MATRIX_INV_AND_DET(3,float);
1123  SPECIALISED_MATRIX_INV_AND_DET(4,float);
1124  SPECIALISED_MATRIX_INV_AND_DET(2,double);
1125  SPECIALISED_MATRIX_INV_AND_DET(3,double);
1126  SPECIALISED_MATRIX_INV_AND_DET(4,double);
1127 
1128 
1129  #undef SPECIALISED_MATRIX_INV_AND_DET
1130 
1132  #endif
1133 
1134 
1135 #ifdef WIN32
1136  // this is temporary fix!
1137  // because Homography2D is exported and therefore the base class is exported too
1138  // we need to import this in executables/libraries
1139  template class ICLMath_API FixedMatrix<float, 3, 3>;
1140  template class ICLMath_API FixedMatrix<double, 3, 3>;
1141 #endif
1142 
1143  } // namespace math
1144 }
1145 
FixedMatrix< T, 3, 3 > create_hom_3x3_trans(T dx, T dy)
creates a 2D homogen matrix with translation part only (defined for float and double)
Definition: FixedMatrix.h:990
Definition: FixedMatrix.h:999
unsigned int difference_type
just for compatibility with STL
Definition: FixedMatrix.h:465
difference_type operator-(const col_iterator &i) const
steps between two iterators ... (todo: check!)
Definition: FixedMatrix.h:580
FixedMatrix & operator/=(T f)
Divide all elements by a scalar.
Definition: FixedMatrix.h:332
Definition: FixedMatrix.h:1000
const col_iterator const_col_iterator
Definition: FixedMatrix.h:615
FixedMatrixPart< T, ROWS, col_iterator > col(unsigned int idx)
returns a matrix col-reference iterator pair
Definition: FixedMatrix.h:781
Definition: FixedMatrix.h:1002
const col_iterator & operator-=(difference_type n) const
jump backward next n elements (inplace) (const)
Definition: FixedMatrix.h:545
const T * const_row_iterator
const row_iterator
Definition: FixedMatrix.h:444
FixedMatrix(const FixedMatrixPart< otherT, DIM, Iterator > &r)
Create matrix of a sub-part of another matrix (compatible types)
Definition: FixedMatrix.h:255
ICLMath_IMP FixedMatrix< T, 2, 2 > create_rot_2D(T angle)
creates a 2D rotation matrix (defined for float and double)
col_iterator & operator+=(difference_type n)
jump next n elements (inplace)
Definition: FixedMatrix.h:526
Definition: FixedMatrix.h:1002
const T & at(unsigned int col, unsigned int row) const
Element access index save (with exception if index is invalid) (const)
Definition: FixedMatrix.h:416
Powerful and highly flexible matrix class implementation.
Definition: FixedMatrix.h:172
bool operator!=(const FixedMatrix< otherT, COLS, ROWS > &m) const
Element-wise comparison with other matrix.
Definition: FixedMatrix.h:865
ICLMath_IMP FixedMatrix< T, 4, 4 > create_rot_4x4(T axisX, T axisY, T axisZ, T angle)
create 4D homogeneous matrix that rotates about given axis by given angle (defined for float and doub...
FixedMatrixPart & operator=(const FixedMatrixPart< OtherT, N, OtherIterator > &other)
Assignment with another (compatible) instance of FixedMatrixPart.
Definition: FixedMatrix.h:120
undocument this line if you encounter any issues!
Definition: Any.h:37
ICLMath_IMP FixedMatrix< T, 1, 3 > extract_euler_angles(const FixedMatrix< T, 3, 3 > &m, AXES axes=AXES_DEFAULT)
compute euler angles for rotation matrix assuming specified axes order
const_row_iterator row_begin(unsigned int row) const
returns an iterator iterating over a certain row (const)
Definition: FixedMatrix.h:648
FixedMatrix & operator=(const FixedMatrixPart< otherT, DIM, Iterator > &r)
Assign matrix elements with sup-part of another matrix (compatible types)
Definition: FixedMatrix.h:292
ICLQt_API core::Img< T > norm(const core::Img< T > &image)
normalize an images range to [0,255]
#define ICLMath_API
Definition: CompatMacros.h:173
Definition: FixedMatrix.h:999
col_iterator col_begin(unsigned int col)
returns an iterator iterating over a certain column
Definition: FixedMatrix.h:630
FixedMatrixPart< T, WIDTH *HEIGHT, MatrixSubRectIterator< T > > part()
extracts a rectangular matrix sub region
Definition: FixedMatrix.h:792
FixedMatrix operator-(const T &t) const
Substract a scalar from each element.
Definition: FixedMatrix.h:352
FixedMatrixPart(Iterator begin, Iterator end)
Creates a new FixedMatrixPart instance with given Iterator Pair.
Definition: FixedMatrix.h:102
Fixed C++-array wrapper class for data handling.
Definition: FixedArray.h:45
const_iterator end() const
returns an iterator after the last element (const)
Definition: FixedMatrix.h:627
void svd(FixedMatrix< T, COLS, ROWS > &U, FixedMatrix< T, 1, COLS > &s, FixedMatrix< T, COLS, COLS > &V) const
computes Singular Value Decomposition of this Matrix A = U diag(s) V'
Definition: FixedMatrix.h:896
Definition: FixedMatrix.h:1001
FixedMatrix operator+(const FixedMatrix &m) const
Element-wise matrix addition.
Definition: FixedMatrix.h:365
iterator begin()
returns an iterator to first element iterating over each element (row-major order)
Definition: FixedMatrix.h:618
D clipped_cast(S src)
utility cast function wrapping the standard lib's numerical_limits template
Definition: ClippedCast.h:57
FixedMatrixPart< T, COLS, const_row_iterator > row(unsigned int idx) const
returns a matrix row-reference iterator pair (const)
Definition: FixedMatrix.h:776
Highly flexible and optimized matrix class implementation.
Definition: DynMatrix.h:81
Definition: FixedMatrix.h:1002
T element_wise_inner_product(const FixedMatrix< T, OTHER_COLS, DIM/OTHER_COLS > &other) const
inner product of data pointers (not matrix-mulitiplication)
Definition: FixedMatrix.h:740
Iterator begin
Start iterator.
Definition: FixedMatrix.h:96
FixedMatrixPart< T, COLS, row_iterator > row(unsigned int idx)
returns a matrix row-reference iterator pair
Definition: FixedMatrix.h:771
static unsigned int cols()
compatibility-function returns template parameter COLS
Definition: FixedMatrix.h:450
Definition: FixedMatrix.h:1001
Definition: FixedMatrix.h:1001
T * p
wrapped data pointer (held shallowly)
Definition: FixedMatrix.h:468
DynMatrix< T > operator,(const DynMatrix< T > &left, const DynMatrix< T > &right)
vertical concatenation of matrices
Definition: DynMatrix.h:1270
const col_iterator operator+(difference_type n) const
jump next n elements (const)
Definition: FixedMatrix.h:559
FixedMatrix & operator=(const FixedMatrix< otherT, COLS, ROWS > &other)
Assignment operator (with compatible data type) (deep copy)
Definition: FixedMatrix.h:270
ICLMath_IMP std::ostream & operator<<(std::ostream &s, const DynMatrix< T > &m)
ostream operator implemented for uchar, short, int, float and double matrices
FixedMatrix< T, 1, ROWS > diag() const
returns a vector of the diagonal elements (only for squared matrices)
Definition: FixedMatrix.h:871
FixedMatrix(const FixedMatrixPart< T, DIM, Iterator > &r)
Create matrix of a sub-part of another matrix (identical types)
Definition: FixedMatrix.h:249
Utility struct for FixedMatrix sub-parts.
Definition: FixedMatrix.h:92
Definition: FixedMatrix.h:1001
const FixedMatrixPart< T, WIDTH *HEIGHT, MatrixSubRectIterator< T > > part() const
extracts a rectangular matrix sub region (const)
Definition: FixedMatrix.h:800
static const unsigned int DIM
count of matrix elements (COLS x ROWS)
Definition: FixedMatrix.h:189
FixedMatrixPart< T, ROWS, const_col_iterator > col(unsigned int idx) const
returns a matrix col-reference iterator pair (const)
Definition: FixedMatrix.h:786
FixedMatrix< T, COLS, ROWS > normalized(T norm=2) const
create a normalized version of this matrix
Definition: FixedMatrix.h:850
col_iterator & operator-=(difference_type n)
jump backward next n elements (inplace)
Definition: FixedMatrix.h:539
Definition: FixedMatrix.h:999
const col_iterator operator--(int) const
postfix decrement operator (const)
Definition: FixedMatrix.h:519
static const MatrixSubRectIterator< Type > create_end_iterator(const Type *dataOrigin, int matrixWidth, int subRectX, int subRectY, int subRectWidth, int subRectHeight)
Definition: MatrixSubRectIterator.h:83
const DynMatrix< T > dyn() const
creates a shallow copied DynMatrix instance wrapping this' data (const)
Definition: FixedMatrix.h:180
DynMatrix< T > operator%(const DynMatrix< T > &top, const DynMatrix< T > &bottom)
horizontal concatenation of matrices
Definition: DynMatrix.h:1286
FixedMatrix< T, NEW_WIDTH, NEW_HEIGHT > resize(const T &init=T(0)) const
extends/shrinks matrix dimensions while preserving content on remaining elements (without scaling)
Definition: FixedMatrix.h:811
bool operator==(const col_iterator &i) const
comparison operator ==
Definition: FixedMatrix.h:595
const_row_iterator row_end(unsigned int row) const
row end iterator (const)
Definition: FixedMatrix.h:651
Special linear algebra exception type .
Definition: DynMatrix.h:66
FixedMatrix(const FixedMatrix &other)
Definition: FixedMatrix.h:236
FixedMatrix & operator=(const FixedMatrix &other)
Assignment operator (with compatible data type) (deep copy)
Definition: FixedMatrix.h:260
T trace() const
computes the sum of all diagonal elements
Definition: FixedMatrix.h:761
T * data()
return internal data pointer
Definition: FixedMatrix.h:453
Special linear algebra exception type .
Definition: DynMatrix.h:56
FixedMatrix operator-() const
Prefix - operator.
Definition: FixedMatrix.h:392
ICLMath_API const AXES AXES_DEFAULT
col_iterator & operator++()
prefix increment operator
Definition: FixedMatrix.h:477
void decompose_RQ(FixedMatrix< T, ROWS, ROWS > &R, FixedMatrix< T, ROWS, ROWS > &Q) const
computes the RQ decomposition of a matrix
Definition: FixedMatrix.h:889
FixedMatrix operator/(T f) const
Divide all elements by a scalar.
Definition: FixedMatrix.h:325
const col_iterator & operator++() const
prefix increment operator (const)
Definition: FixedMatrix.h:482
FixedMatrix< T, V_COLS, M_ROWS_AND_COLS > & operator *=(FixedMatrix< T, V_COLS, M_ROWS_AND_COLS > &v, const FixedMatrix< T, M_ROWS_AND_COLS, M_ROWS_AND_COLS > &m)
Matrix multiplication (inplace)
Definition: FixedMatrix.h:959
Definition: FixedMatrix.h:1000
FixedMatrix operator-(const FixedMatrix &m) const
Element-wise matrix subtraction.
Definition: FixedMatrix.h:378
Definition: FixedMatrix.h:1000
Iterator class used to iterate through a sub rect of 2D data.
Definition: MatrixSubRectIterator.h:70
#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
FixedMatrix & operator=(const FixedMatrixPart< T, DIM, Iterator > &r)
Assign matrix elements with sup-part of another matrix (identical types)
Definition: FixedMatrix.h:284
double cond(const double p=2) const
computes the condition of a fixed matrix
Definition: FixedMatrix.h:755
FixedMatrix(OtherIterator begin, OtherIterator end)
Range based constructor for STL compatiblitiy.
Definition: FixedMatrix.h:230
internal struct for row-wise iteration with stride=COLS
Definition: FixedMatrix.h:462
const T * data() const
return internal data pointer (const)
Definition: FixedMatrix.h:456
const col_iterator operator-(difference_type n) const
jump backward next n elements (const)
Definition: FixedMatrix.h:573
const_col_iterator col_begin(unsigned int col) const
returns an iterator iterating over a certain column (const)
Definition: FixedMatrix.h:636
const col_iterator & operator+=(difference_type n) const
jump next n elements (inplace) (const)
Definition: FixedMatrix.h:532
FixedMatrix inv() const
invert the matrix (only implemented with IPP_OPTIMIZATION and only for icl32f and icl64f)
Definition: FixedMatrix.h:715
#define C4(A, B, C, D)
col_iterator operator-(difference_type n)
jump backward next n elements
Definition: FixedMatrix.h:566
FixedMatrix & operator=(const T &t)
Assign all elements with given value.
Definition: FixedMatrix.h:277
static FixedMatrix< T, ROWS, COLS > id()
create identity matrix
Definition: FixedMatrix.h:826
T & at(unsigned int col, unsigned int row)
Element access index save (with exception if index is invalid)
Definition: FixedMatrix.h:410
void normalize(T norm=2)
inplace normalization
Definition: FixedMatrix.h:844
Definition: FixedMatrix.h:1001
double length(T norm=2) const
Calculates the length of the matrix data vector.
Definition: FixedMatrix.h:835
FixedMatrix operator+(const T &t) const
Add a scalar to each element.
Definition: FixedMatrix.h:338
AXES
axes order specifications for euler angles
Definition: FixedMatrix.h:999
const T * const_iterator
const iterator type
Definition: FixedMatrix.h:438
ICLMath_IMP std::istream & operator>>(std::istream &s, DynMatrix< T > &m)
istream operator implemented for uchar, short, int, float and double matrices
T & operator[](unsigned int idx)
linear data view element access
Definition: FixedMatrix.h:423
Definition: FixedMatrix.h:1001
Definition: FixedMatrix.h:1000
FixedMatrix()
Default constructor.
Definition: FixedMatrix.h:195
FixedMatrix(const T *srcdata)
Create matrix with given data pointer (const version)
Definition: FixedMatrix.h:207
bool operator<(const col_iterator &i) const
comparison operator <
Definition: FixedMatrix.h:601
bool operator>(const col_iterator &i) const
comparison operator >
Definition: FixedMatrix.h:610
void mult(const FixedMatrix< T, MCOLS, COLS > &m, FixedMatrix< T, MCOLS, ROWS > &dst) const
inplace matrix multiplication (dst = (*this)*m)
Definition: FixedMatrix.h:667
iterator end()
returns an iterator after the last element
Definition: FixedMatrix.h:621
col_iterator & operator--()
prefix decrement operator
Definition: FixedMatrix.h:500
row_iterator row_end(unsigned int row)
row end iterator
Definition: FixedMatrix.h:645
T & operator()(unsigned int col, unsigned int row)
Element access operator.
Definition: FixedMatrix.h:400
FixedMatrix operator *(T f) const
Multiply all elements by a scalar.
Definition: FixedMatrix.h:312
T & operator *()
Dereference operator.
Definition: FixedMatrix.h:585
row_iterator row_begin(unsigned int row)
returns an iterator iterating over a certain row
Definition: FixedMatrix.h:642
static const unsigned int STRIDE
the stride is equal to parent Matrix' classes COLS template parameter
Definition: FixedMatrix.h:471
FixedMatrix operator/(const FixedMatrix &m) const
Matrix devision.
Definition: FixedMatrix.h:302
col_iterator operator+(difference_type n)
jump next n elements
Definition: FixedMatrix.h:552
const col_iterator & operator--() const
prefix decrement operator (const)
Definition: FixedMatrix.h:506
FixedMatrixPart & operator=(const T &value)
Assignment with a new value (all data in range will be assigned with that value)
Definition: FixedMatrix.h:105
FixedMatrixPart & operator=(const FixedMatrixPart &other)
Assignment with another (identical) instance of FixedMatrixPart.
Definition: FixedMatrix.h:111
Iterator end
End iterator.
Definition: FixedMatrix.h:99
FixedMatrix & operator+=(const T &t)
Add a scalar to each element (inplace)
Definition: FixedMatrix.h:345
FixedMatrix & operator+=(const FixedMatrix &m)
Element-wise matrix addition (inplace)
Definition: FixedMatrix.h:372
T det() const
calculate matrix determinant (only implemented with IPP_OPTIMIZATION and only for icl32f and icl64f)
Definition: FixedMatrix.h:723
FixedMatrix< T, ROWS, COLS > transp() const
returns matrix's transposed
Definition: FixedMatrix.h:728
bool operator<=(const col_iterator &i) const
comparison operator <=
Definition: FixedMatrix.h:604
FixedMatrix & operator-=(const T &t)
Substract a scalar from each element (inplace)
Definition: FixedMatrix.h:359
const col_iterator operator++(int) const
postfix increment operator (const)
Definition: FixedMatrix.h:493
static unsigned int rows()
compatibility-function returns template parameter ROWS
Definition: FixedMatrix.h:447
T cond(const double p=2) const
computes the condition of a matrix
Definition: DynMatrix.h:1069
Definition: FixedMatrix.h:1002
col_iterator operator--(int)
postfix decrement operator
Definition: FixedMatrix.h:512
#define ICLMath_IMP
Definition: CompatMacros.h:172
const_iterator begin() const
returns an iterator to first element iterating over each element (row-major order) (const)
Definition: FixedMatrix.h:624
FixedMatrix< T, ROWS, COLS > pinv(bool useSVD=0, float zeroThreshold=0.00000000000000001) const
Computes the Matrix's pseudo-inverse.
Definition: FixedMatrix.h:903
void eigen(FixedMatrix &eigenvectors, FixedMatrix< T, 1, COLS > &eigenvalues) const
Extracts the matrix's eigenvalues and eigenvectors.
Definition: FixedMatrix.h:921
bool operator>=(const col_iterator &i) const
comparison operator >=
Definition: FixedMatrix.h:607
const T & operator[](unsigned int idx) const
linear data view element access (const)
Definition: FixedMatrix.h:429
col_iterator operator++(int)
postfix increment operator
Definition: FixedMatrix.h:487
T * row_iterator
row_iterator
Definition: FixedMatrix.h:441
T * iterator
iterator type
Definition: FixedMatrix.h:435
FixedMatrix & operator/=(const FixedMatrix &m)
Matrix devision (inplace)
Definition: FixedMatrix.h:307
FixedMatrix< T, OTHER_COLS, COLS > dot(const FixedMatrix< T, OTHER_COLS, ROWS > &M) const
returns the inner product of two matrices (i.e. dot-product)
Definition: FixedMatrix.h:750
static unsigned int dim()
return static member variable DIM (COLS*ROWS)
Definition: FixedMatrix.h:459
OPTIMIZED_MATRIX_MULTIPLICATION(2, 2, 2, float, 32f)
const_col_iterator col_end(unsigned int col) const
row end iterator const
Definition: FixedMatrix.h:639
bool operator==(const FixedMatrix< otherT, COLS, ROWS > &m) const
Element-wise comparison with other matrix.
Definition: FixedMatrix.h:857
ICLMath_IMP FixedMatrix< T, 3, 3 > create_hom_3x3(T angle, T dx=0, T dy=0, T v0=0, T v1=0)
creates a 2D homogen matrix (defined for float and double)
void copy(const T *src, const T *srcEnd, T *dst)
moves data from source to destination array (no casting possible)
Definition: CoreFunctions.h:216
void decompose_QR(FixedMatrix< T, COLS, ROWS > &Q, FixedMatrix< T, COLS, COLS > &R) const
computes the QR decomposition of a matrix
Definition: FixedMatrix.h:882
bool operator!=(const col_iterator &i) const
comparison operator !=
Definition: FixedMatrix.h:598
FixedMatrix & operator-=(const FixedMatrix &m)
Element-wise matrix subtraction (inplace)
Definition: FixedMatrix.h:384
FixedMatrix(const FixedMatrix< otherT, COLS, ROWS > &other)
Definition: FixedMatrix.h:243
static void optimized_copy(SrcIterator srcBegin, SrcIterator srcEnd, DstIterator dstBegin)
Optimized copy function template (for N>30 using std::copy, otherwise a simple loop is used)
Definition: FixedMatrix.h:59
Definition: FixedMatrix.h:1000
Definition: FixedMatrix.h:999
Definition: FixedMatrix.h:1000
FixedMatrix(const T &v0, const T &v1, const T &v2=0, const T &v3=0, const T &v4=0, const T &v5=0, const T &v6=0, const T &v7=0, const T &v8=0, const T &v9=0, const T &v10=0, const T &v11=0, const T &v12=0, const T &v13=0, const T &v14=0, const T &v15=0)
Create matrix with given initializer elements (16 values max)
Definition: FixedMatrix.h:216
FixedMatrix & operator *=(T f)
moved outside the class Multiply all elements by a scalar (inplace)
Definition: FixedMatrix.h:319
DynMatrix< T > dyn()
creates a shallow copied DynMatrix instance wrapping this' data
Definition: FixedMatrix.h:177
ICLMath_IMP FixedMatrix< T, 4, 4 > create_hom_4x4(T rx, T ry, T rz, T dx=0, T dy=0, T dz=0, T v0=0, T v1=0, T v2=0, AXES axes=AXES_DEFAULT)
creates a 3D homogeneous matrix (defined for float and double)
Definition: FixedMatrix.h:999
FixedMatrix base struct defining datamode enum.
Definition: FixedMatrix.h:56
col_iterator col_end(unsigned int col)
row end iterator
Definition: FixedMatrix.h:633
Definition: FixedMatrix.h:999
Definition: FixedMatrix.h:1002
Definition: FixedMatrix.h:1002
col_iterator(T *col_begin)
Constructor.
Definition: FixedMatrix.h:474
FixedMatrix< T, 4, 4 > create_hom_4x4_trans(T dx, T dy, T dz)
creates 4D homogeneous matrix with translation part only (defined for float and double)
Definition: FixedMatrix.h:1026
ICLMath_IMP FixedMatrix< T, 3, 3 > create_rot_3D(T axisX, T axisY, T axisZ, T angle)
create 3D rotation matrix from rotation axis and angle (defined for float and double only)
FixedMatrix(const T &initValue)
Create Matrix and initialize elements with given value.
Definition: FixedMatrix.h:198