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 
71  // move
72  Matrix(Matrix<Symmetric,Var,Var,AT> &&src) {
73  M=src.M;
74  src.M=0;
75  ele=src.ele;
76  src.ele=nullptr;
77  }
78  Matrix<Symmetric,Var,Var,AT>& operator=(Matrix<Symmetric,Var,Var,AT> &&src) {
79  FMATVEC_ASSERT(M == src.M, AT);
80  src.M=0;
81  delete[]ele;
82  ele=src.ele;
83  src.ele=nullptr;
84  return *this;
85  }
86 
92  Matrix(const Matrix<Symmetric,Var,Var,AT> &A) : M(A.M), ele(new AT[M*M]) {
93  copy(A);
94  }
95 
101  template<class Row>
102  Matrix(const Matrix<Symmetric,Row,Row,AT> &A) : M(A.size()), ele(new AT[M*M]) {
103  copy(A);
104  }
105 
111  template<class Type, class Row, class Col>
112  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*M]) {
113  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
114  copy(A);
115  }
116 
120  delete[] ele;
121  }
122 
123  Matrix<Symmetric,Var,Var,AT>& resize(int m, Noinit) {
124  delete[] ele;
125  M = m;
126  ele = new AT[M*M];
127  return *this;
128  }
129 
130  Matrix<Symmetric,Var,Var,AT>& resize(int m, Init ini=INIT, const AT &a=AT()) { return resize(m,Noinit()).init(a); }
131 
132  Matrix<Symmetric,Var,Var,AT>& resize(int m, Eye ini, const AT &a=1) { return resize(m,Noinit()).init(ini,a); }
133 
141  FMATVEC_ASSERT(M == A.M, AT);
142  return copy(A);
143  }
144 
151  template<class Type, class Row, class Col>
153  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
154  FMATVEC_ASSERT(M == 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  if(M!=A.rows()) resize(A.rows(),NONINIT);
168  return copy(A);
169  }
170  // move
171  template<class Type, class Row, class Col>
172  inline Matrix<Symmetric,Var,Var,AT>& operator<<=(Matrix<Type,Row,Col,AT> &&src) {
173  FMATVEC_ASSERT(src.rows() == src.cols(), AT);
174  M=src.M;
175  src.M=0;
176  delete[]ele;
177  ele=src.ele;
178  src.ele=nullptr;
179  return *this;
180  }
181 
184  void resize(int n, int m) {
185  if(n!=m)
186  throw std::runtime_error("A symmetric matrix cannot have different dimensions for rows and columns.");
187  resize(n);
188  }
189 
199  AT& operator()(int i, int j) {
200  FMATVEC_ASSERT(i>=0, AT);
201  FMATVEC_ASSERT(j>=0, AT);
202  FMATVEC_ASSERT(i<M, AT);
203  FMATVEC_ASSERT(j<M, AT);
204 
205  return e(i,j);
206  }
207 
212  const AT& operator()(int i, int j) const {
213  FMATVEC_ASSERT(i>=0, AT);
214  FMATVEC_ASSERT(j>=0, AT);
215  FMATVEC_ASSERT(i<M, AT);
216  FMATVEC_ASSERT(j<M, AT);
217 
218  return e(i,j);
219  }
220 
221  AT& ei(int i, int j) {
222  return ele[i*M+j];
223  }
224 
225  const AT& ei(int i, int j) const {
226  return ele[i*M+j];
227  }
228 
229  AT& ej(int i, int j) {
230  return ele[i+j*M];
231  }
232 
233  const AT& ej(int i, int j) const {
234  return ele[i+j*M];
235  }
236 
237  AT& e(int i, int j) {
238  return j > i ? ej(i,j) : ei(i,j);
239  }
240 
241  const AT& e(int i, int j) const {
242  return j > i ? ej(i,j) : ei(i,j);
243  }
244 
250  AT* operator()() {return ele;}
251 
256  const AT* operator()() const {return ele;}
257 
262  constexpr int size() const {return M;}
263 
268  constexpr int rows() const {return M;}
269 
274  constexpr int cols() const {return M;}
275 
280  int ldim() const {return M;}
281 
289  const CBLAS_ORDER blasOrder() const {
290  return CblasRowMajor;
291  }
292 
300  const CBLAS_UPLO blasUplo() const {
301  return CblasLower;
302  }
303 
311  inline Matrix<Symmetric,Var,Var,AT>& init(const AT &val=AT());
312  inline Matrix<Symmetric,Var,Var,AT>& init(Init, const AT &a=AT()) { return init(a); }
313  inline Matrix<Symmetric,Var,Var,AT>& init(Eye eye, const AT &val=1);
314  inline Matrix<Symmetric,Var,Var,AT>& init(Noinit, const AT &a=AT()) { return *this; }
315 
320  explicit inline operator std::vector<std::vector<AT>>() const;
321 
322  inline int countElements() const;
323  };
324 
325  template <class AT>
327 
328  for(int i=0; i<M; i++)
329  for(int j=i; j<M; j++)
330  ej(i,j) = val;
331 
332  return *this;
333  }
334 
335  template <class AT>
337 
338  for(int i=0; i<size(); i++) {
339  ej(i,i) = val;
340  for(int j=i+1; j<size(); j++) {
341  ej(i,j) = 0;
342  }
343  }
344  return *this;
345  }
346 
347  template <class AT>
348  inline Matrix<Symmetric,Var,Var,AT>::operator std::vector<std::vector<AT>>() const {
349  std::vector<std::vector<AT>> ret(rows());
350  for(int r=0; r<rows(); r++) {
351  ret[r].resize(cols());
352  for(int c=0; c<cols(); c++)
353  ret[r][c]=operator()(r,c);
354  }
355  return ret;
356  }
357 
359 
360  template <class AT> template <class Type, class Row, class Col>
362  for(int i=0; i<M; i++)
363  for(int j=i; j<M; j++)
364  ej(i,j) = A.e(i,j);
365  return *this;
366  }
367 
368  template <class AT> template <class Row>
369  inline Matrix<Symmetric,Var,Var,AT>& Matrix<Symmetric,Var,Var,AT>::copy(const Matrix<Symmetric,Row,Row,AT> &A) {
370  for(int i=0; i<M; i++)
371  for(int j=i; j<M; j++)
372  ej(i,j) = A.ej(i,j);
373  return *this;
374  }
375 
376  template <class AT>
377  inline int Matrix<Symmetric,Var,Var,AT>::countElements() const {
378  int k = M;
379  for (int j = 0; j < M; j++) {
380  for (int i = j+1; i < M; i++) {
381  if (fabs(e(i, j)) > 1e-16) {
382  k+=2;
383  }
384  }
385  }
386  return k;
387  }
388 
390 
391 }
392 
393 #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:300
Shape class for symmetric matrices.
Definition: types.h:152
Definition: types.h:89
Definition: types.h:101
constexpr int rows() const
Number of rows.
Definition: var_symmetric_matrix.h:268
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_symmetric_matrix.h:289
int cols() const
Number of columns.
Matrix(const Matrix< Symmetric, Var, Var, AT > &A)
Copy Constructor.
Definition: var_symmetric_matrix.h:92
AT * operator()()
Pointer operator.
Definition: var_symmetric_matrix.h:250
Matrix< Symmetric, Var, Var, AT > & operator=(const Matrix< Symmetric, Var, Var, AT > &A)
Assignment operator.
Definition: var_symmetric_matrix.h:140
AT & operator()(int i, int j)
Element operator.
Definition: var_symmetric_matrix.h:199
Matrix< Symmetric, Var, Var, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: var_symmetric_matrix.h:152
const AT * operator()() const
Pointer operator.
Definition: var_symmetric_matrix.h:256
Namespace fmatvec.
Definition: _memory.cc:28
int ldim() const
Leading dimension.
Definition: var_symmetric_matrix.h:280
~Matrix()
Destructor.
Definition: var_symmetric_matrix.h:119
Matrix(const Matrix< Symmetric, Row, Row, AT > &A)
Copy Constructor.
Definition: var_symmetric_matrix.h:102
constexpr int size() const
Size.
Definition: var_symmetric_matrix.h:262
Matrix()
Standard constructor.
Definition: var_symmetric_matrix.h:61
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: var_symmetric_matrix.h:112
constexpr int cols() const
Number of columns.
Definition: var_symmetric_matrix.h:274
Definition: types.h:88
void resize(int n, int m)
Definition: var_symmetric_matrix.h:184
int rows() const
Number of rows.
const AT & operator()(int i, int j) const
Element operator.
Definition: var_symmetric_matrix.h:212
Definition: types.h:90