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 <cstdlib>
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 value_type;
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 std::string &strs);
109  Matrix(const char *strs);
110 
111  template <class Type, class Row, class Col>
112  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
124  AT& operator()(int i, int j) {
125 #ifndef FMATVEC_NO_BOUNDS_CHECK
126  assert(i>=0);
127  assert(j>=0);
128  assert(i<M);
129  assert(j<N);
130 #endif
131 
132  return e(i,j);
133  };
134 
139  const AT& operator()(int i, int j) const {
140 #ifndef FMATVEC_NO_BOUNDS_CHECK
141  assert(i>=0);
142  assert(j>=0);
143  assert(i<M);
144  assert(j<N);
145 #endif
146 
147  return e(i,j);
148  };
149 
150  AT& e(int i, int j) {
151  return ele[i][j];
152  };
153 
158  const AT& e(int i, int j) const {
159  return ele[i][j];
160  };
161 
167  AT* operator()() {return &(ele[0][0]);};
168 
173  const AT* operator()() const {return &(ele[0][0]);};
174 
179  int rows() const {return M;};
180 
185  int cols() const {return N;};
186 
191  int ldim() const {return N;};
192 
194  bool transposed() const {
195  return true;
196  };
197 
200  void resize(int m, int n) {
201  if(m!=M || n!=N)
202  throw std::runtime_error("A fixed matrix cannot be resized.");
203  }
204 
211  const CBLAS_TRANSPOSE blasTrans() const {
212  return CblasNoTrans;
213  };
214 
222  const CBLAS_ORDER blasOrder() const {
223  return CblasRowMajor;
224  };
225 
226 // /*! \brief Submatrix operator.
227 // *
228 // * Returns a submatrix of the calling matrix.
229 // * \attention The submatrix and the
230 // * calling matrix will share the same physical memory.
231 // * \param i1 The starting row.
232 // * \param j1 The starting column.
233 // * \param i2 The ending row.
234 // * \param j2 The ending column.
235 // * \return A submatrix of the calling matrix.
236 // * */
237 // inline Matrix<General, AT> operator()(int i1, int j1, int i2, int j2);
238 //
239 // /*! \brief Submatrix operator.
240 // *
241 // * See operator()(int,int,int,int);
242 // * */
243 // inline const Matrix<General, AT> operator()(int i1, int j1, int i2, int j2) const;
244 
271 // inline Matrix<General, Var, AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J);
272 
277  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
278 
279  template <int M1, int M2, int N1, int N2>
280  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;
281  template <int M1, int M2>
282  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2> > &I, const Range<Var,Var > &J) const;
283 
284  template <int N1, int N2>
285  inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> operator()(const Range<Var,Var> &I, const Range<Fixed<N1>,Fixed<N2> > &J) const;
286 
287  inline const RowVector<Fixed<N>,AT> row(int i) const;
288  inline const Vector<Fixed<M>,AT> col(int j) const;
289 
297  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(const AT &val=0);
298  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Init, const AT &a=0) { return init(a); };
299  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Eye, const AT &val=1);
300  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Noinit, const AT &a=0) { return *this; }
301 
306  inline operator std::vector<std::vector<AT> >() const;
307 
313  Matrix(std::vector<std::vector<AT> > m);
314 
315  inline const Matrix<General,Fixed<N>,Fixed<M>,AT> T() const;
316 
320  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
321 
325  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
326 
333  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);
334 
338  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
339 
343  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
344 
345  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);
346 
347  };
348 
349  template <int M, int N, class AT>
350  Matrix<General,Fixed<M>,Fixed<N>,AT>::Matrix(const std::string &strs) {
351  std::istringstream iss(strs);
352  iss>>*this;
353 
354  // check end of stream
355  iss>>std::ws;
356  if(!iss.eof())
357  throw std::runtime_error("Input not fully read.");
358  }
359  template <int M, int N, class AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::Matrix(const char *strs) :
360  Matrix<General,Fixed<M>,Fixed<N>,AT>::Matrix(std::string(strs)) {}
361 
362  template <int M, int N, class AT> template<class Type, class Row, class Col>
363  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::operator=(const Matrix<Type,Row,Col,AT> &A) {
364 
365 #ifndef FMATVEC_NO_SIZE_CHECK
366  assert(A.rows() == M);
367  assert(A.cols() == N);
368 #endif
369 
370  deepCopy(A);
371 
372  return *this;
373  }
374 
375  template <int M, int N, class AT>
376  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::init(const AT &val) {
377  for(int i=0; i<M; i++)
378  for(int j=0; j<N; j++)
379  e(i,j) = val;
380  return *this;
381  }
382 
383  template <int M, int N, class AT>
384  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::init(Eye, const AT &val) {
385  for(int i=0; i<M; i++)
386  for(int j=0; j<N; j++)
387  e(i,j) = (i==j) ? val : 0;
388  return *this;
389  }
390 
391  template <int M, int N, class AT>
392  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 {
393 #ifndef FMATVEC_NO_BOUNDS_CHECK
394  assert(I.end()<M);
395  assert(J.end()<N);
396 #endif
397  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
398 
399  for(int i=0; i<A.rows(); i++)
400  for(int j=0; j<A.cols(); j++)
401  A.e(i,j) = e(I.start()+i,J.start()+j);
402 
403  return A;
404  }
405 
406  template <int M, int N, class AT> template <int M1, int M2, int N1, int N2>
407  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 {
408 #ifndef FMATVEC_NO_BOUNDS_CHECK
409  assert(M2<M);
410  assert(N2<N);
411 #endif
412  Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> A(NONINIT);
413 
414  for(int i=0; i<A.rows(); i++)
415  for(int j=0; j<A.cols(); j++)
416  A.e(i,j) = e(M1+i,J.start()+j);
417 
418  return A;
419  }
420 
421  template <int M, int N, class AT> template <int M1, int M2>
422  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 {
423 #ifndef FMATVEC_NO_BOUNDS_CHECK
424  assert(M2<M);
425  assert(J.end()<N);
426 #endif
427  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
428 
429  for(int i=0; i<A.rows(); i++)
430  for(int j=0; j<A.cols(); j++)
431  A.e(i,j) = e(I.start()+i,J.start()+j);
432 
433  return A;
434  }
435 
436  template <int M, int N, class AT> template <int N1, int N2>
437  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 {
438 #ifndef FMATVEC_NO_BOUNDS_CHECK
439  assert(I.end()<M);
440  assert(N2<N);
441 #endif
442  Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.start()+1,NONINIT);
443 
444  for(int i=0; i<A.rows(); i++)
445  for(int j=0; j<A.cols(); j++)
446  A.e(i,j) = e(I.start()+i,J.start()+j);
447 
448  return A;
449  }
450 
451  template <int M, int N, class AT>
452  inline const RowVector<Fixed<N>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::row(int i) const {
453 
454 #ifndef FMATVEC_NO_BOUNDS_CHECK
455  assert(i>=0);
456  assert(i<M);
457 #endif
458 
459  RowVector<Fixed<N>,AT> x(NONINIT);
460 
461  for(int j=0; j<N; j++)
462  x.e(j) = e(i,j);
463 
464  return x;
465 
466  }
467 
468  template <int M, int N, class AT>
469  inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::col(int j) const {
470 
471 #ifndef FMATVEC_NO_BOUNDS_CHECK
472  assert(j>=0);
473  assert(j<N);
474 #endif
475 
476  Vector<Fixed<M>,AT> x(NONINIT);
477 
478  for(int i=0; i<M; i++)
479  x.e(i) = e(i,j);
480 
481  return x;
482 
483  }
484 
485  template <int M, int N, class AT>
486  inline const Matrix<General,Fixed<N>,Fixed<M>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::T() const {
487  Matrix<General,Fixed<N>,Fixed<M>,AT> A(NONINIT);
488  for(int i=0; i<N; i++)
489  for(int j=0; j<M; j++)
490  A.e(i,j) = e(j,i);
491  return A;
492  }
493 
494  template <int M, int N, class AT> template <class Row>
495  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::set(int j, const Vector<Row,AT> &x) {
496 #ifndef FMATVEC_NO_BOUNDS_CHECK
497  assert(j<cols());
498  assert(rows()==x.size());
499 #endif
500  for(int i=0; i<rows(); i++)
501  e(i,j) = x.e(i);
502  }
503 
504  template <int M, int N, class AT> template <class Col>
505  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::set(int i, const RowVector<Col,AT> &x) {
506 #ifndef FMATVEC_NO_BOUNDS_CHECK
507  assert(i<rows());
508  assert(cols()==x.size());
509 #endif
510  for(int j=0; j<cols(); j++)
511  e(i,j) = x.e(j);
512  }
513 
514  template <int M, int N, class AT> template<class Type, class Row, class Col>
515  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) {
516 
517 #ifndef FMATVEC_NO_BOUNDS_CHECK
518  assert(I.end()<rows());
519  assert(J.end()<cols());
520  assert(I.size()==A.rows());
521  assert(J.size()==A.cols());
522 #endif
523 
524  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
525  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
526  e(i,j) = A.e(ii,jj);
527  }
528 
529  template <int M, int N, class AT> template <class Row>
530  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::add(int j, const Vector<Row,AT> &x) {
531 #ifndef FMATVEC_NO_BOUNDS_CHECK
532  assert(j<cols());
533  assert(rows()==x.size());
534 #endif
535  for(int i=0; i<rows(); i++)
536  e(i,j) += x.e(i);
537  }
538 
539  template <int M, int N, class AT> template <class Col>
540  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::add(int i, const RowVector<Col,AT> &x) {
541 #ifndef FMATVEC_NO_BOUNDS_CHECK
542  assert(i<rows());
543  assert(cols()==x.size());
544 #endif
545  for(int j=0; j<cols(); j++)
546  e(i,j) += x.e(j);
547  }
548 
549  template <int M, int N, class AT> template<class Type, class Row, class Col>
550  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) {
551 
552 #ifndef FMATVEC_NO_BOUNDS_CHECK
553  assert(I.end()<rows());
554  assert(J.end()<cols());
555  assert(I.size()==A.rows());
556  assert(J.size()==A.cols());
557 #endif
558 
559  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
560  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
561  e(i,j) += A.e(ii,jj);
562  }
563 
564  template <int M, int N, class AT>
565  inline Matrix<General,Fixed<M>,Fixed<N>,AT>::operator std::vector<std::vector<AT> >() const {
566  std::vector<std::vector<AT> > ret(rows());
567  for(int r=0; r<rows(); r++) {
568  ret[r].resize(cols());
569  for(int c=0; c<cols(); c++)
570  ret[r][c]=e(r,c);
571  }
572  return ret;
573  }
574 
575  template <int M, int N, class AT>
576  inline Matrix<General,Fixed<M>,Fixed<N>,AT>::Matrix(std::vector<std::vector<AT> > m) {
577  if(m.size() != M)
578  throw std::runtime_error("The input has "+std::to_string(m.size())+" rows but "+std::to_string(M)+" rows are required.");
579  if(m[0].size() != N)
580  throw std::runtime_error("The input has "+std::to_string(m[0].size())+" columne but "+std::to_string(N)+" columns are required.");
581  for(int r=0; r<rows(); r++) {
582  if(static_cast<int>(m[r].size())!=cols())
583  throw std::runtime_error("The rows of the input have different length.");
584  for(int c=0; c<cols(); c++)
585  e(r,c)=m[r][c];
586  }
587  }
588 
590 
591  template <int M, int N, class AT> template <class Type, class Row, class Col>
592  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) {
593  for(int i=0; i<M; i++)
594  for(int j=0; j<N; j++)
595  e(i,j) = A.e(i,j);
596  }
597 
598  template<int M, int N, class AT> template<class Row>
599  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::deepCopy(const Matrix<Symmetric,Row,Row,AT> &A) {
600  for(int i=0; i<A.size(); i++) {
601  e(i,i) = A.ej(i,i);
602  for(int j=i+1; j<A.size(); j++)
603  e(i,j) = e(j,i) = A.ej(i,j);
604  }
605  }
606 
608 
616  template<int M>
617  class Matrix<Rotation,Fixed<M>,Fixed<M>,double> : public Matrix<General,Fixed<M>,Fixed<M>,double> {
618  public:
619  typedef double value_type;
620  // Constructors are not inherited. Hence we must redefine all ctors here.
621 
622  Matrix(Noinit ini) { }
623  Matrix(Init ini=INIT, const double &a=0) { this->init(a); }
624  Matrix(Eye ini, const double &a=1) { this->init(ini,a); }
625  Matrix(int m, int n, Noinit ini) { }
626  Matrix(int m, int n, Init ini, const double &a=0) { this->init(a); }
627  Matrix(int m, int n, Eye ini, const double &a=1) { this->init(ini,a); }
628  explicit Matrix(const Matrix<General,Fixed<M>,Fixed<M>,double>& A) { this->deepCopy(A); }
629 
630  template<class Row>
632 #ifndef FMATVEC_NO_SIZE_CHECK
633  assert(A.size() == M);
634 #endif
635  this->deepCopy(A);
636  }
637 
638  template<class Type, class Row, class Col>
639  explicit Matrix(const Matrix<Type,Row,Col,double> &A) {
640 #ifndef FMATVEC_NO_SIZE_CHECK
641  assert(A.rows() == M);
642  assert(A.cols() == M);
643 #endif
644  this->deepCopy(A);
645  }
646 
647  // TODO: we should add some overloaded member functions here which capitalize the special
648  // properties of rotation matrices, link "RotMat inv() { return trans(*this); }"
649  };
650 
651 
652 }
653 
654 #endif
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:139
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:194
const AT * operator()() const
Pointer operator.
Definition: fixed_general_matrix.h:173
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT * operator()()
Pointer operator.
Definition: fixed_general_matrix.h:167
This is a matrix class for general matrices.
Definition: var_general_matrix.h:39
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:90
Definition: matrix.h:43
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:222
int cols() const
Number of columns.
Definition: fixed_general_matrix.h:185
Definition: fmatvec.h:47
const AT & e(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:158
int rows() const
Number of rows.
Definition: fixed_general_matrix.h:179
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:211
int ldim() const
Leading dimension.
Definition: fixed_general_matrix.h:191
Definition: matrix.h:42
AT & operator()(int i, int j)
Element operator.
Definition: fixed_general_matrix.h:124
Basic shape class for matrices.
Definition: types.h:100
void resize(int m, int n)
Definition: fixed_general_matrix.h:200
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:44

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home