All Classes Namespaces Functions Variables Typedefs Enumerations Pages
symmetric_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 symmetric_matrix_h
23 #define symmetric_matrix_h
24 
25 #include "types.h"
26 #include "matrix.h"
27 #include "_memory.h"
28 
29 namespace fmatvec {
30 
39  template <class AT> class Matrix<Symmetric,Ref,Ref,AT> {
40 
41  protected:
42 
44 
45  Memory<AT> memory;
46  AT *ele;
47  int n{0};
48  int lda{0};
49 
50  template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A);
51  inline void deepCopy(const Matrix<Symmetric,Ref,Ref,AT> &A);
52 
53  const AT* elePtr(int i, int j) const {
54  return j > i ? ele+i*lda+j : ele+i+j*lda;
55  };
56 
57  AT* elePtr(int i, int j) {
58  return j > i ? ele+i*lda+j : ele+i+j*lda;
59  };
60 
61  Matrix(int n_, int lda_, Memory<AT> memory_, const AT* ele_) : memory(memory_), ele((AT*)ele_), n(n_), lda(lda_) {
62  }
63 
65 
66  public:
67 
68  typedef AT value_type;
69 
74  Matrix() : memory(), ele(nullptr) {
75  }
76 
77 // template<class Ini=All<AT> >
78 // Matrix(int n_, Ini ini=All<AT>()) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) {
79 // init(ini);
80 // }
81 // template<class Ini=All<AT> >
82 // Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) {
83 // init(ini);
84 // }
85 
86  Matrix(int n_, Noinit) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { }
87  Matrix(int n_, Init ini=INIT, const AT &a=0) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(a); }
88  Matrix(int n_, Eye ini, const AT &a=1) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(ini,a); }
89  Matrix(int m_, int n_, Noinit) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { }
90  Matrix(int m_, int n_, Init ini=INIT, const AT &a=0) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(a); }
91  Matrix(int m_, int n_, Eye ini, const AT &a=1) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(ini,a); }
92 
100  Matrix(const Matrix<Symmetric,Ref,Ref,AT> &A) : memory(A.memory), ele(A.ele) , n(A.n), lda(A.lda) { }
101 
102 
107  explicit Matrix(const Matrix<General,Ref,Ref,AT>& A) : memory(A.memory), ele(A.ele) , n(A.n), lda(A.lda) {
108 #ifndef FMATVEC_NO_SIZE_CHECK
109  assert(A.rows() == A.cols());
110 #endif
111  }
112 
113  template<class Type, class Row, class Col>
114  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : memory(A.rows()*A.cols()), ele((AT*)memory.get()), n(A.cols()), lda(A.cols()) {
115 #ifndef FMATVEC_NO_SIZE_CHECK
116  assert(A.rows() == A.cols());
117 #endif
118 
119  deepCopy(A);
120  }
121 
129  Matrix(int n_, AT* ele_) : memory(), ele(ele_), n(n_), lda(n_) { }
130 
131  Matrix(const std::vector<std::vector<AT>> &A);
132 
135  ~Matrix() = default;
136 
137  Matrix<Symmetric,Ref,Ref,AT>& resize() {
138  n = lda = 0;
139  memory.resize(0);
140  ele = 0;
141  return *this;
142  }
143 
144  Matrix<Symmetric,Ref,Ref,AT>& resize(int n_, Noinit) {
145  n = n_; lda = n;
146  memory.resize(n*n);
147  ele = (AT*)memory.get();
148  return *this;
149  }
150 
151  Matrix<Symmetric,Ref,Ref,AT>& resize(int n, Init ini=INIT, const AT &a=0) { return resize(n,Noinit()).init(a); }
152 
153  Matrix<Symmetric,Ref,Ref,AT>& resize(int n, Eye ini, const AT &a=1) { return resize(n,Noinit()).init(ini,a); }
154 
157  void resize(int n, int m) {
158  if(n!=m)
159  throw std::runtime_error("A symmetric matrix cannot have different dimensions for rows and columns.");
160  resize(n);
161  }
162 
164  bool transposed() {
165  return false;
166  }
167 
177 
178  template<class Type, class Row, class Col>
179  inline Matrix<Symmetric,Ref,Ref,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
180 
187  template<class Type, class Row, class Col>
188  inline Matrix<Symmetric,Ref,Ref,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
189 
197 
209  AT& operator()(int i, int j) {
210 #ifndef FMATVEC_NO_BOUNDS_CHECK
211  assert(i>=0);
212  assert(j>=0);
213  assert(i<n);
214  assert(j<n);
215 #endif
216  return e(i,j);
217  };
218 
223  const AT& operator()(int i, int j) const {
224 #ifndef FMATVEC_NO_BOUNDS_CHECK
225  assert(i>=0);
226  assert(j>=0);
227  assert(i<n);
228  assert(j<n);
229 #endif
230 
231  return e(i,j);
232  };
233 
234  AT& ei(int i, int j) {
235  return ele[i+j*lda];
236  };
237 
238  const AT& ei(int i, int j) const {
239  return ele[i+j*lda];
240  };
241 
242  AT& ej(int i, int j) {
243  return ele[i*lda+j];
244  };
245 
246  const AT& ej(int i, int j) const {
247  return ele[i*lda+j];
248  };
249 
250  AT& e(int i, int j) {
251  return j > i ? ej(i,j) : ei(i,j);
252  };
253 
254  const AT& e(int i, int j) const {
255  return j > i ? ej(i,j) : ei(i,j);
256  };
257 
263  AT* operator()() {return ele;};
264 
269  const AT* operator()() const {return ele;};
270 
275  int size() const {return n;};
276 
281  int rows() const {return n;};
282 
287  int cols() const {return n;};
288 
293  int ldim() const {return lda;};
294 
302  const CBLAS_ORDER blasOrder() const {
303  return CblasColMajor;
304  };
305 
313  const CBLAS_UPLO blasUplo() const {
314  return CblasLower;
315  };
316 
322  inline Matrix<Symmetric,Ref,Ref,AT> copy() const;
323 
331  inline Matrix<Symmetric,Ref,Ref,AT>& init(const AT &val);
332  inline Matrix<Symmetric,Ref,Ref,AT>& init(Init, const AT &a=0) { return init(a); };
333  inline Matrix<Symmetric,Ref,Ref,AT>& init(Eye, const AT &val=1);
334  inline Matrix<Symmetric,Ref,Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
335 
347  inline Matrix<General,Ref,Ref,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J);
348 
353  inline const Matrix<General,Ref,Ref,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
354 
366  inline Matrix<Symmetric,Ref,Ref,AT> operator()(const Range<Var,Var> &I);
367 
372  inline const Matrix<Symmetric,Ref,Ref,AT> operator()(const Range<Var,Var> &I) const;
373 
385  inline Matrix<General,Ref,Ref,AT> operator()(int i1, int j1, int i2, int j2);
386 
391  inline const Matrix<General,Ref,Ref,AT> operator()(int i1, int j1, int i2, int j2) const;
392 
397  inline operator std::vector<std::vector<AT> >() const;
398  };
399 
400  template <class AT>
401  Matrix<Symmetric,Ref,Ref,AT>::Matrix(const std::vector<std::vector<AT>> &A) : Matrix<Symmetric,Ref,Ref,AT>(A.size()) {
402  for(int r=0; r<rows(); r++) {
403  if(static_cast<int>(A[r].size())!=cols())
404  throw std::runtime_error("The rows of the input have different length.");
405  for(int c=0; c<cols(); c++) {
406  e(r,c)=A[r][c];
407  if(r>c && fabs(A[r][c]-A[c][r])>fabs(A[r][c])*1e-13+1e-13)
408  throw std::runtime_error("The input is not symmetric.");
409  }
410  }
411  }
412 
413  template <class AT>
415 
416  n=A.n;
417  memory = A.memory;
418  ele = A.ele;
419  lda = A.lda;
420 
421  return *this;
422  }
423 
424  template <class AT>
426 
427  if(!ele) {
428  n = A.size();
429  lda = n;
430  memory.resize(n*n);
431  ele = (AT*)memory.get();
432  } else {
433 #ifndef FMATVEC_NO_SIZE_CHECK
434  assert(n == A.size());
435 #endif
436  }
437 
438  deepCopy(A);
439 
440  return *this;
441  }
442 
443  template <class AT> template< class Type, class Row, class Col>
445 
446  if(!ele) {
447  n = A.rows();
448  lda = n;
449  memory.resize(n*n);
450  ele = (AT*)memory.get();
451  } else {
452 #ifndef FMATVEC_NO_SIZE_CHECK
453  assert(A.rows() == A.cols());
454  assert(n == A.rows());
455 #endif
456  }
457 
458  deepCopy(A);
459 
460  return *this;
461  }
462 
463 
464  template <class AT> template< class Type, class Row, class Col>
466 
467 #ifndef FMATVEC_NO_SIZE_CHECK
468  assert(A.rows() == A.cols());
469 #endif
470 
471  if(n!=A.rows()) {
472  n = A.rows();
473  lda = n;
474  memory.resize(n*n);
475  ele = (AT*)memory.get();
476  }
477 
478  deepCopy(A);
479 
480  return *this;
481  }
482 
483  template <class AT>
485 
486  for(int i=0; i<rows(); i++)
487  for(int j=i; j<cols(); j++)
488  ej(i,j) = val;
489 
490  return *this;
491  }
492 
493  template <class AT>
495 
496  for(int i=0; i<size(); i++) {
497  ej(i,i) = val;
498  for(int j=i+1; j<size(); j++) {
499  ej(i,j) = 0;
500  }
501  }
502  return *this;
503  }
504 
505  template <class AT>
507 
508  Matrix<Symmetric,Ref,Ref,AT> A(n,NONINIT);
509  A.deepCopy(*this);
510 
511  return A;
512  }
513 
514  template <class AT>
515  inline const Matrix<General,Ref,Ref,AT> Matrix<Symmetric,Ref,Ref,AT>::operator()(int i1, int j1, int i2, int j2) const {
516  return operator()(Range<Var,Var>(i1,i2),Range<Var,Var>(j1,j2));
517  }
518 
519  template <class AT>
521  return operator()(Range<Var,Var>(i1,i2),Range<Var,Var>(j1,j2));
522  }
523 
524  template <class AT>
526 #ifndef FMATVEC_NO_BOUNDS_CHECK
527  assert(I.end()<n);
528  assert(J.end()<n);
529 #endif
530 
531  if(I.start() >= J.start()) {
532  assert(J.end() <= I.start());
533  return Matrix<General,Ref,Ref,AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,false,memory,elePtr(I.start(),J.start()));
534  } else {
535  assert(I.end() <= J.start());
536  return Matrix<General,Ref,Ref,AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,true,memory,elePtr(J.start(),I.start()));
537  }
538  }
539 
540  template <class AT>
542 #ifndef FMATVEC_NO_BOUNDS_CHECK
543  assert(I.end()<n);
544  assert(J.end()<n);
545 #endif
546 
547  if(I.start() >= J.start()) {
548  assert(J.end() <= I.start());
549  return Matrix<General,Ref,Ref,AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,false,memory,elePtr(I.start(),J.start()));
550  } else {
551  assert(I.end() <= J.start());
552  return Matrix<General,Ref,Ref,AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,true,memory,elePtr(J.start(),I.start()));
553  }
554  }
555 
556  template <class AT>
558 #ifndef FMATVEC_NO_BOUNDS_CHECK
559  assert(I.end()<n);
560 #endif
561 
562  return Matrix<Symmetric,Ref,Ref,AT>(I.end()-I.start()+1,lda,memory,elePtr(I.start(),I.start()));
563  }
564 
565  template <class AT>
567 #ifndef FMATVEC_NO_BOUNDS_CHECK
568  assert(I.end()<n);
569 #endif
570 
571  return Matrix<Symmetric,Ref,Ref,AT>(I.end()-I.start()+1,lda,memory,elePtr(I.start(),I.start()));
572  }
573 
574  template <class AT>
575  inline Matrix<Symmetric,Ref,Ref,AT>::operator std::vector<std::vector<AT> >() const {
576  std::vector<std::vector<AT> > ret(rows());
577  for(int r=0; r<rows(); r++) {
578  ret[r].resize(cols());
579  for(int c=0; c<cols(); c++)
580  ret[r][c]=operator()(r,c);
581  }
582  return ret;
583  }
584 
586 
587  template <class AT>
589  for(int i=0; i<n; i++)
590  for(int j=i; j<n; j++)
591  ej(i,j) = A.ej(i,j);
592  }
593 
594  template <class AT> template <class Type, class Row, class Col>
595  inline void Matrix<Symmetric,Ref,Ref,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) {
596  for(int i=0; i<n; i++)
597  for(int j=i; j<n; j++)
598  ej(i,j) = A.e(i,j);
599  }
600 
602 
603 }
604 
605 #endif
Matrix(const Matrix< General, Ref, Ref, AT > &A)
Element operator.
Definition: symmetric_matrix.h:107
AT & operator()(int i, int j)
Element operator.
Definition: symmetric_matrix.h:209
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Shape class for symmetric matrices.
Definition: types.h:116
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
bool transposed()
The storage format of a symmetric matrix is fortran-storage order -&gt; return always false...
Definition: symmetric_matrix.h:164
int rows() const
Number of rows.
Definition: symmetric_matrix.h:281
int rows() const
Number of rows.
int cols() const
Number of columns.
Definition: types.h:62
const CBLAS_UPLO blasUplo() const
Symmetry convention.
Definition: symmetric_matrix.h:313
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: symmetric_matrix.h:302
int cols() const
Number of columns.
Definition: symmetric_matrix.h:287
Matrix(int n_, AT *ele_)
Regular Constructor.
Definition: symmetric_matrix.h:129
This is a matrix class for general matrices.
Definition: general_matrix.h:41
int size() const
Size.
Definition: symmetric_matrix.h:275
AT * operator()()
Pointer operator.
Definition: symmetric_matrix.h:263
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:166
const AT & operator()(int i, int j) const
Element operator.
Definition: symmetric_matrix.h:223
int ldim() const
Leading dimension.
Definition: symmetric_matrix.h:293
Matrix(const Matrix< Symmetric, Ref, Ref, AT > &A)
Copy Constructor.
Definition: symmetric_matrix.h:100
Definition: matrix.h:42
This is a matrix class for symmetric matrices.
Definition: symmetric_matrix.h:39
int end() const
Last element.
Definition: range.h:95
const AT * operator()() const
Pointer operator.
Definition: symmetric_matrix.h:269
Matrix()
Standard constructor.
Definition: symmetric_matrix.h:74
void resize(int n, int m)
Definition: symmetric_matrix.h:157
Matrix< Symmetric, Ref, Ref, AT > & init(const AT &val)
Initialization.
Definition: symmetric_matrix.h:484
This is an index class for creating submatrices.
Definition: range.h:44
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