All Classes Namespaces Functions Variables Typedefs Enumerations Pages
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  template <class T> friend Matrix<General,Ref,Ref,T> trans(const Matrix<General,Ref,Ref,T> &A);
55 
56  protected:
57 
58  Memory<AT> memory;
59  AT *ele;
60  int m{0};
61  int n{0};
62  int lda{0};
63  bool tp{false};
64 
65  template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A);
66  inline void deepCopy(const Matrix<General,Ref,Ref,AT> &A);
67  inline void deepCopy(const Matrix<Symmetric,Ref,Ref,AT> &A);
68 
69  const AT* elePtr(int i, int j) const {
70  return tp ? ele+i*lda+j : ele+i+j*lda;
71  };
72 
73  AT* elePtr(int i, int j) {
74  return tp ? ele+i*lda+j : ele+i+j*lda;
75  };
76 
77  Matrix(int m_, int n_, int lda_, bool tp_, Memory<AT> memory_, const AT* ele_) : memory(memory_), ele((AT*)ele_), m(m_), n(n_), lda(lda_), tp(tp_) {
78  }
79 
81 
82  public:
83 
88  Matrix() : memory(), ele(nullptr) { }
89 
90 // Works with -std=gnu++0x only
91 // template<class Ini=All<AT> >
92 // Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_), tp(false) {
93 // init(ini);
94 // }
95 
96  Matrix(int m_, int n_, Noinit) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { }
97  Matrix(int m_, int n_, Init ini=INIT, const AT &a=0) : memory(m_*n_), ele((AT*)memory.get()), m(m_), n(n_), lda(m_) { init0(a); }
98  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); }
99 
107  Matrix(const Matrix<General,Ref,Ref,AT> &A) : memory(A.memory), ele(A.ele) ,m(A.m), n(A.n), lda(A.lda), tp(A.tp) {
108  }
109 
117  Matrix(int m_, int n_, AT* ele_) : memory(), ele(ele_), m(m_), n(n_), lda(m_) {
118  }
119 
132  Matrix(const std::string &strs);
133  Matrix(const char *strs);
134 
137  ~Matrix() = default;
138 
139  template<class Type, class Row, class Col>
140  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) {
141 
142  deepCopy(A);
143  }
144 
145  Matrix<General,Ref,Ref,AT>& resize() {
146  m = n = lda = 0;
147  tp = false;
148  memory.resize(0);
149  ele = nullptr;
150  return *this;
151  }
152 
153  Matrix<General,Ref,Ref,AT>& resize(int m_, int n_, Noinit) {
154  m = m_; n = n_; lda = m;
155  tp = false;
156  memory.resize(m*n);
157  ele = (AT*)memory.get();
158  return *this;
159  }
160 
161  Matrix<General,Ref,Ref,AT>& resize(int m, int n, Init ini=INIT, const AT &a=0) { return resize(m,n,Noinit()).init0(a); }
162 
163  Matrix<General,Ref,Ref,AT>& resize(int m, int n, Eye ini, const AT &a=1) { return resize(m,n,Noinit()).init(ini,a); }
164 
173  inline Matrix<General,Ref,Ref,AT>& operator=(const Matrix<General,Ref,Ref,AT> &A);
174 
175  template<class Type, class Row, class Col>
176  Matrix<General,Ref,Ref,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
177 
184  template<class Type, class Row, class Col>
185  inline Matrix<General,Ref,Ref,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
186 
193  inline Matrix<General,Ref,Ref,AT>& operator>>(const Matrix<General,Ref,Ref,AT> &A);
194 
206  AT& operator()(int i, int j) {
207 #ifndef FMATVEC_NO_BOUNDS_CHECK
208  assert(i>=0);
209  assert(j>=0);
210  assert(i<m);
211  assert(j<n);
212 #endif
213 
214  return e(i,j);
215  };
216 
221  const AT& operator()(int i, int j) const {
222 #ifndef FMATVEC_NO_BOUNDS_CHECK
223  assert(i>=0);
224  assert(j>=0);
225  assert(i<m);
226  assert(j<n);
227 #endif
228 
229  return e(i,j);
230  };
231 
232  AT& er(int i, int j) {
233  return ele[i+j*lda];
234  };
235 
236  const AT& er(int i, int j) const {
237  return ele[i+j*lda];
238  };
239 
240  AT& et(int i, int j) {
241  return ele[i*lda+j];
242  };
243 
244  const AT& et(int i, int j) const {
245  return ele[i*lda+j];
246  };
247 
248  AT& e(int i, int j) {
249  return tp ? et(i,j) : er(i,j);
250  };
251 
252  const AT& e(int i, int j) const {
253  return tp ? et(i,j) : er(i,j);
254  };
255 
261  AT* operator()() {return ele;};
262 
267  const AT* operator()() const {return ele;};
268 
273  int rows() const {return m;};
274 
279  int cols() const {return n;};
280 
285  int ldim() const {return lda;};
286 
292  bool transposed() const {return tp;};
293 
300  const CBLAS_TRANSPOSE blasTrans() const {
301  return (tp)? CblasTrans : CblasNoTrans;
302  };
303 
311  const CBLAS_ORDER blasOrder() const {
312  return CblasColMajor;
313  };
314 
326  inline Matrix<General,Ref,Ref,AT> operator()(int i1, int j1, int i2, int j2);
327 
332  inline const Matrix<General,Ref,Ref,AT> operator()(int i1, int j1, int i2, int j2) const;
333 
361 
366  inline const Matrix<General,Ref,Ref,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
367 
377 
382  inline const SquareMatrix<Ref,AT> operator()(const Range<Var,Var> &I) const;
383 
391  inline Vector<Ref,AT> col(int i);
392 
397  inline const Vector<Ref,AT> col(int i) const;
398 
406  inline RowVector<Ref,AT> row(int i);
407 
412  inline const RowVector<Ref,AT> row(int i) const;
413 
419  inline Matrix<General,Ref,Ref,AT> copy() const;
420 
428  inline Matrix<General,Ref,Ref,AT>& init(const AT &val=0);
429  inline Matrix<General,Ref,Ref,AT>& init(Init, const AT &a=0) { return init(a); }
430  inline Matrix<General,Ref,Ref,AT>& init(Eye, const AT &val=1);
431  inline Matrix<General,Ref,Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
432  inline Matrix<General,Ref,Ref,AT>& init0(const AT &val=0);
433  inline Matrix<General,Ref,Ref,AT>& init0(Init, const AT &a=0) { return init0(a); }
434 
439  inline operator std::vector<std::vector<AT> >() const;
440 
446  inline Matrix(std::vector<std::vector<AT> > m);
447 
448  Matrix<General,Ref,Ref,AT> T() {
449  return Matrix<General,Ref,Ref,AT>(n,m,lda,tp?false:true,memory,ele);
450  };
451 
452  const Matrix<General,Ref,Ref,AT> T() const {
453  return Matrix<General,Ref,Ref,AT>(n,m,lda,tp?false:true,memory,ele);
454  }
455  };
456 
457  template <class AT>
458  Matrix<General,Ref,Ref,AT>::Matrix(const std::string &strs) : memory(), ele(nullptr) {
459  std::istringstream iss(strs);
460  iss>>*this;
461 
462  // check end of stream
463  iss>>std::ws;
464  if(!iss.eof())
465  throw std::runtime_error("Input not fully read.");
466  }
467  template <class AT> Matrix<General,Ref,Ref,AT>::Matrix(const char *strs) : Matrix<General,Ref,Ref,AT>::Matrix(std::string(strs)) {}
468 
469  template <class AT>
471 
472  m=A.m;
473  n=A.n;
474  memory = A.memory;
475  ele = A.ele;
476  lda = A.lda;
477  tp = A.tp;
478 
479  return *this;
480  }
481 
482  template <class AT>
484 
485  if(!ele) {
486  m = A.rows();
487  n = A.cols();
488  lda = m;
489  tp = false;
490  memory.resize(m*n);
491  ele = (AT*)memory.get();
492  } else {
493 #ifndef FMATVEC_NO_SIZE_CHECK
494  assert(m == A.rows());
495  assert(n == A.cols());
496 #endif
497  }
498 
499  deepCopy(A);
500 
501  return *this;
502  }
503 
504  template <class AT> template< class Type, class Row, class Col>
506 
507  if(!ele) {
508  m = A.rows();
509  n = A.cols();
510  lda = m;
511  tp = false;
512  memory.resize(m*n);
513  ele = (AT*)memory.get();
514  } else {
515 #ifndef FMATVEC_NO_SIZE_CHECK
516  assert(m == A.rows());
517  assert(n == A.cols());
518 #endif
519  }
520 
521  deepCopy(A);
522 
523  return *this;
524  }
525 
526  template <class AT> template< class Type, class Row, class Col>
528 
529  if(m!=A.rows() || n!=A.cols()) {
530  m = A.rows();
531  n = A.cols();
532  lda = m;
533  tp = false;
534  memory.resize(m*n);
535  ele = (AT*)memory.get();
536  }
537 
538  deepCopy(A);
539 
540  return *this;
541  }
542 
543  template <class AT>
545  for(int i=0; i<m*n; i++) ele[i]=val;
546  return *this;
547  }
548 
549  template <class AT>
551 
552  if(tp) {
553  for(int i=0; i<rows(); i++)
554  for(int j=0; j<cols(); j++)
555  et(i,j) = val;
556  }
557  else {
558  for(int i=0; i<rows(); i++)
559  for(int j=0; j<cols(); j++)
560  er(i,j) = val;
561  }
562 
563  return *this;
564  }
565 
566  template <class AT>
568  if(tp)
569  for(int i=0; i<m; i++)
570  for(int j=0; j<n; j++)
571  et(i,j) = (i==j) ? val : 0;
572  else
573  for(int i=0; i<m; i++)
574  for(int j=0; j<n; j++)
575  er(i,j) = (i==j) ? val : 0;
576  return *this;
577  }
578 
579  template <class AT>
581  return operator()(Range<Var,Var>(i1,i2),Range<Var,Var>(j1,j2));
582  }
583 
584  template <class AT>
585  inline const Matrix<General,Ref,Ref,AT> Matrix<General,Ref,Ref,AT>::operator()(int i1, int j1, int i2, int j2) const {
586  return operator()(Range<Var,Var>(i1,i2),Range<Var,Var>(j1,j2));
587  }
588 
589  template <class AT>
591 #ifndef FMATVEC_NO_BOUNDS_CHECK
592  assert(I.end()<m);
593  assert(J.end()<n);
594 #endif
595  return Matrix<General,Ref,Ref,AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,tp,memory,elePtr(I.start(),J.start()));
596  }
597 
598  template <class AT>
600 #ifndef FMATVEC_NO_BOUNDS_CHECK
601  assert(I.end()<m);
602  assert(J.end()<n);
603 #endif
604  return Matrix<General,Ref,Ref,AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,tp,memory,elePtr(I.start(),J.start()));
605  }
606 
607  template <class AT>
609 #ifndef FMATVEC_NO_BOUNDS_CHECK
610  assert(I.end()<m);
611 #endif
612  return SquareMatrix<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start(),I.start()));
613  }
614 
615  template <class AT>
617 #ifndef FMATVEC_NO_BOUNDS_CHECK
618  assert(I.end()<m);
619 #endif
620  return SquareMatrix<Ref,AT>(I.end()-I.start()+1,lda,tp,memory,elePtr(I.start(),I.start()));
621  }
622 
623  template <class AT>
625 
626 #ifndef FMATVEC_NO_BOUNDS_CHECK
627  assert(i>=0);
628  assert(i<n);
629 #endif
630 
631  return Vector<Ref,AT>(m,lda,tp,memory,elePtr(0,i));
632  }
633 
634  template <class AT>
636 
637 #ifndef FMATVEC_NO_BOUNDS_CHECK
638  assert(i>=0);
639  assert(i<n);
640 #endif
641 
642  return Vector<Ref,AT>(m,lda,tp,memory,elePtr(0,i));
643  }
644 
645  template <class AT>
647 
648 #ifndef FMATVEC_NO_BOUNDS_CHECK
649  assert(i>=0);
650  assert(i<m);
651 #endif
652 
653  return RowVector<Ref,AT>(n,lda,tp,memory,elePtr(i,0));
654  }
655 
656  template <class AT>
658 
659 #ifndef FMATVEC_NO_BOUNDS_CHECK
660  assert(i>=0);
661  assert(i<m);
662 #endif
663 
664  return RowVector<Ref,AT>(n,lda,tp,memory,elePtr(i,0));
665  }
666 
667  template <class AT>
669 
670  Matrix<General,Ref,Ref,AT> A(m,n,NONINIT);
671  A.deepCopy(*this);
672 
673  return A;
674  }
675 
676  template <class AT>
677  inline Matrix<General,Ref,Ref,AT>::operator std::vector<std::vector<AT> >() const {
678  std::vector<std::vector<AT> > ret(rows());
679  if(tp) {
680  for(int r=0; r<rows(); r++) {
681  ret[r].resize(cols());
682  for(int c=0; c<cols(); c++)
683  ret[r][c]= et(r,c);
684  }
685  }
686  else {
687  for(int r=0; r<rows(); r++) {
688  ret[r].resize(cols());
689  for(int c=0; c<cols(); c++)
690  ret[r][c]= er(r,c);
691  }
692  }
693  return ret;
694  }
695 
696  template <class AT>
697  inline Matrix<General,Ref,Ref,AT>::Matrix(std::vector<std::vector<AT> > m) : memory(m.size()*m[0].size()), ele((AT*)memory.get()), m(m.size()), n(m[0].size()), lda(m.size()) {
698 #ifndef FMATVEC_NO_INITIALIZATION
699  init(0);
700 #endif
701  for(int r=0; r<rows(); r++) {
702  if(static_cast<int>(m[r].size())!=cols())
703  throw std::runtime_error("The rows of the input have different length.");
704  for(int c=0; c<cols(); c++)
705  er(r,c)=m[r][c];
706  }
707  }
708 
710 
711  template <class AT> template <class Type, class Row, class Col>
713  if(tp) {
714  for(int i=0; i<m; i++)
715  for(int j=0; j<n; j++)
716  et(i,j) = A.e(i,j);
717  }
718  else {
719  for(int i=0; i<m; i++)
720  for(int j=0; j<n; j++)
721  er(i,j) = A.e(i,j);
722  }
723  }
724 
725  template <class AT>
726  inline void Matrix<General,Ref,Ref,AT>::deepCopy(const Matrix<General,Ref,Ref,AT> &A) {
727  if(A.tp) {
728  if(tp) {
729  for(int i=0; i<m; i++)
730  for(int j=0; j<n; j++)
731  et(i,j) = A.et(i,j);
732  }
733  else {
734  for(int i=0; i<m; i++)
735  for(int j=0; j<n; j++)
736  er(i,j) = A.et(i,j);
737  }
738  } else {
739  if(tp) {
740  for(int i=0; i<m; i++)
741  for(int j=0; j<n; j++)
742  et(i,j) = A.er(i,j);
743  }
744  else {
745  for(int i=0; i<m; i++)
746  for(int j=0; j<n; j++)
747  er(i,j) = A.er(i,j);
748  }
749  }
750  }
751 
752  template<class AT>
753  inline void Matrix<General,Ref,Ref,AT>::deepCopy(const Matrix<Symmetric,Ref,Ref,AT> &A) {
754  for(int i=0; i<A.size(); i++) {
755  er(i,i) = A.ej(i,i);
756  for(int j=i+1; j<A.size(); j++)
757  er(i,j) = et(i,j) = A.ej(i,j);
758  }
759  }
760 
762 
763 }
764 
765 #endif
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
int ldim() const
Leading dimension.
Definition: general_matrix.h:285
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:90
int rows() const
Number of rows.
Definition: general_matrix.h:273
Definition: matrix.h:43
AT & operator()(int i, int j)
Element operator.
Definition: general_matrix.h:206
Matrix(const Matrix< General, Ref, Ref, AT > &A)
Copy Constructor.
Definition: general_matrix.h:107
Definition: fmatvec.h:50
const CBLAS_TRANSPOSE blasTrans() const
Transposed status.
Definition: general_matrix.h:300
int rows() const
Number of rows.
int cols() const
Number of columns.
Definition: types.h:62
This is a rowvector class of general shape in dense storage format.
Definition: row_vector.h:36
Definition: fmatvec.h:44
Definition: fmatvec.h:47
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: general_matrix.h:311
This is a matrix class for general matrices.
Definition: general_matrix.h:41
const AT & operator()(int i, int j) const
Element operator.
Definition: general_matrix.h:221
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:166
AT * operator()()
Pointer operator.
Definition: general_matrix.h:261
Matrix(int m_, int n_, AT *ele_)
Regular Constructor.
Definition: general_matrix.h:117
Definition: matrix.h:42
RowVector< Ref, AT > trans(const Vector< Ref, AT > &x)
Transpose of a vector.
Definition: linear_algebra.h:1470
This is a matrix class for symmetric matrices.
Definition: symmetric_matrix.h:39
bool transposed() const
Transposed status.
Definition: general_matrix.h:292
Basic shape class for matrices.
Definition: types.h:100
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:88
int end() const
Last element.
Definition: range.h:95
This is an index class for creating submatrices.
Definition: range.h:44
const AT * operator()() const
Pointer operator.
Definition: general_matrix.h:267
int start() const
First element.
Definition: range.h:89
int cols() const
Number of columns.
Definition: general_matrix.h:279
Definition: matrix.h:44

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home