All Classes Namespaces Functions Variables Typedefs Enumerations Pages
fixed_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 fixed_general_matrix_h
23 #define fixed_general_matrix_h
24 
25 #include "types.h"
26 #include <stdlib.h>
27 #include <stdexcept>
28 
29 namespace fmatvec {
30 
39  template <int M, int N, class AT> class Matrix<General,Fixed<M>,Fixed<N>,AT> {
40 
41  public:
42 
43  typedef AT AtomicType;
44 
46 
47  protected:
48 
49  AT ele[M][N];
50 
51  template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A);
52  template<class Row> inline void deepCopy(const Matrix<Symmetric,Row,Row,AT> &A);
53 
55 
56  public:
57 
58 // Works with -std=gnu++0x only
59 // template<class Ini=All<AT> >
60 // Matrix(Ini ini=All<AT>()) {
61 // init(ini);
62 // }
63 // template<class Ini=All<AT> >
64 // Matrix(int m_, int n_, Ini ini=All<AT>()) {
65 // init(ini);
66 // }
67 
68  Matrix(Noinit) { }
69  Matrix(Init ini=INIT, const AT &a=0) { init(a); }
70  Matrix(Eye ini, const AT &a=1) { init(ini,a); }
71  Matrix(int m, int n, Noinit) { }
72  Matrix(int m, int n, Init ini=INIT, const AT &a=0) { init(a); }
73  Matrix(int m, int n, Eye ini, const AT &a=1) { init(ini,a); }
74 
75  template<class Row, class Col>
77 #ifndef FMATVEC_NO_SIZE_CHECK
78  assert(A.rows() == M);
79  assert(A.cols() == N);
80 #endif
81 
82  deepCopy(A);
83  }
84 
85  template<class Type, class Row, class Col>
86  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) {
87 
88 #ifndef FMATVEC_NO_SIZE_CHECK
89  assert(A.rows() == M);
90  assert(A.cols() == N);
91 #endif
92 
93  deepCopy(A);
94  }
95 
108  Matrix(const char *str);
109 
110  template <class Type, class Row, class Col>
111  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
123  AT& operator()(int i, int j) {
124 #ifndef FMATVEC_NO_BOUNDS_CHECK
125  assert(i>=0);
126  assert(j>=0);
127  assert(i<M);
128  assert(j<N);
129 #endif
130 
131  return e(i,j);
132  };
133 
138  const AT& operator()(int i, int j) const {
139 #ifndef FMATVEC_NO_BOUNDS_CHECK
140  assert(i>=0);
141  assert(j>=0);
142  assert(i<M);
143  assert(j<N);
144 #endif
145 
146  return e(i,j);
147  };
148 
149  AT& e(int i, int j) {
150  return ele[i][j];
151  };
152 
157  const AT& e(int i, int j) const {
158  return ele[i][j];
159  };
160 
166  AT* operator()() {return &(ele[0][0]);};
167 
172  const AT* operator()() const {return &(ele[0][0]);};
173 
178  int rows() const {return M;};
179 
184  int cols() const {return N;};
185 
190  int ldim() const {return N;};
191 
193  bool transposed() const {
194  return true;
195  };
196 
199  void resize(int m, int n) {
200  if(m!=M || n!=N)
201  throw std::runtime_error("A fixed matrix cannot be resized.");
202  }
203 
210  const CBLAS_TRANSPOSE blasTrans() const {
211  return CblasNoTrans;
212  };
213 
221  const CBLAS_ORDER blasOrder() const {
222  return CblasRowMajor;
223  };
224 
225 // /*! \brief Submatrix operator.
226 // *
227 // * Returns a submatrix of the calling matrix.
228 // * \attention The submatrix and the
229 // * calling matrix will share the same physical memory.
230 // * \param i1 The starting row.
231 // * \param j1 The starting column.
232 // * \param i2 The ending row.
233 // * \param j2 The ending column.
234 // * \return A submatrix of the calling matrix.
235 // * */
236 // inline Matrix<General, AT> operator()(int i1, int j1, int i2, int j2);
237 //
238 // /*! \brief Submatrix operator.
239 // *
240 // * See operator()(int,int,int,int);
241 // * */
242 // inline const Matrix<General, AT> operator()(int i1, int j1, int i2, int j2) const;
243 
270 // inline Matrix<General, Var, AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J);
271 
276  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
277 
278  template <int M1, int M2, int N1, int N2>
279  inline const Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Fixed<N1>,Fixed<N2> > &J) const;
280  template <int M1, int M2>
281  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Var,Var > &J) const;
282 
283  template <int N1, int N2>
284  inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> operator()(const Range<Var,Var> &I, const Range<Fixed<N1>,Fixed<N2> > &J) const;
285 
286  inline const RowVector<Fixed<N>,AT> row(int j) const;
287  inline const Vector<Fixed<M>,AT> col(int i) const;
288 
296  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(const AT &a=0);
297  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Init, const AT &a=0) { return init(a); };
298  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Eye, const AT &a=1);
299  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Noinit, const AT &a=0) { return *this; }
300 
305  inline operator std::vector<std::vector<AT> >();
306 
312  Matrix(std::vector<std::vector<AT> > m);
313 
314  inline const Matrix<General,Fixed<N>,Fixed<M>,AT> T() const;
315 
319  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
320 
324  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
325 
332  template<class Type, class Row, class Col> inline void set(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A);
333 
337  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
338 
342  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
343 
344  template<class Type, class Row, class Col> inline void add(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A);
345 
346  };
347 
348  template <int M, int N, class AT>
350  // if 'strs' is a single scalar, surround it first with '[' and ']'.
351  // This is more Matlab-like, because e.g. '5' and '[5]' is just the same.
352  // (This functionallitiy is needed e.g. by MBXMLUtils (OpenMBV,MBSim))
353  std::istringstream iss(strs);
354  char c;
355  iss>>c;
356  if(c=='[') iss.str(strs);
357  else iss.str(std::string("[")+strs+"]");
358 
359  int m=0, n=0;
360  int buf=0;
361  iss >> c;
362  iss >> c;
363  if(c!=']') {
364  iss.putback(c);
365  AT x;
366  do {
367  iss >> x;
368  iss >> c;
369  if(c==';') {
370  if(buf)
371  assert(buf == n);
372 
373  buf=n;
374  n=0;
375  m++;
376  }
377  else if(c==',')
378  n++;
379  c='0';
380  } while(iss);
381 
382  n++; m++;
383  iss.clear();
384  iss.seekg(0);
385  iss >> c;
386  for(int i=0; i<M; i++)
387  for(int j=0; j<N; j++) {
388  iss >> e(i,j);
389  iss >> c;
390  }
391  }
392  assert(m==M);
393  assert(n==N);
394  }
395 
396  template <int M, int N, class AT> template<class Type, class Row, class Col>
398 
399 #ifndef FMATVEC_NO_SIZE_CHECK
400  assert(A.rows() == M);
401  assert(A.cols() == N);
402 #endif
403 
404  deepCopy(A);
405 
406  return *this;
407  }
408 
409  template <int M, int N, class AT>
410  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::init(const AT &val) {
411  for(int i=0; i<M; i++)
412  for(int j=0; j<N; j++)
413  e(i,j) = val;
414  return *this;
415  }
416 
417  template <int M, int N, class AT>
418  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::init(Eye, const AT &val) {
419  for(int i=0; i<M; i++)
420  for(int j=0; j<N; j++)
421  e(i,j) = (i==j) ? val : 0;
422  return *this;
423  }
424 
425  template <int M, int N, class AT>
426  inline const Matrix<General,Var,Var,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const {
427 #ifndef FMATVEC_NO_BOUNDS_CHECK
428  assert(I.end()<M);
429  assert(J.end()<N);
430 #endif
431  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
432 
433  for(int i=0; i<A.rows(); i++)
434  for(int j=0; j<A.cols(); j++)
435  A.e(i,j) = e(I.start()+i,J.start()+j);
436 
437  return A;
438  }
439 
440  template <int M, int N, class AT> template <int M1, int M2, int N1, int N2>
441  inline const Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Fixed<N1>,Fixed<N2> > &J) const {
442 #ifndef FMATVEC_NO_BOUNDS_CHECK
443  assert(M2<M);
444  assert(N2<N);
445 #endif
446  Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> A(NONINIT);
447 
448  for(int i=0; i<A.rows(); i++)
449  for(int j=0; j<A.cols(); j++)
450  A.e(i,j) = e(M1+i,J.start()+j);
451 
452  return A;
453  }
454 
455  template <int M, int N, class AT> template <int M1, int M2>
456  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Var,Var> &J) const {
457 #ifndef FMATVEC_NO_BOUNDS_CHECK
458  assert(M2<M);
459  assert(J.end()<N);
460 #endif
461  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
462 
463  for(int i=0; i<A.rows(); i++)
464  for(int j=0; j<A.cols(); j++)
465  A.e(i,j) = e(I.start()+i,J.start()+j);
466 
467  return A;
468  }
469 
470  template <int M, int N, class AT> template <int N1, int N2>
471  inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::operator()(const Range<Var,Var> &I, const Range<Fixed<N1>,Fixed<N2> > &J) const {
472 #ifndef FMATVEC_NO_BOUNDS_CHECK
473  assert(I.end()<M);
474  assert(N2<N);
475 #endif
476  Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.start()+1,NONINIT);
477 
478  for(int i=0; i<A.rows(); i++)
479  for(int j=0; j<A.cols(); j++)
480  A.e(i,j) = e(I.start()+i,J.start()+j);
481 
482  return A;
483  }
484 
485  template <int M, int N, class AT>
486  inline const RowVector<Fixed<N>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::row(int i) const {
487 
488 #ifndef FMATVEC_NO_BOUNDS_CHECK
489  assert(i>=0);
490  assert(i<M);
491 #endif
492 
493  RowVector<Fixed<N>,AT> x(NONINIT);
494 
495  for(int j=0; j<N; j++)
496  x.e(j) = e(i,j);
497 
498  return x;
499 
500  }
501 
502  template <int M, int N, class AT>
503  inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::col(int j) const {
504 
505 #ifndef FMATVEC_NO_BOUNDS_CHECK
506  assert(j>=0);
507  assert(j<N);
508 #endif
509 
510  Vector<Fixed<M>,AT> x(NONINIT);
511 
512  for(int i=0; i<M; i++)
513  x.e(i) = e(i,j);
514 
515  return x;
516 
517  }
518 
519  template <int M, int N, class AT>
520  inline const Matrix<General,Fixed<N>,Fixed<M>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::T() const {
521  Matrix<General,Fixed<N>,Fixed<M>,AT> A(NONINIT);
522  for(int i=0; i<N; i++)
523  for(int j=0; j<M; j++)
524  A.e(i,j) = e(j,i);
525  return A;
526  }
527 
528  template <int M, int N, class AT> template <class Row>
529  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::set(int j, const Vector<Row,AT> &x) {
530 #ifndef FMATVEC_NO_BOUNDS_CHECK
531  assert(j<cols());
532  assert(rows()==x.size());
533 #endif
534  for(int i=0; i<rows(); i++)
535  e(i,j) = x.e(i);
536  }
537 
538  template <int M, int N, class AT> template <class Col>
539  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::set(int i, const RowVector<Col,AT> &x) {
540 #ifndef FMATVEC_NO_BOUNDS_CHECK
541  assert(i<rows());
542  assert(cols()==x.size());
543 #endif
544  for(int j=0; j<cols(); j++)
545  e(i,j) = x.e(j);
546  }
547 
548  template <int M, int N, class AT> template<class Type, class Row, class Col>
549  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::set(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
550 
551 #ifndef FMATVEC_NO_BOUNDS_CHECK
552  assert(I.end()<rows());
553  assert(J.end()<cols());
554  assert(I.size()==A.rows());
555  assert(J.size()==A.cols());
556 #endif
557 
558  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
559  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
560  e(i,j) = A.e(ii,jj);
561  }
562 
563  template <int M, int N, class AT> template <class Row>
564  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::add(int j, const Vector<Row,AT> &x) {
565 #ifndef FMATVEC_NO_BOUNDS_CHECK
566  assert(j<cols());
567  assert(rows()==x.size());
568 #endif
569  for(int i=0; i<rows(); i++)
570  e(i,j) += x.e(i);
571  }
572 
573  template <int M, int N, class AT> template <class Col>
574  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::add(int i, const RowVector<Col,AT> &x) {
575 #ifndef FMATVEC_NO_BOUNDS_CHECK
576  assert(i<rows());
577  assert(cols()==x.size());
578 #endif
579  for(int j=0; j<cols(); j++)
580  e(i,j) += x.e(j);
581  }
582 
583  template <int M, int N, class AT> template<class Type, class Row, class Col>
584  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::add(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
585 
586 #ifndef FMATVEC_NO_BOUNDS_CHECK
587  assert(I.end()<rows());
588  assert(J.end()<cols());
589  assert(I.size()==A.rows());
590  assert(J.size()==A.cols());
591 #endif
592 
593  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
594  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
595  e(i,j) += A.e(ii,jj);
596  }
597 
598  template <int M, int N, class AT>
599  inline Matrix<General,Fixed<M>,Fixed<N>,AT>::operator std::vector<std::vector<AT> >() {
600  std::vector<std::vector<AT> > ret(rows());
601  for(int r=0; r<rows(); r++) {
602  ret[r].resize(cols());
603  for(int c=0; c<cols(); c++)
604  ret[r][c]=e(r,c);
605  }
606  return ret;
607  }
608 
609  template <int M, int N, class AT>
610  inline Matrix<General,Fixed<M>,Fixed<N>,AT>::Matrix(std::vector<std::vector<AT> > m) {
611 #ifndef FMATVEC_NO_SIZE_CHECK
612  assert(m.size() == M);
613  assert(m[0].size() == N);
614 #endif
615  for(int r=0; r<rows(); r++) {
616  assert(m[r].size()==cols());
617  for(int c=0; c<cols(); c++)
618  e(r,c)=m[r][c];
619  }
620  }
621 
623 
624  template <int M, int N, class AT> template <class Type, class Row, class Col>
625  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) {
626  for(int i=0; i<M; i++)
627  for(int j=0; j<N; j++)
628  e(i,j) = A.e(i,j);
629  }
630 
631  template<int M, int N, class AT> template<class Row>
632  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::deepCopy(const Matrix<Symmetric,Row,Row,AT> &A) {
633  for(int i=0; i<A.size(); i++) {
634  e(i,i) = A.ej(i,i);
635  for(int j=i+1; j<A.size(); j++)
636  e(i,j) = e(j,i) = A.ej(i,j);
637  }
638  }
639 
641 
649  template<int M>
650  class Matrix<Rotation,Fixed<M>,Fixed<M>,double> : public Matrix<General,Fixed<M>,Fixed<M>,double> {
651  public:
652  typedef double AtomicType;
653  // Constructors are not inherited. Hence we must redefine all ctors here.
654 
655  Matrix(Noinit ini) { }
656  Matrix(Init ini=INIT, const double &a=0) { this->init(a); }
657  Matrix(Eye ini, const double &a=1) { this->init(ini,a); }
658  Matrix(int m, int n, Noinit ini) { }
659  Matrix(int m, int n, Init ini, const double &a=0) { this->init(a); }
660  Matrix(int m, int n, Eye ini, const double &a=1) { this->init(ini,a); }
661  explicit Matrix(const Matrix<General,Fixed<M>,Fixed<M>,double>& A) { this->deepCopy(A); }
662 
663  template<class Row>
665 #ifndef FMATVEC_NO_SIZE_CHECK
666  assert(A.size() == M);
667 #endif
668  this->deepCopy(A);
669  }
670 
671  template<class Type, class Row, class Col>
672  explicit Matrix(const Matrix<Type,Row,Col,double> &A) {
673 #ifndef FMATVEC_NO_SIZE_CHECK
674  assert(A.rows() == M);
675  assert(A.cols() == M);
676 #endif
677  this->deepCopy(A);
678  }
679 
680  // TODO: we should add some overloaded member functions here which capitalize the special
681  // properties of rotation matrices, link "RotMat inv() { return trans(*this); }"
682  };
683 
684 
685 }
686 
687 #endif
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:138
Definition: fmatvec.h:38
bool transposed() const
A fixed matrix is stored in c-storage order -&gt; transposed is always true.
Definition: fixed_general_matrix.h:193
const AT * operator()() const
Pointer operator.
Definition: fixed_general_matrix.h:172
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT * operator()()
Pointer operator.
Definition: fixed_general_matrix.h:166
This is a matrix class for general matrices.
Definition: var_general_matrix.h:38
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:86
Definition: matrix.h:39
Definition: types.h:65
Definition: fmatvec.h:50
Shape class for rotation matrices.
Definition: types.h:124
int rows() const
Number of rows.
int cols() const
Number of columns.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_general_matrix.h:221
int cols() const
Number of columns.
Definition: fixed_general_matrix.h:184
Definition: fmatvec.h:47
const AT & e(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:157
int rows() const
Number of rows.
Definition: fixed_general_matrix.h:178
This is an index class for creating submatrices.
Definition: range.h:35
int size() const
Size.
Definition: range.h:101
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_general_matrix.h:210
int ldim() const
Leading dimension.
Definition: fixed_general_matrix.h:190
Definition: matrix.h:38
AT & operator()(int i, int j)
Element operator.
Definition: fixed_general_matrix.h:123
Basic shape class for matrices.
Definition: types.h:100
void resize(int m, int n)
Definition: fixed_general_matrix.h:199
int end() const
Last element.
Definition: range.h:95
This is an index class for creating submatrices.
Definition: range.h:44
int start() const
First element.
Definition: range.h:89
Definition: matrix.h:40

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML