fmatvec  2.0.0
Fast Matrix/Vector Library
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 <cstdlib>
27 #include <stdexcept>
28 
29 namespace fmatvec {
30 
39  template <class AT> class Matrix<fmatvec::General,fmatvec::Var,fmatvec::Var,AT> {
40 
41  public:
42  typedef AT value_type;
43 
45 
46  protected:
47 
48  int M{0};
49  int N{0};
50 
51  AT *ele;
52 
53  template <class Type, class Row, class Col> inline Matrix<General,Var,Var,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
54 
56 
57  public:
58 
63  explicit Matrix() : ele(nullptr) { }
64 
65 // Works with -std=gnu++0x only
66 // template<class Ini=All<AT>>
67 // Matrix(int m, int n, Ini ini=All<AT>()) : M(m), N(n), ele(new AT[M*N]) {
68 // init(ini);
69 // }
70 
71  explicit Matrix(int m, int n, Noinit) : M(m), N(n), ele(new AT[M*N]) { }
72  explicit Matrix(int m, int n, Init ini=INIT, const AT &a=AT()) : M(m), N(n), ele(new AT[M*N]) { init(a); }
73  explicit Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), N(n), ele(new AT[M*N]) { init(ini,a); }
74 
80  Matrix(const Matrix<General,Var,Var,AT> &A) : M(A.M), N(A.N), ele(new AT[M*N]) {
81  copy(A);
82  }
83 
89  template<class Row, class Col>
90  Matrix(const Matrix<General,Row,Col,AT> &A) : M(A.rows()), N(A.cols()), ele(new AT[M*N]) {
91  copy(A);
92  }
93 
99  template<class Type, class Row, class Col>
100  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), N(A.cols()), ele(new AT[M*N]) {
101  copy(A);
102  }
103 
116  Matrix(const std::string &strs);
117  Matrix(const char *strs);
118 
122  delete[] ele;
123  }
124 
125  Matrix<General,Var,Var,AT>& resize(int m, int n, Noinit) {
126  delete[] ele;
127  M = m;
128  N = n;
129  ele = new AT[M*N];
130  return *this;
131  }
132 
133  Matrix<General,Var,Var,AT>& resize(int m, int n, Init ini=INIT, const AT &a=AT()) { return resize(m,n,Noinit()).init(a); }
134 
135  Matrix<General,Var,Var,AT>& resize(int m, int n, Eye ini, const AT &a=1) { return resize(m,n,Noinit()).init(ini,a); }
136 
144  FMATVEC_ASSERT(M == A.rows(), AT);
145  FMATVEC_ASSERT(N == A.cols(), AT);
146  return copy(A);
147  }
148 
155  template <class Type, class Row, class Col>
157  FMATVEC_ASSERT(M == A.rows(), AT);
158  FMATVEC_ASSERT(N == A.cols(), AT);
159  return copy(A);
160  }
161 
168  template<class Type, class Row, class Col>
169  inline Matrix<General,Var,Var,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
170  if(M!=A.rows() || N!=A.cols()) resize(A.rows(),A.cols(),NONINIT);
171  return copy(A);
172  }
173 
174  template <class AT2>
175  operator Matrix<General,Var,Var,AT2>() const {
177  for(size_t r=0; r<rows(); ++r)
178  for(size_t c=0; c<cols(); ++c)
179  ret(r,c) = (*this)(r,c);
180  return ret;
181  }
182 
192  AT& operator()(int i, int j) {
193  FMATVEC_ASSERT(i>=0, AT);
194  FMATVEC_ASSERT(j>=0, AT);
195  FMATVEC_ASSERT(i<M, AT);
196  FMATVEC_ASSERT(j<N, AT);
197 
198  return e(i,j);
199  }
200 
205  const AT& operator()(int i, int j) const {
206  FMATVEC_ASSERT(i>=0, AT);
207  FMATVEC_ASSERT(j>=0, AT);
208  FMATVEC_ASSERT(i<M, AT);
209  FMATVEC_ASSERT(j<N, AT);
210 
211  return e(i,j);
212  }
213 
214  AT& e(int i, int j) {
215  return ele[i*N+j];
216  }
217 
222  const AT& e(int i, int j) const {
223  return ele[i*N+j];
224  }
225 
226  AT& e(int i) {
227  return ele[i];
228  }
229 
234  const AT& e(int i) const {
235  return ele[i];
236  }
237 
243  AT* operator()() {return ele;}
244 
249  const AT* operator()() const {return ele;}
250 
255  int rows() const {return M;}
256 
261  int cols() const {return N;}
262 
267  int ldim() const {return N;}
268 
275  const CBLAS_TRANSPOSE blasTrans() const {
276  return CblasNoTrans;
277  }
278 
286  const CBLAS_ORDER blasOrder() const {
287  return CblasRowMajor;
288  }
289 
295 
296  template <int M1, int M2, int N1, int N2>
297  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;
298 
299  template <int M1, int M2>
300  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;
301 
302  template <int N1, int N2>
303  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;
304 
305  inline const RowVector<Var,AT> row(int i) const;
306  inline const Vector<Var,AT> col(int j) const;
307 
315  inline Matrix<General,Var,Var,AT>& init(const AT &val=AT());
316  inline Matrix<General,Var,Var,AT>& init(Init all, const AT &a=AT()) { return init(a); }
317  inline Matrix<General,Var,Var,AT>& init(Eye eye, const AT &val=1);
318  inline Matrix<General,Var,Var,AT>& init(Noinit, const AT &a=AT()) { return *this; }
319 
324  explicit inline operator std::vector<std::vector<AT>>() const;
325 
331  explicit inline Matrix(const std::vector<std::vector<AT>> &m);
332 
333 // /*! \brief Cast to AT.
334 // *
335 // * \return The AT representation of the matrix
336 // * */
337 // explicit operator AT() const {
338 // FMATVEC_ASSERT(M==1, AT);
339 // FMATVEC_ASSERT(N==1, AT);
340 // return ele[0];
341 // }
342 //
343 // /*! \brief AT Constructor.
344 // * Constructs and initializes a matrix with a AT object.
345 // * \param x The AT the matrix will be initialized with.
346 // * */
347 // explicit Matrix(const AT &x) : M(1), N(1), ele(new AT[1]) { ele[0] = x; }
348 
349  inline const Matrix<General,Var,Var,AT> T() const;
350 
351  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
352 
353  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
354 
355  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);
356 
357  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
358 
359  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
360 
361  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);
362  };
363 
364  template <class AT>
365  Matrix<General,Var,Var,AT>::Matrix(const std::string &strs) : ele(nullptr) {
366  std::istringstream iss(strs);
367  iss>>*this;
368 
369  // check end of stream
370  iss>>std::ws;
371  if(!iss.eof())
372  throw std::runtime_error("Input not fully read.");
373  }
374  template <class AT> Matrix<General,Var,Var,AT>::Matrix(const char *strs) : Matrix<General,Var,Var,AT>::Matrix(std::string(strs)) {}
375 
376  template <class AT>
377  inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::init(const AT &val) {
378  for(int i=0; i<M*N; i++)
379  e(i) = val;
380  return *this;
381  }
382 
383  template <class AT>
384  inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::init(Eye eye, const AT &val) {
385  for(int i=0; i<M; i++)
386  for(int j=0; j<N; j++)
387  e(i,j) = (i==j) ? val : 0;
388  return *this;
389  }
390 
391  template <class AT>
392  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 {
393  FMATVEC_ASSERT(I.end()<M, AT);
394  FMATVEC_ASSERT(J.end()<N, AT);
395  Matrix<General,Var,Var,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
396 
397  for(int i=0; i<A.rows(); i++)
398  for(int j=0; j<A.cols(); j++)
399  A.e(i,j) = e(I.start()+i,J.start()+j);
400 
401  return A;
402  }
403 
404  template <class AT> template <int M1, int M2, int N1, int N2>
405  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 {
406  FMATVEC_ASSERT(M2<M, AT);
407  FMATVEC_ASSERT(N2<N, AT);
408  Matrix<General,Fixed<M2-M1+1>,Fixed<N2-N1+1>,AT> A(NONINIT);
409 
410  for(int i=0; i<A.rows(); i++)
411  for(int j=0; j<A.cols(); j++)
412  A.e(i,j) = e(I.start()+i,J.start()+j);
413 
414  return A;
415  }
416 
417  template <class AT> template <int M1, int M2>
418  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 {
419  FMATVEC_ASSERT(M2<M, AT);
420  FMATVEC_ASSERT(J.end()<N, AT);
421  Matrix<General,Fixed<M2-M1+1>,Var,AT> A(J.end()-J.start()+1,NONINIT);
422 
423  for(int i=0; i<A.rows(); i++)
424  for(int j=0; j<A.cols(); j++)
425  A.e(i,j) = e(I.start()+i,J.start()+j);
426 
427  return A;
428  }
429 
430  template <class AT> template <int N1, int N2>
431  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 {
432  FMATVEC_ASSERT(I.end()<M, AT);
433  FMATVEC_ASSERT(N2<N, AT);
434  Matrix<General,Var,Fixed<N2-N1+1>,AT> A(I.end()-I.start()+1,NONINIT);
435 
436  for(int i=0; i<A.rows(); i++)
437  for(int j=0; j<A.cols(); j++)
438  A.e(i,j) = e(I.start()+i,J.start()+j);
439 
440  return A;
441  }
442 
443  template <class AT>
444  inline const RowVector<Var,AT> Matrix<General,Var,Var,AT>::row(int i) const {
445 
446  FMATVEC_ASSERT(i>=0, AT);
447  FMATVEC_ASSERT(i<M, AT);
448 
449  RowVector<Var,AT> x(N,NONINIT);
450 
451  for(int j=0; j<N; j++)
452  x.e(j) = e(i,j);
453 
454  return x;
455 
456  }
457 
458  template <class AT>
459  inline const Vector<Var,AT> Matrix<General,Var,Var,AT>::col(int j) const {
460 
461  FMATVEC_ASSERT(j>=0, AT);
462  FMATVEC_ASSERT(j<N, AT);
463 
464  Vector<Var,AT> x(M,NONINIT);
465 
466  for(int i=0; i<M; i++)
467  x.e(i) = e(i,j);
468 
469  return x;
470 
471  }
472 
473  template <class AT>
474  inline const Matrix<General,Var,Var,AT> Matrix<General,Var,Var,AT>::T() const {
475  Matrix<General,Var,Var,AT> A(N,M,NONINIT);
476  for(int i=0; i<N; i++)
477  for(int j=0; j<M; j++)
478  A.e(i,j) = e(j,i);
479  return A;
480  }
481 
482  template <class AT> template <class Row>
483  inline void Matrix<General,Var,Var,AT>::set(int j, const Vector<Row,AT> &x) {
484  FMATVEC_ASSERT(j<cols(), AT);
485  FMATVEC_ASSERT(rows()==x.size(), AT);
486  for(int i=0; i<rows(); i++)
487  e(i,j) = x.e(i);
488  }
489 
490  template <class AT> template <class Col>
491  inline void Matrix<General,Var,Var,AT>::set(int i, const RowVector<Col,AT> &x) {
492  FMATVEC_ASSERT(i<rows(), AT);
493  FMATVEC_ASSERT(cols()==x.size(), AT);
494  for(int j=0; j<cols(); j++)
495  e(i,j) = x.e(j);
496  }
497 
498  template <class AT> template<class Type, class Row, class Col>
499  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) {
500 
501  FMATVEC_ASSERT(I.end()<rows(), AT);
502  FMATVEC_ASSERT(J.end()<cols(), AT);
503  FMATVEC_ASSERT(I.size()==A.rows(), AT);
504  FMATVEC_ASSERT(J.size()==A.cols(), AT);
505 
506  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
507  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
508  e(i,j) = A.e(ii,jj);
509  }
510 
511  template <class AT> template <class Row>
512  inline void Matrix<General,Var,Var,AT>::add(int j, const Vector<Row,AT> &x) {
513  FMATVEC_ASSERT(j<cols(), AT);
514  FMATVEC_ASSERT(rows()==x.size(), AT);
515  for(int i=0; i<rows(); i++)
516  e(i,j) += x.e(i);
517  }
518 
519  template <class AT> template <class Col>
520  inline void Matrix<General,Var,Var,AT>::add(int i, const RowVector<Col,AT> &x) {
521  FMATVEC_ASSERT(i<rows(), AT);
522  FMATVEC_ASSERT(cols()==x.size(), AT);
523  for(int j=0; j<cols(); j++)
524  e(i,j) += x.e(j);
525  }
526 
527  template <class AT> template<class Type, class Row, class Col>
528  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) {
529 
530  FMATVEC_ASSERT(I.end()<rows(), AT);
531  FMATVEC_ASSERT(J.end()<cols(), AT);
532  FMATVEC_ASSERT(I.size()==A.rows(), AT);
533  FMATVEC_ASSERT(J.size()==A.cols(), AT);
534 
535  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
536  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
537  e(i,j) += A.e(ii,jj);
538  }
539 
540  template <class AT>
541  inline Matrix<General,Var,Var,AT>::operator std::vector<std::vector<AT>>() const {
542  std::vector<std::vector<AT>> ret(rows());
543  for(int r=0; r<rows(); r++) {
544  ret[r].resize(cols());
545  for(int c=0; c<cols(); c++)
546  ret[r][c]=e(r,c);
547  }
548  return ret;
549  }
550 
551  template <class AT>
552  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]) {
553  for(int r=0; r<rows(); r++) {
554  if(static_cast<int>(m[r].size())!=cols())
555  throw std::runtime_error("The rows of the input have different length.");
556  for(int c=0; c<cols(); c++)
557  e(r,c)=m[r][c];
558  }
559  }
560 
562 
563  template <class AT> template <class Type, class Row, class Col>
564  inline Matrix<General,Var,Var,AT>& Matrix<General,Var,Var,AT>::copy(const Matrix<Type,Row,Col,AT> &A) {
565  for(int i=0; i<M; i++)
566  for(int j=0; j<N; j++)
567  e(i,j) = A.e(i,j);
568  return *this;
569  }
570 
572 
573 }
574 
575 #endif
int end() const
Last element.
Definition: range.h:91
Definition: fmatvec.h:38
Matrix< General, Var, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_general_matrix.h:156
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
Definition: types.h:85
Definition: types.h:97
const AT & operator()(int i, int j) const
Element operator.
Definition: var_general_matrix.h:205
int ldim() const
Leading dimension.
Definition: var_general_matrix.h:267
Matrix< General, Var, Var, AT > & operator=(const Matrix< General, Var, Var, AT > &A)
Assignment operator.
Definition: var_general_matrix.h:143
Matrix(const Matrix< General, Var, Var, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:80
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: var_general_matrix.h:275
int start() const
First element.
Definition: range.h:85
Definition: fmatvec.h:47
AT * operator()()
Pointer operator.
Definition: var_general_matrix.h:243
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:90
~Matrix()
Destructor.
Definition: var_general_matrix.h:121
const AT & e(int i) const
Element operator.
Definition: var_general_matrix.h:234
int cols() const
Number of columns.
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_general_matrix.h:100
This is an index class for creating submatrices.
Definition: range.h:35
int cols() const
Number of columns.
Definition: var_general_matrix.h:261
Namespace fmatvec.
Definition: _memory.cc:28
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_general_matrix.h:286
int size() const
Size.
Definition: range.h:97
int rows() const
Number of rows.
Definition: var_general_matrix.h:255
Matrix()
Standard constructor.
Definition: var_general_matrix.h:63
const AT & e(int i, int j) const
Element operator.
Definition: var_general_matrix.h:222
Definition: types.h:84
Basic shape class for matrices.
Definition: types.h:132
AT & operator()(int i, int j)
Element operator.
Definition: var_general_matrix.h:192
const AT * operator()() const
Pointer operator.
Definition: var_general_matrix.h:249
int rows() const
Number of rows.
This is an index class for creating submatrices.
Definition: range.h:44
This is a vector class of general shape in dense storage format.
Definition: var_row_vector.h:39