fmatvec  2.0.0
Fast Matrix/Vector Library
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  friend class Matrix<General,Ref,Ref,AT>;
51 
52  template <class Type, class Row, class Col> inline Matrix<Symmetric,Ref,Ref,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
54 
55  const AT* elePtr(int i, int j) const {
56  return j > i ? ele+i*lda+j : ele+i+j*lda;
57  }
58 
59  AT* elePtr(int i, int j) {
60  return j > i ? ele+i*lda+j : ele+i+j*lda;
61  }
62 
64 
65  public:
66 
67  typedef AT value_type;
68 
73  explicit Matrix() : memory(), ele(nullptr) {
74  }
75 
76  explicit Matrix(int n_, Noinit) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { }
77  explicit Matrix(int n_, Init ini=INIT, const AT &a=AT()) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(a); }
78  explicit Matrix(int n_, Eye ini, const AT &a=1) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(ini,a); }
79  explicit Matrix(int m_, int n_, Noinit) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { }
80  explicit Matrix(int m_, int n_, Init ini=INIT, const AT &a=AT()) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(a); }
81  explicit 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); }
82 
88  Matrix(const Matrix<Symmetric,Ref,Ref,AT> &A) : memory(A.n*A.n), ele((AT*)memory.get()) , n(A.n), lda(n) {
89  copy(A);
90  }
91 
97  template<class Row>
98  Matrix(const Matrix<Symmetric,Row,Row,AT> &A) : memory(A.size()*A.size()), ele((AT*)memory.get()), n(A.size()), lda(n) {
99  copy(A);
100  }
101 
107  template<class Type, class Row, class Col>
108  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : memory(A.rows()*A.cols()), ele((AT*)memory.get()), n(A.cols()), lda(n) {
109  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
110  copy(A);
111  }
112 
120  explicit Matrix(int n_, AT* ele_) : memory(), ele(ele_), n(n_), lda(n_) { }
121 
124  ~Matrix() = default;
125 
126  Matrix<Symmetric,Ref,Ref,AT>& resize(int n_, Noinit) {
127  n = n_; lda = n;
128  memory.resize(n*n);
129  ele = (AT*)memory.get();
130  return *this;
131  }
132 
133  Matrix<Symmetric,Ref,Ref,AT>& resize(int n, Init ini=INIT, const AT &a=AT()) { return resize(n,Noinit()).init(a); }
134 
135  Matrix<Symmetric,Ref,Ref,AT>& resize(int n, Eye ini, const AT &a=1) { return resize(n,Noinit()).init(ini,a); }
136 
139  void resize(int n, int m) {
140  if(n!=m)
141  throw std::runtime_error("A symmetric matrix cannot have different dimensions for rows and columns.");
142  resize(n);
143  }
144 
152  FMATVEC_ASSERT(n == A.rows(), AT);
153  return copy(A);
154  }
155 
162  template<class Type, class Row, class Col>
164  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
165  FMATVEC_ASSERT(n == A.rows(), AT);
166  return copy(A);
167  }
168 
176  n=A.n;
177  memory = A.memory;
178  ele = A.ele;
179  lda = A.lda;
180  return *this;
181  }
182 
189  template<class Type, class Row, class Col>
191  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
192  if(n!=A.rows()) resize(A.rows(),NONINIT);
193  return copy(A);
194  }
195 
205  AT& operator()(int i, int j) {
206  FMATVEC_ASSERT(i>=0, AT);
207  FMATVEC_ASSERT(j>=0, AT);
208  FMATVEC_ASSERT(i<n, AT);
209  FMATVEC_ASSERT(j<n, AT);
210  return e(i,j);
211  }
212 
217  const AT& operator()(int i, int j) const {
218  FMATVEC_ASSERT(i>=0, AT);
219  FMATVEC_ASSERT(j>=0, AT);
220  FMATVEC_ASSERT(i<n, AT);
221  FMATVEC_ASSERT(j<n, AT);
222 
223  return e(i,j);
224  }
225 
226  AT& ei(int i, int j) {
227  return ele[i+j*lda];
228  }
229 
230  const AT& ei(int i, int j) const {
231  return ele[i+j*lda];
232  }
233 
234  AT& ej(int i, int j) {
235  return ele[i*lda+j];
236  }
237 
238  const AT& ej(int i, int j) const {
239  return ele[i*lda+j];
240  }
241 
242  AT& e(int i, int j) {
243  return j > i ? ej(i,j) : ei(i,j);
244  }
245 
246  const AT& e(int i, int j) const {
247  return j > i ? ej(i,j) : ei(i,j);
248  }
249 
255  AT* operator()() {return ele;}
256 
261  const AT* operator()() const {return ele;}
262 
267  int size() const {return n;}
268 
273  int rows() const {return n;}
274 
279  int cols() const {return n;}
280 
285  int ldim() const {return lda;}
286 
294  const CBLAS_ORDER blasOrder() const {
295  return CblasColMajor;
296  }
297 
305  const CBLAS_UPLO blasUplo() const {
306  return CblasLower;
307  }
308 
316  inline Matrix<Symmetric,Ref,Ref,AT>& init(const AT &val);
317  inline Matrix<Symmetric,Ref,Ref,AT>& init(Init, const AT &a=AT()) { return init(a); }
318  inline Matrix<Symmetric,Ref,Ref,AT>& init(Eye, const AT &val=1);
319  inline Matrix<Symmetric,Ref,Ref,AT>& init(Noinit, const AT &a=AT()) { return *this; }
320 
323  inline const Matrix<General,Ref,Ref,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
324 
327  inline const Matrix<Symmetric,Ref,Ref,AT> operator()(const Range<Var,Var> &I) const;
328 
329  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);
330  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);
331 
332  template<class Row> inline void set(const fmatvec::Range<Var,Var> &I, const Matrix<Symmetric,Row,Row,AT> &A);
333  template<class Row> inline void add(const fmatvec::Range<Var,Var> &I, const Matrix<Symmetric,Row,Row,AT> &A);
334 
335  inline void ref(Matrix<Symmetric,Ref,Ref,AT> &A, const fmatvec::Range<Var,Var> &I);
336 
341  explicit inline operator std::vector<std::vector<AT>>() const;
342 
348  explicit Matrix(const std::vector<std::vector<AT>> &m);
349 
350 // /*! \brief Cast to AT.
351 // *
352 // * \return The AT representation of the matrix
353 // * */
354 // explicit operator AT() const {
355 // FMATVEC_ASSERT(n==1, AT);
356 // return e(0);
357 // }
358 //
359 // /*! \brief AT Constructor.
360 // * Constructs and initializes a matrix with a AT object.
361 // * \param x The AT the matrix will be initialized with.
362 // * */
363 // explicit Matrix(const AT &x) : memory(1), ele((AT*)memory.get()), n(1), lda(1) { ele[0] = x; }
364 
365  inline int countElements() const;
366  };
367 
368  template <class AT>
370 
371  for(int i=0; i<rows(); i++)
372  for(int j=i; j<cols(); j++)
373  ej(i,j) = val;
374 
375  return *this;
376  }
377 
378  template <class AT>
380 
381  for(int i=0; i<size(); i++) {
382  ej(i,i) = val;
383  for(int j=i+1; j<size(); j++) {
384  ej(i,j) = 0;
385  }
386  }
387  return *this;
388  }
389 
390  template <class AT>
392  FMATVEC_ASSERT(I.end()<n, AT);
393  FMATVEC_ASSERT(J.end()<n, AT);
394  Matrix<General,Ref,Ref,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
395 
396  for(int i=0; i<A.rows(); i++)
397  for(int j=0; j<A.cols(); j++)
398  A.e(i,j) = e(I.start()+i,J.start()+j);
399 
400  return A;
401  }
402 
403  template <class AT>
405  FMATVEC_ASSERT(I.end()<n, AT);
406 
407  Matrix<Symmetric,Ref,Ref,AT> A(I.end()-I.start()+1,NONINIT);
408 
409  for(int i=0; i<A.rows(); i++)
410  for(int j=i; j<A.cols(); j++)
411  A.e(i,j) = e(I.start()+i,I.start()+j);
412 
413  return A;
414  }
415 
416  template <class AT> template<class Type, class Row, class Col>
418 
419  if(I.start()>=J.start()) FMATVEC_ASSERT(I.start()>=J.end(), AT)
420  else FMATVEC_ASSERT(J.start()>=I.end(), AT);
421  FMATVEC_ASSERT(I.end()<rows(), AT);
422  FMATVEC_ASSERT(J.end()<cols(), AT);
423  FMATVEC_ASSERT(I.size()==A.rows(), AT);
424  FMATVEC_ASSERT(J.size()==A.cols(), AT);
425 
426  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
427  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
428  e(i,j) = A.e(ii,jj);
429  }
430 
431  template <class AT> template<class Type, class Row, class Col>
432  inline void Matrix<Symmetric,Ref,Ref,AT>::add(const fmatvec::Range<Var,Var> &I, const fmatvec::Range<Var,Var> &J, const Matrix<Type,Row,Col,AT> &A) {
433 
434  if(I.start()>=J.start()) FMATVEC_ASSERT(I.start()>=J.end(), AT)
435  else FMATVEC_ASSERT(J.start()>=I.end(), AT);
436  FMATVEC_ASSERT(I.end()<rows(), AT);
437  FMATVEC_ASSERT(J.end()<cols(), AT);
438  FMATVEC_ASSERT(I.size()==A.rows(), AT);
439  FMATVEC_ASSERT(J.size()==A.cols(), AT);
440 
441  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
442  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
443  e(i,j) += A.e(ii,jj);
444  }
445 
446  template <class AT> template<class Row>
447  inline void Matrix<Symmetric,Ref,Ref,AT>::set(const fmatvec::Range<Var,Var> &I, const Matrix<Symmetric,Row,Row,AT> &A) {
448 
449  FMATVEC_ASSERT(I.end()<size(), AT);
450  FMATVEC_ASSERT(I.size()==A.size(), AT);
451 
452  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
453  for(int j=i, jj=ii; j<=I.end(); j++, jj++)
454  e(i,j) = A.e(ii,jj);
455  }
456 
457  template <class AT> template<class Row>
458  inline void Matrix<Symmetric,Ref,Ref,AT>::add(const fmatvec::Range<Var,Var> &I, const Matrix<Symmetric,Row,Row,AT> &A) {
459 
460  FMATVEC_ASSERT(I.end()<size(), AT);
461  FMATVEC_ASSERT(I.size()==A.size(), AT);
462 
463  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
464  for(int j=i, jj=ii; j<=I.end(); j++, jj++)
465  e(i,j) += A.e(ii,jj);
466  }
467 
468  template <class AT>
469  inline void Matrix<Symmetric,Ref,Ref,AT>::ref(Matrix<Symmetric,Ref,Ref,AT> &A, const fmatvec::Range<Var,Var> &I) {
470  FMATVEC_ASSERT(I.end()<A.size(), AT);
471  n=I.size();
472  memory = A.memory;
473  ele = A.elePtr(I.start(),I.start());
474  lda = A.lda;
475  }
476 
477  template <class AT>
478  inline Matrix<Symmetric,Ref,Ref,AT>::operator std::vector<std::vector<AT>>() const {
479  std::vector<std::vector<AT>> ret(rows());
480  for(int r=0; r<rows(); r++) {
481  ret[r].resize(cols());
482  for(int c=0; c<cols(); c++)
483  ret[r][c]=operator()(r,c);
484  }
485  return ret;
486  }
487 
488  template <class AT>
489  Matrix<Symmetric,Ref,Ref,AT>::Matrix(const std::vector<std::vector<AT>> &m) : Matrix<Symmetric,Ref,Ref,AT>(static_cast<int>(m.size())) {
490  for(int r=0; r<rows(); r++) {
491  if(static_cast<int>(m[r].size())!=cols())
492  throw std::runtime_error("The rows of the input have different length.");
493  for(int c=0; c<cols(); c++) {
494  e(r,c)=m[r][c];
495  if(r>c && abs(m[r][c]-m[c][r])>abs(m[r][c])*1e-13+1e-13)
496  throw std::runtime_error("The input is not symmetric.");
497  }
498  }
499  }
500 
501  template <class AT>
503  int k = n;
504  for (int j = 0; j < n; j++) {
505  for (int i = j+1; i < n; i++) {
506  if (fabs(e(i, j)) > 1e-16) {
507  k+=2;
508  }
509  }
510  }
511  return k;
512  }
513 
515 
516  template <class AT>
518  for(int i=0; i<n; i++)
519  for(int j=i; j<n; j++)
520  ej(i,j) = A.ej(i,j);
521  return *this;
522  }
523 
524  template <class AT> template <class Type, class Row, class Col>
525  inline Matrix<Symmetric,Ref,Ref,AT>& Matrix<Symmetric,Ref,Ref,AT>::copy(const Matrix<Type,Row,Col,AT> &A) {
526  for(int i=0; i<n; i++)
527  for(int j=i; j<n; j++)
528  ej(i,j) = A.e(i,j);
529  return *this;
530  }
531 
533 
534 }
535 
536 #endif
int end() const
Last element.
Definition: range.h:91
const AT & operator()(int i, int j) const
Element operator.
Definition: symmetric_matrix.h:217
int cols() const
Number of columns.
Definition: symmetric_matrix.h:279
AT & operator()(int i, int j)
Element operator.
Definition: symmetric_matrix.h:205
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Shape class for symmetric matrices.
Definition: types.h:148
Matrix< Symmetric, Ref, Ref, AT > & operator=(const Matrix< Symmetric, Ref, Ref, AT > &A)
Assignment operator.
Definition: symmetric_matrix.h:151
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:82
Definition: types.h:85
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: symmetric_matrix.h:108
const CBLAS_UPLO blasUplo() const
Symmetry convention.
Definition: symmetric_matrix.h:305
const AT * operator()() const
Pointer operator.
Definition: symmetric_matrix.h:261
Definition: types.h:94
int start() const
First element.
Definition: range.h:85
int ldim() const
Leading dimension.
Definition: symmetric_matrix.h:285
int cols() const
Number of columns.
Matrix(int n_, AT *ele_)
Regular Constructor.
Definition: symmetric_matrix.h:120
This is a matrix class for general matrices.
Definition: general_matrix.h:41
AT * operator()()
Pointer operator.
Definition: symmetric_matrix.h:255
Matrix(const Matrix< Symmetric, Row, Row, AT > &A)
Copy Constructor.
Definition: symmetric_matrix.h:98
Namespace fmatvec.
Definition: _memory.cc:28
int rows() const
Number of rows.
Definition: symmetric_matrix.h:273
int size() const
Size.
Definition: range.h:97
Matrix(const Matrix< Symmetric, Ref, Ref, AT > &A)
Copy Constructor.
Definition: symmetric_matrix.h:88
Definition: types.h:84
This is a matrix class for symmetric matrices.
Definition: symmetric_matrix.h:39
Basic shape class for matrices.
Definition: types.h:132
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: symmetric_matrix.h:294
Matrix()
Standard constructor.
Definition: symmetric_matrix.h:73
int rows() const
Number of rows.
void resize(int n, int m)
Definition: symmetric_matrix.h:139
Matrix< Symmetric, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: symmetric_matrix.h:163
Matrix< Symmetric, Ref, Ref, AT > & init(const AT &val)
Initialization.
Definition: symmetric_matrix.h:369
int size() const
Size.
Definition: symmetric_matrix.h:267
This is an index class for creating submatrices.
Definition: range.h:44
Matrix< Symmetric, Ref, Ref, AT > & operator&=(Matrix< Symmetric, Ref, Ref, AT > &A)
Reference operator.
Definition: symmetric_matrix.h:175
Definition: types.h:86