fmatvec  2.0.0
Fast Matrix/Vector Library
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 general_matrix_h
23 #define general_matrix_h
24 
25 #include "_memory.h"
26 #include "types.h"
27 #include "matrix.h"
28 #include <cstdlib>
29 #include <stdexcept>
30 
31 namespace fmatvec {
32 
41  template <class AT> class Matrix<General,Ref,Ref,AT> {
42 
43  public:
44 
45  typedef AT value_type;
46 
48 
49  friend class Vector<Ref,AT>;
50  friend class RowVector<Ref,AT>;
51  friend class SquareMatrix<Ref,AT>;
52  friend class Matrix<Symmetric,Ref,Ref,AT>;
53 
54  protected:
55 
56  Memory<AT> memory;
57  AT *ele;
58  int m{0};
59  int n{0};
60  int lda{0};
61 
62  template <class Type, class Row, class Col> inline Matrix<General,Ref,Ref,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
63 
64  const AT* elePtr(int i, int j) const {
65  return ele+i+j*lda;
66  }
67 
68  AT* elePtr(int i, int j) {
69  return ele+i+j*lda;
70  }
71 
73 
74  public:
75 
80  explicit Matrix() : memory(), ele(nullptr) { }
81 
82 // Works with -std=gnu++0x only
83 // template<class Ini=All<AT>>
84 // Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) {
85 // init(ini);
86 // }
87 
88  explicit Matrix(int m_, int n_, Noinit) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { }
89  explicit Matrix(int m_, int n_, Init ini=INIT, const AT &a=AT()) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { init0(a); }
90  explicit Matrix(int m_, int n_, Eye ini, const AT &a=1) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { init(ini,a); }
91 
97  Matrix(const Matrix<General,Ref,Ref,AT> &A) : memory(A.m*A.n), ele((AT*)memory.get()) ,m(A.m), n(A.n), lda(m) {
98  copy(A);
99  }
100 
106  template<class Row, class Col>
107  Matrix(const Matrix<General,Row,Col,AT> &A) : memory(A.rows()*A.cols()), ele((AT*)memory.get()), m(A.rows()), n(A.cols()), lda(m) {
108  copy(A);
109  }
110 
116  template<class Type, class Row, class Col>
117  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : memory(A.rows()*A.cols()), ele((AT*)memory.get()), m(A.rows()), n(A.cols()), lda(m) {
118  copy(A);
119  }
120 
128  explicit Matrix(int m_, int n_, AT* ele_) : memory(), ele(ele_), m(m_), n(n_), lda(m_) {
129  }
130 
143  Matrix(const std::string &strs);
144  Matrix(const char *strs);
145 
148  ~Matrix() = default;
149 
150  Matrix<General,Ref,Ref,AT>& resize(int m_, int n_, Noinit) {
151  m = m_; n = n_; lda = m;
152  memory.resize(m*n);
153  ele = (AT*)memory.get();
154  return *this;
155  }
156 
157  Matrix<General,Ref,Ref,AT>& resize(int m, int n, Init ini=INIT, const AT &a=AT()) { return resize(m,n,Noinit()).init0(a); }
158 
159  Matrix<General,Ref,Ref,AT>& resize(int m, int n, Eye ini, const AT &a=1) { return resize(m,n,Noinit()).init(ini,a); }
160 
168  FMATVEC_ASSERT(m == A.rows(), AT);
169  FMATVEC_ASSERT(n == A.cols(), AT);
170  return copy(A);
171  }
172 
179  template<class Type, class Row, class Col>
181  FMATVEC_ASSERT(m == A.rows(), AT);
182  FMATVEC_ASSERT(n == A.cols(), AT);
183  return copy(A);
184  }
185 
193  m=A.m;
194  n=A.n;
195  memory = A.memory;
196  ele = A.ele;
197  lda = A.lda;
198  return *this;
199  }
200 
207  template<class Type, class Row, class Col>
208  inline Matrix<General,Ref,Ref,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
209  if(m!=A.rows() || n!=A.cols()) resize(A.rows(),A.cols(),NONINIT);
210  return copy(A);
211  }
212 
213  template <class AT2>
214  operator Matrix<General,Ref,Ref,AT2>() const {
216  for(size_t r=0; r<rows(); ++r)
217  for(size_t c=0; c<cols(); ++c)
218  ret(r,c) = (*this)(r,c);
219  return ret;
220  }
221 
231  AT& operator()(int i, int j) {
232  FMATVEC_ASSERT(i>=0, AT);
233  FMATVEC_ASSERT(j>=0, AT);
234  FMATVEC_ASSERT(i<m, AT);
235  FMATVEC_ASSERT(j<n, AT);
236 
237  return e(i,j);
238  }
239 
244  const AT& operator()(int i, int j) const {
245  FMATVEC_ASSERT(i>=0, AT);
246  FMATVEC_ASSERT(j>=0, AT);
247  FMATVEC_ASSERT(i<m, AT);
248  FMATVEC_ASSERT(j<n, AT);
249 
250  return e(i,j);
251  }
252 
253  AT& e(int i, int j) {
254  return ele[i+j*lda];
255  }
256 
257  const AT& e(int i, int j) const {
258  return ele[i+j*lda];
259  }
260 
266  AT* operator()() {return ele;}
267 
272  const AT* operator()() const {return ele;}
273 
278  int rows() const {return m;}
279 
284  int cols() const {return n;}
285 
290  int ldim() const {return lda;}
291 
298  const CBLAS_TRANSPOSE blasTrans() const {
299  return CblasNoTrans;
300  }
301 
309  const CBLAS_ORDER blasOrder() const {
310  return CblasColMajor;
311  }
312 
344  inline const Matrix<General,Ref,Ref,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
345 
348  inline const SquareMatrix<Ref,AT> operator()(const Range<Var,Var> &I) const;
349 
352  inline const RowVector<Ref,AT> row(int i) const;
353 
356  inline const Vector<Ref,AT> col(int j) const;
357 
365  inline Matrix<General,Ref,Ref,AT>& init(const AT &val=AT());
366  inline Matrix<General,Ref,Ref,AT>& init(Init, const AT &a=AT()) { return init(a); }
367  inline Matrix<General,Ref,Ref,AT>& init(Eye, const AT &val=1);
368  inline Matrix<General,Ref,Ref,AT>& init(Noinit, const AT &a=AT()) { return *this; }
369  inline Matrix<General,Ref,Ref,AT>& init0(const AT &val=AT());
370  inline Matrix<General,Ref,Ref,AT>& init0(Init, const AT &a=AT()) { return init0(a); }
371 
376  explicit inline operator std::vector<std::vector<AT>>() const;
377 
383  explicit inline Matrix(const std::vector<std::vector<AT>> &m);
384 
385 // /*! \brief Cast to AT.
386 // *
387 // * \return The AT representation of the matrix
388 // * */
389 // explicit operator AT() const {
390 // FMATVEC_ASSERT(m==1, AT);
391 // FMATVEC_ASSERT(n==1, AT);
392 // return e(0);
393 // }
394 //
395 // /*! \brief AT Constructor.
396 // * Constructs and initializes a matrix with a AT object.
397 // * \param x The AT the matrix will be initialized with.
398 // * */
399 // explicit Matrix(const AT &x) : memory(1), ele((AT*)memory.get()), m(1), n(1), lda(1) { ele[0] = x; }
400 
401  const Matrix<General,Ref,Ref,AT> T() const;
402 
403  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
404 
405  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
406 
407  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);
408 
409  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
410 
411  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
412 
413  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);
414 
415  template<class Type, class Row, class Col> inline void sub(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A);
416 
417  inline void ref(Matrix<General,Ref,Ref,AT> &A, const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J);
418 
419  inline void ref(Matrix<Symmetric,Ref,Ref,AT> &A, const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J);
420 
427  int countElements() const;
428  };
429 
430  template <class AT>
431  Matrix<General,Ref,Ref,AT>::Matrix(const std::string &strs) : memory(), ele(nullptr) {
432  std::istringstream iss(strs);
433  iss>>*this;
434 
435  // check end of stream
436  iss>>std::ws;
437  if(!iss.eof())
438  throw std::runtime_error("Input not fully read.");
439  }
440  template <class AT> Matrix<General,Ref,Ref,AT>::Matrix(const char *strs) : Matrix<General,Ref,Ref,AT>::Matrix(std::string(strs)) {}
441 
442  template <class AT>
443  inline Matrix<General,Ref,Ref,AT>& Matrix<General,Ref,Ref,AT>::init0(const AT &val) {
444  for(int i=0; i<m*n; i++) ele[i]=val;
445  return *this;
446  }
447 
448  template <class AT>
450  for(int i=0; i<rows(); i++)
451  for(int j=0; j<cols(); j++)
452  e(i,j) = val;
453  return *this;
454  }
455 
456  template <class AT>
458  for(int i=0; i<m; i++)
459  for(int j=0; j<n; j++)
460  e(i,j) = (i==j) ? val : 0;
461  return *this;
462  }
463 
464  template <class AT>
466  FMATVEC_ASSERT(I.end()<m, AT);
467  FMATVEC_ASSERT(J.end()<n, AT);
468  Matrix<General,Ref,Ref,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
469 
470  for(int i=0; i<A.rows(); i++)
471  for(int j=0; j<A.cols(); j++)
472  A.e(i,j) = e(I.start()+i,J.start()+j);
473 
474  return A;
475  }
476 
477  template <class AT>
479  FMATVEC_ASSERT(I.end()<m, AT);
480  SquareMatrix<Ref,AT> A(I.end()-I.start()+1,NONINIT);
481 
482  for(int i=0; i<A.rows(); i++)
483  for(int j=0; j<A.cols(); j++)
484  A.e(i,j) = e(I.start()+i,I.start()+j);
485 
486  return A;
487  }
488 
489  template <class AT>
491 
492  FMATVEC_ASSERT(i>=0, AT);
493  FMATVEC_ASSERT(i<m, AT);
494 
495  RowVector<Ref,AT> x(n,NONINIT);
496 
497  for(int j=0; j<n; j++)
498  x.e(j) = e(i,j);
499 
500  return x;
501  }
502 
503  template <class AT>
505 
506  FMATVEC_ASSERT(j>=0, AT);
507  FMATVEC_ASSERT(j<n, AT);
508 
509  Vector<Ref,AT> x(m,NONINIT);
510 
511  for(int i=0; i<m; i++)
512  x.e(i) = e(i,j);
513 
514  return x;
515  }
516 
517  template <class AT>
518  inline Matrix<General,Ref,Ref,AT>::operator std::vector<std::vector<AT>>() const {
519  std::vector<std::vector<AT>> ret(rows());
520  for(int r=0; r<rows(); r++) {
521  ret[r].resize(cols());
522  for(int c=0; c<cols(); c++)
523  ret[r][c]= e(r,c);
524  }
525  return ret;
526  }
527 
528  template <class AT>
529  inline Matrix<General,Ref,Ref,AT>::Matrix(const std::vector<std::vector<AT>> &m) : memory(m.size()*m[0].size()), ele((AT*)memory.get()), m(static_cast<int>(m.size())), n(static_cast<int>(m[0].size())), lda(static_cast<int>(m.size())) {
530  for(int r=0; r<rows(); r++) {
531  if(static_cast<int>(m[r].size())!=cols())
532  throw std::runtime_error("The rows of the input have different length.");
533  for(int c=0; c<cols(); c++)
534  e(r,c)=m[r][c];
535  }
536  }
537 
539 
540  template <class AT> template <class Type, class Row, class Col>
542  for(int i=0; i<m; i++)
543  for(int j=0; j<n; j++)
544  e(i,j) = A.e(i,j);
545  return *this;
546  }
547 
548  template <class AT>
549  inline const Matrix<General,Ref,Ref,AT> Matrix<General,Ref,Ref,AT>::T() const {
550  Matrix<General,Ref,Ref,AT> A(n,m,NONINIT);
551  for(int i=0; i<n; i++)
552  for(int j=0; j<m; j++)
553  A.e(i,j) = e(j,i);
554  return A;
555  }
556 
557  template <class AT> template <class Row>
558  inline void Matrix<General,Ref,Ref,AT>::set(int j, const Vector<Row,AT> &x) {
559  FMATVEC_ASSERT(j<cols(), AT);
560  FMATVEC_ASSERT(rows()==x.size(), AT);
561  for(int i=0; i<rows(); i++)
562  e(i,j) = x.e(i);
563  }
564 
565  template <class AT> template <class Col>
566  inline void Matrix<General,Ref,Ref,AT>::set(int i, const RowVector<Col,AT> &x) {
567  FMATVEC_ASSERT(i<rows(), AT);
568  FMATVEC_ASSERT(cols()==x.size(), AT);
569  for(int j=0; j<cols(); j++)
570  e(i,j) = x.e(j);
571  }
572 
573  template <class AT> template<class Type, class Row, class Col>
574  inline void Matrix<General,Ref,Ref,AT>::set(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
575 
576  FMATVEC_ASSERT(I.end()<rows(), AT);
577  FMATVEC_ASSERT(J.end()<cols(), AT);
578  FMATVEC_ASSERT(I.size()==A.rows(), AT);
579  FMATVEC_ASSERT(J.size()==A.cols(), AT);
580 
581  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
582  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
583  e(i,j) = A.e(ii,jj);
584  }
585 
586  template <class AT> template <class Row>
587  inline void Matrix<General,Ref,Ref,AT>::add(int j, const Vector<Row,AT> &x) {
588  FMATVEC_ASSERT(j<cols(), AT);
589  FMATVEC_ASSERT(rows()==x.size(), AT);
590  for(int i=0; i<rows(); i++)
591  e(i,j) += x.e(i);
592  }
593 
594  template <class AT> template <class Col>
595  inline void Matrix<General,Ref,Ref,AT>::add(int i, const RowVector<Col,AT> &x) {
596  FMATVEC_ASSERT(i<rows(), AT);
597  FMATVEC_ASSERT(cols()==x.size(), AT);
598  for(int j=0; j<cols(); j++)
599  e(i,j) += x.e(j);
600  }
601 
602  template <class AT> template<class Type, class Row, class Col>
603  inline void Matrix<General,Ref,Ref,AT>::add(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
604 
605  FMATVEC_ASSERT(I.end()<rows(), AT);
606  FMATVEC_ASSERT(J.end()<cols(), AT);
607  FMATVEC_ASSERT(I.size()==A.rows(), AT);
608  FMATVEC_ASSERT(J.size()==A.cols(), AT);
609 
610  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
611  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
612  e(i,j) += A.e(ii,jj);
613  }
614 
615  template <class AT> template<class Type, class Row, class Col>
616  inline void Matrix<General,Ref,Ref,AT>::sub(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
617 
618  FMATVEC_ASSERT(I.end()<rows(), AT);
619  FMATVEC_ASSERT(J.end()<cols(), AT);
620  FMATVEC_ASSERT(I.size()==A.rows(), AT);
621  FMATVEC_ASSERT(J.size()==A.cols(), AT);
622 
623  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
624  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
625  e(i,j) -= A.e(ii,jj);
626  }
627 
628  template <class AT>
629  inline void Matrix<General,Ref,Ref,AT>::ref(Matrix<General,Ref,Ref,AT> &A, const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J) {
630  FMATVEC_ASSERT(I.end()<A.rows(), AT);
631  FMATVEC_ASSERT(J.end()<A.cols(), AT);
632  m=I.size();
633  n=J.size();
634  memory = A.memory;
635  ele = A.elePtr(I.start(),J.start());
636  lda = A.lda;
637  }
638 
639  template <class AT>
640  inline void Matrix<General,Ref,Ref,AT>::ref(Matrix<Symmetric,Ref,Ref,AT> &A, const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J) {
641  FMATVEC_ASSERT(I.end()<A.size(), AT);
642  FMATVEC_ASSERT(J.end()<A.size(), AT);
643  FMATVEC_ASSERT(I.start()>=J.start(), AT);
644  FMATVEC_ASSERT(J.end()<=I.start(), AT);
645  m=I.size();
646  n=J.size();
647  memory = A.memory;
648  ele = A.elePtr(I.start(),J.start());
649  lda = A.lda;
650  }
651 
652  template <class AT>
654  int k = 0;
655  for (int i = 0; i < m; i++) {
656  for (int j = 0; j < n; j++) {
657  if (fabs(e(i, j)) > 1e-16 || i==j) {
658  k++;
659  }
660  }
661  }
662  return k;
663  }
664 
666 
667 }
668 
669 #endif
int end() const
Last element.
Definition: range.h:91
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: general_matrix.h:309
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: general_matrix.h:107
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Shape class for symmetric matrices.
Definition: types.h:148
int rows() const
Number of rows.
Definition: general_matrix.h:278
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
Definition: types.h:85
AT & operator()(int i, int j)
Element operator.
Definition: general_matrix.h:231
Matrix(const Matrix< General, Ref, Ref, AT > &A)
Copy Constructor.
Definition: general_matrix.h:97
Definition: fmatvec.h:50
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: general_matrix.h:298
Definition: types.h:94
const Matrix< General, Ref, Ref, AT > T() const
Cast to AT.
This is a rowvector class of general shape in dense storage format.
Definition: row_vector.h:36
int start() const
First element.
Definition: range.h:85
Matrix< General, Ref, Ref, AT > & init(const AT &val=AT())
Initialization.
Definition: general_matrix.h:449
Definition: fmatvec.h:44
Definition: fmatvec.h:47
int countElements() const
Count nonzero elements.
const AT * operator()() const
Pointer operator.
Definition: general_matrix.h:272
int cols() const
Number of columns.
This is a matrix class for general matrices.
Definition: general_matrix.h:41
Namespace fmatvec.
Definition: _memory.cc:28
int cols() const
Number of columns.
Definition: general_matrix.h:284
int size() const
Size.
Definition: range.h:97
Matrix< General, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: general_matrix.h:180
AT * operator()()
Pointer operator.
Definition: general_matrix.h:266
Matrix< General, Ref, Ref, AT > & operator&=(Matrix< General, Ref, Ref, AT > &A)
Reference operator.
Definition: general_matrix.h:192
int ldim() const
Leading dimension.
Definition: general_matrix.h:290
Matrix(int m_, int n_, AT *ele_)
Regular Constructor.
Definition: general_matrix.h:128
Definition: types.h:84
Basic shape class for matrices.
Definition: types.h:132
const AT & operator()(int i, int j) const
Element operator.
Definition: general_matrix.h:244
This is a vector class of general shape in dense storage format.
Definition: vector.h:39
This is a matrix class of general quadratic matrices.
Definition: square_matrix.h:39
Matrix()
Standard constructor.
Definition: general_matrix.h:80
Matrix< General, Ref, Ref, AT > & operator=(const Matrix< General, Ref, Ref, AT > &A)
Assignment operator.
Definition: general_matrix.h:167
int rows() const
Number of rows.
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: general_matrix.h:117
This is an index class for creating submatrices.
Definition: range.h:44
Definition: types.h:86