fmatvec  0.0.0
Fast Matrix/Vector Library
var_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 var_symmetric_matrix_h
23 #define var_symmetric_matrix_h
24 
25 #include "types.h"
26 
27 namespace fmatvec {
28 
37  template <class AT> class Matrix<Symmetric,Var,Var,AT> {
38 
39  protected:
40 
42 
43  int M{0};
44 
45  AT *ele;
46 
47  template <class Type, class Row, class Col> inline Matrix<Symmetric,Var,Var,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
48  template <class Row> inline Matrix<Symmetric,Var,Var,AT>& copy(const Matrix<Symmetric,Row,Row,AT> &A);
49 
51 
52  public:
53  static constexpr bool isVector {false};
54  typedef AT value_type;
55  typedef Symmetric shape_type;
56 
61  explicit Matrix() : ele(nullptr) {
62  }
63 
64  explicit Matrix(int m, Noinit) : M(m), ele(new AT[M*M]) { }
65  explicit Matrix(int m, Init ini=INIT, const AT &a=AT()) : M(m), ele(new AT[M*M]) { init(a); }
66  explicit Matrix(int m, Eye ini, const AT &a=1) : M(m), ele(new AT[M*M]) { init(ini,a); }
67  explicit Matrix(int m, int n, Noinit) : M(m), ele(new AT[M*M]) { }
68  explicit Matrix(int m, int n, Init ini=INIT, const AT &a=AT()) : M(m), ele(new AT[M*M]) { init(a); }
69  explicit Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), ele(new AT[M*M]) { init(ini,a); }
70 
76  Matrix(const Matrix<Symmetric,Var,Var,AT> &A) : M(A.M), ele(new AT[M*M]) {
77  copy(A);
78  }
79 
85  template<class Row>
86  Matrix(const Matrix<Symmetric,Row,Row,AT> &A) : M(A.size()), ele(new AT[M*M]) {
87  copy(A);
88  }
89 
95  template<class Type, class Row, class Col>
96  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*M]) {
97  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
98  copy(A);
99  }
100 
104  delete[] ele;
105  }
106 
107  Matrix<Symmetric,Var,Var,AT>& resize(int m, Noinit) {
108  delete[] ele;
109  M = m;
110  ele = new AT[M*M];
111  return *this;
112  }
113 
114  Matrix<Symmetric,Var,Var,AT>& resize(int m, Init ini=INIT, const AT &a=AT()) { return resize(m,Noinit()).init(a); }
115 
116  Matrix<Symmetric,Var,Var,AT>& resize(int m, Eye ini, const AT &a=1) { return resize(m,Noinit()).init(ini,a); }
117 
125  FMATVEC_ASSERT(M == A.M, AT);
126  return copy(A);
127  }
128 
135  template<class Type, class Row, class Col>
137  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
138  FMATVEC_ASSERT(M == A.rows(), AT);
139  return copy(A);
140  }
141 
148  template<class Type, class Row, class Col>
150  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
151  if(M!=A.rows()) resize(A.rows(),NONINIT);
152  return copy(A);
153  }
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 
172  AT& operator()(int i, int j) {
173  FMATVEC_ASSERT(i>=0, AT);
174  FMATVEC_ASSERT(j>=0, AT);
175  FMATVEC_ASSERT(i<M, AT);
176  FMATVEC_ASSERT(j<M, AT);
177 
178  return e(i,j);
179  }
180 
185  const AT& operator()(int i, int j) const {
186  FMATVEC_ASSERT(i>=0, AT);
187  FMATVEC_ASSERT(j>=0, AT);
188  FMATVEC_ASSERT(i<M, AT);
189  FMATVEC_ASSERT(j<M, AT);
190 
191  return e(i,j);
192  }
193 
194  AT& ei(int i, int j) {
195  return ele[i*M+j];
196  }
197 
198  const AT& ei(int i, int j) const {
199  return ele[i*M+j];
200  }
201 
202  AT& ej(int i, int j) {
203  return ele[i+j*M];
204  }
205 
206  const AT& ej(int i, int j) const {
207  return ele[i+j*M];
208  }
209 
210  AT& e(int i, int j) {
211  return j > i ? ej(i,j) : ei(i,j);
212  }
213 
214  const AT& e(int i, int j) const {
215  return j > i ? ej(i,j) : ei(i,j);
216  }
217 
223  AT* operator()() {return ele;}
224 
229  const AT* operator()() const {return ele;}
230 
235  constexpr int size() const {return M;}
236 
241  constexpr int rows() const {return M;}
242 
247  constexpr int cols() const {return M;}
248 
253  int ldim() const {return M;}
254 
262  const CBLAS_ORDER blasOrder() const {
263  return CblasRowMajor;
264  }
265 
273  const CBLAS_UPLO blasUplo() const {
274  return CblasLower;
275  }
276 
284  inline Matrix<Symmetric,Var,Var,AT>& init(const AT &val=AT());
285  inline Matrix<Symmetric,Var,Var,AT>& init(Init, const AT &a=AT()) { return init(a); }
286  inline Matrix<Symmetric,Var,Var,AT>& init(Eye eye, const AT &val=1);
287  inline Matrix<Symmetric,Var,Var,AT>& init(Noinit, const AT &a=AT()) { return *this; }
288 
293  explicit inline operator std::vector<std::vector<AT>>() const;
294 
295  inline int countElements() const;
296  };
297 
298  template <class AT>
300 
301  for(int i=0; i<M; i++)
302  for(int j=i; j<M; j++)
303  ej(i,j) = val;
304 
305  return *this;
306  }
307 
308  template <class AT>
310 
311  for(int i=0; i<size(); i++) {
312  ej(i,i) = val;
313  for(int j=i+1; j<size(); j++) {
314  ej(i,j) = 0;
315  }
316  }
317  return *this;
318  }
319 
320  template <class AT>
321  inline Matrix<Symmetric,Var,Var,AT>::operator std::vector<std::vector<AT>>() const {
322  std::vector<std::vector<AT>> ret(rows());
323  for(int r=0; r<rows(); r++) {
324  ret[r].resize(cols());
325  for(int c=0; c<cols(); c++)
326  ret[r][c]=operator()(r,c);
327  }
328  return ret;
329  }
330 
332 
333  template <class AT> template <class Type, class Row, class Col>
335  for(int i=0; i<M; i++)
336  for(int j=i; j<M; j++)
337  ej(i,j) = A.e(i,j);
338  return *this;
339  }
340 
341  template <class AT> template <class Row>
342  inline Matrix<Symmetric,Var,Var,AT>& Matrix<Symmetric,Var,Var,AT>::copy(const Matrix<Symmetric,Row,Row,AT> &A) {
343  for(int i=0; i<M; i++)
344  for(int j=i; j<M; j++)
345  ej(i,j) = A.ej(i,j);
346  return *this;
347  }
348 
349  template <class AT>
350  inline int Matrix<Symmetric,Var,Var,AT>::countElements() const {
351  int k = M;
352  for (int j = 0; j < M; j++) {
353  for (int i = j+1; i < M; i++) {
354  if (fabs(e(i, j)) > 1e-16) {
355  k+=2;
356  }
357  }
358  }
359  return k;
360  }
361 
363 
364 }
365 
366 #endif
This is a matrix class for symmetric matrices.
Definition: var_symmetric_matrix.h:37
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
const CBLAS_UPLO blasUplo() const
Symmetry convention.
Definition: var_symmetric_matrix.h:273
Shape class for symmetric matrices.
Definition: types.h:148
Definition: types.h:85
Definition: types.h:97
constexpr int rows() const
Number of rows.
Definition: var_symmetric_matrix.h:241
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_symmetric_matrix.h:262
int cols() const
Number of columns.
Matrix(const Matrix< Symmetric, Var, Var, AT > &A)
Copy Constructor.
Definition: var_symmetric_matrix.h:76
AT * operator()()
Pointer operator.
Definition: var_symmetric_matrix.h:223
Matrix< Symmetric, Var, Var, AT > & operator=(const Matrix< Symmetric, Var, Var, AT > &A)
Assignment operator.
Definition: var_symmetric_matrix.h:124
AT & operator()(int i, int j)
Element operator.
Definition: var_symmetric_matrix.h:172
Matrix< Symmetric, Var, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_symmetric_matrix.h:136
const AT * operator()() const
Pointer operator.
Definition: var_symmetric_matrix.h:229
Namespace fmatvec.
Definition: _memory.cc:28
int ldim() const
Leading dimension.
Definition: var_symmetric_matrix.h:253
~Matrix()
Destructor.
Definition: var_symmetric_matrix.h:103
Matrix(const Matrix< Symmetric, Row, Row, AT > &A)
Copy Constructor.
Definition: var_symmetric_matrix.h:86
constexpr int size() const
Size.
Definition: var_symmetric_matrix.h:235
Matrix()
Standard constructor.
Definition: var_symmetric_matrix.h:61
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_symmetric_matrix.h:96
constexpr int cols() const
Number of columns.
Definition: var_symmetric_matrix.h:247
Definition: types.h:84
void resize(int n, int m)
Definition: var_symmetric_matrix.h:157
int rows() const
Number of rows.
const AT & operator()(int i, int j) const
Element operator.
Definition: var_symmetric_matrix.h:185
Definition: types.h:86