All Classes Namespaces Functions 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 
28 namespace fmatvec {
29 
38  template <int M, int N, class AT> class Matrix<General,Fixed<M>,Fixed<N>,AT> {
39 
40  public:
41 
43 
44  protected:
45 
46  AT ele[M][N];
47 
48  template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A);
49  template<class Row> inline void deepCopy(const Matrix<Symmetric,Row,Row,AT> &A);
50 
52 
53  public:
54 
55 // Works with -std=gnu++0x only
56 // template<class Ini=All<AT> >
57 // Matrix(Ini ini=All<AT>()) {
58 // init(ini);
59 // }
60 // template<class Ini=All<AT> >
61 // Matrix(int m_, int n_, Ini ini=All<AT>()) {
62 // init(ini);
63 // }
64 
65  Matrix(Noinit) { }
66  Matrix(Init ini=INIT, const AT &a=0) { init(a); }
67  Matrix(Eye ini, const AT &a=1) { init(ini,a); }
68  Matrix(int m, int n, Noinit) { }
69  Matrix(int m, int n, Init ini=INIT, const AT &a=0) { init(a); }
70  Matrix(int m, int n, Eye ini, const AT &a=1) { init(ini,a); }
71 
72  template<class Row, class Col>
74 #ifndef FMATVEC_NO_SIZE_CHECK
75  assert(A.rows() == M);
76  assert(A.cols() == N);
77 #endif
78 
79  deepCopy(A);
80  }
81 
82  template<class Type, class Row, class Col>
83  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) {
84 
85 #ifndef FMATVEC_NO_SIZE_CHECK
86  assert(A.rows() == M);
87  assert(A.cols() == N);
88 #endif
89 
90  deepCopy(A);
91  }
92 
105  Matrix(const char *str);
106 
107  template <class Type, class Row, class Col>
108  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
120  AT& operator()(int i, int j) {
121 #ifndef FMATVEC_NO_BOUNDS_CHECK
122  assert(i>=0);
123  assert(j>=0);
124  assert(i<M);
125  assert(j<N);
126 #endif
127 
128  return e(i,j);
129  };
130 
135  const AT& operator()(int i, int j) const {
136 #ifndef FMATVEC_NO_BOUNDS_CHECK
137  assert(i>=0);
138  assert(j>=0);
139  assert(i<M);
140  assert(j<N);
141 #endif
142 
143  return e(i,j);
144  };
145 
146  AT& e(int i, int j) {
147  return ele[i][j];
148  };
149 
154  const AT& e(int i, int j) const {
155  return ele[i][j];
156  };
157 
163  AT* operator()() {return &(ele[0][0]);};
164 
169  const AT* operator()() const {return &(ele[0][0]);};
170 
175  int rows() const {return M;};
176 
181  int cols() const {return N;};
182 
187  int ldim() const {return M;};
188 
195  const CBLAS_TRANSPOSE blasTrans() const {
196  return CblasNoTrans;
197  };
198 
206  const CBLAS_ORDER blasOrder() const {
207  return CblasRowMajor;
208  };
209 
210 // /*! \brief Submatrix operator.
211 // *
212 // * Returns a submatrix of the calling matrix.
213 // * \attention The submatrix and the
214 // * calling matrix will share the same physical memory.
215 // * \param i1 The starting row.
216 // * \param j1 The starting column.
217 // * \param i2 The ending row.
218 // * \param j2 The ending column.
219 // * \return A submatrix of the calling matrix.
220 // * */
221 // inline Matrix<General, AT> operator()(int i1, int j1, int i2, int j2);
222 //
223 // /*! \brief Submatrix operator.
224 // *
225 // * See operator()(int,int,int,int);
226 // * */
227 // inline const Matrix<General, AT> operator()(int i1, int j1, int i2, int j2) const;
228 
255 // inline Matrix<General, Var, AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J);
256 
261  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
262 
263  template <int M1, int M2, int N1, int N2>
264  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;
265  template <int M1, int M2>
266  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Var,Var > &J) const;
267 
268  template <int N1, int N2>
269  inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> operator()(const Range<Var,Var> &I, const Range<Fixed<N1>,Fixed<N2> > &J) const;
270 
271  inline const RowVector<Fixed<N>,AT> row(int j) const;
272  inline const Vector<Fixed<M>,AT> col(int i) const;
273 
281  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(const AT &a=0);
282  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Init, const AT &a=0) { return init(a); };
283  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Eye, const AT &a=1);
284  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Noinit, const AT &a=0) { return *this; }
285 
290  inline operator std::vector<std::vector<AT> >();
291 
297  Matrix(std::vector<std::vector<AT> > m);
298 
299  inline const Matrix<General,Fixed<N>,Fixed<M>,AT> T() const;
300 
304  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
305 
309  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
310 
317  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);
318 
322  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
323 
327  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
328 
329  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);
330 
331  };
332 
333  template <int M, int N, class AT>
335  // if 'strs' is a single scalar, surround it first with '[' and ']'.
336  // This is more Matlab-like, because e.g. '5' and '[5]' is just the same.
337  // (This functionallitiy is needed e.g. by MBXMLUtils (OpenMBV,MBSim))
338  std::istringstream iss(strs);
339  char c;
340  iss>>c;
341  if(c=='[') iss.str(strs);
342  else iss.str(std::string("[")+strs+"]");
343 
344  int m=0, n=0;
345  int buf=0;
346  iss >> c;
347  iss >> c;
348  if(c!=']') {
349  iss.putback(c);
350  AT x;
351  do {
352  iss >> x;
353  iss >> c;
354  if(c==';') {
355  if(buf)
356  assert(buf == n);
357 
358  buf=n;
359  n=0;
360  m++;
361  }
362  else if(c==',')
363  n++;
364  c='0';
365  } while(iss);
366 
367  n++; m++;
368  iss.clear();
369  iss.seekg(0);
370  iss >> c;
371  for(int i=0; i<M; i++)
372  for(int j=0; j<N; j++) {
373  iss >> e(i,j);
374  iss >> c;
375  }
376  }
377  assert(m==M);
378  assert(n==N);
379  }
380 
381  template <int M, int N, class AT> template<class Type, class Row, class Col>
383 
384 #ifndef FMATVEC_NO_SIZE_CHECK
385  assert(A.rows() == M);
386  assert(A.cols() == N);
387 #endif
388 
389  deepCopy(A);
390 
391  return *this;
392  }
393 
394  template <int M, int N, class AT>
395  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::init(const AT &val) {
396  for(int i=0; i<M; i++)
397  for(int j=0; j<N; j++)
398  e(i,j) = val;
399  return *this;
400  }
401 
402  template <int M, int N, class AT>
403  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::init(Eye, const AT &val) {
404  for(int i=0; i<M; i++)
405  for(int j=0; j<N; j++)
406  e(i,j) = (i==j) ? val : 0;
407  return *this;
408  }
409 
410  template <int M, int N, class AT>
411  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 {
412 #ifndef FMATVEC_NO_BOUNDS_CHECK
413  assert(I.end()<M);
414  assert(J.end()<N);
415 #endif
416  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
417 
418  for(int i=0; i<A.rows(); i++)
419  for(int j=0; j<A.cols(); j++)
420  A.e(i,j) = e(I.start()+i,J.start()+j);
421 
422  return A;
423  }
424 
425  template <int M, int N, class AT> template <int M1, int M2, int N1, int N2>
426  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 {
427 #ifndef FMATVEC_NO_BOUNDS_CHECK
428  assert(M2<M);
429  assert(N2<N);
430 #endif
431  Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> A(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(M1+i,J.start()+j);
436 
437  return A;
438  }
439 
440  template <int M, int N, class AT> template <int M1, int M2>
441  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 {
442 #ifndef FMATVEC_NO_BOUNDS_CHECK
443  assert(M2<M);
444  assert(J.end()<N);
445 #endif
446  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,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(I.start()+i,J.start()+j);
451 
452  return A;
453  }
454 
455  template <int M, int N, class AT> template <int N1, int N2>
456  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 {
457 #ifndef FMATVEC_NO_BOUNDS_CHECK
458  assert(I.end()<M);
459  assert(N2<N);
460 #endif
461  Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.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>
471  inline const RowVector<Fixed<N>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::row(int i) const {
472 
473 #ifndef FMATVEC_NO_BOUNDS_CHECK
474  assert(i>=0);
475  assert(i<M);
476 #endif
477 
478  RowVector<Fixed<N>,AT> x(NONINIT);
479 
480  for(int j=0; j<N; j++)
481  x.e(j) = e(i,j);
482 
483  return x;
484 
485  }
486 
487  template <int M, int N, class AT>
488  inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::col(int j) const {
489 
490 #ifndef FMATVEC_NO_BOUNDS_CHECK
491  assert(j>=0);
492  assert(j<N);
493 #endif
494 
495  Vector<Fixed<M>,AT> x(NONINIT);
496 
497  for(int i=0; i<M; i++)
498  x.e(i) = e(i,j);
499 
500  return x;
501 
502  }
503 
504  template <int M, int N, class AT>
505  inline const Matrix<General,Fixed<N>,Fixed<M>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::T() const {
506  Matrix<General,Fixed<N>,Fixed<M>,AT> A(NONINIT);
507  for(int i=0; i<N; i++)
508  for(int j=0; j<M; j++)
509  A.e(i,j) = e(j,i);
510  return A;
511  }
512 
513  template <int M, int N, class AT> template <class Row>
514  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::set(int j, const Vector<Row,AT> &x) {
515 #ifndef FMATVEC_NO_BOUNDS_CHECK
516  assert(j<cols());
517  assert(rows()==x.size());
518 #endif
519  for(int i=0; i<rows(); i++)
520  e(i,j) = x.e(i);
521  }
522 
523  template <int M, int N, class AT> template <class Col>
524  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::set(int i, const RowVector<Col,AT> &x) {
525 #ifndef FMATVEC_NO_BOUNDS_CHECK
526  assert(i<rows());
527  assert(cols()==x.size());
528 #endif
529  for(int j=0; j<cols(); j++)
530  e(i,j) = x.e(j);
531  }
532 
533  template <int M, int N, class AT> template<class Type, class Row, class Col>
534  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) {
535 
536 #ifndef FMATVEC_NO_BOUNDS_CHECK
537  assert(I.end()<rows());
538  assert(J.end()<cols());
539  assert(I.size()==A.rows());
540  assert(J.size()==A.cols());
541 #endif
542 
543  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
544  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
545  e(i,j) = A.e(ii,jj);
546  }
547 
548  template <int M, int N, class AT> template <class Row>
549  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::add(int j, const Vector<Row,AT> &x) {
550 #ifndef FMATVEC_NO_BOUNDS_CHECK
551  assert(j<cols());
552  assert(rows()==x.size());
553 #endif
554  for(int i=0; i<rows(); i++)
555  e(i,j) += x.e(i);
556  }
557 
558  template <int M, int N, class AT> template <class Col>
559  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::add(int i, const RowVector<Col,AT> &x) {
560 #ifndef FMATVEC_NO_BOUNDS_CHECK
561  assert(i<rows());
562  assert(cols()==x.size());
563 #endif
564  for(int j=0; j<cols(); j++)
565  e(i,j) += x.e(j);
566  }
567 
568  template <int M, int N, class AT> template<class Type, class Row, class Col>
569  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) {
570 
571 #ifndef FMATVEC_NO_BOUNDS_CHECK
572  assert(I.end()<rows());
573  assert(J.end()<cols());
574  assert(I.size()==A.rows());
575  assert(J.size()==A.cols());
576 #endif
577 
578  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
579  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
580  e(i,j) += A.e(ii,jj);
581  }
582 
583  template <int M, int N, class AT>
584  inline Matrix<General,Fixed<M>,Fixed<N>,AT>::operator std::vector<std::vector<AT> >() {
585  std::vector<std::vector<AT> > ret(rows());
586  for(int r=0; r<rows(); r++) {
587  ret[r].resize(cols());
588  for(int c=0; c<cols(); c++)
589  ret[r][c]=e(r,c);
590  }
591  return ret;
592  }
593 
594  template <int M, int N, class AT>
595  inline Matrix<General,Fixed<M>,Fixed<N>,AT>::Matrix(std::vector<std::vector<AT> > m) {
596 #ifndef FMATVEC_NO_SIZE_CHECK
597  assert(m.size() == M);
598  assert(m[0].size() == N);
599 #endif
600  for(int r=0; r<rows(); r++) {
601  assert(m[r].size()==cols());
602  for(int c=0; c<cols(); c++)
603  e(r,c)=m[r][c];
604  }
605  }
606 
608 
609  template <int M, int N, class AT> template <class Type, class Row, class Col>
610  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) {
611  for(int i=0; i<M; i++)
612  for(int j=0; j<N; j++)
613  e(i,j) = A.e(i,j);
614  }
615 
616  template<int M, int N, class AT> template<class Row>
617  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::deepCopy(const Matrix<Symmetric,Row,Row,AT> &A) {
618  for(int i=0; i<A.size(); i++) {
619  e(i,i) = A.ej(i,i);
620  for(int j=i+1; j<A.size(); j++)
621  e(i,j) = e(j,i) = A.ej(i,j);
622  }
623  }
624 
626 
634  template<int M>
635  class Matrix<Rotation,Fixed<M>,Fixed<M>,double> : public Matrix<General,Fixed<M>,Fixed<M>,double> {
636  public:
637  // Constructors are not inherited. Hence we must redefine all ctors here.
638 
639  Matrix(Noinit ini) { }
640  Matrix(Init ini=INIT, const double &a=0) { this->init(a); }
641  Matrix(Eye ini, const double &a=1) { this->init(ini,a); }
642  Matrix(int m, int n, Noinit ini) { }
643  Matrix(int m, int n, Init ini, const double &a=0) { this->init(a); }
644  Matrix(int m, int n, Eye ini, const double &a=1) { this->init(ini,a); }
645  explicit Matrix(const Matrix<General,Fixed<M>,Fixed<M>,double>& A) { this->deepCopy(A); }
646 
647  template<class Row>
649 #ifndef FMATVEC_NO_SIZE_CHECK
650  assert(A.size() == M);
651 #endif
652  this->deepCopy(A);
653  }
654 
655  template<class Type, class Row, class Col>
656  explicit Matrix(const Matrix<Type,Row,Col,double> &A) {
657 #ifndef FMATVEC_NO_SIZE_CHECK
658  assert(A.rows() == M);
659  assert(A.cols() == M);
660 #endif
661  this->deepCopy(A);
662  }
663 
664  // TODO: we should add some overloaded member functions here which capitalize the special
665  // properties of rotation matrices, link "RotMat inv() { return trans(*this); }"
666  };
667 
668 
669 }
670 
671 #endif
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:135
Definition: types.h:68
const AT * operator()() const
Pointer operator.
Definition: fixed_general_matrix.h:169
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:56
AT * operator()()
Pointer operator.
Definition: fixed_general_matrix.h:163
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:85
Definition: matrix.h:39
Definition: types.h:65
Definition: matrix.h:218
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:206
int cols() const
Number of columns.
Definition: fixed_general_matrix.h:181
Definition: matrix.h:215
const AT & e(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:154
int rows() const
Number of rows.
Definition: fixed_general_matrix.h:175
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:195
int ldim() const
Leading dimension.
Definition: fixed_general_matrix.h:187
Definition: matrix.h:38
AT & operator()(int i, int j)
Element operator.
Definition: fixed_general_matrix.h:120
Basic shape class for matrices.
Definition: types.h:100
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