fmatvec  0.0.0
Fast Matrix/Vector Library
var_general_matrix.h
1 /* Copyright (C) 2003-2012 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 var_general_matrix_h
23 #define var_general_matrix_h
24 
25 #include "types.h"
26 #include <cstdlib>
27 #include <stdexcept>
28 
29 namespace fmatvec {
30 
39  template <class AT> class Matrix<fmatvec::General,fmatvec::Var,fmatvec::Var,AT> {
40 
41  public:
42  static constexpr bool isVector {false};
43  typedef AT value_type;
44  typedef General shape_type;
45 
47 
48  protected:
49 
50  int M{0};
51  int N{0};
52 
53  AT *ele;
54 
55  template <class Type, class Row, class Col> inline Matrix<General,Var,Var,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
56 
58 
59  public:
60 
65  explicit Matrix() : ele(nullptr) { }
66 
67 // Works with -std=gnu++0x only
68 // template<class Ini=All<AT>>
69 // Matrix(int m, int n, Ini ini=All<AT>()) : M(m), N(n), ele(new AT[M*N]) {
70 // init(ini);
71 // }
72 
73  explicit Matrix(int m, int n, Noinit) : M(m), N(n), ele(new AT[M*N]) { }
74  explicit Matrix(int m, int n, Init ini=INIT, const AT &a=AT()) : M(m), N(n), ele(new AT[M*N]) { init(a); }
75  explicit Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), N(n), ele(new AT[M*N]) { init(ini,a); }
76 
77  // move
78  Matrix(Matrix<General,Var,Var,AT> &&src) {
79  M=src.M;
80  src.M=0;
81  N=src.N;
82  src.N=0;
83  ele=src.ele;
84  src.ele=nullptr;
85  }
86  Matrix<General,Var,Var,AT>& operator=(Matrix<General,Var,Var,AT> &&src) {
87  FMATVEC_ASSERT(M == src.rows(), AT);
88  FMATVEC_ASSERT(N == src.cols(), AT);
89  src.M=0;
90  src.N=0;
91  delete[]ele;
92  ele=src.ele;
93  src.ele=nullptr;
94  return *this;
95  }
96 
102  Matrix(const Matrix<General,Var,Var,AT> &A) : M(A.M), N(A.N), ele(new AT[M*N]) {
103  copy(A);
104  }
105 
111  template<class Row, class Col>
112  Matrix(const Matrix<General,Row,Col,AT> &A) : M(A.rows()), N(A.cols()), ele(new AT[M*N]) {
113  copy(A);
114  }
115 
121  template<class Type, class Row, class Col>
122  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), N(A.cols()), ele(new AT[M*N]) {
123  copy(A);
124  }
125 
138  Matrix(const std::string &strs);
139  Matrix(const char *strs);
140 
144  delete[] ele;
145  }
146 
147  typedef AT* iterator;
148  typedef const AT* const_iterator;
149  iterator begin() { return &ele[0]; }
150  iterator end() { return &ele[M*N]; }
151  const_iterator begin() const { return &ele[0]; }
152  const_iterator end() const { return &ele[M*N]; }
153 
154  Matrix<General,Var,Var,AT>& resize(int m, int n, Noinit) {
155  delete[] ele;
156  M = m;
157  N = n;
158  ele = new AT[M*N];
159  return *this;
160  }
161 
162  Matrix<General,Var,Var,AT>& resize(int m, int n, Init ini=INIT, const AT &a=AT()) { return resize(m,n,Noinit()).init(a); }
163 
164  Matrix<General,Var,Var,AT>& resize(int m, int n, Eye ini, const AT &a=1) { return resize(m,n,Noinit()).init(ini,a); }
165 
173  FMATVEC_ASSERT(M == A.rows(), AT);
174  FMATVEC_ASSERT(N == A.cols(), AT);
175  return copy(A);
176  }
177 
184  template <class Type, class Row, class Col>
186  FMATVEC_ASSERT(M == A.rows(), AT);
187  FMATVEC_ASSERT(N == A.cols(), AT);
188  return copy(A);
189  }
190 
197  template<class Type, class Row, class Col>
198  inline Matrix<General,Var,Var,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
199  if(M!=A.rows() || N!=A.cols()) resize(A.rows(),A.cols(),NONINIT);
200  return copy(A);
201  }
202  // move
203  template<class Type, class Row, class Col>
204  inline Matrix<General,Var,Var,AT>& operator<<=(Matrix<General,Var,Var,AT> &&src) {
205  M=src.M;
206  src.M=0;
207  N=src.N;
208  src.N=0;
209  delete[]ele;
210  ele=src.ele;
211  src.ele=nullptr;
212  return *this;
213  }
214 
215  template <class AT2>
216  operator Matrix<General,Var,Var,AT2>() const {
217  Matrix<General,Var,Var,AT2> ret(rows(), cols());
218  for(size_t r=0; r<rows(); ++r)
219  for(size_t c=0; c<cols(); ++c)
220  ret(r,c) = (*this)(r,c);
221  return ret;
222  }
223 
233  AT& operator()(int i, int j) {
234  FMATVEC_ASSERT(i>=0, AT);
235  FMATVEC_ASSERT(j>=0, AT);
236  FMATVEC_ASSERT(i<M, AT);
237  FMATVEC_ASSERT(j<N, AT);
238 
239  return e(i,j);
240  }
241 
246  const AT& operator()(int i, int j) const {
247  FMATVEC_ASSERT(i>=0, AT);
248  FMATVEC_ASSERT(j>=0, AT);
249  FMATVEC_ASSERT(i<M, AT);
250  FMATVEC_ASSERT(j<N, AT);
251 
252  return e(i,j);
253  }
254 
255  AT& e(int i, int j) {
256  return ele[i*N+j];
257  }
258 
263  const AT& e(int i, int j) const {
264  return ele[i*N+j];
265  }
266 
267  AT& e(int i) {
268  return ele[i];
269  }
270 
275  const AT& e(int i) const {
276  return ele[i];
277  }
278 
284  AT* operator()() {return ele;}
285 
290  const AT* operator()() const {return ele;}
291 
296  constexpr int rows() const {return M;}
297 
302  constexpr int cols() const {return N;}
303 
308  int ldim() const {return N;}
309 
316  const CBLAS_TRANSPOSE blasTrans() const {
317  return CblasNoTrans;
318  }
319 
327  const CBLAS_ORDER blasOrder() const {
328  return CblasRowMajor;
329  }
330 
336 
337  template <int M1, int M2, int N1, int N2>
338  inline const Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> operator()(const fmatvec::Range<Fixed<M1>,Fixed<M2>> &I, const fmatvec::Range<Fixed<N1>,Fixed<N2>> &J) const;
339 
340  template <int M1, int M2>
341  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const fmatvec::Range<Fixed<M1>,Fixed<M2>> &I, const fmatvec::Range<Var,Var > &J) const;
342 
343  template <int N1, int N2>
344  inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> operator()(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Fixed<N1>,Fixed<N2>> &J) const;
345 
346  inline const RowVector<Var,AT> row(int i) const;
347  inline const Vector<Var,AT> col(int j) const;
348 
356  inline Matrix<General,Var,Var,AT>& init(const AT &val=AT());
357  inline Matrix<General,Var,Var,AT>& init(Init all, const AT &a=AT()) { return init(a); }
358  inline Matrix<General,Var,Var,AT>& init(Eye eye, const AT &val=1);
359  inline Matrix<General,Var,Var,AT>& init(Noinit, const AT &a=AT()) { return *this; }
360 
365  explicit inline operator std::vector<std::vector<AT>>() const;
366 
372  explicit inline Matrix(const std::vector<std::vector<AT>> &m);
373 
374 // /*! \brief Cast to AT.
375 // *
376 // * \return The AT representation of the matrix
377 // * */
378 // explicit operator AT() const {
379 // FMATVEC_ASSERT(M==1, AT);
380 // FMATVEC_ASSERT(N==1, AT);
381 // return ele[0];
382 // }
383 //
384 // /*! \brief AT Constructor.
385 // * Constructs and initializes a matrix with a AT object.
386 // * \param x The AT the matrix will be initialized with.
387 // * */
388 // explicit Matrix(const AT &x) : M(1), N(1), ele(new AT[1]) { ele[0] = x; }
389 
390  inline const Matrix<General,Var,Var,AT> T() const;
391 
392  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
393 
394  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
395 
396  template<class Type, class Row, class Col> inline void set(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A);
397 
398  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
399 
400  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
401 
402  template<class Type, class Row, class Col> inline void add(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A);
403  };
404 
405  template <class AT>
406  Matrix<General,Var,Var,AT>::Matrix(const std::string &strs) : ele(nullptr) {
407  std::istringstream iss(strs);
408  iss>>*this;
409 
410  // check end of stream
411  iss>>std::ws;
412  if(!iss.eof())
413  throw std::runtime_error("Input not fully read.");
414  }
415  template <class AT> Matrix<General,Var,Var,AT>::Matrix(const char *strs) : Matrix<General,Var,Var,AT>::Matrix(std::string(strs)) {}
416 
417  template <class AT>
418  inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::init(const AT &val) {
419  for(int i=0; i<M*N; i++)
420  e(i) = val;
421  return *this;
422  }
423 
424  template <class AT>
425  inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::init(Eye eye, const AT &val) {
426  for(int i=0; i<M; i++)
427  for(int j=0; j<N; j++)
428  e(i,j) = (i==j) ? val : 0;
429  return *this;
430  }
431 
432  template <class AT>
433  inline const Matrix<General,Var,Var,AT> Matrix<General,Var,Var,AT>::operator()(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J) const {
434  FMATVEC_ASSERT(I.end()<M, AT);
435  FMATVEC_ASSERT(J.end()<N, AT);
436  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
437 
438  for(int i=0; i<A.rows(); i++)
439  for(int j=0; j<A.cols(); j++)
440  A.e(i,j) = e(I.start()+i,J.start()+j);
441 
442  return A;
443  }
444 
445  template <class AT> template <int M1, int M2, int N1, int N2>
446  inline const Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> Matrix<General,Var,Var,AT>::operator()(const fmatvec::Range<Fixed<M1>,Fixed<M2>> &I, const fmatvec::Range<Fixed<N1>,Fixed<N2>> &J) const {
447  FMATVEC_ASSERT(M2<M, AT);
448  FMATVEC_ASSERT(N2<N, AT);
449  Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> A(NONINIT);
450 
451  for(int i=0; i<A.rows(); i++)
452  for(int j=0; j<A.cols(); j++)
453  A.e(i,j) = e(I.start()+i,J.start()+j);
454 
455  return A;
456  }
457 
458  template <class AT> template <int M1, int M2>
459  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> Matrix<General,Var,Var,AT>::operator()(const fmatvec::Range<Fixed<M1>,Fixed<M2>> &I, const fmatvec::Range<Var,Var> &J) const {
460  FMATVEC_ASSERT(M2<M, AT);
461  FMATVEC_ASSERT(J.end()<N, AT);
462  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
463 
464  for(int i=0; i<A.rows(); i++)
465  for(int j=0; j<A.cols(); j++)
466  A.e(i,j) = e(I.start()+i,J.start()+j);
467 
468  return A;
469  }
470 
471  template <class AT> template <int N1, int N2>
472  inline const Matrix<General,Var,Fixed<N2-N1+1>,AT> Matrix<General,Var,Var,AT>::operator()(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Fixed<N1>,Fixed<N2>> &J) const {
473  FMATVEC_ASSERT(I.end()<M, AT);
474  FMATVEC_ASSERT(N2<N, AT);
475  Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.start()+1,NONINIT);
476 
477  for(int i=0; i<A.rows(); i++)
478  for(int j=0; j<A.cols(); j++)
479  A.e(i,j) = e(I.start()+i,J.start()+j);
480 
481  return A;
482  }
483 
484  template <class AT>
485  inline const RowVector<Var,AT> Matrix<General,Var,Var,AT>::row(int i) const {
486 
487  FMATVEC_ASSERT(i>=0, AT);
488  FMATVEC_ASSERT(i<M, AT);
489 
490  RowVector<Var,AT> x(N,NONINIT);
491 
492  for(int j=0; j<N; j++)
493  x.e(j) = e(i,j);
494 
495  return x;
496 
497  }
498 
499  template <class AT>
500  inline const Vector<Var,AT> Matrix<General,Var,Var,AT>::col(int j) const {
501 
502  FMATVEC_ASSERT(j>=0, AT);
503  FMATVEC_ASSERT(j<N, AT);
504 
505  Vector<Var,AT> x(M,NONINIT);
506 
507  for(int i=0; i<M; i++)
508  x.e(i) = e(i,j);
509 
510  return x;
511 
512  }
513 
514  template <class AT>
515  inline const Matrix<General,Var,Var,AT> Matrix<General,Var,Var,AT>::T() const {
516  Matrix<General,Var,Var,AT> A(N,M,NONINIT);
517  for(int i=0; i<N; i++)
518  for(int j=0; j<M; j++)
519  A.e(i,j) = e(j,i);
520  return A;
521  }
522 
523  template <class AT> template <class Row>
524  inline void Matrix<General,Var,Var,AT>::set(int j, const Vector<Row,AT> &x) {
525  FMATVEC_ASSERT(j<cols(), AT);
526  FMATVEC_ASSERT(rows()==x.size(), AT);
527  for(int i=0; i<rows(); i++)
528  e(i,j) = x.e(i);
529  }
530 
531  template <class AT> template <class Col>
532  inline void Matrix<General,Var,Var,AT>::set(int i, const RowVector<Col,AT> &x) {
533  FMATVEC_ASSERT(i<rows(), AT);
534  FMATVEC_ASSERT(cols()==x.size(), AT);
535  for(int j=0; j<cols(); j++)
536  e(i,j) = x.e(j);
537  }
538 
539  template <class AT> template<class Type, class Row, class Col>
540  inline void Matrix<General,Var,Var,AT>::set(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
541 
542  FMATVEC_ASSERT(I.end()<rows(), AT);
543  FMATVEC_ASSERT(J.end()<cols(), AT);
544  FMATVEC_ASSERT(I.size()==A.rows(), AT);
545  FMATVEC_ASSERT(J.size()==A.cols(), AT);
546 
547  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
548  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
549  e(i,j) = A.e(ii,jj);
550  }
551 
552  template <class AT> template <class Row>
553  inline void Matrix<General,Var,Var,AT>::add(int j, const Vector<Row,AT> &x) {
554  FMATVEC_ASSERT(j<cols(), AT);
555  FMATVEC_ASSERT(rows()==x.size(), AT);
556  for(int i=0; i<rows(); i++)
557  e(i,j) += x.e(i);
558  }
559 
560  template <class AT> template <class Col>
561  inline void Matrix<General,Var,Var,AT>::add(int i, const RowVector<Col,AT> &x) {
562  FMATVEC_ASSERT(i<rows(), AT);
563  FMATVEC_ASSERT(cols()==x.size(), AT);
564  for(int j=0; j<cols(); j++)
565  e(i,j) += x.e(j);
566  }
567 
568  template <class AT> template<class Type, class Row, class Col>
569  inline void Matrix<General,Var,Var,AT>::add(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
570 
571  FMATVEC_ASSERT(I.end()<rows(), AT);
572  FMATVEC_ASSERT(J.end()<cols(), AT);
573  FMATVEC_ASSERT(I.size()==A.rows(), AT);
574  FMATVEC_ASSERT(J.size()==A.cols(), AT);
575 
576  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
577  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
578  e(i,j) += A.e(ii,jj);
579  }
580 
581  template <class AT>
582  inline Matrix<General,Var,Var,AT>::operator std::vector<std::vector<AT>>() const {
583  std::vector<std::vector<AT>> ret(rows());
584  for(int r=0; r<rows(); r++) {
585  ret[r].resize(cols());
586  for(int c=0; c<cols(); c++)
587  ret[r][c]=e(r,c);
588  }
589  return ret;
590  }
591 
592  template <class AT>
593  inline Matrix<General,Var,Var,AT>::Matrix(const std::vector<std::vector<AT>> &m) : M(static_cast<int>(m.size())), N(static_cast<int>(m[0].size())), ele(new AT[M*N]) {
594  for(int r=0; r<rows(); r++) {
595  if(static_cast<int>(m[r].size())!=cols())
596  throw std::runtime_error("The rows of the input have different length.");
597  for(int c=0; c<cols(); c++)
598  e(r,c)=m[r][c];
599  }
600  }
601 
603 
604  template <class AT> template <class Type, class Row, class Col>
605  inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::copy(const Matrix<Type,Row,Col,AT> &A) {
606  for(int i=0; i<M; i++)
607  for(int j=0; j<N; j++)
608  e(i,j) = A.e(i,j);
609  return *this;
610  }
611 
613 
614 }
615 
616 #endif
int end() const
Last element.
Definition: range.h:91
Definition: fmatvec.h:38
constexpr int cols() const
Number of columns.
Definition: var_general_matrix.h:302
Matrix< General, Var, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_general_matrix.h:185
constexpr int rows() const
Number of rows.
Definition: var_general_matrix.h:296
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
Definition: types.h:89
Definition: types.h:101
const AT & operator()(int i, int j) const
Element operator.
Definition: var_general_matrix.h:246
int ldim() const
Leading dimension.
Definition: var_general_matrix.h:308
Matrix< General, Var, Var, AT > & operator=(const Matrix< General, Var, Var, AT > &A)
Assignment operator.
Definition: var_general_matrix.h:172
Matrix(const Matrix< General, Var, Var, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:102
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_general_matrix.h:316
int start() const
First element.
Definition: range.h:85
Definition: fmatvec.h:47
AT * operator()()
Pointer operator.
Definition: var_general_matrix.h:284
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:112
~Matrix()
Destructor.
Definition: var_general_matrix.h:143
const AT & e(int i) const
Element operator.
Definition: var_general_matrix.h:275
int cols() const
Number of columns.
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:122
This is an index class for creating submatrices.
Definition: range.h:35
Namespace fmatvec.
Definition: _memory.cc:28
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_general_matrix.h:327
int size() const
Size.
Definition: range.h:97
Matrix()
Standard constructor.
Definition: var_general_matrix.h:65
const AT & e(int i, int j) const
Element operator.
Definition: var_general_matrix.h:263
Definition: types.h:88
Basic shape class for matrices.
Definition: types.h:136
AT & operator()(int i, int j)
Element operator.
Definition: var_general_matrix.h:233
const AT * operator()() const
Pointer operator.
Definition: var_general_matrix.h:290
int rows() const
Number of rows.
This is an index class for creating submatrices.
Definition: range.h:44
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39