fmatvec  2.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  typedef AT value_type;
54 
59  explicit Matrix() : ele(nullptr) {
60  }
61 
62  explicit Matrix(int m, Noinit) : M(m), ele(new AT[M*M]) { }
63  explicit Matrix(int m, Init ini=INIT, const AT &a=AT()) : M(m), ele(new AT[M*M]) { init(a); }
64  explicit Matrix(int m, Eye ini, const AT &a=1) : M(m), ele(new AT[M*M]) { init(ini,a); }
65  explicit Matrix(int m, int n, Noinit) : M(m), ele(new AT[M*M]) { }
66  explicit Matrix(int m, int n, Init ini=INIT, const AT &a=AT()) : M(m), ele(new AT[M*M]) { init(a); }
67  explicit Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), ele(new AT[M*M]) { init(ini,a); }
68 
74  Matrix(const Matrix<Symmetric,Var,Var,AT> &A) : M(A.M), ele(new AT[M*M]) {
75  copy(A);
76  }
77 
83  template<class Row>
84  Matrix(const Matrix<Symmetric,Row,Row,AT> &A) : M(A.size()), ele(new AT[M*M]) {
85  copy(A);
86  }
87 
93  template<class Type, class Row, class Col>
94  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*M]) {
95  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
96  copy(A);
97  }
98 
102  delete[] ele;
103  }
104 
105  Matrix<Symmetric,Var,Var,AT>& resize(int m, Noinit) {
106  delete[] ele;
107  M = m;
108  ele = new AT[M*M];
109  return *this;
110  }
111 
112  Matrix<Symmetric,Var,Var,AT>& resize(int m, Init ini=INIT, const AT &a=AT()) { return resize(m,Noinit()).init(a); }
113 
114  Matrix<Symmetric,Var,Var,AT>& resize(int m, Eye ini, const AT &a=1) { return resize(m,Noinit()).init(ini,a); }
115 
123  FMATVEC_ASSERT(M == A.M, AT);
124  return copy(A);
125  }
126 
133  template<class Type, class Row, class Col>
135  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
136  FMATVEC_ASSERT(M == A.rows(), AT);
137  return copy(A);
138  }
139 
146  template<class Type, class Row, class Col>
148  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
149  if(M!=A.rows()) resize(A.rows(),NONINIT);
150  return copy(A);
151  }
152 
155  void resize(int n, int m) {
156  if(n!=m)
157  throw std::runtime_error("A symmetric matrix cannot have different dimensions for rows and columns.");
158  resize(n);
159  }
160 
170  AT& operator()(int i, int j) {
171  FMATVEC_ASSERT(i>=0, AT);
172  FMATVEC_ASSERT(j>=0, AT);
173  FMATVEC_ASSERT(i<M, AT);
174  FMATVEC_ASSERT(j<M, AT);
175 
176  return e(i,j);
177  }
178 
183  const AT& operator()(int i, int j) const {
184  FMATVEC_ASSERT(i>=0, AT);
185  FMATVEC_ASSERT(j>=0, AT);
186  FMATVEC_ASSERT(i<M, AT);
187  FMATVEC_ASSERT(j<M, AT);
188 
189  return e(i,j);
190  }
191 
192  AT& ei(int i, int j) {
193  return ele[i*M+j];
194  }
195 
196  const AT& ei(int i, int j) const {
197  return ele[i*M+j];
198  }
199 
200  AT& ej(int i, int j) {
201  return ele[i+j*M];
202  }
203 
204  const AT& ej(int i, int j) const {
205  return ele[i+j*M];
206  }
207 
208  AT& e(int i, int j) {
209  return j > i ? ej(i,j) : ei(i,j);
210  }
211 
212  const AT& e(int i, int j) const {
213  return j > i ? ej(i,j) : ei(i,j);
214  }
215 
221  AT* operator()() {return ele;}
222 
227  const AT* operator()() const {return ele;}
228 
233  int size() const {return M;}
234 
239  int rows() const {return M;}
240 
245  int cols() const {return M;}
246 
251  int ldim() const {return M;}
252 
260  const CBLAS_ORDER blasOrder() const {
261  return CblasRowMajor;
262  }
263 
271  const CBLAS_UPLO blasUplo() const {
272  return CblasLower;
273  }
274 
282  inline Matrix<Symmetric,Var,Var,AT>& init(const AT &val=AT());
283  inline Matrix<Symmetric,Var,Var,AT>& init(Init, const AT &a=AT()) { return init(a); }
284  inline Matrix<Symmetric,Var,Var,AT>& init(Eye eye, const AT &val=1);
285  inline Matrix<Symmetric,Var,Var,AT>& init(Noinit, const AT &a=AT()) { return *this; }
286 
291  explicit inline operator std::vector<std::vector<AT>>() const;
292 
293  inline int countElements() const;
294  };
295 
296  template <class AT>
298 
299  for(int i=0; i<M; i++)
300  for(int j=i; j<M; j++)
301  ej(i,j) = val;
302 
303  return *this;
304  }
305 
306  template <class AT>
308 
309  for(int i=0; i<size(); i++) {
310  ej(i,i) = val;
311  for(int j=i+1; j<size(); j++) {
312  ej(i,j) = 0;
313  }
314  }
315  return *this;
316  }
317 
318  template <class AT>
319  inline Matrix<Symmetric,Var,Var,AT>::operator std::vector<std::vector<AT>>() const {
320  std::vector<std::vector<AT>> ret(rows());
321  for(int r=0; r<rows(); r++) {
322  ret[r].resize(cols());
323  for(int c=0; c<cols(); c++)
324  ret[r][c]=operator()(r,c);
325  }
326  return ret;
327  }
328 
330 
331  template <class AT> template <class Type, class Row, class Col>
333  for(int i=0; i<M; i++)
334  for(int j=i; j<M; j++)
335  ej(i,j) = A.e(i,j);
336  return *this;
337  }
338 
339  template <class AT> template <class Row>
340  inline Matrix<Symmetric,Var,Var,AT>& Matrix<Symmetric,Var,Var,AT>::copy(const Matrix<Symmetric,Row,Row,AT> &A) {
341  for(int i=0; i<M; i++)
342  for(int j=i; j<M; j++)
343  ej(i,j) = A.ej(i,j);
344  return *this;
345  }
346 
347  template <class AT>
348  inline int Matrix<Symmetric,Var,Var,AT>::countElements() const {
349  int k = M;
350  for (int j = 0; j < M; j++) {
351  for (int i = j+1; i < M; i++) {
352  if (fabs(e(i, j)) > 1e-16) {
353  k+=2;
354  }
355  }
356  }
357  return k;
358  }
359 
361 
362 }
363 
364 #endif
This is a matrix class for symmetric matrices.
Definition: var_symmetric_matrix.h:37
int cols() const
Number of columns.
Definition: var_symmetric_matrix.h:245
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:271
Shape class for symmetric matrices.
Definition: types.h:148
int size() const
Size.
Definition: var_symmetric_matrix.h:233
Definition: types.h:85
Definition: types.h:97
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_symmetric_matrix.h:260
int cols() const
Number of columns.
Matrix(const Matrix< Symmetric, Var, Var, AT > &A)
Copy Constructor.
Definition: var_symmetric_matrix.h:74
AT * operator()()
Pointer operator.
Definition: var_symmetric_matrix.h:221
Matrix< Symmetric, Var, Var, AT > & operator=(const Matrix< Symmetric, Var, Var, AT > &A)
Assignment operator.
Definition: var_symmetric_matrix.h:122
AT & operator()(int i, int j)
Element operator.
Definition: var_symmetric_matrix.h:170
Matrix< Symmetric, Var, Var, AT > & init(const AT &val=AT())
Initialization.
Definition: var_symmetric_matrix.h:297
Matrix< Symmetric, Var, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_symmetric_matrix.h:134
const AT * operator()() const
Pointer operator.
Definition: var_symmetric_matrix.h:227
Namespace fmatvec.
Definition: _memory.cc:28
int ldim() const
Leading dimension.
Definition: var_symmetric_matrix.h:251
~Matrix()
Destructor.
Definition: var_symmetric_matrix.h:101
Matrix(const Matrix< Symmetric, Row, Row, AT > &A)
Copy Constructor.
Definition: var_symmetric_matrix.h:84
Matrix()
Standard constructor.
Definition: var_symmetric_matrix.h:59
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_symmetric_matrix.h:94
Definition: types.h:84
void resize(int n, int m)
Definition: var_symmetric_matrix.h:155
int rows() const
Number of rows.
Definition: var_symmetric_matrix.h:239
int rows() const
Number of rows.
const AT & operator()(int i, int j) const
Element operator.
Definition: var_symmetric_matrix.h:183
Definition: types.h:86