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