fmatvec  0.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  static constexpr bool isVector {false};
45 
46  typedef AT value_type;
47  typedef General shape_type;
48 
50 
51  friend class Vector<Ref,AT>;
52  friend class RowVector<Ref,AT>;
53  friend class SquareMatrix<Ref,AT>;
54  friend class Matrix<Symmetric,Ref,Ref,AT>;
55 
56  protected:
57 
58  template<bool Const>
59  class Iterator {
61  public:
62  Iterator(std::conditional_t<Const, const Mat, Mat> &mat_, int row_, int col_) : mat(mat_), row(row_), col(col_) {}
63  bool operator!=(const Iterator& x) const { return row!=x.row || col!=x.col; }
64  Iterator& operator++() { ++row; if(row>=mat.m) { row=0; ++col; } return *this; }
65  std::conditional_t<Const, const AT, AT>& operator*() { return mat.e(row,col); }
66  std::conditional_t<Const, const AT, AT>& operator->() { return mat.e(row,col); }
67  const AT& operator*() const { return mat.e(row,col); }
68  const AT& operator->() const { return mat.e(row,col); }
69  private:
70  std::conditional_t<Const, const Mat, Mat> &mat;
71  int row;
72  int col;
73  };
74 
75  Memory<AT> memory;
76  AT *ele;
77  int m{0};
78  int n{0};
79  int lda{0};
80 
81  template <class Type, class Row, class Col> inline Matrix<General,Ref,Ref,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
82 
83  const AT* elePtr(int i, int j) const {
84  return ele+i+j*lda;
85  }
86 
87  AT* elePtr(int i, int j) {
88  return ele+i+j*lda;
89  }
90 
92 
93  public:
94 
99  explicit Matrix() : memory(), ele(nullptr) { }
100 
101 // Works with -std=gnu++0x only
102 // template<class Ini=All<AT>>
103 // Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) {
104 // init(ini);
105 // }
106 
107  explicit Matrix(int m_, int n_, Noinit) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { }
108  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); }
109  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); }
110 
116  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) {
117  copy(A);
118  }
119 
125  template<class Row, class Col>
126  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) {
127  copy(A);
128  }
129 
135  template<class Type, class Row, class Col>
136  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) {
137  copy(A);
138  }
139 
147  explicit Matrix(int m_, int n_, AT* ele_) : memory(), ele(ele_), m(m_), n(n_), lda(m_) {
148  }
149 
162  Matrix(const std::string &strs);
163  Matrix(const char *strs);
164 
167  ~Matrix() = default;
168 
169  typedef Iterator<false> iterator;
170  typedef Iterator<true> const_iterator;
171  iterator begin() { return iterator(*this,0,0); }
172  iterator end() { return iterator(*this,0,n); }
173  const_iterator begin() const { return const_iterator(*this,0,0); }
174  const_iterator end() const { return const_iterator(*this,0,n); }
175 
176  Matrix<General,Ref,Ref,AT>& resize(int m_, int n_, Noinit) {
177  m = m_; n = n_; lda = m;
178  memory.resize(m*n);
179  ele = (AT*)memory.get();
180  return *this;
181  }
182 
183  Matrix<General,Ref,Ref,AT>& resize(int m, int n, Init ini=INIT, const AT &a=AT()) { return resize(m,n,Noinit()).init0(a); }
184 
185  Matrix<General,Ref,Ref,AT>& resize(int m, int n, Eye ini, const AT &a=1) { return resize(m,n,Noinit()).init(ini,a); }
186 
194  FMATVEC_ASSERT(m == A.rows(), AT);
195  FMATVEC_ASSERT(n == A.cols(), AT);
196  return copy(A);
197  }
198 
205  template<class Type, class Row, class Col>
207  FMATVEC_ASSERT(m == A.rows(), AT);
208  FMATVEC_ASSERT(n == A.cols(), AT);
209  return copy(A);
210  }
211 
219  m=A.m;
220  n=A.n;
221  memory = A.memory;
222  ele = A.ele;
223  lda = A.lda;
224  return *this;
225  }
226 
233  template<class Type, class Row, class Col>
234  inline Matrix<General,Ref,Ref,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
235  if(m!=A.rows() || n!=A.cols()) resize(A.rows(),A.cols(),NONINIT);
236  return copy(A);
237  }
238 
239  template <class AT2>
240  operator Matrix<General,Ref,Ref,AT2>() const {
242  for(size_t r=0; r<rows(); ++r)
243  for(size_t c=0; c<cols(); ++c)
244  ret(r,c) = (*this)(r,c);
245  return ret;
246  }
247 
257  AT& operator()(int i, int j) {
258  FMATVEC_ASSERT(i>=0, AT);
259  FMATVEC_ASSERT(j>=0, AT);
260  FMATVEC_ASSERT(i<m, AT);
261  FMATVEC_ASSERT(j<n, AT);
262 
263  return e(i,j);
264  }
265 
270  const AT& operator()(int i, int j) const {
271  FMATVEC_ASSERT(i>=0, AT);
272  FMATVEC_ASSERT(j>=0, AT);
273  FMATVEC_ASSERT(i<m, AT);
274  FMATVEC_ASSERT(j<n, AT);
275 
276  return e(i,j);
277  }
278 
279  AT& e(int i, int j) {
280  return ele[i+j*lda];
281  }
282 
283  const AT& e(int i, int j) const {
284  return ele[i+j*lda];
285  }
286 
292  AT* operator()() {return ele;}
293 
298  const AT* operator()() const {return ele;}
299 
304  int rows() const {return m;}
305 
310  int cols() const {return n;}
311 
316  int ldim() const {return lda;}
317 
324  const CBLAS_TRANSPOSE blasTrans() const {
325  return CblasNoTrans;
326  }
327 
335  const CBLAS_ORDER blasOrder() const {
336  return CblasColMajor;
337  }
338 
370  inline const Matrix<General,Ref,Ref,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
371 
374  inline const SquareMatrix<Ref,AT> operator()(const Range<Var,Var> &I) const;
375 
378  inline const RowVector<Ref,AT> row(int i) const;
379 
382  inline const Vector<Ref,AT> col(int j) const;
383 
391  inline Matrix<General,Ref,Ref,AT>& init(const AT &val=AT());
392  inline Matrix<General,Ref,Ref,AT>& init(Init, const AT &a=AT()) { return init(a); }
393  inline Matrix<General,Ref,Ref,AT>& init(Eye, const AT &val=1);
394  inline Matrix<General,Ref,Ref,AT>& init(Noinit, const AT &a=AT()) { return *this; }
395  inline Matrix<General,Ref,Ref,AT>& init0(const AT &val=AT());
396  inline Matrix<General,Ref,Ref,AT>& init0(Init, const AT &a=AT()) { return init0(a); }
397 
402  explicit inline operator std::vector<std::vector<AT>>() const;
403 
409  explicit inline Matrix(const std::vector<std::vector<AT>> &m);
410 
411 // /*! \brief Cast to AT.
412 // *
413 // * \return The AT representation of the matrix
414 // * */
415 // explicit operator AT() const {
416 // FMATVEC_ASSERT(m==1, AT);
417 // FMATVEC_ASSERT(n==1, AT);
418 // return e(0);
419 // }
420 //
421 // /*! \brief AT Constructor.
422 // * Constructs and initializes a matrix with a AT object.
423 // * \param x The AT the matrix will be initialized with.
424 // * */
425 // explicit Matrix(const AT &x) : memory(1), ele((AT*)memory.get()), m(1), n(1), lda(1) { ele[0] = x; }
426 
427  const Matrix<General,Ref,Ref,AT> T() const;
428 
429  template<class Row> inline void set(int j, const Vector<Row,AT> &x);
430 
431  template<class Col> inline void set(int i, const RowVector<Col,AT> &x);
432 
433  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);
434 
435  template<class Row> inline void add(int j, const Vector<Row,AT> &x);
436 
437  template<class Col> inline void add(int i, const RowVector<Col,AT> &x);
438 
439  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);
440 
441  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);
442 
443  inline void ref(Matrix<General,Ref,Ref,AT> &A, const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J);
444 
445  inline void ref(Matrix<Symmetric,Ref,Ref,AT> &A, const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J);
446 
453  int countElements() const;
454  };
455 
456  template <class AT>
457  Matrix<General,Ref,Ref,AT>::Matrix(const std::string &strs) : memory(), ele(nullptr) {
458  std::istringstream iss(strs);
459  iss>>*this;
460 
461  // check end of stream
462  iss>>std::ws;
463  if(!iss.eof())
464  throw std::runtime_error("Input not fully read.");
465  }
466  template <class AT> Matrix<General,Ref,Ref,AT>::Matrix(const char *strs) : Matrix<General,Ref,Ref,AT>::Matrix(std::string(strs)) {}
467 
468  template <class AT>
469  inline Matrix<General,Ref,Ref,AT>& Matrix<General,Ref,Ref,AT>::init0(const AT &val) {
470  for(int i=0; i<m*n; i++) ele[i]=val;
471  return *this;
472  }
473 
474  template <class AT>
476  for(int i=0; i<rows(); i++)
477  for(int j=0; j<cols(); j++)
478  e(i,j) = val;
479  return *this;
480  }
481 
482  template <class AT>
484  for(int i=0; i<m; i++)
485  for(int j=0; j<n; j++)
486  e(i,j) = (i==j) ? val : 0;
487  return *this;
488  }
489 
490  template <class AT>
492  FMATVEC_ASSERT(I.end()<m, AT);
493  FMATVEC_ASSERT(J.end()<n, AT);
494  Matrix<General,Ref,Ref,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
495 
496  for(int i=0; i<A.rows(); i++)
497  for(int j=0; j<A.cols(); j++)
498  A.e(i,j) = e(I.start()+i,J.start()+j);
499 
500  return A;
501  }
502 
503  template <class AT>
505  FMATVEC_ASSERT(I.end()<m, AT);
506  SquareMatrix<Ref,AT> A(I.end()-I.start()+1,NONINIT);
507 
508  for(int i=0; i<A.rows(); i++)
509  for(int j=0; j<A.cols(); j++)
510  A.e(i,j) = e(I.start()+i,I.start()+j);
511 
512  return A;
513  }
514 
515  template <class AT>
517 
518  FMATVEC_ASSERT(i>=0, AT);
519  FMATVEC_ASSERT(i<m, AT);
520 
521  RowVector<Ref,AT> x(n,NONINIT);
522 
523  for(int j=0; j<n; j++)
524  x.e(j) = e(i,j);
525 
526  return x;
527  }
528 
529  template <class AT>
531 
532  FMATVEC_ASSERT(j>=0, AT);
533  FMATVEC_ASSERT(j<n, AT);
534 
535  Vector<Ref,AT> x(m,NONINIT);
536 
537  for(int i=0; i<m; i++)
538  x.e(i) = e(i,j);
539 
540  return x;
541  }
542 
543  template <class AT>
544  inline Matrix<General,Ref,Ref,AT>::operator std::vector<std::vector<AT>>() const {
545  std::vector<std::vector<AT>> ret(rows());
546  for(int r=0; r<rows(); r++) {
547  ret[r].resize(cols());
548  for(int c=0; c<cols(); c++)
549  ret[r][c]= e(r,c);
550  }
551  return ret;
552  }
553 
554  template <class AT>
555  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())) {
556  for(int r=0; r<rows(); r++) {
557  if(static_cast<int>(m[r].size())!=cols())
558  throw std::runtime_error("The rows of the input have different length.");
559  for(int c=0; c<cols(); c++)
560  e(r,c)=m[r][c];
561  }
562  }
563 
565 
566  template <class AT> template <class Type, class Row, class Col>
568  for(int i=0; i<m; i++)
569  for(int j=0; j<n; j++)
570  e(i,j) = A.e(i,j);
571  return *this;
572  }
573 
574  template <class AT>
575  inline const Matrix<General,Ref,Ref,AT> Matrix<General,Ref,Ref,AT>::T() const {
576  Matrix<General,Ref,Ref,AT> A(n,m,NONINIT);
577  for(int i=0; i<n; i++)
578  for(int j=0; j<m; j++)
579  A.e(i,j) = e(j,i);
580  return A;
581  }
582 
583  template <class AT> template <class Row>
584  inline void Matrix<General,Ref,Ref,AT>::set(int j, const Vector<Row,AT> &x) {
585  FMATVEC_ASSERT(j<cols(), AT);
586  FMATVEC_ASSERT(rows()==x.size(), AT);
587  for(int i=0; i<rows(); i++)
588  e(i,j) = x.e(i);
589  }
590 
591  template <class AT> template <class Col>
592  inline void Matrix<General,Ref,Ref,AT>::set(int i, const RowVector<Col,AT> &x) {
593  FMATVEC_ASSERT(i<rows(), AT);
594  FMATVEC_ASSERT(cols()==x.size(), AT);
595  for(int j=0; j<cols(); j++)
596  e(i,j) = x.e(j);
597  }
598 
599  template <class AT> template<class Type, class Row, class Col>
600  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) {
601 
602  FMATVEC_ASSERT(I.end()<rows(), AT);
603  FMATVEC_ASSERT(J.end()<cols(), AT);
604  FMATVEC_ASSERT(I.size()==A.rows(), AT);
605  FMATVEC_ASSERT(J.size()==A.cols(), AT);
606 
607  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
608  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
609  e(i,j) = A.e(ii,jj);
610  }
611 
612  template <class AT> template <class Row>
613  inline void Matrix<General,Ref,Ref,AT>::add(int j, const Vector<Row,AT> &x) {
614  FMATVEC_ASSERT(j<cols(), AT);
615  FMATVEC_ASSERT(rows()==x.size(), AT);
616  for(int i=0; i<rows(); i++)
617  e(i,j) += x.e(i);
618  }
619 
620  template <class AT> template <class Col>
621  inline void Matrix<General,Ref,Ref,AT>::add(int i, const RowVector<Col,AT> &x) {
622  FMATVEC_ASSERT(i<rows(), AT);
623  FMATVEC_ASSERT(cols()==x.size(), AT);
624  for(int j=0; j<cols(); j++)
625  e(i,j) += x.e(j);
626  }
627 
628  template <class AT> template<class Type, class Row, class Col>
629  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) {
630 
631  FMATVEC_ASSERT(I.end()<rows(), AT);
632  FMATVEC_ASSERT(J.end()<cols(), AT);
633  FMATVEC_ASSERT(I.size()==A.rows(), AT);
634  FMATVEC_ASSERT(J.size()==A.cols(), AT);
635 
636  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
637  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
638  e(i,j) += A.e(ii,jj);
639  }
640 
641  template <class AT> template<class Type, class Row, class Col>
642  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) {
643 
644  FMATVEC_ASSERT(I.end()<rows(), AT);
645  FMATVEC_ASSERT(J.end()<cols(), AT);
646  FMATVEC_ASSERT(I.size()==A.rows(), AT);
647  FMATVEC_ASSERT(J.size()==A.cols(), AT);
648 
649  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
650  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
651  e(i,j) -= A.e(ii,jj);
652  }
653 
654  template <class AT>
655  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) {
656  FMATVEC_ASSERT(I.end()<A.rows(), AT);
657  FMATVEC_ASSERT(J.end()<A.cols(), AT);
658  m=I.size();
659  n=J.size();
660  memory = A.memory;
661  ele = A.elePtr(I.start(),J.start());
662  lda = A.lda;
663  }
664 
665  template <class AT>
666  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) {
667  FMATVEC_ASSERT(I.end()<A.size(), AT);
668  FMATVEC_ASSERT(J.end()<A.size(), AT);
669  FMATVEC_ASSERT(I.start()>=J.start(), AT);
670  FMATVEC_ASSERT(J.end()<=I.start(), AT);
671  m=I.size();
672  n=J.size();
673  memory = A.memory;
674  ele = A.elePtr(I.start(),J.start());
675  lda = A.lda;
676  }
677 
678  template <class AT>
680  int k = 0;
681  for (int i = 0; i < m; i++) {
682  for (int j = 0; j < n; j++) {
683  if (fabs(e(i, j)) > 1e-16 || i==j) {
684  k++;
685  }
686  }
687  }
688  return k;
689  }
690 
692 
693 }
694 
695 #endif
int end() const
Last element.
Definition: range.h:91
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: general_matrix.h:335
Matrix(const Matrix< General, Row, Col, AT > &A)
Copy Constructor.
Definition: general_matrix.h:126
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:304
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
Definition: types.h:85
AT & operator()(int i, int j)
Element operator.
Definition: general_matrix.h:257
Matrix(const Matrix< General, Ref, Ref, AT > &A)
Copy Constructor.
Definition: general_matrix.h:116
Definition: fmatvec.h:50
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: general_matrix.h:324
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
Definition: fmatvec.h:44
Definition: fmatvec.h:47
int countElements() const
Count nonzero elements.
Matrix< General, Ref, Ref, double > Mat
General matrix.
Definition: fmatvec.h:50
const AT * operator()() const
Pointer operator.
Definition: general_matrix.h:298
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:310
bool operator!=(const Matrix< Type1, Row1, Col1, AT > &A, const Matrix< Type2, Row2, Col2, AT > &B)
Matrix-matrix comparison.
Definition: linear_algebra.h:1534
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:206
AT * operator()()
Pointer operator.
Definition: general_matrix.h:292
Matrix< General, Ref, Ref, AT > & operator&=(Matrix< General, Ref, Ref, AT > &A)
Reference operator.
Definition: general_matrix.h:218
int ldim() const
Leading dimension.
Definition: general_matrix.h:316
Matrix(int m_, int n_, AT *ele_)
Regular Constructor.
Definition: general_matrix.h:147
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:270
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:99
Matrix< General, Ref, Ref, AT > & operator=(const Matrix< General, Ref, Ref, AT > &A)
Assignment operator.
Definition: general_matrix.h:193
int rows() const
Number of rows.
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: general_matrix.h:136
This is an index class for creating submatrices.
Definition: range.h:44
Definition: types.h:86