fmatvec  0.0.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
fixed_var_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_var_general_matrix_h
23 #define fixed_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 <int M, class AT> class Matrix<General,Fixed<M>,Var,AT> {
42 
43  public:
44  static constexpr bool isVector {false};
45 
46  using value_type = AT;
47  using shape_type = General;
48 
50 
51  protected:
52 
53  int N{0};
54 
55  AT *ele;
56 
57  template <class Type, class Row, class Col> inline Matrix<General,Fixed<M>,Var,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
58 
60 
61  public:
62 
67  explicit Matrix() : ele(nullptr) { }
68 
69  // move
70  Matrix(Matrix<General,Fixed<M>,Var,AT> &&src) noexcept {
71  N=src.N;
72  src.N=0;
73  ele=src.ele;
74  src.ele=nullptr;
75  }
76  Matrix<General,Fixed<M>,Var,AT>& operator=(Matrix<General,Fixed<M>,Var,AT> &&src) noexcept {
77  FMATVEC_ASSERT(N == src.cols(), AT);
78  src.N=0;
79  delete[]ele;
80  ele=src.ele;
81  src.ele=nullptr;
82  return *this;
83  }
84 
85 // template<class Ini=All<AT>>
86 // Matrix(int n, Ini ini=All<AT>()) : N(n), ele(new AT[M*N]) {
87 // init(ini);
88 // }
89 // template<class Ini=All<AT>>
90 // Matrix(int m, int n, Ini ini=All<AT>()) : N(n), ele(new AT[M*N]) {
91 // init(ini);
92 // }
93 
94  explicit Matrix(int n, Noinit) : N(n), ele(new AT[M*N]) { }
95  explicit Matrix(int n, Init ini=INIT, const AT &a=AT()) : N(n), ele(new AT[M*N]) { init(a); }
96  explicit Matrix(int n, Eye ini, const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
97  explicit Matrix(int m, int n, Noinit) : N(n), ele(new AT[M*N]) { FMATVEC_ASSERT(m==M, AT); }
98  explicit Matrix(int m, int n, Init ini=INIT, const AT &a=AT()) : N(n), ele(new AT[M*N]) { FMATVEC_ASSERT(m==M, AT); init(a); }
99  explicit Matrix(int m, int n, Eye ini, const AT &a=1) : N(n), ele(new AT[M*N]) { FMATVEC_ASSERT(m==M, AT); init(ini,a); }
100 
106  Matrix(const Matrix<General,Fixed<M>,Var,AT> &A) : N(A.N), ele(new AT[M*N]) {
107  copy(A);
108  }
109 
115  template<class Row, class Col>
116  Matrix(const Matrix<General,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
117  FMATVEC_ASSERT(A.rows() == M, AT);
118  copy(A);
119  }
120 
126  template<class Type, class Row, class Col>
127  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
128  FMATVEC_ASSERT(A.rows() == M, AT);
129  copy(A);
130  }
131 
144  Matrix(const std::string &strs);
145  Matrix(const char *strs);
146 
147  using iterator = AT *;
148  using const_iterator = const AT *;
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 
157  delete[] ele;
158  }
159 
160  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Noinit) {
161  delete[] ele;
162  N = n;
163  ele = new AT[M*N];
164  return *this;
165  }
166 
167  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Init ini=INIT, const AT &a=AT()) { return resize(n,Noinit()).init(a); }
168 
169  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Eye ini, const AT &a=1) { return resize(n,Noinit()).init(ini,a); }
170 
173  void resize(int m, int n) {
174  if(m!=M)
175  throw std::runtime_error("A fixed-var matrix can only be resized in the second dimension.");
176  resize(n);
177  }
178 
186  FMATVEC_ASSERT(N == A.cols(), AT);
187  return copy(A);
188  }
189 
196  template <class Type, class Row, class Col>
198  FMATVEC_ASSERT(M == A.rows(), AT);
199  FMATVEC_ASSERT(N == A.cols(), AT);
200  return copy(A);
201  }
202 
209  template<class Type, class Row, class Col>
210  inline Matrix<General,Fixed<M>,Var,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
211  FMATVEC_ASSERT(M == A.rows(), AT);
212  if(N!=A.cols()) resize(A.cols(),NONINIT);
213  return copy(A);
214  }
215  // move
216  inline Matrix<General,Fixed<M>,Var,AT>& operator<<=(Matrix<General,Fixed<M>,Var,AT> &&src) {
217  FMATVEC_ASSERT(M == src.rows(), AT);
218  N=src.N;
219  src.N=0;
220  delete[]ele;
221  ele=src.ele;
222  src.ele=nullptr;
223  return *this;
224  }
225 
235  AT& operator()(int i, int j) {
236  FMATVEC_ASSERT(i>=0, AT);
237  FMATVEC_ASSERT(j>=0, AT);
238  FMATVEC_ASSERT(i<M, AT);
239  FMATVEC_ASSERT(j<N, AT);
240 
241  return e(i,j);
242  }
243 
248  const AT& operator()(int i, int j) const {
249  FMATVEC_ASSERT(i>=0, AT);
250  FMATVEC_ASSERT(j>=0, AT);
251  FMATVEC_ASSERT(i<M, AT);
252  FMATVEC_ASSERT(j<N, AT);
253 
254  return e(i,j);
255  }
256 
257  AT& e(int i, int j) {
258  return ele[i*N+j];
259  }
260 
265  const AT& e(int i, int j) const {
266  return ele[i*N+j];
267  }
268 
269  AT& e(int i) {
270  return ele[i];
271  }
272 
277  const AT& e(int i) const {
278  return ele[i];
279  }
280 
286  AT* operator()() {return ele;}
287 
292  const AT* operator()() const {return ele;}
293 
298  constexpr int rows() const {return M;}
299 
304  constexpr int cols() const {return N;}
305 
310  int ldim() const {return N;}
311 
318  const CBLAS_TRANSPOSE blasTrans() const {
319  return CblasNoTrans;
320  }
321 
329  const CBLAS_ORDER blasOrder() const {
330  return CblasRowMajor;
331  }
332 
359  //inline Matrix<GeneralVar, AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J);
360 
365  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
366 
367  template <int M1, int M2>
368  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2>> &I, const Range<Var,Var> &J) const;
369 
370  inline const RowVector<Var,AT> row(int i) const;
371  inline const Vector<Fixed<M>,AT> col(int j) const;
372 
380  inline Matrix<General,Fixed<M>,Var,AT>& init(const AT &val=AT());
381  inline Matrix<General,Fixed<M>,Var,AT>& init(Init, const AT &a=AT()) { return init(a); }
382  inline Matrix<General,Fixed<M>,Var,AT>& init(Eye, const AT &val=1);
383  inline Matrix<General,Fixed<M>,Var,AT>& init(Noinit, const AT &a=AT()) { return *this; }
384 
389  explicit inline operator std::vector<std::vector<AT>>() const;
390 
396  explicit inline Matrix(const std::vector<std::vector<AT>> &m);
397 
398 // /*! \brief Cast to AT.
399 // *
400 // * \return The AT representation of the matrix
401 // * */
402 // explicit operator AT() const {
403 // FMATVEC_ASSERT(M==1, AT);
404 // FMATVEC_ASSERT(N==1, AT);
405 // return ele[0];
406 // }
407 //
408 // /*! \brief AT Constructor.
409 // * Constructs and initializes a matrix with a AT object.
410 // * \param v The AT the matrix will be initialized with.
411 // * */
412 // explicit Matrix(const AT &x) : N(1), ele(new AT[1]) {
413 // FMATVEC_ASSERT(M==1, AT);
414 // ele[0] = x;
415 // }
416 
417  inline const Matrix<General,Var,Fixed<M>,AT> T() const;
418 
419  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
420 
421  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
422 
423  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);
424 
425  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
426 
427  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
428 
429  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);
430 
431  inline const Matrix<General,Var,Var,AT> operator()(const Indices &I, const Indices &J) const;
432 
433  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Indices &J) const;
434 
435  inline const Matrix<General,Var,Var,AT> operator()(const Indices &I, const Range<Var,Var> &J) const;
436  };
437 
438  template <int M, class AT>
439  Matrix<General,Fixed<M>,Var,AT>::Matrix(const std::string &strs) : ele(nullptr) {
440  std::istringstream iss(strs);
441  iss>>*this;
442 
443  // check end of stream
444  iss>>std::ws;
445  if(!iss.eof())
446  throw std::runtime_error("Input not fully read.");
447  }
448  template <int M, class AT> Matrix<General,Fixed<M>,Var,AT>::Matrix(const char *strs) :
449  Matrix<General,Fixed<M>,Var,AT>::Matrix(std::string(strs)) {}
450 
451  template <int M, class AT>
452  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(const AT &val) {
453  for(int i=0; i<M*N; i++)
454  e(i) = val;
455  return *this;
456  }
457 
458  template <int M, class AT>
459  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(Eye eye, const AT &val) {
460  for(int i=0; i<M; i++)
461  for(int j=0; j<N; j++)
462  e(i,j) = (i==j) ? val : 0;
463  return *this;
464  }
465 
466  template <int M, class AT> template <int M1, int M2>
467  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(const Range<Fixed<M1>,Fixed<M2>> &I, const Range<Var,Var> &J) const {
468  FMATVEC_ASSERT(M2<M, AT);
469  FMATVEC_ASSERT(J.end()<N, AT);
470  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.size(),NONINIT);
471 
472  for(int i=0; i<A.rows(); i++)
473  for(int j=0; j<A.cols(); j++)
474  A.e(i,j) = e(M1+i,J.start()+j);
475 
476  return A;
477  }
478 
479  template <int M, class AT>
480  inline const Matrix<General,Var,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const {
481  FMATVEC_ASSERT(I.end()<M, AT);
482  FMATVEC_ASSERT(J.end()<N, AT);
483  Matrix<General,Var,Var,AT> A(I.size(),J.size(),NONINIT);
484 
485  for(int i=0; i<A.rows(); i++)
486  for(int j=0; j<A.cols(); j++)
487  A.e(i,j) = e(I.start()+i,J.start()+j);
488 
489  return A;
490  }
491 
492  template <int M, class AT>
493  inline const RowVector<Var,AT> Matrix<General,Fixed<M>,Var,AT>::row(int i) const {
494 
495  FMATVEC_ASSERT(i>=0, AT);
496  FMATVEC_ASSERT(i<M, AT);
497 
498  RowVector<Var,AT> x(N,NONINIT);
499 
500  for(int j=0; j<N; j++)
501  x.e(j) = e(i,j);
502 
503  return x;
504 
505  }
506 
507  template <int M, class AT>
508  inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(int j) const {
509 
510  FMATVEC_ASSERT(j>=0, AT);
511  FMATVEC_ASSERT(j<N, AT);
512 
513  Vector<Fixed<M>,AT> x(NONINIT);
514 
515  for(int i=0; i<M; i++)
516  x.e(i) = e(i,j);
517 
518  return x;
519 
520  }
521 
522  template <int M, class AT>
523  inline const Matrix<General,Var,Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::T() const {
524  Matrix<General,Var,Fixed<M>,AT> A(cols(),NONINIT);
525  for(int i=0; i<N; i++)
526  for(int j=0; j<M; j++)
527  A.e(i,j) = e(j,i);
528  return A;
529  }
530 
531  template <int M, class AT> template <class Row>
532  inline void Matrix<General,Fixed<M>,Var,AT>::set(int j, const Vector<Row,AT> &x) {
533  FMATVEC_ASSERT(j<cols(), AT);
534  FMATVEC_ASSERT(rows()==x.size(), AT);
535  for(int i=0; i<rows(); i++)
536  e(i,j) = x.e(i);
537  }
538 
539  template <int M, class AT> template <class Col>
540  inline void Matrix<General,Fixed<M>,Var,AT>::set(int i, const RowVector<Col,AT> &x) {
541  FMATVEC_ASSERT(i<rows(), AT);
542  FMATVEC_ASSERT(cols()==x.size(), AT);
543  for(int j=0; j<cols(); j++)
544  e(i,j) = x.e(j);
545  }
546 
547  template <int M, class AT> template<class Type, class Row, class Col>
548  inline void Matrix<General,Fixed<M>,Var,AT>::set(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
549 
550  FMATVEC_ASSERT(I.end()<rows(), AT);
551  FMATVEC_ASSERT(J.end()<cols(), AT);
552  FMATVEC_ASSERT(I.size()==A.rows(), AT);
553  FMATVEC_ASSERT(J.size()==A.cols(), AT);
554 
555  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
556  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
557  e(i,j) = A.e(ii,jj);
558  }
559 
560  template <int M, class AT> template <class Row>
561  inline void Matrix<General,Fixed<M>,Var,AT>::add(int j, const Vector<Row,AT> &x) {
562  FMATVEC_ASSERT(j<cols(), AT);
563  FMATVEC_ASSERT(rows()==x.size(), AT);
564  for(int i=0; i<rows(); i++)
565  e(i,j) += x.e(i);
566  }
567 
568  template <int M, class AT> template <class Col>
569  inline void Matrix<General,Fixed<M>,Var,AT>::add(int i, const RowVector<Col,AT> &x) {
570  FMATVEC_ASSERT(i<rows(), AT);
571  FMATVEC_ASSERT(cols()==x.size(), AT);
572  for(int j=0; j<cols(); j++)
573  e(i,j) += x.e(j);
574  }
575 
576  template <int M, class AT> template<class Type, class Row, class Col>
577  inline void Matrix<General,Fixed<M>,Var,AT>::add(const Range<Var,Var> &I, const Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
578 
579  FMATVEC_ASSERT(I.end()<rows(), AT);
580  FMATVEC_ASSERT(J.end()<cols(), AT);
581  FMATVEC_ASSERT(I.size()==A.rows(), AT);
582  FMATVEC_ASSERT(J.size()==A.cols(), AT);
583 
584  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
585  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
586  e(i,j) += A.e(ii,jj);
587  }
588 
589  template <int M, class AT>
590  inline const Matrix<General,Var,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(const Indices &I, const Indices &J) const {
591  FMATVEC_ASSERT(I.max()<rows(), AT);
592  FMATVEC_ASSERT(J.max()<cols(), AT);
593 
594  Matrix<General,Var,Var,AT> A(I.size(),J.size(),NONINIT);
595 
596  for(int i=0; i<A.rows(); i++)
597  for(int j=0; j<A.cols(); j++)
598  A.e(i,j) = e(I[i],J[j]);
599 
600  return A;
601  }
602 
603  template <int M, class AT>
604  inline const Matrix<General,Var,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(const Range<Var,Var> &I, const Indices &J) const {
605  FMATVEC_ASSERT(I.end()<M, AT);
606  FMATVEC_ASSERT(J.max()<cols(), AT);
607 
608  Matrix<General,Var,Var,AT> A(I.size(),J.size(),NONINIT);
609 
610  for(int i=0; i<A.rows(); i++)
611  for(int j=0; j<A.cols(); j++)
612  A.e(i,j) = e(I.start()+i,J[j]);
613 
614  return A;
615  }
616 
617  template <int M, class AT>
618  inline const Matrix<General,Var,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(const Indices &I, const Range<Var,Var> &J) const {
619  FMATVEC_ASSERT(I.max()<rows(), AT);
620  FMATVEC_ASSERT(J.end()<N, AT);
621 
622  Matrix<General,Var,Var,AT> A(I.size(),J.size(),NONINIT);
623 
624  for(int i=0; i<A.rows(); i++)
625  for(int j=0; j<A.cols(); j++)
626  A.e(i,j) = e(I[i],J.start()+j);
627 
628  return A;
629  }
630 
631  template <int M, class AT>
632  inline Matrix<General,Fixed<M>,Var,AT>::operator std::vector<std::vector<AT>>() const {
633  std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols()));
634  for(int r=0; r<rows(); r++) {
635  for(int c=0; c<cols(); c++)
636  ret[r][c]=e(r,c);
637  }
638  return ret;
639  }
640 
641  template <int M, class AT>
642  inline Matrix<General,Fixed<M>,Var,AT>::Matrix(const std::vector<std::vector<AT>> &m) : N(!m.empty()? static_cast<int>(m[0].size()):0), ele(new AT[M*N]) {
643  if(m.size() != M)
644  throw std::runtime_error("The input has "+std::to_string(m.size())+" rows but "+std::to_string(M)+" rows are required.");
645  for(int r=0; r<rows(); r++) {
646  if(static_cast<int>(m[r].size())!=cols())
647  throw std::runtime_error("The rows of the input have different length.");
648  for(int c=0; c<cols(); c++)
649  e(r,c)=m[r][c];
650  }
651  }
652 
654 
655  template <int M, class AT> template <class Type, class Row, class Col>
656  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::copy(const Matrix<Type,Row,Col,AT> &A) {
657  for(int i=0; i<M; i++)
658  for(int j=0; j<N; j++)
659  e(i,j) = A.e(i,j);
660  return *this;
661  }
662 
664 
665 }
666 
667 #endif
Definition: fmatvec.h:39
void resize(int m, int n)
Definition: fixed_var_general_matrix.h:173
AT & operator()(int i, int j)
Element operator.
Definition: fixed_var_general_matrix.h:235
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:42
Matrix()
Standard constructor.
Definition: fixed_var_general_matrix.h:67
This is a matrix class for general matrices.
Definition: var_general_matrix.h:41
constexpr int cols() const
Number of columns.
Definition: fixed_var_general_matrix.h:304
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: fixed_var_general_matrix.h:248
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_var_general_matrix.h:329
const AT & e(int i) const
Element operator.
Definition: fixed_var_general_matrix.h:277
AT * operator()()
Pointer operator.
Definition: fixed_var_general_matrix.h:286
int rows() const
Number of rows.
const AT & e(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:265
int cols() const
Number of columns.
Definition: fmatvec.h:48
This is an index class for creating submatrices.
Definition: range.h:35
constexpr int rows() const
Number of rows.
Definition: fixed_var_general_matrix.h:298
~Matrix()
Destructor.
Definition: fixed_var_general_matrix.h:156
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:116
int ldim() const
Leading dimension.
Definition: fixed_var_general_matrix.h:310
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_var_general_matrix.h:318
Definition: types.h:88
const AT * operator()() const
Pointer operator.
Definition: fixed_var_general_matrix.h:292
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:127
Shape class for general matrices.
Definition: types.h:112
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:197
This is an index class for creating submatrices.
Definition: range.h:44
Matrix(const Matrix< General, Fixed< M >, Var, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:106
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< General, Fixed< M >, Var, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:185
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39
Definition: types.h:90