fmatvec  0.0.0
Fast Matrix/Vector Library
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 <cstdlib>
28 #include <stdexcept>
29 
30 namespace fmatvec {
31 
40  template <int M, class AT> class Matrix<General,Fixed<M>,Var,AT> {
41 
42  public:
43  static constexpr bool isVector {false};
44 
45  typedef AT value_type;
46  typedef General shape_type;
47 
49 
50  protected:
51 
52  int N{0};
53 
54  AT *ele;
55 
56  template <class Type, class Row, class Col> inline Matrix<General,Fixed<M>,Var,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
57 
59 
60  public:
61 
66  explicit Matrix() : ele(nullptr) { }
67 
68  // move
69  Matrix(Matrix<General,Fixed<M>,Var,AT> &&src) {
70  N=src.N;
71  src.N=0;
72  ele=src.ele;
73  src.ele=nullptr;
74  }
75  Matrix<General,Fixed<M>,Var,AT>& operator=(Matrix<General,Fixed<M>,Var,AT> &&src) {
76  FMATVEC_ASSERT(N == src.cols(), AT);
77  src.N=0;
78  delete[]ele;
79  ele=src.ele;
80  src.ele=nullptr;
81  return *this;
82  }
83 
84 // template<class Ini=All<AT>>
85 // Matrix(int n, Ini ini=All<AT>()) : N(n), ele(new AT[M*N]) {
86 // init(ini);
87 // }
88 // template<class Ini=All<AT>>
89 // Matrix(int m, int n, Ini ini=All<AT>()) : N(n), ele(new AT[M*N]) {
90 // init(ini);
91 // }
92 
93  explicit Matrix(int n, Noinit) : N(n), ele(new AT[M*N]) { }
94  explicit Matrix(int n, Init ini=INIT, const AT &a=AT()) : N(n), ele(new AT[M*N]) { init(a); }
95  explicit Matrix(int n, Eye ini, const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
96  explicit Matrix(int m, int n, Noinit) : N(n), ele(new AT[M*N]) { FMATVEC_ASSERT(m==M, AT); }
97  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); }
98  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); }
99 
105  Matrix(const Matrix<General,Fixed<M>,Var,AT> &A) : N(A.N), ele(new AT[M*N]) {
106  copy(A);
107  }
108 
114  template<class Row, class Col>
115  Matrix(const Matrix<General,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
116  FMATVEC_ASSERT(A.rows() == M, AT);
117  copy(A);
118  }
119 
125  template<class Type, class Row, class Col>
126  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
127  FMATVEC_ASSERT(A.rows() == M, AT);
128  copy(A);
129  }
130 
143  Matrix(const std::string &strs);
144  Matrix(const char *strs);
145 
146  typedef AT* iterator;
147  typedef const AT* const_iterator;
148  iterator begin() { return &ele[0]; }
149  iterator end() { return &ele[M*N]; }
150  const_iterator begin() const { return &ele[0]; }
151  const_iterator end() const { return &ele[M*N]; }
152 
156  delete[] ele;
157  }
158 
159  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Noinit) {
160  delete[] ele;
161  N = n;
162  ele = new AT[M*N];
163  return *this;
164  }
165 
166  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Init ini=INIT, const AT &a=AT()) { return resize(n,Noinit()).init(a); }
167 
168  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Eye ini, const AT &a=1) { return resize(n,Noinit()).init(ini,a); }
169 
172  void resize(int m, int n) {
173  if(m!=M)
174  throw std::runtime_error("A fixed-var matrix can only be resized in the second dimension.");
175  resize(n);
176  }
177 
185  FMATVEC_ASSERT(N == A.cols(), AT);
186  return copy(A);
187  }
188 
195  template <class Type, class Row, class Col>
197  FMATVEC_ASSERT(M == A.rows(), AT);
198  FMATVEC_ASSERT(N == A.cols(), AT);
199  return copy(A);
200  }
201 
208  template<class Type, class Row, class Col>
209  inline Matrix<General,Fixed<M>,Var,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
210  FMATVEC_ASSERT(M == A.rows(), AT);
211  if(N!=A.cols()) resize(A.cols(),NONINIT);
212  return copy(A);
213  }
214  // move
215  template<class Type, class Row, class Col>
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 
432  template <int M, class AT>
433  Matrix<General,Fixed<M>,Var,AT>::Matrix(const std::string &strs) : ele(nullptr) {
434  std::istringstream iss(strs);
435  iss>>*this;
436 
437  // check end of stream
438  iss>>std::ws;
439  if(!iss.eof())
440  throw std::runtime_error("Input not fully read.");
441  }
442  template <int M, class AT> Matrix<General,Fixed<M>,Var,AT>::Matrix(const char *strs) :
443  Matrix<General,Fixed<M>,Var,AT>::Matrix(std::string(strs)) {}
444 
445  template <int M, class AT>
446  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(const AT &val) {
447  for(int i=0; i<M*N; i++)
448  e(i) = val;
449  return *this;
450  }
451 
452  template <int M, class AT>
453  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(Eye eye, const AT &val) {
454  for(int i=0; i<M; i++)
455  for(int j=0; j<N; j++)
456  e(i,j) = (i==j) ? val : 0;
457  return *this;
458  }
459 
460  template <int M, class AT> template <int M1, int M2>
461  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 {
462  FMATVEC_ASSERT(M2<M, AT);
463  FMATVEC_ASSERT(J.end()<N, AT);
464  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
465 
466  for(int i=0; i<A.rows(); i++)
467  for(int j=0; j<A.cols(); j++)
468  A.e(i,j) = e(M1+i,J.start()+j);
469 
470  return A;
471  }
472 
473  template <int M, class AT>
474  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 {
475  FMATVEC_ASSERT(I.end()<M, AT);
476  FMATVEC_ASSERT(J.end()<N, AT);
477  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
478 
479  for(int i=0; i<A.rows(); i++)
480  for(int j=0; j<A.cols(); j++)
481  A.e(i,j) = e(I.start()+i,J.start()+j);
482 
483  return A;
484  }
485 
486  template <int M, class AT>
487  inline const RowVector<Var,AT> Matrix<General,Fixed<M>,Var,AT>::row(int i) const {
488 
489  FMATVEC_ASSERT(i>=0, AT);
490  FMATVEC_ASSERT(i<M, AT);
491 
492  RowVector<Var,AT> x(N,NONINIT);
493 
494  for(int j=0; j<N; j++)
495  x.e(j) = e(i,j);
496 
497  return x;
498 
499  }
500 
501  template <int M, class AT>
502  inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(int j) const {
503 
504  FMATVEC_ASSERT(j>=0, AT);
505  FMATVEC_ASSERT(j<N, AT);
506 
507  Vector<Fixed<M>,AT> x(NONINIT);
508 
509  for(int i=0; i<M; i++)
510  x.e(i) = e(i,j);
511 
512  return x;
513 
514  }
515 
516  template <int M, class AT>
517  inline const Matrix<General,Var,Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::T() const {
518  Matrix<General,Var,Fixed<M>,AT> A(cols(),NONINIT);
519  for(int i=0; i<N; i++)
520  for(int j=0; j<M; j++)
521  A.e(i,j) = e(j,i);
522  return A;
523  }
524 
525  template <int M, class AT> template <class Row>
526  inline void Matrix<General,Fixed<M>,Var,AT>::set(int j, const Vector<Row,AT> &x) {
527  FMATVEC_ASSERT(j<cols(), AT);
528  FMATVEC_ASSERT(rows()==x.size(), AT);
529  for(int i=0; i<rows(); i++)
530  e(i,j) = x.e(i);
531  }
532 
533  template <int M, class AT> template <class Col>
534  inline void Matrix<General,Fixed<M>,Var,AT>::set(int i, const RowVector<Col,AT> &x) {
535  FMATVEC_ASSERT(i<rows(), AT);
536  FMATVEC_ASSERT(cols()==x.size(), AT);
537  for(int j=0; j<cols(); j++)
538  e(i,j) = x.e(j);
539  }
540 
541  template <int M, class AT> template<class Type, class Row, class Col>
542  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) {
543 
544  FMATVEC_ASSERT(I.end()<rows(), AT);
545  FMATVEC_ASSERT(J.end()<cols(), AT);
546  FMATVEC_ASSERT(I.size()==A.rows(), AT);
547  FMATVEC_ASSERT(J.size()==A.cols(), AT);
548 
549  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
550  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
551  e(i,j) = A.e(ii,jj);
552  }
553 
554  template <int M, class AT> template <class Row>
555  inline void Matrix<General,Fixed<M>,Var,AT>::add(int j, const Vector<Row,AT> &x) {
556  FMATVEC_ASSERT(j<cols(), AT);
557  FMATVEC_ASSERT(rows()==x.size(), AT);
558  for(int i=0; i<rows(); i++)
559  e(i,j) += x.e(i);
560  }
561 
562  template <int M, class AT> template <class Col>
563  inline void Matrix<General,Fixed<M>,Var,AT>::add(int i, const RowVector<Col,AT> &x) {
564  FMATVEC_ASSERT(i<rows(), AT);
565  FMATVEC_ASSERT(cols()==x.size(), AT);
566  for(int j=0; j<cols(); j++)
567  e(i,j) += x.e(j);
568  }
569 
570  template <int M, class AT> template<class Type, class Row, class Col>
571  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) {
572 
573  FMATVEC_ASSERT(I.end()<rows(), AT);
574  FMATVEC_ASSERT(J.end()<cols(), AT);
575  FMATVEC_ASSERT(I.size()==A.rows(), AT);
576  FMATVEC_ASSERT(J.size()==A.cols(), AT);
577 
578  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
579  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
580  e(i,j) += A.e(ii,jj);
581  }
582 
583  template <int M, class AT>
584  inline Matrix<General,Fixed<M>,Var,AT>::operator std::vector<std::vector<AT>>() const {
585  std::vector<std::vector<AT>> ret(rows());
586  for(int r=0; r<rows(); r++) {
587  ret[r].resize(cols());
588  for(int c=0; c<cols(); c++)
589  ret[r][c]=e(r,c);
590  }
591  return ret;
592  }
593 
594  template <int M, class AT>
595  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]) {
596  if(m.size() != M)
597  throw std::runtime_error("The input has "+std::to_string(m.size())+" rows but "+std::to_string(M)+" rows are required.");
598  for(int r=0; r<rows(); r++) {
599  if(static_cast<int>(m[r].size())!=cols())
600  throw std::runtime_error("The rows of the input have different length.");
601  for(int c=0; c<cols(); c++)
602  e(r,c)=m[r][c];
603  }
604  }
605 
607 
608  template <int M, class AT> template <class Type, class Row, class Col>
610  for(int i=0; i<M; i++)
611  for(int j=0; j<N; j++)
612  e(i,j) = A.e(i,j);
613  return *this;
614  }
615 
617 
618 }
619 
620 #endif
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:248
Definition: fmatvec.h:38
void resize(int m, int n)
Definition: fixed_var_general_matrix.h:172
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:41
Matrix()
Standard constructor.
Definition: fixed_var_general_matrix.h:66
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:47
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:35
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:155
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:115
Definition: types.h:88
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:126
Basic shape class for matrices.
Definition: types.h:136
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:196
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:44
Matrix(const Matrix< General, Fixed< M >, Var, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:105
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< General, Fixed< M >, Var, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:184
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39
Definition: types.h:90