fmatvec  2.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 
44  typedef AT value_type;
45 
47 
48  protected:
49 
50  int N{0};
51 
52  AT *ele;
53 
54  template <class Type, class Row, class Col> inline Matrix<General,Fixed<M>,Var,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
55 
57 
58  public:
59 
64  explicit Matrix() : ele(nullptr) { }
65 
66 // template<class Ini=All<AT>>
67 // Matrix(int n, Ini ini=All<AT>()) : N(n), ele(new AT[M*N]) {
68 // init(ini);
69 // }
70 // template<class Ini=All<AT>>
71 // Matrix(int m, int n, Ini ini=All<AT>()) : N(n), ele(new AT[M*N]) {
72 // init(ini);
73 // }
74 
75  explicit Matrix(int n, Noinit) : N(n), ele(new AT[M*N]) { }
76  explicit Matrix(int n, Init ini=INIT, const AT &a=AT()) : N(n), ele(new AT[M*N]) { init(a); }
77  explicit Matrix(int n, Eye ini, const AT &a=1) : N(n), ele(new AT[M*N]) { init(ini,a); }
78  explicit Matrix(int m, int n, Noinit) : N(n), ele(new AT[M*N]) { FMATVEC_ASSERT(m==M, AT); }
79  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); }
80  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); }
81 
87  Matrix(const Matrix<General,Fixed<M>,Var,AT> &A) : N(A.N), ele(new AT[M*N]) {
88  copy(A);
89  }
90 
96  template<class Row, class Col>
97  Matrix(const Matrix<General,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
98  FMATVEC_ASSERT(A.rows() == M, AT);
99  copy(A);
100  }
101 
107  template<class Type, class Row, class Col>
108  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : N(A.cols()), ele(new AT[M*N]) {
109  FMATVEC_ASSERT(A.rows() == M, AT);
110  copy(A);
111  }
112 
125  Matrix(const std::string &strs);
126  Matrix(const char *strs);
127 
131  delete[] ele;
132  }
133 
134  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Noinit) {
135  delete[] ele;
136  N = n;
137  ele = new AT[M*N];
138  return *this;
139  }
140 
141  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Init ini=INIT, const AT &a=AT()) { return resize(n,Noinit()).init(a); }
142 
143  Matrix<General,Fixed<M>,Var,AT>& resize(int n, Eye ini, const AT &a=1) { return resize(n,Noinit()).init(ini,a); }
144 
147  void resize(int m, int n) {
148  if(m!=M)
149  throw std::runtime_error("A fixed-var matrix can only be resized in the second dimension.");
150  resize(n);
151  }
152 
160  FMATVEC_ASSERT(N == A.cols(), AT);
161  return copy(A);
162  }
163 
170  template <class Type, class Row, class Col>
172  FMATVEC_ASSERT(M == A.rows(), AT);
173  FMATVEC_ASSERT(N == A.cols(), AT);
174  return copy(A);
175  }
176 
183  template<class Type, class Row, class Col>
184  inline Matrix<General,Fixed<M>,Var,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
185  FMATVEC_ASSERT(M == A.rows(), AT);
186  if(N!=A.cols()) resize(A.cols(),NONINIT);
187  return copy(A);
188  }
189 
199  AT& operator()(int i, int j) {
200  FMATVEC_ASSERT(i>=0, AT);
201  FMATVEC_ASSERT(j>=0, AT);
202  FMATVEC_ASSERT(i<M, AT);
203  FMATVEC_ASSERT(j<N, AT);
204 
205  return e(i,j);
206  }
207 
212  const AT& operator()(int i, int j) const {
213  FMATVEC_ASSERT(i>=0, AT);
214  FMATVEC_ASSERT(j>=0, AT);
215  FMATVEC_ASSERT(i<M, AT);
216  FMATVEC_ASSERT(j<N, AT);
217 
218  return e(i,j);
219  }
220 
221  AT& e(int i, int j) {
222  return ele[i*N+j];
223  }
224 
229  const AT& e(int i, int j) const {
230  return ele[i*N+j];
231  }
232 
233  AT& e(int i) {
234  return ele[i];
235  }
236 
241  const AT& e(int i) const {
242  return ele[i];
243  }
244 
250  AT* operator()() {return ele;}
251 
256  const AT* operator()() const {return ele;}
257 
262  int rows() const {return M;}
263 
268  int cols() const {return N;}
269 
274  int ldim() const {return N;}
275 
282  const CBLAS_TRANSPOSE blasTrans() const {
283  return CblasNoTrans;
284  }
285 
293  const CBLAS_ORDER blasOrder() const {
294  return CblasRowMajor;
295  }
296 
323  //inline Matrix<GeneralVar, AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J);
324 
329  inline const Matrix<General,Var,Var,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
330 
331  template <int M1, int M2>
332  inline const Matrix<General,Fixed<M2-M1+1>,Var,AT> operator()(const Range<Fixed<M1>,Fixed<M2>> &I, const Range<Var,Var> &J) const;
333 
334  inline const RowVector<Var,AT> row(int i) const;
335  inline const Vector<Fixed<M>,AT> col(int j) const;
336 
344  inline Matrix<General,Fixed<M>,Var,AT>& init(const AT &val=AT());
345  inline Matrix<General,Fixed<M>,Var,AT>& init(Init, const AT &a=AT()) { return init(a); }
346  inline Matrix<General,Fixed<M>,Var,AT>& init(Eye, const AT &val=1);
347  inline Matrix<General,Fixed<M>,Var,AT>& init(Noinit, const AT &a=AT()) { return *this; }
348 
353  explicit inline operator std::vector<std::vector<AT>>() const;
354 
360  explicit inline Matrix(const std::vector<std::vector<AT>> &m);
361 
362 // /*! \brief Cast to AT.
363 // *
364 // * \return The AT representation of the matrix
365 // * */
366 // explicit operator AT() const {
367 // FMATVEC_ASSERT(M==1, AT);
368 // FMATVEC_ASSERT(N==1, AT);
369 // return ele[0];
370 // }
371 //
372 // /*! \brief AT Constructor.
373 // * Constructs and initializes a matrix with a AT object.
374 // * \param v The AT the matrix will be initialized with.
375 // * */
376 // explicit Matrix(const AT &x) : N(1), ele(new AT[1]) {
377 // FMATVEC_ASSERT(M==1, AT);
378 // ele[0] = x;
379 // }
380 
381  inline const Matrix<General,Var,Fixed<M>,AT> T() const;
382 
383  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
384 
385  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
386 
387  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);
388 
389  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
390 
391  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
392 
393  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);
394  };
395 
396  template <int M, class AT>
397  Matrix<General,Fixed<M>,Var,AT>::Matrix(const std::string &strs) : ele(nullptr) {
398  std::istringstream iss(strs);
399  iss>>*this;
400 
401  // check end of stream
402  iss>>std::ws;
403  if(!iss.eof())
404  throw std::runtime_error("Input not fully read.");
405  }
406  template <int M, class AT> Matrix<General,Fixed<M>,Var,AT>::Matrix(const char *strs) :
407  Matrix<General,Fixed<M>,Var,AT>::Matrix(std::string(strs)) {}
408 
409  template <int M, class AT>
410  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(const AT &val) {
411  for(int i=0; i<M*N; i++)
412  e(i) = val;
413  return *this;
414  }
415 
416  template <int M, class AT>
417  inline Matrix<General,Fixed<M>,Var,AT>& Matrix<General,Fixed<M>,Var,AT>::init(Eye eye, const AT &val) {
418  for(int i=0; i<M; i++)
419  for(int j=0; j<N; j++)
420  e(i,j) = (i==j) ? val : 0;
421  return *this;
422  }
423 
424  template <int M, class AT> template <int M1, int M2>
425  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 {
426  FMATVEC_ASSERT(M2<M, AT);
427  FMATVEC_ASSERT(J.end()<N, AT);
428  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
429 
430  for(int i=0; i<A.rows(); i++)
431  for(int j=0; j<A.cols(); j++)
432  A.e(i,j) = e(M1+i,J.start()+j);
433 
434  return A;
435  }
436 
437  template <int M, class AT>
438  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 {
439  FMATVEC_ASSERT(I.end()<M, AT);
440  FMATVEC_ASSERT(J.end()<N, AT);
441  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
442 
443  for(int i=0; i<A.rows(); i++)
444  for(int j=0; j<A.cols(); j++)
445  A.e(i,j) = e(I.start()+i,J.start()+j);
446 
447  return A;
448  }
449 
450  template <int M, class AT>
451  inline const RowVector<Var,AT> Matrix<General,Fixed<M>,Var,AT>::row(int i) const {
452 
453  FMATVEC_ASSERT(i>=0, AT);
454  FMATVEC_ASSERT(i<M, AT);
455 
456  RowVector<Var,AT> x(N,NONINIT);
457 
458  for(int j=0; j<N; j++)
459  x.e(j) = e(i,j);
460 
461  return x;
462 
463  }
464 
465  template <int M, class AT>
466  inline const Vector<Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::col(int j) const {
467 
468  FMATVEC_ASSERT(j>=0, AT);
469  FMATVEC_ASSERT(j<N, AT);
470 
471  Vector<Fixed<M>,AT> x(NONINIT);
472 
473  for(int i=0; i<M; i++)
474  x.e(i) = e(i,j);
475 
476  return x;
477 
478  }
479 
480  template <int M, class AT>
481  inline const Matrix<General,Var,Fixed<M>,AT> Matrix<General,Fixed<M>,Var,AT>::T() const {
482  Matrix<General,Var,Fixed<M>,AT> A(cols(),NONINIT);
483  for(int i=0; i<N; i++)
484  for(int j=0; j<M; j++)
485  A.e(i,j) = e(j,i);
486  return A;
487  }
488 
489  template <int M, class AT> template <class Row>
490  inline void Matrix<General,Fixed<M>,Var,AT>::set(int j, const Vector<Row,AT> &x) {
491  FMATVEC_ASSERT(j<cols(), AT);
492  FMATVEC_ASSERT(rows()==x.size(), AT);
493  for(int i=0; i<rows(); i++)
494  e(i,j) = x.e(i);
495  }
496 
497  template <int M, class AT> template <class Col>
498  inline void Matrix<General,Fixed<M>,Var,AT>::set(int i, const RowVector<Col,AT> &x) {
499  FMATVEC_ASSERT(i<rows(), AT);
500  FMATVEC_ASSERT(cols()==x.size(), AT);
501  for(int j=0; j<cols(); j++)
502  e(i,j) = x.e(j);
503  }
504 
505  template <int M, class AT> template<class Type, class Row, class Col>
506  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) {
507 
508  FMATVEC_ASSERT(I.end()<rows(), AT);
509  FMATVEC_ASSERT(J.end()<cols(), AT);
510  FMATVEC_ASSERT(I.size()==A.rows(), AT);
511  FMATVEC_ASSERT(J.size()==A.cols(), AT);
512 
513  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
514  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
515  e(i,j) = A.e(ii,jj);
516  }
517 
518  template <int M, class AT> template <class Row>
519  inline void Matrix<General,Fixed<M>,Var,AT>::add(int j, const Vector<Row,AT> &x) {
520  FMATVEC_ASSERT(j<cols(), AT);
521  FMATVEC_ASSERT(rows()==x.size(), AT);
522  for(int i=0; i<rows(); i++)
523  e(i,j) += x.e(i);
524  }
525 
526  template <int M, class AT> template <class Col>
527  inline void Matrix<General,Fixed<M>,Var,AT>::add(int i, const RowVector<Col,AT> &x) {
528  FMATVEC_ASSERT(i<rows(), AT);
529  FMATVEC_ASSERT(cols()==x.size(), AT);
530  for(int j=0; j<cols(); j++)
531  e(i,j) += x.e(j);
532  }
533 
534  template <int M, class AT> template<class Type, class Row, class Col>
535  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) {
536 
537  FMATVEC_ASSERT(I.end()<rows(), AT);
538  FMATVEC_ASSERT(J.end()<cols(), AT);
539  FMATVEC_ASSERT(I.size()==A.rows(), AT);
540  FMATVEC_ASSERT(J.size()==A.cols(), AT);
541 
542  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
543  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
544  e(i,j) += A.e(ii,jj);
545  }
546 
547  template <int M, class AT>
548  inline Matrix<General,Fixed<M>,Var,AT>::operator std::vector<std::vector<AT>>() const {
549  std::vector<std::vector<AT>> ret(rows());
550  for(int r=0; r<rows(); r++) {
551  ret[r].resize(cols());
552  for(int c=0; c<cols(); c++)
553  ret[r][c]=e(r,c);
554  }
555  return ret;
556  }
557 
558  template <int M, class AT>
559  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]) {
560  if(m.size() != M)
561  throw std::runtime_error("The input has "+std::to_string(m.size())+" rows but "+std::to_string(M)+" rows are required.");
562  for(int r=0; r<rows(); r++) {
563  if(static_cast<int>(m[r].size())!=cols())
564  throw std::runtime_error("The rows of the input have different length.");
565  for(int c=0; c<cols(); c++)
566  e(r,c)=m[r][c];
567  }
568  }
569 
571 
572  template <int M, class AT> template <class Type, class Row, class Col>
574  for(int i=0; i<M; i++)
575  for(int j=0; j<N; j++)
576  e(i,j) = A.e(i,j);
577  return *this;
578  }
579 
581 
582 }
583 
584 #endif
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_var_general_matrix.h:212
Definition: fmatvec.h:38
void resize(int m, int n)
Definition: fixed_var_general_matrix.h:147
AT & operator()(int i, int j)
Element operator.
Definition: fixed_var_general_matrix.h:199
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Matrix()
Standard constructor.
Definition: fixed_var_general_matrix.h:64
int cols() const
Number of columns.
Definition: fixed_var_general_matrix.h:268
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
Definition: types.h:85
Definition: types.h:97
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: fixed_var_general_matrix.h:282
const AT & e(int i) const
Element operator.
Definition: fixed_var_general_matrix.h:241
const AT * operator()() const
Pointer operator.
Definition: fixed_var_general_matrix.h:256
AT * operator()()
Pointer operator.
Definition: fixed_var_general_matrix.h:250
Definition: fmatvec.h:47
int ldim() const
Leading dimension.
Definition: fixed_var_general_matrix.h:274
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:229
Namespace fmatvec.
Definition: _memory.cc:28
~Matrix()
Destructor.
Definition: fixed_var_general_matrix.h:130
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:97
Definition: types.h:84
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_var_general_matrix.h:108
Basic shape class for matrices.
Definition: types.h:132
int rows() const
Number of rows.
Definition: fixed_var_general_matrix.h:262
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:171
int rows() const
Number of rows.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_var_general_matrix.h:293
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:87
Matrix< General, Fixed< M >, Var, AT > & operator=(const Matrix< General, Fixed< M >, Var, AT > &A)
Assignment operator.
Definition: fixed_var_general_matrix.h:159
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39
Definition: types.h:86