fmatvec  0.0.0
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 
434  template <int M, class AT>
435  Matrix<General,Fixed<M>,Var,AT>::Matrix(const std::string &strs) : ele(nullptr) {
436  std::istringstream iss(strs);
437  iss>>*this;
438 
439  // check end of stream
440  iss>>std::ws;
441  if(!iss.eof())
442  throw std::runtime_error("Input not fully read.");
443  }
444  template <int M, class AT> Matrix<General,Fixed<M>,Var,AT>::Matrix(const char *strs) :
445  Matrix<General,Fixed<M>,Var,AT>::Matrix(std::string(strs)) {}
446 
447  template <int M, class AT>
448  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(const AT &val) {
449  for(int i=0; i<M*N; i++)
450  e(i) = val;
451  return *this;
452  }
453 
454  template <int M, class AT>
455  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(Eye eye, const AT &val) {
456  for(int i=0; i<M; i++)
457  for(int j=0; j<N; j++)
458  e(i,j) = (i==j) ? val : 0;
459  return *this;
460  }
461 
462  template <int M, class AT> template <int M1, int M2>
463  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 {
464  FMATVEC_ASSERT(M2<M, AT);
465  FMATVEC_ASSERT(J.end()<N, AT);
466  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
467 
468  for(int i=0; i<A.rows(); i++)
469  for(int j=0; j<A.cols(); j++)
470  A.e(i,j) = e(M1+i,J.start()+j);
471 
472  return A;
473  }
474 
475  template <int M, class AT>
476  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 {
477  FMATVEC_ASSERT(I.end()<M, AT);
478  FMATVEC_ASSERT(J.end()<N, AT);
479  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
480 
481  for(int i=0; i<A.rows(); i++)
482  for(int j=0; j<A.cols(); j++)
483  A.e(i,j) = e(I.start()+i,J.start()+j);
484 
485  return A;
486  }
487 
488  template <int M, class AT>
489  inline const RowVector<Var,AT> Matrix<General,Fixed<M>,Var,AT>::row(int i) const {
490 
491  FMATVEC_ASSERT(i>=0, AT);
492  FMATVEC_ASSERT(i<M, AT);
493 
494  RowVector<Var,AT> x(N,NONINIT);
495 
496  for(int j=0; j<N; j++)
497  x.e(j) = e(i,j);
498 
499  return x;
500 
501  }
502 
503  template <int M, class AT>
504  inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(int j) const {
505 
506  FMATVEC_ASSERT(j>=0, AT);
507  FMATVEC_ASSERT(j<N, AT);
508 
509  Vector<Fixed<M>,AT> x(NONINIT);
510 
511  for(int i=0; i<M; i++)
512  x.e(i) = e(i,j);
513 
514  return x;
515 
516  }
517 
518  template <int M, class AT>
519  inline const Matrix<General,Var,Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::T() const {
520  Matrix<General,Var,Fixed<M>,AT> A(cols(),NONINIT);
521  for(int i=0; i<N; i++)
522  for(int j=0; j<M; j++)
523  A.e(i,j) = e(j,i);
524  return A;
525  }
526 
527  template <int M, class AT> template <class Row>
528  inline void Matrix<General,Fixed<M>,Var,AT>::set(int j, const Vector<Row,AT> &x) {
529  FMATVEC_ASSERT(j<cols(), AT);
530  FMATVEC_ASSERT(rows()==x.size(), AT);
531  for(int i=0; i<rows(); i++)
532  e(i,j) = x.e(i);
533  }
534 
535  template <int M, class AT> template <class Col>
536  inline void Matrix<General,Fixed<M>,Var,AT>::set(int i, const RowVector<Col,AT> &x) {
537  FMATVEC_ASSERT(i<rows(), AT);
538  FMATVEC_ASSERT(cols()==x.size(), AT);
539  for(int j=0; j<cols(); j++)
540  e(i,j) = x.e(j);
541  }
542 
543  template <int M, class AT> template<class Type, class Row, class Col>
544  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) {
545 
546  FMATVEC_ASSERT(I.end()<rows(), AT);
547  FMATVEC_ASSERT(J.end()<cols(), AT);
548  FMATVEC_ASSERT(I.size()==A.rows(), AT);
549  FMATVEC_ASSERT(J.size()==A.cols(), AT);
550 
551  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
552  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
553  e(i,j) = A.e(ii,jj);
554  }
555 
556  template <int M, class AT> template <class Row>
557  inline void Matrix<General,Fixed<M>,Var,AT>::add(int j, const Vector<Row,AT> &x) {
558  FMATVEC_ASSERT(j<cols(), AT);
559  FMATVEC_ASSERT(rows()==x.size(), AT);
560  for(int i=0; i<rows(); i++)
561  e(i,j) += x.e(i);
562  }
563 
564  template <int M, class AT> template <class Col>
565  inline void Matrix<General,Fixed<M>,Var,AT>::add(int i, const RowVector<Col,AT> &x) {
566  FMATVEC_ASSERT(i<rows(), AT);
567  FMATVEC_ASSERT(cols()==x.size(), AT);
568  for(int j=0; j<cols(); j++)
569  e(i,j) += x.e(j);
570  }
571 
572  template <int M, class AT> template<class Type, class Row, class Col>
573  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) {
574 
575  FMATVEC_ASSERT(I.end()<rows(), AT);
576  FMATVEC_ASSERT(J.end()<cols(), AT);
577  FMATVEC_ASSERT(I.size()==A.rows(), AT);
578  FMATVEC_ASSERT(J.size()==A.cols(), AT);
579 
580  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
581  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
582  e(i,j) += A.e(ii,jj);
583  }
584 
585  template <int M, class AT>
586  inline const Matrix<General,Var,Var,AT> Matrix<General,Fixed<M>,Var,AT>::operator()(const Indices &I, const Indices &J) const {
587  FMATVEC_ASSERT(I.max()<rows(), AT);
588  FMATVEC_ASSERT(J.max()<cols(), AT);
589 
590  Matrix<General,Var,Var,AT> A(I.size(),J.size(),NONINIT);
591 
592  for(int i=0; i<A.rows(); i++)
593  for(int j=0; j<A.cols(); j++)
594  A.e(i,j) = e(I[i],J[j]);
595 
596  return A;
597  }
598 
599  template <int M, class AT>
600  inline Matrix<General,Fixed<M>,Var,AT>::operator std::vector<std::vector<AT>>() const {
601  std::vector<std::vector<AT>> ret(rows(),std::vector<AT>(cols()));
602  for(int r=0; r<rows(); r++) {
603  for(int c=0; c<cols(); c++)
604  ret[r][c]=e(r,c);
605  }
606  return ret;
607  }
608 
609  template <int M, class AT>
610  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]) {
611  if(m.size() != M)
612  throw std::runtime_error("The input has "+std::to_string(m.size())+" rows but "+std::to_string(M)+" rows are required.");
613  for(int r=0; r<rows(); r++) {
614  if(static_cast<int>(m[r].size())!=cols())
615  throw std::runtime_error("The rows of the input have different length.");
616  for(int c=0; c<cols(); c++)
617  e(r,c)=m[r][c];
618  }
619  }
620 
622 
623  template <int M, class AT> template <class Type, class Row, class Col>
625  for(int i=0; i<M; i++)
626  for(int j=0; j<N; j++)
627  e(i,j) = A.e(i,j);
628  return *this;
629  }
630 
632 
633 }
634 
635 #endif
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:248
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
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
Definition: types.h:89
Definition: types.h:101
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_var_general_matrix.h:318
const AT & e(int i) const
Element operator.
Definition: fixed_var_general_matrix.h:277
const AT * operator()() const
Pointer operator.
Definition: fixed_var_general_matrix.h:292
AT * operator()()
Pointer operator.
Definition: fixed_var_general_matrix.h:286
constexpr int rows() const
Number of rows.
Definition: fixed_var_general_matrix.h:298
Definition: fmatvec.h:48
int ldim() const
Leading dimension.
Definition: fixed_var_general_matrix.h:310
int cols() const
Number of columns.
This is an index class for creating submatrices.
Definition: range.h:36
const AT & e(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:265
Namespace fmatvec.
Definition: _memory.cc:28
~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
Definition: types.h:88
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
int rows() const
Number of rows.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_var_general_matrix.h:329
constexpr int cols() const
Number of columns.
Definition: fixed_var_general_matrix.h:304
This is an index class for creating submatrices.
Definition: range.h:45
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