general_matrix.h
1 /* Copyright (C) 2003-2005 Martin Förg
2 
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact:
18  * martin.o.foerg@googlemail.com
19  *
20  */
21 
22 #ifndef general_matrix_h
23 #define general_matrix_h
24 
25 #include "_memory.h"
26 #include "types.h"
27 #include "index.h"
28 #include <stdlib.h>
29 
30 namespace fmatvec {
31 
32  template <class AT> class Vector;
33  template <class AT> class RowVector;
34  template <class AT> class SquareMatrix;
35  //template <class AT> class Matrix<Symmetric, AT>;
36 
45  template <class AT> class Matrix<General, AT> {
46 
47  public:
48 
50 
51  friend class Matrix<Symmetric, AT>;
52 
53  template <class T> friend Matrix<General, T> trans(const Matrix<General, T> &A);
54 
55  protected:
56 
57  Memory<AT> memory;
58  AT *ele;
59  int m;
60  int n;
61  int lda;
62  bool tp;
63 
64  template <class Type> void deepCopy(const Matrix<Type, AT> &A);
65 
66  const AT* elePtr(int i, int j) const {
67  return tp ? ele+i*lda+j : ele+i+j*lda;
68  };
69 
70  AT* elePtr(int i, int j) {
71  return tp ? ele+i*lda+j : ele+i+j*lda;
72  };
73 
74  Matrix(int m_, int n_, int lda_, bool tp_, Memory<AT> memory_, const AT* ele_) : memory(memory_), ele((AT*)ele_), m(m_), n(n_), lda(lda_), tp(tp_) {
75  }
76 
78 
79  public:
80 
85  Matrix() : memory(), ele(0), m(0), n(0), lda(0) {
86  }
87 
97  Matrix(int m_, int n_) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_), tp(false) {
98 
99 #ifndef FMATVEC_NO_INITIALIZATION
100  init(0);
101 #endif
102  }
103 
114  Matrix(int m_, int n_, Initialization ini, const AT &a=0) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_), tp(false) {
115 
116  if(ini == INIT) {
117  AT *el=ele;
118  for(int i=0; i<m*n; i++)
119  *el++=a;
120  } else if(ini == EYE ) {
121  for(int i=0; i<m; i++) {
122  for(int j=0; j<n; j++) {
123  if (i==j) ele[i+j*lda] = 1; // operator()(i,i) = 1;
124  else ele[i+j*lda] = 0; // operator()(i,j) = 0;
125  }
126  }
127  }
128  }
129 
137  Matrix(const Matrix<General, AT> &A) : memory(A.memory), ele(A.ele) ,m(A.m), n(A.n), lda(A.lda), tp(A.tp) {
138  }
139 
147  Matrix(int m_, int n_, AT* ele_) : memory(), ele(ele_), m(m_), n(n_), lda(m_), tp(false) {
148  }
149 
162  Matrix(const char *str);
163 
167  }
168 
179  Matrix<General, AT>& resize(int m_, int n_) {
180  m=m_;n=n_;
181  lda=m;
182  tp = false;
183  memory.resize(m*n);
184  ele = (AT*)memory.get();
185 
186 #ifndef FMATVEC_NO_INITIALIZATION
187  init(0);
188 #endif
189  return *this;
190  }
191 
203  Matrix<General, AT>& resize(int m_, int n_, Initialization ini, const AT &a=0) {
204  m=m_;n=n_;
205  lda=m;
206  tp = false;
207  memory.resize(m*n);
208  ele = (AT*)memory.get();
209 
210  if(ini == INIT)
211  init(a);
212  else if(ini == EYE ) {
213  for(int i=0; i<m; i++) {
214  for(int j=0; j<n; j++) {
215  if (i==j) ele[i+j*lda] = 1; // operator()(i,i) = 1;
216  else ele[i+j*lda] = 0; // operator()(i,j) = 0;
217  }
218  }
219  }
220 
221  return *this;
222  }
223 
233 #ifndef FMATVEC_NO_DEEP_ASSIGNMENT
234  return operator<<(A);
235 #else
236  return operator>>(A);
237 #endif
238  }
239 
246  template<class T> Matrix<General, AT>& operator<<(const Matrix<T, AT> &A);
247 
255 
267  AT& operator()(int i, int j) {
268 #ifndef FMATVEC_NO_BOUNDS_CHECK
269  assert(i>=0);
270  assert(j>=0);
271  assert(i<m);
272  assert(j<n);
273 #endif
274 
275  return tp ? ele[i*lda+j] : ele[i+j*lda];
276  };
277 
282  const AT& operator()(int i, int j) const {
283 #ifndef FMATVEC_NO_BOUNDS_CHECK
284  assert(i>=0);
285  assert(j>=0);
286  assert(i<m);
287  assert(j<n);
288 #endif
289 
290  return tp ? ele[i*lda+j] : ele[i+j*lda];// return ele[i*lda+j*ldb];
291  };
292 
298  AT* operator()() {return ele;};
299 
304  const AT* operator()() const {return ele;};
305 
310  int rows() const {return m;};
311 
316  int cols() const {return n;};
317 
322  int ldim() const {return lda;};
323 
329  bool transposed() const {return tp;};
330 
337  const CBLAS_TRANSPOSE blasTrans() const {
338  return (tp)? CblasTrans : CblasNoTrans;
339  };
340 
348  const CBLAS_ORDER blasOrder() const {
349  return CblasColMajor;
350  };
351 
363  Matrix<General, AT> operator()(int i1, int j1, int i2, int j2);
364 
369  const Matrix<General, AT> operator()(int i1, int j1, int i2, int j2) const;
370 
397  Matrix<General, AT> operator()(const Index &I, const Index &J);
398 
403  const Matrix<General, AT> operator()(const Index &I, const Index &J) const;
404 
414 
419  const SquareMatrix<AT> operator()(const Index &I) const;
420 
428  Vector<AT> col(int i);
429 
434  const Vector<AT> col(int i) const;
435 
443  RowVector<AT> row(int i);
444 
449  const RowVector<AT> row(int i) const;
450 
456  Matrix<General, AT>& resize() {m=0;n=0;return *this;};
457 
463  Matrix<General, AT> copy() const;
464 
472  Matrix<General, AT>& init(const AT &a);
473 
478  operator std::vector<std::vector<AT> >();
479 
485  Matrix(std::vector<std::vector<AT> > m);
486 
487  Matrix<General, AT> T() {
488  return Matrix<General, AT>(n,m,lda,tp?false:true,memory,ele);
489  };
490 
491  const Matrix<General, AT> T() const {
492  return Matrix<General, AT>(n,m,lda,tp?false:true,memory,ele);
493  }
494  };
495  // ------------------------- Constructors -------------------------------------
496  template <class AT>
497  Matrix<General, AT>::Matrix(const char *strs) {
498  // if 'strs' is a single scalar, surround it first with '[' and ']'.
499  // This is more Matlab-like, because e.g. '5' and '[5]' is just the same.
500  // (This functionallitiy is needed e.g. by MBXMLUtils (OpenMBV,MBSim))
501  std::istringstream iss(strs);
502  char c;
503  iss>>c;
504  if(c=='[') iss.str(strs);
505  else iss.str(std::string("[")+strs+"]");
506 
507  m = 0,n=0;
508  int buf=0;
509  iss >> c;
510  AT x;
511  do {
512  iss >> x;
513  iss >> c;
514  if(c==';') {
515  if(buf)
516  assert(buf == n);
517 
518  buf=n;
519  n=0;
520  m++;
521  }
522  else if(c==',')
523  n++;
524  c='0';
525  } while(iss);
526 
527  n++; m++;
528  lda=m;
529  tp = false;
530  memory.resize(m*n);
531  ele = (AT*)memory.get();
532  iss.clear();
533  iss.seekg(0);
534  iss >> c;
535  for(int i=0; i<m; i++)
536  for(int j=0; j<n; j++) {
537  iss >> ele[i+j*lda]; //iss >> operator()(i,j);
538  iss >> c;
539  }
540  }
541  // ----------------------------------------------------------------------------
542 
543  template <class AT>
545 
546  if(m==0) {
547  m=A.m;
548  n=A.n;
549  } else {
550 #ifdef FMATVEC_SIZE_CHECK
551  assert(m == A.m);
552  assert(n == A.n);
553 #endif
554  }
555 
556  memory = A.memory;
557  ele = A.ele;
558  lda = A.lda;
559  tp = A.tp;
560 
561  return *this;
562  }
563 
564  template <class AT> template< class Type>
566 
567  if(A.rows() == 0 || A.cols() == 0)
568  return *this;
569 
570  if(m==0) {
571  m = A.rows();
572  n = A.cols();
573  lda = m;
574  tp = false;
575  memory.resize(m*n);
576  ele = (AT*)memory.get();
577  } else {
578 #ifdef FMATVEC_SIZE_CHECK
579  assert(m == A.rows());
580  assert(n == A.cols());
581 #endif
582  }
583 
584  deepCopy(A);
585 
586  return *this;
587  }
588 
589  template <class AT>
591 
592  if(tp) {
593  for(int i=0; i<rows(); i++)
594  for(int j=0; j<cols(); j++)
595  ele[i*lda+j] = val; // operator()(i,j) = val;
596  }
597  else {
598  for(int i=0; i<rows(); i++)
599  for(int j=0; j<cols(); j++)
600  ele[i+j*lda] = val; // operator()(i,j) = val;
601  }
602 
603  return *this;
604  }
605 
606  template <class AT>
607  Matrix<General, AT> Matrix<General, AT>::operator()(int i1, int j1, int i2, int j2) {
608  return operator()(Index(i1,i2),Index(j1,j2));
609  }
610 
611  template <class AT>
612  const Matrix<General, AT> Matrix<General, AT>::operator()(int i1, int j1, int i2, int j2) const {
613  return operator()(Index(i1,i2),Index(j1,j2));
614  }
615 
616  template <class AT> Matrix<General, AT> Matrix<General, AT>::operator()(const Index &I, const Index &J) {
617 #ifndef FMATVEC_NO_BOUNDS_CHECK
618  assert(I.end()<m);
619  assert(J.end()<n);
620 #endif
621  return Matrix<General, AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,tp,memory,elePtr(I.start(),J.start()));
622  }
623 
624  template <class AT> const Matrix<General, AT> Matrix<General, AT>::operator()(const Index &I, const Index &J) const {
625 #ifndef FMATVEC_NO_BOUNDS_CHECK
626  assert(I.end()<m);
627  assert(J.end()<n);
628 #endif
629  return Matrix<General, AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,tp,memory,elePtr(I.start(),J.start()));
630  }
631 
632  template <class AT> const SquareMatrix<AT> Matrix<General, AT>::operator()(const Index &I) const {
633 #ifndef FMATVEC_NO_BOUNDS_CHECK
634  assert(I.end()<m);
635 #endif
636  return SquareMatrix<AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start(),I.start()));
637  }
639 #ifndef FMATVEC_NO_BOUNDS_CHECK
640  assert(I.end()<m);
641 #endif
642  return SquareMatrix<AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start(),I.start()));
643  }
644 
645  template <class AT>
647 
648 #ifndef FMATVEC_NO_BOUNDS_CHECK
649  assert(i>=0);
650  assert(i<n);
651 #endif
652 
653  return Vector<AT>(m,lda,tp,memory,elePtr(0,i));
654  }
655 
656  template <class AT>
658 
659 #ifndef FMATVEC_NO_BOUNDS_CHECK
660  assert(i>=0);
661  assert(i<n);
662 #endif
663 
664  return Vector<AT>(m,lda,tp,memory,elePtr(0,i));
665  }
666 
667  template <class AT>
669 
670 #ifndef FMATVEC_NO_BOUNDS_CHECK
671  assert(i>=0);
672  assert(i<m);
673 #endif
674 
675  return RowVector<AT>(n,lda,tp,memory,elePtr(i,0));
676  }
677 
678  template <class AT>
680 
681 #ifndef FMATVEC_NO_BOUNDS_CHECK
682  assert(i>=0);
683  assert(i<m);
684 #endif
685 
686  return RowVector<AT>(n,lda,tp,memory,elePtr(i,0));
687  }
688 
689  template <class AT>
691 
692  Matrix<General, AT> A(m,n,NONINIT);
693  A.deepCopy(*this);
694 
695  return A;
696  }
697 
698  template <class AT> template <class Type>
700  if(tp) {
701  for(int i=0; i<m; i++)
702  for(int j=0; j<n; j++)
703  ele[i*lda+j] = A(i,j); //operator()(i,j) = A.operator()(i,j);
704  }
705  else {
706  for(int i=0; i<m; i++)
707  for(int j=0; j<n; j++)
708  ele[i+j*lda] = A(i,j); //operator()(i,j) = A.operator()(i,j);
709  }
710  }
711 
712  template <class AT>
713  Matrix<General, AT>::operator std::vector<std::vector<AT> >() {
714  std::vector<std::vector<AT> > ret(rows());
715  if(tp) {
716  for(int r=0; r<rows(); r++) {
717  ret[r].resize(cols());
718  for(int c=0; c<cols(); c++)
719  ret[r][c]= ele[r*lda+c];
720  }
721  }
722  else {
723  for(int r=0; r<rows(); r++) {
724  ret[r].resize(cols());
725  for(int c=0; c<cols(); c++)
726  ret[r][c]=ele[r+c*lda];
727  }
728  }
729  return ret;
730  }
731 
732  template <class AT>
733  Matrix<General, AT>::Matrix(std::vector<std::vector<AT> > m) : memory(m.size()*m[0].size()), ele((AT*)memory.get()), m(m.size()), n(m[0].size()), lda(m.size()), tp(false) {
734 #ifndef FMATVEC_NO_INITIALIZATION
735  init(0);
736 #endif
737  for(int r=0; r<rows(); r++) {
738  assert(m[r].size()==cols());
739  for(int c=0; c<cols(); c++)
740  ele[r+c*lda]=m[r][c];
741  }
742  }
743 
745 
746  template<> template<>
748 
749  template<> template<>
751 
753 
754 }
755 
756 #endif