fmatvec  0.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  static constexpr bool isVector {false};
68  typedef AT value_type;
69  typedef Symmetric shape_type;
70 
75  explicit Matrix() : memory(), ele(nullptr) {
76  }
77 
78  explicit Matrix(int n_, Noinit) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { }
79  explicit Matrix(int n_, Init ini=INIT, const AT &a=AT()) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(a); }
80  explicit Matrix(int n_, Eye ini, const AT &a=1) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { init(ini,a); }
81  explicit Matrix(int m_, int n_, Noinit) : memory(n_*n_), ele((AT*)memory.get()), n(n_), lda(n_) { }
82  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); }
83  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); }
84 
90  Matrix(const Matrix<Symmetric,Ref,Ref,AT> &A) : memory(A.n*A.n), ele((AT*)memory.get()) , n(A.n), lda(n) {
91  copy(A);
92  }
93 
99  template<class Row>
100  Matrix(const Matrix<Symmetric,Row,Row,AT> &A) : memory(A.size()*A.size()), ele((AT*)memory.get()), n(A.size()), lda(n) {
101  copy(A);
102  }
103 
109  template<class Type, class Row, class Col>
110  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : memory(A.rows()*A.cols()), ele((AT*)memory.get()), n(A.cols()), lda(n) {
111  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
112  copy(A);
113  }
114 
122  explicit Matrix(int n_, AT* ele_) : memory(), ele(ele_), n(n_), lda(n_) { }
123 
126  ~Matrix() = default;
127 
128  Matrix<Symmetric,Ref,Ref,AT>& resize(int n_, Noinit) {
129  n = n_; lda = n;
130  memory.resize(n*n);
131  ele = (AT*)memory.get();
132  return *this;
133  }
134 
135  Matrix<Symmetric,Ref,Ref,AT>& resize(int n, Init ini=INIT, const AT &a=AT()) { return resize(n,Noinit()).init(a); }
136 
137  Matrix<Symmetric,Ref,Ref,AT>& resize(int n, Eye ini, const AT &a=1) { return resize(n,Noinit()).init(ini,a); }
138 
141  void resize(int n, int m) {
142  if(n!=m)
143  throw std::runtime_error("A symmetric matrix cannot have different dimensions for rows and columns.");
144  resize(n);
145  }
146 
154  FMATVEC_ASSERT(n == A.rows(), AT);
155  return copy(A);
156  }
157 
164  template<class Type, class Row, class Col>
166  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
167  FMATVEC_ASSERT(n == A.rows(), AT);
168  return copy(A);
169  }
170 
178  n=A.n;
179  memory = A.memory;
180  ele = A.ele;
181  lda = A.lda;
182  return *this;
183  }
184 
191  template<class Type, class Row, class Col>
193  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
194  if(n!=A.rows()) resize(A.rows(),NONINIT);
195  return copy(A);
196  }
197 
207  AT& operator()(int i, int j) {
208  FMATVEC_ASSERT(i>=0, AT);
209  FMATVEC_ASSERT(j>=0, AT);
210  FMATVEC_ASSERT(i<n, AT);
211  FMATVEC_ASSERT(j<n, AT);
212  return e(i,j);
213  }
214 
219  const AT& operator()(int i, int j) const {
220  FMATVEC_ASSERT(i>=0, AT);
221  FMATVEC_ASSERT(j>=0, AT);
222  FMATVEC_ASSERT(i<n, AT);
223  FMATVEC_ASSERT(j<n, AT);
224 
225  return e(i,j);
226  }
227 
228  AT& ei(int i, int j) {
229  return ele[i+j*lda];
230  }
231 
232  const AT& ei(int i, int j) const {
233  return ele[i+j*lda];
234  }
235 
236  AT& ej(int i, int j) {
237  return ele[i*lda+j];
238  }
239 
240  const AT& ej(int i, int j) const {
241  return ele[i*lda+j];
242  }
243 
244  AT& e(int i, int j) {
245  return j > i ? ej(i,j) : ei(i,j);
246  }
247 
248  const AT& e(int i, int j) const {
249  return j > i ? ej(i,j) : ei(i,j);
250  }
251 
257  AT* operator()() {return ele;}
258 
263  const AT* operator()() const {return ele;}
264 
269  int size() const {return n;}
270 
275  int rows() const {return n;}
276 
281  int cols() const {return n;}
282 
287  int ldim() const {return lda;}
288 
296  const CBLAS_ORDER blasOrder() const {
297  return CblasColMajor;
298  }
299 
307  const CBLAS_UPLO blasUplo() const {
308  return CblasLower;
309  }
310 
318  inline Matrix<Symmetric,Ref,Ref,AT>& init(const AT &val);
319  inline Matrix<Symmetric,Ref,Ref,AT>& init(Init, const AT &a=AT()) { return init(a); }
320  inline Matrix<Symmetric,Ref,Ref,AT>& init(Eye, const AT &val=1);
321  inline Matrix<Symmetric,Ref,Ref,AT>& init(Noinit, const AT &a=AT()) { return *this; }
322 
325  inline const Matrix<General,Ref,Ref,AT> operator()(const Range<Var,Var> &I, const Range<Var,Var> &J) const;
326 
329  inline const Matrix<Symmetric,Ref,Ref,AT> operator()(const Range<Var,Var> &I) const;
330 
331  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);
332  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);
333 
334  template<class Row> inline void set(const fmatvec::Range<Var,Var> &I, const Matrix<Symmetric,Row,Row,AT> &A);
335  template<class Row> inline void add(const fmatvec::Range<Var,Var> &I, const Matrix<Symmetric,Row,Row,AT> &A);
336 
337  inline void ref(Matrix<Symmetric,Ref,Ref,AT> &A, const fmatvec::Range<Var,Var> &I);
338 
343  explicit inline operator std::vector<std::vector<AT>>() const;
344 
350  explicit Matrix(const std::vector<std::vector<AT>> &m);
351 
352 // /*! \brief Cast to AT.
353 // *
354 // * \return The AT representation of the matrix
355 // * */
356 // explicit operator AT() const {
357 // FMATVEC_ASSERT(n==1, AT);
358 // return e(0);
359 // }
360 //
361 // /*! \brief AT Constructor.
362 // * Constructs and initializes a matrix with a AT object.
363 // * \param x The AT the matrix will be initialized with.
364 // * */
365 // explicit Matrix(const AT &x) : memory(1), ele((AT*)memory.get()), n(1), lda(1) { ele[0] = x; }
366 
367  inline int countElements() const;
368  };
369 
370  template <class AT>
372 
373  for(int i=0; i<rows(); i++)
374  for(int j=i; j<cols(); j++)
375  ej(i,j) = val;
376 
377  return *this;
378  }
379 
380  template <class AT>
382 
383  for(int i=0; i<size(); i++) {
384  ej(i,i) = val;
385  for(int j=i+1; j<size(); j++) {
386  ej(i,j) = 0;
387  }
388  }
389  return *this;
390  }
391 
392  template <class AT>
394  FMATVEC_ASSERT(I.end()<n, AT);
395  FMATVEC_ASSERT(J.end()<n, AT);
396  Matrix<General,Ref,Ref,AT> A(I.end()-I.start()+1,J.end()-J.start()+1,NONINIT);
397 
398  for(int i=0; i<A.rows(); i++)
399  for(int j=0; j<A.cols(); j++)
400  A.e(i,j) = e(I.start()+i,J.start()+j);
401 
402  return A;
403  }
404 
405  template <class AT>
407  FMATVEC_ASSERT(I.end()<n, AT);
408 
409  Matrix<Symmetric,Ref,Ref,AT> A(I.end()-I.start()+1,NONINIT);
410 
411  for(int i=0; i<A.rows(); i++)
412  for(int j=i; j<A.cols(); j++)
413  A.e(i,j) = e(I.start()+i,I.start()+j);
414 
415  return A;
416  }
417 
418  template <class AT> template<class Type, class Row, class Col>
420 
421  if(I.start()>=J.start()) FMATVEC_ASSERT(I.start()>=J.end(), AT)
422  else FMATVEC_ASSERT(J.start()>=I.end(), AT);
423  FMATVEC_ASSERT(I.end()<rows(), AT);
424  FMATVEC_ASSERT(J.end()<cols(), AT);
425  FMATVEC_ASSERT(I.size()==A.rows(), AT);
426  FMATVEC_ASSERT(J.size()==A.cols(), AT);
427 
428  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
429  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
430  e(i,j) = A.e(ii,jj);
431  }
432 
433  template <class AT> template<class Type, class Row, class Col>
434  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) {
435 
436  if(I.start()>=J.start()) FMATVEC_ASSERT(I.start()>=J.end(), AT)
437  else FMATVEC_ASSERT(J.start()>=I.end(), AT);
438  FMATVEC_ASSERT(I.end()<rows(), AT);
439  FMATVEC_ASSERT(J.end()<cols(), AT);
440  FMATVEC_ASSERT(I.size()==A.rows(), AT);
441  FMATVEC_ASSERT(J.size()==A.cols(), AT);
442 
443  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
444  for(int j=J.start(), jj=0; j<=J.end(); j++, jj++)
445  e(i,j) += A.e(ii,jj);
446  }
447 
448  template <class AT> template<class Row>
449  inline void Matrix<Symmetric,Ref,Ref,AT>::set(const fmatvec::Range<Var,Var> &I, const Matrix<Symmetric,Row,Row,AT> &A) {
450 
451  FMATVEC_ASSERT(I.end()<size(), AT);
452  FMATVEC_ASSERT(I.size()==A.size(), AT);
453 
454  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
455  for(int j=i, jj=ii; j<=I.end(); j++, jj++)
456  e(i,j) = A.e(ii,jj);
457  }
458 
459  template <class AT> template<class Row>
460  inline void Matrix<Symmetric,Ref,Ref,AT>::add(const fmatvec::Range<Var,Var> &I, const Matrix<Symmetric,Row,Row,AT> &A) {
461 
462  FMATVEC_ASSERT(I.end()<size(), AT);
463  FMATVEC_ASSERT(I.size()==A.size(), AT);
464 
465  for(int i=I.start(), ii=0; i<=I.end(); i++, ii++)
466  for(int j=i, jj=ii; j<=I.end(); j++, jj++)
467  e(i,j) += A.e(ii,jj);
468  }
469 
470  template <class AT>
471  inline void Matrix<Symmetric,Ref,Ref,AT>::ref(Matrix<Symmetric,Ref,Ref,AT> &A, const fmatvec::Range<Var,Var> &I) {
472  FMATVEC_ASSERT(I.end()<A.size(), AT);
473  n=I.size();
474  memory = A.memory;
475  ele = A.elePtr(I.start(),I.start());
476  lda = A.lda;
477  }
478 
479  template <class AT>
480  inline Matrix<Symmetric,Ref,Ref,AT>::operator std::vector<std::vector<AT>>() const {
481  std::vector<std::vector<AT>> ret(rows());
482  for(int r=0; r<rows(); r++) {
483  ret[r].resize(cols());
484  for(int c=0; c<cols(); c++)
485  ret[r][c]=operator()(r,c);
486  }
487  return ret;
488  }
489 
490  template <class AT>
491  Matrix<Symmetric,Ref,Ref,AT>::Matrix(const std::vector<std::vector<AT>> &m) : Matrix<Symmetric,Ref,Ref,AT>(static_cast<int>(m.size())) {
492  for(int r=0; r<rows(); r++) {
493  if(static_cast<int>(m[r].size())!=cols())
494  throw std::runtime_error("The rows of the input have different length.");
495  for(int c=0; c<cols(); c++) {
496  e(r,c)=m[r][c];
497  if(r>c && abs(m[r][c]-m[c][r])>abs(m[r][c])*1e-13+1e-13)
498  throw std::runtime_error("The input is not symmetric.");
499  }
500  }
501  }
502 
503  template <class AT>
505  int k = n;
506  for (int j = 0; j < n; j++) {
507  for (int i = j+1; i < n; i++) {
508  if (fabs(e(i, j)) > 1e-16) {
509  k+=2;
510  }
511  }
512  }
513  return k;
514  }
515 
517 
518  template <class AT>
520  for(int i=0; i<n; i++)
521  for(int j=i; j<n; j++)
522  ej(i,j) = A.ej(i,j);
523  return *this;
524  }
525 
526  template <class AT> template <class Type, class Row, class Col>
527  inline Matrix<Symmetric,Ref,Ref,AT>& Matrix<Symmetric,Ref,Ref,AT>::copy(const Matrix<Type,Row,Col,AT> &A) {
528  for(int i=0; i<n; i++)
529  for(int j=i; j<n; j++)
530  ej(i,j) = A.e(i,j);
531  return *this;
532  }
533 
535 
536 }
537 
538 #endif
int end() const
Last element.
Definition: range.h:91
const AT & operator()(int i, int j) const
Element operator.
Definition: symmetric_matrix.h:219
int cols() const
Number of columns.
Definition: symmetric_matrix.h:281
AT & operator()(int i, int j)
Element operator.
Definition: symmetric_matrix.h:207
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:153
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:84
Definition: types.h:85
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: symmetric_matrix.h:110
const CBLAS_UPLO blasUplo() const
Symmetry convention.
Definition: symmetric_matrix.h:307
const AT * operator()() const
Pointer operator.
Definition: symmetric_matrix.h:263
Definition: types.h:94
int start() const
First element.
Definition: range.h:85
int ldim() const
Leading dimension.
Definition: symmetric_matrix.h:287
int cols() const
Number of columns.
Matrix(int n_, AT *ele_)
Regular Constructor.
Definition: symmetric_matrix.h:122
This is a matrix class for general matrices.
Definition: general_matrix.h:41
AT * operator()()
Pointer operator.
Definition: symmetric_matrix.h:257
Matrix(const Matrix< Symmetric, Row, Row, AT > &A)
Copy Constructor.
Definition: symmetric_matrix.h:100
Namespace fmatvec.
Definition: _memory.cc:28
int rows() const
Number of rows.
Definition: symmetric_matrix.h:275
int size() const
Size.
Definition: range.h:97
Matrix(const Matrix< Symmetric, Ref, Ref, AT > &A)
Copy Constructor.
Definition: symmetric_matrix.h:90
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:296
Matrix()
Standard constructor.
Definition: symmetric_matrix.h:75
int rows() const
Number of rows.
void resize(int n, int m)
Definition: symmetric_matrix.h:141
Matrix< Symmetric, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: symmetric_matrix.h:165
Matrix< Symmetric, Ref, Ref, AT > & init(const AT &val)
Initialization.
Definition: symmetric_matrix.h:371
int size() const
Size.
Definition: symmetric_matrix.h:269
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:177
Definition: types.h:86