fmatvec  2.0.0
Fast Matrix/Vector Library
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 Matrix<General,Fixed<M>,Fixed<N>,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
52  template<class Row> inline Matrix<General,Fixed<M>,Fixed<N>,AT>& copy(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  explicit Matrix(Noinit) { }
69  explicit Matrix(Init ini=INIT, const AT &a=AT()) { init(a); }
70  explicit Matrix(Eye ini, const AT &a=1) { init(ini,a); }
71  explicit Matrix(int m, int n, Noinit) { FMATVEC_ASSERT(m==M && n==N, AT); }
72  explicit Matrix(int m, int n, Init ini=INIT, const AT &a=AT()) { FMATVEC_ASSERT(m==M && n==N, AT); init(a); }
73  explicit Matrix(int m, int n, Eye ini, const AT &a=1) { FMATVEC_ASSERT(m==M && n==N, AT); init(ini,a); }
74 
80  Matrix(const Matrix<General,Fixed<M>,Fixed<N>,AT> &A) = default;
81 
87  template<class Row, class Col>
89  FMATVEC_ASSERT(A.rows() == M, AT);
90  FMATVEC_ASSERT(A.cols() == N, AT);
91  copy(A);
92  }
93 
99  template<class Type, class Row, class Col>
100  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) {
101  FMATVEC_ASSERT(A.rows() == M, AT);
102  FMATVEC_ASSERT(A.cols() == N, AT);
103  copy(A);
104  }
105 
118  Matrix(const std::string &strs);
119  Matrix(const char *strs);
120 
127  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& operator=(const Matrix<General,Fixed<M>,Fixed<N>,AT> &A) = default;
128 
135  template <class Type, class Row, class Col>
137  FMATVEC_ASSERT(A.rows() == M, AT);
138  FMATVEC_ASSERT(A.cols() == N, AT);
139  return copy(A);
140  }
141 
148  template<class Type, class Row, class Col>
149  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) { return operator=(A); }
150 
151  template <class AT2>
152  operator Matrix<General,Fixed<M>,Fixed<N>,AT2>() const {
154  for(size_t r=0; r<M; ++r)
155  for(size_t c=0; c<N; ++c)
156  ret(r,c) = (*this)(r,c);
157  return ret;
158  }
159 
169  AT& operator()(int i, int j) {
170  FMATVEC_ASSERT(i>=0, AT);
171  FMATVEC_ASSERT(j>=0, AT);
172  FMATVEC_ASSERT(i<M, AT);
173  FMATVEC_ASSERT(j<N, AT);
174 
175  return e(i,j);
176  }
177 
182  const AT& operator()(int i, int j) const {
183  FMATVEC_ASSERT(i>=0, AT);
184  FMATVEC_ASSERT(j>=0, AT);
185  FMATVEC_ASSERT(i<M, AT);
186  FMATVEC_ASSERT(j<N, AT);
187 
188  return e(i,j);
189  }
190 
191  AT& e(int i, int j) {
192  return ele[i][j];
193  }
194 
199  const AT& e(int i, int j) const {
200  return ele[i][j];
201  }
202 
208  AT* operator()() {return &(ele[0][0]);}
209 
214  const AT* operator()() const {return &(ele[0][0]);}
215 
220  int rows() const {return M;}
221 
226  int cols() const {return N;}
227 
232  int ldim() const {return N;}
233 
236  void resize(int m, int n) {
237  if(m!=M || n!=N)
238  throw std::runtime_error("A fixed matrix cannot be resized.");
239  }
240 
247  const CBLAS_TRANSPOSE blasTrans() const {
248  return CblasNoTrans;
249  }
250 
258  const CBLAS_ORDER blasOrder() const {
259  return CblasRowMajor;
260  }
261 
266  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
267 
268  template <int M1, int M2, int N1, int N2>
269  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;
270  template <int M1, int M2>
271  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2>> &I, const Range<Var,Var > &J) const;
272 
273  template <int N1, int N2>
274  inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> operator()(const Range<Var,Var> &I, const Range<Fixed<N1>,Fixed<N2>> &J) const;
275 
276  inline const RowVector<Fixed<N>,AT> row(int i) const;
277  inline const Vector<Fixed<M>,AT> col(int j) const;
278 
286  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(const AT &val=AT());
287  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Init, const AT &a=AT()) { return init(a); }
288  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Eye, const AT &val=1);
289  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Noinit, const AT &a=AT()) { return *this; }
290 
295  explicit inline operator std::vector<std::vector<AT>>() const;
296 
302  explicit Matrix(const std::vector<std::vector<AT>> &m);
303 
304 // /*! \brief Cast to AT.
305 // *
306 // * \return The AT representation of the matrix
307 // * */
308 // explicit operator AT() const {
309 // FMATVEC_ASSERT(M==1, AT);
310 // FMATVEC_ASSERT(N==1, AT);
311 // return ele[0][0];
312 // }
313 //
314 // /*! \brief AT Constructor.
315 // * Constructs and initializes a matrix with a AT object.
316 // * \param v The AT the matrix will be initialized with.
317 // * */
318 // explicit Matrix(const AT &x) {
319 // FMATVEC_ASSERT(M==1, AT);
320 // FMATVEC_ASSERT(N==1, AT);
321 // ele[0][0] = x;
322 // }
323 
324  inline const Matrix<General,Fixed<N>,Fixed<M>,AT> T() const;
325 
329  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
330 
334  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
335 
342  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);
343 
347  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
348 
352  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
353 
354  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);
355  };
356 
357  template <int M, int N, class AT>
358  Matrix<General,Fixed<M>,Fixed<N>,AT>::Matrix(const std::string &strs) {
359  std::istringstream iss(strs);
360  iss>>*this;
361 
362  // check end of stream
363  iss>>std::ws;
364  if(!iss.eof())
365  throw std::runtime_error("Input not fully read.");
366  }
367  template <int M, int N, class AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::Matrix(const char *strs) :
368  Matrix<General,Fixed<M>,Fixed<N>,AT>::Matrix(std::string(strs)) {}
369 
370  template <int M, int N, class AT>
371  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::init(const AT &val) {
372  for(int i=0; i<M; i++)
373  for(int j=0; j<N; j++)
374  e(i,j) = val;
375  return *this;
376  }
377 
378  template <int M, int N, class AT>
379  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::init(Eye, const AT &val) {
380  for(int i=0; i<M; i++)
381  for(int j=0; j<N; j++)
382  e(i,j) = (i==j) ? val : 0;
383  return *this;
384  }
385 
386  template <int M, int N, class AT>
387  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 {
388  FMATVEC_ASSERT(I.end()<M, AT);
389  FMATVEC_ASSERT(J.end()<N, AT);
390  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
391 
392  for(int i=0; i<A.rows(); i++)
393  for(int j=0; j<A.cols(); j++)
394  A.e(i,j) = e(I.start()+i,J.start()+j);
395 
396  return A;
397  }
398 
399  template <int M, int N, class AT> template <int M1, int M2, int N1, int N2>
400  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 {
401  FMATVEC_ASSERT(M2<M, AT);
402  FMATVEC_ASSERT(N2<N, AT);
403  Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> A(NONINIT);
404 
405  for(int i=0; i<A.rows(); i++)
406  for(int j=0; j<A.cols(); j++)
407  A.e(i,j) = e(M1+i,J.start()+j);
408 
409  return A;
410  }
411 
412  template <int M, int N, class AT> template <int M1, int M2>
413  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 {
414  FMATVEC_ASSERT(M2<M, AT);
415  FMATVEC_ASSERT(J.end()<N, AT);
416  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(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 N1, int N2>
426  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 {
427  FMATVEC_ASSERT(I.end()<M, AT);
428  FMATVEC_ASSERT(N2<N, AT);
429  Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.start()+1,NONINIT);
430 
431  for(int i=0; i<A.rows(); i++)
432  for(int j=0; j<A.cols(); j++)
433  A.e(i,j) = e(I.start()+i,J.start()+j);
434 
435  return A;
436  }
437 
438  template <int M, int N, class AT>
439  inline const RowVector<Fixed<N>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::row(int i) const {
440 
441  FMATVEC_ASSERT(i>=0, AT);
442  FMATVEC_ASSERT(i<M, AT);
443 
444  RowVector<Fixed<N>,AT> x(NONINIT);
445 
446  for(int j=0; j<N; j++)
447  x.e(j) = e(i,j);
448 
449  return x;
450  }
451 
452  template <int M, int N, class AT>
453  inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::col(int j) const {
454 
455  FMATVEC_ASSERT(j>=0, AT);
456  FMATVEC_ASSERT(j<N, AT);
457 
458  Vector<Fixed<M>,AT> x(NONINIT);
459 
460  for(int i=0; i<M; i++)
461  x.e(i) = e(i,j);
462 
463  return x;
464  }
465 
466  template <int M, int N, class AT>
468  Matrix<General,Fixed<N>,Fixed<M>,AT> A(NONINIT);
469  for(int i=0; i<N; i++)
470  for(int j=0; j<M; j++)
471  A.e(i,j) = e(j,i);
472  return A;
473  }
474 
475  template <int M, int N, class AT> template <class Row>
476  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::set(int j, const Vector<Row,AT> &x) {
477  FMATVEC_ASSERT(j<cols(), AT);
478  FMATVEC_ASSERT(rows()==x.size(), AT);
479  for(int i=0; i<rows(); i++)
480  e(i,j) = x.e(i);
481  }
482 
483  template <int M, int N, class AT> template <class Col>
484  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::set(int i, const RowVector<Col,AT> &x) {
485  FMATVEC_ASSERT(i<rows(), AT);
486  FMATVEC_ASSERT(cols()==x.size(), AT);
487  for(int j=0; j<cols(); j++)
488  e(i,j) = x.e(j);
489  }
490 
491  template <int M, int N, class AT> template<class Type, class Row, class Col>
492  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) {
493 
494  FMATVEC_ASSERT(I.end()<rows(), AT);
495  FMATVEC_ASSERT(J.end()<cols(), AT);
496  FMATVEC_ASSERT(I.size()==A.rows(), AT);
497  FMATVEC_ASSERT(J.size()==A.cols(), AT);
498 
499  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
500  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
501  e(i,j) = A.e(ii,jj);
502  }
503 
504  template <int M, int N, class AT> template <class Row>
505  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::add(int j, const Vector<Row,AT> &x) {
506  FMATVEC_ASSERT(j<cols(), AT);
507  FMATVEC_ASSERT(rows()==x.size(), AT);
508  for(int i=0; i<rows(); i++)
509  e(i,j) += x.e(i);
510  }
511 
512  template <int M, int N, class AT> template <class Col>
513  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::add(int i, const RowVector<Col,AT> &x) {
514  FMATVEC_ASSERT(i<rows(), AT);
515  FMATVEC_ASSERT(cols()==x.size(), AT);
516  for(int j=0; j<cols(); j++)
517  e(i,j) += x.e(j);
518  }
519 
520  template <int M, int N, class AT> template<class Type, class Row, class Col>
521  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) {
522 
523  FMATVEC_ASSERT(I.end()<rows(), AT);
524  FMATVEC_ASSERT(J.end()<cols(), AT);
525  FMATVEC_ASSERT(I.size()==A.rows(), AT);
526  FMATVEC_ASSERT(J.size()==A.cols(), AT);
527 
528  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
529  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
530  e(i,j) += A.e(ii,jj);
531  }
532 
533  template <int M, int N, class AT>
534  inline Matrix<General,Fixed<M>,Fixed<N>,AT>::operator std::vector<std::vector<AT>>() const {
535  std::vector<std::vector<AT>> ret(rows());
536  for(int r=0; r<rows(); r++) {
537  ret[r].resize(cols());
538  for(int c=0; c<cols(); c++)
539  ret[r][c]=e(r,c);
540  }
541  return ret;
542  }
543 
544  template <int M, int N, class AT>
545  inline Matrix<General,Fixed<M>,Fixed<N>,AT>::Matrix(const std::vector<std::vector<AT>> &m) {
546  if(m.size() != M)
547  throw std::runtime_error("The input has "+std::to_string(m.size())+" rows but "+std::to_string(M)+" rows are required.");
548  if(m[0].size() != N)
549  throw std::runtime_error("The input has "+std::to_string(m[0].size())+" columne but "+std::to_string(N)+" columns are required.");
550  for(int r=0; r<rows(); r++) {
551  if(static_cast<int>(m[r].size())!=cols())
552  throw std::runtime_error("The rows of the input have different length.");
553  for(int c=0; c<cols(); c++)
554  e(r,c)=m[r][c];
555  }
556  }
557 
559 
560  template <int M, int N, class AT> template <class Type, class Row, class Col>
562  for(int i=0; i<M; i++)
563  for(int j=0; j<N; j++)
564  e(i,j) = A.e(i,j);
565  return *this;
566  }
567 
568  template<int M, int N, class AT> template<class Row>
569  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::copy(const Matrix<Symmetric,Row,Row,AT> &A) {
570  for(int i=0; i<A.size(); i++) {
571  e(i,i) = A.ej(i,i);
572  for(int j=i+1; j<A.size(); j++)
573  e(i,j) = e(j,i) = A.ej(i,j);
574  }
575  return *this;
576  }
577 
579 
587  template<int M, class AT>
588  class Matrix<Rotation,Fixed<M>,Fixed<M>,AT> : public Matrix<General,Fixed<M>,Fixed<M>,AT> {
589  public:
590  typedef AT value_type;
591  // Constructors are not inherited. Hence we must redefine all ctors here.
592 
593  Matrix(Noinit ini) { }
594  Matrix(Init ini=INIT, const AT &a=0) { this->init(a); }
595  Matrix(Eye ini, const AT &a=1) { this->init(ini,a); }
596 
597  Matrix(int m, int n, Noinit ini) { }
598  Matrix(int m, int n, Init ini, const AT &a=0) { this->init(a); }
599  Matrix(int m, int n, Eye ini, const AT &a=1) { this->init(ini,a); }
600 
601  Matrix(const Matrix<Rotation,Fixed<M>,Fixed<M>,AT> &A) = default;
602 
603  explicit Matrix(const Matrix<General,Fixed<M>,Fixed<M>,AT> &A) : Matrix<General,Fixed<M>,Fixed<M>,AT>(A) { }
604 
605  template<class Row>
607 
608  template<class Type, class Row, class Col>
609  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<M>,Fixed<M>,AT>(A) { }
610 
611  explicit Matrix(const std::vector<std::vector<AT>> &m) : Matrix<General,Fixed<M>,Fixed<M>,AT>(m) { }
612 
613  inline const Matrix<Rotation,Fixed<M>,Fixed<M>,AT> T() const;
614 
615  using Matrix<General,Fixed<M>,Fixed<M>,AT>::e;
616 
617  // TODO: we should add some overloaded member functions here which capitalize the special
618  // properties of rotation matrices, link "RotMat inv() { return trans(*this); }"
619  };
620 
621  template <int M, class AT>
622  inline const Matrix<Rotation,Fixed<M>,Fixed<M>,AT> Matrix<Rotation,Fixed<M>,Fixed<M>,AT>::T() const {
623  Matrix<Rotation,Fixed<M>,Fixed<M>,AT> A(NONINIT);
624  for(int i=0; i<M; i++)
625  for(int j=0; j<M; j++)
626  A.e(i,j) = e(j,i);
627  return A;
628  }
629 
630 }
631 
632 #endif
int end() const
Last element.
Definition: range.h:91
Matrix< General, Fixed< M >, Fixed< N >, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: fixed_general_matrix.h:136
Definition: fmatvec.h:38
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:182
AT * operator()()
Pointer operator.
Definition: fixed_general_matrix.h:208
int ldim() const
Leading dimension.
Definition: fixed_general_matrix.h:232
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
Definition: types.h:85
Definition: types.h:97
Definition: fmatvec.h:50
Shape class for rotation matrices.
Definition: types.h:156
int start() const
First element.
Definition: range.h:85
Definition: fmatvec.h:47
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_general_matrix.h:100
int cols() const
Number of columns.
const AT & e(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:199
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_general_matrix.h:247
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_general_matrix.h:258
This is an index class for creating submatrices.
Definition: range.h:35
Namespace fmatvec.
Definition: _memory.cc:28
int size() const
Size.
Definition: range.h:97
int cols() const
Number of columns.
Definition: fixed_general_matrix.h:226
int rows() const
Number of rows.
Definition: fixed_general_matrix.h:220
const AT * operator()() const
Pointer operator.
Definition: fixed_general_matrix.h:214
Definition: types.h:84
AT & operator()(int i, int j)
Element operator.
Definition: fixed_general_matrix.h:169
Basic shape class for matrices.
Definition: types.h:132
void resize(int m, int n)
Definition: fixed_general_matrix.h:236
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_general_matrix.h:88
int rows() const
Number of rows.
This is an index class for creating submatrices.
Definition: range.h:44
Definition: types.h:86