fmatvec  0.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  static constexpr bool isVector {false};
43 
44  typedef AT* iterator;
45  typedef const AT* const_iterator;
46  typedef AT value_type;
47  typedef General shape_type;
48 
50 
51  protected:
52 
53  AT ele[M][N];
54 
55  template <class Type, class Row, class Col> inline Matrix<General,Fixed<M>,Fixed<N>,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
56  template<class Row> inline Matrix<General,Fixed<M>,Fixed<N>,AT>& copy(const Matrix<Symmetric,Row,Row,AT> &A);
57 
59 
60  public:
61 
62 // Works with -std=gnu++0x only
63 // template<class Ini=All<AT>>
64 // Matrix(Ini ini=All<AT>()) {
65 // init(ini);
66 // }
67 // template<class Ini=All<AT>>
68 // Matrix(int m_, int n_, Ini ini=All<AT>()) {
69 // init(ini);
70 // }
71 
72  explicit Matrix(Noinit) { }
73  explicit Matrix(Init ini=INIT, const AT &a=AT()) { init(a); }
74  explicit Matrix(Eye ini, const AT &a=1) { init(ini,a); }
75  explicit Matrix(int m, int n, Noinit) { FMATVEC_ASSERT(m==M && n==N, AT); }
76  explicit Matrix(int m, int n, Init ini=INIT, const AT &a=AT()) { FMATVEC_ASSERT(m==M && n==N, AT); init(a); }
77  explicit Matrix(int m, int n, Eye ini, const AT &a=1) { FMATVEC_ASSERT(m==M && n==N, AT); init(ini,a); }
78 
84  Matrix(const Matrix<General,Fixed<M>,Fixed<N>,AT> &A) = default;
85 
91  template<class Row, class Col>
93  FMATVEC_ASSERT(A.rows() == M, AT);
94  FMATVEC_ASSERT(A.cols() == N, AT);
95  copy(A);
96  }
97 
103  template<class Type, class Row, class Col>
104  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) {
105  FMATVEC_ASSERT(A.rows() == M, AT);
106  FMATVEC_ASSERT(A.cols() == N, AT);
107  copy(A);
108  }
109 
122  Matrix(const std::string &strs);
123  Matrix(const char *strs);
124 
131  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& operator=(const Matrix<General,Fixed<M>,Fixed<N>,AT> &A) = default;
132 
139  template <class Type, class Row, class Col>
141  FMATVEC_ASSERT(A.rows() == M, AT);
142  FMATVEC_ASSERT(A.cols() == N, AT);
143  return copy(A);
144  }
145 
152  template<class Type, class Row, class Col>
153  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) { return operator=(A); }
154 
155  template <class AT2>
156  operator Matrix<General,Fixed<M>,Fixed<N>,AT2>() const {
158  for(size_t r=0; r<M; ++r)
159  for(size_t c=0; c<N; ++c)
160  ret(r,c) = (*this)(r,c);
161  return ret;
162  }
163 
164  iterator begin() { return &ele[0][0]; }
165  iterator end() { return &ele[M-1][N]; }
166  const_iterator begin() const { return &ele[0][0]; }
167  const_iterator end() const { return &ele[M-1][N]; }
168 
178  AT& operator()(int i, int j) {
179  FMATVEC_ASSERT(i>=0, AT);
180  FMATVEC_ASSERT(j>=0, AT);
181  FMATVEC_ASSERT(i<M, AT);
182  FMATVEC_ASSERT(j<N, AT);
183 
184  return e(i,j);
185  }
186 
191  const AT& operator()(int i, int j) const {
192  FMATVEC_ASSERT(i>=0, AT);
193  FMATVEC_ASSERT(j>=0, AT);
194  FMATVEC_ASSERT(i<M, AT);
195  FMATVEC_ASSERT(j<N, AT);
196 
197  return e(i,j);
198  }
199 
200  AT& e(int i, int j) {
201  return ele[i][j];
202  }
203 
208  const AT& e(int i, int j) const {
209  return ele[i][j];
210  }
211 
217  AT* operator()() {return &(ele[0][0]);}
218 
223  const AT* operator()() const {return &(ele[0][0]);}
224 
229  constexpr int rows() const {return M;}
230 
235  constexpr int cols() const {return N;}
236 
241  int ldim() const {return N;}
242 
245  void resize(int m, int n) {
246  if(m!=M || n!=N)
247  throw std::runtime_error("A fixed matrix cannot be resized.");
248  }
249 
256  const CBLAS_TRANSPOSE blasTrans() const {
257  return CblasNoTrans;
258  }
259 
267  const CBLAS_ORDER blasOrder() const {
268  return CblasRowMajor;
269  }
270 
275  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
276 
277  template <int M1, int M2, int N1, int N2>
278  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;
279  template <int M1, int M2>
280  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2>> &I, const Range<Var,Var > &J) const;
281 
282  template <int N1, int N2>
283  inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> operator()(const Range<Var,Var> &I, const Range<Fixed<N1>,Fixed<N2>> &J) const;
284 
285  inline const RowVector<Fixed<N>,AT> row(int i) const;
286  inline const Vector<Fixed<M>,AT> col(int j) const;
287 
295  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(const AT &val=AT());
296  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Init, const AT &a=AT()) { return init(a); }
297  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Eye, const AT &val=1);
298  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& init(Noinit, const AT &a=AT()) { return *this; }
299 
304  explicit inline operator std::vector<std::vector<AT>>() const;
305 
311  explicit Matrix(const std::vector<std::vector<AT>> &m);
312 
313 // /*! \brief Cast to AT.
314 // *
315 // * \return The AT representation of the matrix
316 // * */
317 // explicit operator AT() const {
318 // FMATVEC_ASSERT(M==1, AT);
319 // FMATVEC_ASSERT(N==1, AT);
320 // return ele[0][0];
321 // }
322 //
323 // /*! \brief AT Constructor.
324 // * Constructs and initializes a matrix with a AT object.
325 // * \param v The AT the matrix will be initialized with.
326 // * */
327 // explicit Matrix(const AT &x) {
328 // FMATVEC_ASSERT(M==1, AT);
329 // FMATVEC_ASSERT(N==1, AT);
330 // ele[0][0] = x;
331 // }
332 
333  inline const Matrix<General,Fixed<N>,Fixed<M>,AT> T() const;
334 
338  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
339 
343  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
344 
351  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);
352 
356  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
357 
361  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
362 
363  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);
364  };
365 
366  template <int M, int N, class AT>
367  Matrix<General,Fixed<M>,Fixed<N>,AT>::Matrix(const std::string &strs) {
368  std::istringstream iss(strs);
369  iss>>*this;
370 
371  // check end of stream
372  iss>>std::ws;
373  if(!iss.eof())
374  throw std::runtime_error("Input not fully read.");
375  }
376  template <int M, int N, class AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::Matrix(const char *strs) :
377  Matrix<General,Fixed<M>,Fixed<N>,AT>::Matrix(std::string(strs)) {}
378 
379  template <int M, int N, class AT>
380  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::init(const AT &val) {
381  for(int i=0; i<M; i++)
382  for(int j=0; j<N; j++)
383  e(i,j) = val;
384  return *this;
385  }
386 
387  template <int M, int N, class AT>
388  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::init(Eye, const AT &val) {
389  for(int i=0; i<M; i++)
390  for(int j=0; j<N; j++)
391  e(i,j) = (i==j) ? val : 0;
392  return *this;
393  }
394 
395  template <int M, int N, class AT>
396  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 {
397  FMATVEC_ASSERT(I.end()<M, AT);
398  FMATVEC_ASSERT(J.end()<N, AT);
399  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
400 
401  for(int i=0; i<A.rows(); i++)
402  for(int j=0; j<A.cols(); j++)
403  A.e(i,j) = e(I.start()+i,J.start()+j);
404 
405  return A;
406  }
407 
408  template <int M, int N, class AT> template <int M1, int M2, int N1, int N2>
409  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 {
410  FMATVEC_ASSERT(M2<M, AT);
411  FMATVEC_ASSERT(N2<N, AT);
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  FMATVEC_ASSERT(M2<M, AT);
424  FMATVEC_ASSERT(J.end()<N, AT);
425  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
426 
427  for(int i=0; i<A.rows(); i++)
428  for(int j=0; j<A.cols(); j++)
429  A.e(i,j) = e(I.start()+i,J.start()+j);
430 
431  return A;
432  }
433 
434  template <int M, int N, class AT> template <int N1, int N2>
435  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 {
436  FMATVEC_ASSERT(I.end()<M, AT);
437  FMATVEC_ASSERT(N2<N, AT);
438  Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.start()+1,NONINIT);
439 
440  for(int i=0; i<A.rows(); i++)
441  for(int j=0; j<A.cols(); j++)
442  A.e(i,j) = e(I.start()+i,J.start()+j);
443 
444  return A;
445  }
446 
447  template <int M, int N, class AT>
448  inline const RowVector<Fixed<N>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::row(int i) const {
449 
450  FMATVEC_ASSERT(i>=0, AT);
451  FMATVEC_ASSERT(i<M, AT);
452 
453  RowVector<Fixed<N>,AT> x(NONINIT);
454 
455  for(int j=0; j<N; j++)
456  x.e(j) = e(i,j);
457 
458  return x;
459  }
460 
461  template <int M, int N, class AT>
462  inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Fixed<N>,AT>::col(int j) const {
463 
464  FMATVEC_ASSERT(j>=0, AT);
465  FMATVEC_ASSERT(j<N, AT);
466 
467  Vector<Fixed<M>,AT> x(NONINIT);
468 
469  for(int i=0; i<M; i++)
470  x.e(i) = e(i,j);
471 
472  return x;
473  }
474 
475  template <int M, int N, class AT>
477  Matrix<General,Fixed<N>,Fixed<M>,AT> A(NONINIT);
478  for(int i=0; i<N; i++)
479  for(int j=0; j<M; j++)
480  A.e(i,j) = e(j,i);
481  return A;
482  }
483 
484  template <int M, int N, class AT> template <class Row>
485  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::set(int j, const Vector<Row,AT> &x) {
486  FMATVEC_ASSERT(j<cols(), AT);
487  FMATVEC_ASSERT(rows()==x.size(), AT);
488  for(int i=0; i<rows(); i++)
489  e(i,j) = x.e(i);
490  }
491 
492  template <int M, int N, class AT> template <class Col>
493  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::set(int i, const RowVector<Col,AT> &x) {
494  FMATVEC_ASSERT(i<rows(), AT);
495  FMATVEC_ASSERT(cols()==x.size(), AT);
496  for(int j=0; j<cols(); j++)
497  e(i,j) = x.e(j);
498  }
499 
500  template <int M, int N, class AT> template<class Type, class Row, class Col>
501  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) {
502 
503  FMATVEC_ASSERT(I.end()<rows(), AT);
504  FMATVEC_ASSERT(J.end()<cols(), AT);
505  FMATVEC_ASSERT(I.size()==A.rows(), AT);
506  FMATVEC_ASSERT(J.size()==A.cols(), AT);
507 
508  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
509  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
510  e(i,j) = A.e(ii,jj);
511  }
512 
513  template <int M, int N, class AT> template <class Row>
514  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::add(int j, const Vector<Row,AT> &x) {
515  FMATVEC_ASSERT(j<cols(), AT);
516  FMATVEC_ASSERT(rows()==x.size(), AT);
517  for(int i=0; i<rows(); i++)
518  e(i,j) += x.e(i);
519  }
520 
521  template <int M, int N, class AT> template <class Col>
522  inline void Matrix<General,Fixed<M>,Fixed<N>,AT>::add(int i, const RowVector<Col,AT> &x) {
523  FMATVEC_ASSERT(i<rows(), AT);
524  FMATVEC_ASSERT(cols()==x.size(), AT);
525  for(int j=0; j<cols(); j++)
526  e(i,j) += x.e(j);
527  }
528 
529  template <int M, int N, class AT> template<class Type, class Row, class Col>
530  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) {
531 
532  FMATVEC_ASSERT(I.end()<rows(), AT);
533  FMATVEC_ASSERT(J.end()<cols(), AT);
534  FMATVEC_ASSERT(I.size()==A.rows(), AT);
535  FMATVEC_ASSERT(J.size()==A.cols(), AT);
536 
537  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
538  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
539  e(i,j) += A.e(ii,jj);
540  }
541 
542  template <int M, int N, class AT>
543  inline Matrix<General,Fixed<M>,Fixed<N>,AT>::operator std::vector<std::vector<AT>>() const {
544  std::vector<std::vector<AT>> ret(rows());
545  for(int r=0; r<rows(); r++) {
546  ret[r].resize(cols());
547  for(int c=0; c<cols(); c++)
548  ret[r][c]=e(r,c);
549  }
550  return ret;
551  }
552 
553  template <int M, int N, class AT>
554  inline Matrix<General,Fixed<M>,Fixed<N>,AT>::Matrix(const std::vector<std::vector<AT>> &m) {
555  if(m.size() != M)
556  throw std::runtime_error("The input has "+std::to_string(m.size())+" rows but "+std::to_string(M)+" rows are required.");
557  if(m[0].size() != N)
558  throw std::runtime_error("The input has "+std::to_string(m[0].size())+" columne but "+std::to_string(N)+" columns are required.");
559  for(int r=0; r<rows(); r++) {
560  if(static_cast<int>(m[r].size())!=cols())
561  throw std::runtime_error("The rows of the input have different length.");
562  for(int c=0; c<cols(); c++)
563  e(r,c)=m[r][c];
564  }
565  }
566 
568 
569  template <int M, int N, class AT> template <class Type, class Row, class Col>
571  for(int i=0; i<M; i++)
572  for(int j=0; j<N; j++)
573  e(i,j) = A.e(i,j);
574  return *this;
575  }
576 
577  template<int M, int N, class AT> template<class Row>
578  inline Matrix<General,Fixed<M>,Fixed<N>,AT>& Matrix<General,Fixed<M>,Fixed<N>,AT>::copy(const Matrix<Symmetric,Row,Row,AT> &A) {
579  for(int i=0; i<A.size(); i++) {
580  e(i,i) = A.ej(i,i);
581  for(int j=i+1; j<A.size(); j++)
582  e(i,j) = e(j,i) = A.ej(i,j);
583  }
584  return *this;
585  }
586 
588 
596  template<int M, class AT>
597  class Matrix<Rotation,Fixed<M>,Fixed<M>,AT> : public Matrix<General,Fixed<M>,Fixed<M>,AT> {
598  public:
599  typedef AT value_type;
600  typedef Rotation shape_type;
601  // Constructors are not inherited. Hence we must redefine all ctors here.
602 
603  Matrix(Noinit ini) { }
604  Matrix(Init ini=INIT, const AT &a=0) { this->init(a); }
605  Matrix(Eye ini, const AT &a=1) { this->init(ini,a); }
606 
607  Matrix(int m, int n) { }
608  Matrix(int m, int n, Noinit ini) { }
609  Matrix(int m, int n, Init ini, const AT &a=0) { this->init(a); }
610  Matrix(int m, int n, Eye ini, const AT &a=1) { this->init(ini,a); }
611 
612  Matrix(const Matrix<Rotation,Fixed<M>,Fixed<M>,AT> &A) = default;
613 
614  explicit Matrix(const Matrix<General,Fixed<M>,Fixed<M>,AT> &A) : Matrix<General,Fixed<M>,Fixed<M>,AT>(A) { }
615 
616  template<class Row>
618 
619  template<class Type, class Row, class Col>
620  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : Matrix<General,Fixed<M>,Fixed<M>,AT>(A) { }
621 
622  explicit Matrix(const std::vector<std::vector<AT>> &m) : Matrix<General,Fixed<M>,Fixed<M>,AT>(m) { }
623 
624  inline const Matrix<Rotation,Fixed<M>,Fixed<M>,AT> T() const;
625 
626  using Matrix<General,Fixed<M>,Fixed<M>,AT>::e;
627 
628  // TODO: we should add some overloaded member functions here which capitalize the special
629  // properties of rotation matrices, link "RotMat inv() { return trans(*this); }"
630  };
631 
632  template <int M, class AT>
633  inline const Matrix<Rotation,Fixed<M>,Fixed<M>,AT> Matrix<Rotation,Fixed<M>,Fixed<M>,AT>::T() const {
634  Matrix<Rotation,Fixed<M>,Fixed<M>,AT> A(NONINIT);
635  for(int i=0; i<M; i++)
636  for(int j=0; j<M; j++)
637  A.e(i,j) = e(j,i);
638  return A;
639  }
640 
641 }
642 
643 #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:140
constexpr int rows() const
Number of rows.
Definition: fixed_general_matrix.h:229
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:191
AT * operator()()
Pointer operator.
Definition: fixed_general_matrix.h:217
int ldim() const
Leading dimension.
Definition: fixed_general_matrix.h:241
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
Definition: types.h:89
Definition: types.h:101
Definition: fmatvec.h:50
Shape class for rotation matrices.
Definition: types.h:160
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:104
int cols() const
Number of columns.
const AT & e(int i, int j) const
Element operator.
Definition: fixed_general_matrix.h:208
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_general_matrix.h:256
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_general_matrix.h:267
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
const AT * operator()() const
Pointer operator.
Definition: fixed_general_matrix.h:223
Definition: types.h:88
AT & operator()(int i, int j)
Element operator.
Definition: fixed_general_matrix.h:178
Basic shape class for matrices.
Definition: types.h:136
void resize(int m, int n)
Definition: fixed_general_matrix.h:245
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_general_matrix.h:92
constexpr int cols() const
Number of columns.
Definition: fixed_general_matrix.h:235
int rows() const
Number of rows.
This is an index class for creating submatrices.
Definition: range.h:44
Definition: types.h:90