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 "index.h"
26 #include "types.h"
27 #include "_memory.h"
28 
29 namespace fmatvec {
30 
39  template <class AT> class Matrix<Symmetric, AT> {
40 
41  protected:
42 
44 
45  Memory<AT> memory;
46  AT *ele;
47  int n;
48  int lda;
49 
50  void deepCopy(const Matrix<Symmetric, AT> &A);
51 
52  const AT* elePtr(int i, int j) const {
53  return j > i ? ele+i*lda+j : ele+i+j*lda;
54  };
55 
56  AT* elePtr(int i, int j) {
57  return j > i ? ele+i*lda+j : ele+i+j*lda;
58  };
59 
60  Matrix(int n_, int lda_, Memory<AT> memory_, const AT* ele_) : memory(memory_), ele((AT*)ele_), n(n_), lda(lda_) {
61  }
62 
64 
65  public:
66 
71  Matrix() : memory(), ele(0), n(0), lda(0) {
72  }
73 
82  Matrix(int n_) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) {
83 
84 #ifndef FMATVEC_NO_INITIALIZATION
85  init(0);
86 #endif
87  }
88 
99  Matrix(int m_, int n_) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) {
100  assert(m_ == n_);
101 
102 #ifndef FMATVEC_NO_INITIALIZATION
103  init(0);
104 #endif
105  }
106 
117  Matrix(int n_, Initialization ini, const AT &a=0) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) {
118 
119  if(ini == INIT)
120  init(a);
121  else if(ini == EYE) {
122  init(0);
123  for(int i=0; i<n; i++)
124  ele[i+i*lda] = 1; // operator()(i,i) = 1;
125  }
126  }
127 
139  Matrix(int m_, int n_, Initialization ini, const AT &a=0) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) {
140 
141  assert(m_ == n_);
142 
143  if(ini == INIT)
144  init(a);
145  else if(ini == EYE) {
146  init(0);
147  for(int i=0; i<n; i++)
148  ele[i+i*lda] = 1; // operator()(i,i) = 1;
149  }
150 
151  }
152 
160  Matrix(const Matrix<Symmetric, AT> &A) : memory(A.memory), ele(A.ele) , n(A.n), lda(A.lda) {
161  }
162 
163 
168  explicit Matrix(const Matrix<General, AT>& A) : memory(A.memory), ele(A.ele) , n(A.n), lda(A.lda) {
169 #ifdef FMATVEC_SIZE_CHECK
170  assert(A.rows() == A.cols());
171 #endif
172  }
173 
181  Matrix(int n_, AT* ele_) : memory(), ele(ele_), n(n_), lda(n_) {
182  }
183 
187  }
188 
194  Matrix<Symmetric, AT>& resize() {n=0;return *this;};
195 
206  n=n_;
207  lda=n;
208  memory.resize(n*n);
209  ele = (AT*)memory.get();
210 
211 #ifndef FMATVEC_NO_INITIALIZATION
212  init(0);
213 #endif
214  return *this;
215  }
216 
228  Matrix<Symmetric, AT>& resize(int n_, Initialization ini, const AT &a=0) {
229  n=n_;
230  lda=n;
231  memory.resize(n*n);
232  ele = (AT*)memory.get();
233 
234  if(ini == INIT)
235  init(a);
236  else if(ini == EYE) {
237  init(0);
238  for(int i=0; i<n; i++)
239  ele[i+i*lda] = 1; // operator()(i,i) = 1;
240  }
241 
242 
243  return *this;
244  }
245 
255 #ifndef FMATVEC_NO_DEEP_ASSIGNMENT
256  return operator<<(A);
257 #else
258  return operator>>(A);
259 #endif
260  }
261 
268  Matrix<Symmetric, AT>& operator<<(const Matrix<Symmetric, AT> &A);
269 
277 
289  AT& operator()(int i, int j) {
290 #ifndef FMATVEC_NO_BOUNDS_CHECK
291  assert(i>=0);
292  assert(j>=0);
293  assert(i<n);
294  assert(j<n);
295 #endif
296  return j > i ? ele[i*lda+j] : ele[i+j*lda];
297  };
298 
303  const AT& operator()(int i, int j) const {
304 #ifndef FMATVEC_NO_BOUNDS_CHECK
305  assert(i>=0);
306  assert(j>=0);
307  assert(i<n);
308  assert(j<n);
309 #endif
310 
311  return j > i ? ele[i*lda+j] : ele[i+j*lda];// return ele[i*lda+j*ldb];
312  };
313 
319  AT* operator()() {return ele;};
320 
325  const AT* operator()() const {return ele;};
326 
331  int size() const {return n;};
332 
337  int rows() const {return n;};
338 
343  int cols() const {return n;};
344 
349  int ldim() const {return lda;};
350 
358  const CBLAS_ORDER blasOrder() const {
359  return CblasColMajor;
360  };
361 
369  const CBLAS_UPLO blasUplo() const {
370  return CblasLower;
371  };
372 
378  Matrix<Symmetric, AT> copy() const;
379 
387  Matrix<Symmetric, AT>& init(const AT &a);
388 
400  Matrix<General, AT> operator()(const Index &I, const Index &J);
401 
406  const Matrix<General, AT> operator()(const Index &I, const Index &J) const;
407 
420 
425  const Matrix<Symmetric, AT> operator()(const Index &I) const;
426 
438  Matrix<General, AT> operator()(int i1, int j1, int i2, int j2);
439 
444  const Matrix<General, AT> operator()(int i1, int j1, int i2, int j2) const;
445 
450  operator std::vector<std::vector<AT> >();
451  };
452  // ------------------------- Constructors -------------------------------------
453  // ----------------------------------------------------------------------------
454 
455  template <class AT>
457 
458  if(n==0) {
459  n=A.n;
460  } else {
461 #ifdef FMATVEC_SIZE_CHECK
462  assert(n == A.n);
463 #endif
464  }
465 
466  memory = A.memory;
467  ele = A.ele;
468  lda = A.lda;
469 
470  return *this;
471  }
472 
473  template <class AT>
475 
476  if(A.size() == 0)
477  return *this;
478 
479  if(n==0) {
480  n = A.n;
481  lda = n;
482  memory.resize(n*n);
483  ele = (AT*)memory.get();
484  } else {
485 #ifdef FMATVEC_SIZE_CHECK
486  assert(n == A.n);
487 #endif
488  }
489 
490  deepCopy(A);
491 
492  return *this;
493  }
494 
495  template <class AT>
497 
498  for(int i=0; i<rows(); i++)
499  for(int j=i; j<cols(); j++)
500  ele[i*lda+j] = val; // operator()(i,j) = val;
501 
502  return *this;
503  }
504 
505  template <class AT>
507 
508  Matrix<Symmetric, AT> A(n,NONINIT);
509  A.deepCopy(*this);
510 
511  return A;
512  }
513 
514  template <class AT>
516  for(int i=0; i<n; i++)
517  for(int j=i; j<n; j++)
518  ele[i*lda+j] = A.ele[i*A.lda+j];
519  }
520 
521 
522  template <class AT> const Matrix<General, AT> Matrix<Symmetric, AT>::operator()(int i1, int j1, int i2, int j2) const {
523  return operator()(Index(i1,i2),Index(j1,j2));
524  }
525 
526  template <class AT> Matrix<General, AT> Matrix<Symmetric, AT>::operator()(int i1, int j1, int i2, int j2) {
527  return operator()(Index(i1,i2),Index(j1,j2));
528  }
529 
530  template <class AT> const Matrix<General, AT> Matrix<Symmetric, AT>::operator()(const Index &I, const Index &J) const {
531 #ifndef FMATVEC_NO_BOUNDS_CHECK
532  assert(I.end()<n);
533  assert(J.end()<n);
534 #endif
535 
536  if(I.start() >= J.start()) {
537  assert(J.end() <= I.start());
538  return Matrix<General, AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,false,memory,elePtr(I.start(),J.start()));
539  } else {
540  assert(I.end() <= J.start());
541  return Matrix<General, AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,true,memory,elePtr(J.start(),I.start()));
542  }
543  }
544 
545  template <class AT> Matrix<General, AT> Matrix<Symmetric, AT>::operator()(const Index &I, const Index &J) {
546 #ifndef FMATVEC_NO_BOUNDS_CHECK
547  assert(I.end()<n);
548  assert(J.end()<n);
549 #endif
550 
551  if(I.start() >= J.start()) {
552  assert(J.end() <= I.start());
553  return Matrix<General, AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,false,memory,elePtr(I.start(),J.start()));
554  } else {
555  assert(I.end() <= J.start());
556  return Matrix<General, AT>(I.end()-I.start()+1,J.end()-J.start()+1,lda,true,memory,elePtr(J.start(),I.start()));
557  }
558  }
559 
560 
561 
563 #ifndef FMATVEC_NO_BOUNDS_CHECK
564  assert(I.end()<n);
565 #endif
566 
567  return Matrix<Symmetric, AT>(I.end()-I.start()+1,lda,memory,elePtr(I.start(),I.start()));
568  }
569 
570  template <class AT> const Matrix<Symmetric, AT> Matrix<Symmetric, AT>::operator()(const Index &I) const {
571  #ifndef FMATVEC_NO_BOUNDS_CHECK
572  assert(I.end()<n);
573 #endif
574 
575  return Matrix<Symmetric, AT>(I.end()-I.start()+1,lda,memory,elePtr(I.start(),I.start()));
576  }
577 
578  template <class AT>
579  Matrix<Symmetric, AT>::operator std::vector<std::vector<AT> >() {
580  std::vector<std::vector<AT> > ret(rows());
581  for(int r=0; r<rows(); r++) {
582  ret[r].resize(cols());
583  for(int c=0; c<cols(); c++)
584  ret[r][c]=operator()(r,c);
585  }
586  return ret;
587  }
588 
589  //extern void Matrix<Symmetric, double>::deepCopy(const Matrix<Symmetric, double> &A);
590 
591 }
592 
593 #endif