All Classes Namespaces Functions Variables Typedefs Enumerations Pages
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;
44 
45  AT *ele;
46 
47  template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A);
48  template <class Row> inline void deepCopy(const Matrix<Symmetric,Row,Row,AT> &A);
49 
51 
52  public:
53  typedef AT AtomicType;
54 
59  Matrix() : M(0), ele(0) {
60  }
61 
62 // template<class Ini=All<AT> >
63 // Matrix(int m, Ini ini=All<AT>()) : M(m), ele(new AT[M*M]) {
64 // init(ini);
65 // }
66 // template<class Ini=All<AT> >
67 // Matrix(int m, int n, Ini ini=All<AT>()) : M(m), ele(new AT[M*M]) {
68 // init(ini);
69 // }
70 
71  Matrix(int m, Noinit) : M(m), ele(new AT[M*M]) { }
72  Matrix(int m, Init ini=INIT, const AT &a=0) : M(m), ele(new AT[M*M]) { init(a); }
73  Matrix(int m, Eye ini, const AT &a=1) : M(m), ele(new AT[M*M]) { init(ini,a); }
74  Matrix(int m, int n, Noinit) : M(m), ele(new AT[M*M]) { }
75  Matrix(int m, int n, Init ini=INIT, const AT &a=0) : M(m), ele(new AT[M*M]) { init(a); }
76  Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), ele(new AT[M*M]) { init(ini,a); }
77 
85  Matrix(const Matrix<Symmetric,Var,Var,AT> &A) : M(A.M), ele(new AT[M*M]) {
86  deepCopy(A);
87  }
88 
89  template<class Row>
90  Matrix(const Matrix<Symmetric,Row,Row,AT> &A) : M(A.size()), ele(new AT[M*M]) {
91  deepCopy(A);
92  }
93 
94  template<int M>
95  explicit Matrix(const Matrix<General,Fixed<M>,Fixed<M>,AT>& A) : M(A.M), ele(new AT[M*M]) {
96  deepCopy(A);
97  }
98 
99  template<class Type, class Row, class Col>
100  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*M]) {
101 
102 #ifndef FMATVEC_NO_SIZE_CHECK
103  assert(A.rows() == M);
104  assert(A.cols() == M);
105 #endif
106 
107  deepCopy(A);
108  }
109 
113  delete[] ele;
114  }
115 
116  Matrix<Symmetric,Var,Var,AT>& resize() {
117  delete[] ele;
118  M = 0;
119  ele = 0;
120  return *this;
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=0) { 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 
140  inline Matrix<Symmetric,Var,Var,AT>& operator=(const Matrix<Symmetric,Var,Var,AT> &A);
141 
142  template<class Type, class Row, class Col>
143  inline Matrix<Symmetric,Var,Var,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
144 
145  template<class Type, class Row, class Col>
146  inline Matrix<Symmetric,Var,Var,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
147 
150  void resize(int n, int m) {
151  if(n!=m)
152  throw std::runtime_error("A symmetric matrix cannot have different dimensions for rows and columns.");
153  resize(n);
154  }
155 
157  bool transposed() {
158  return false;
159  }
160 
172  AT& operator()(int i, int j) {
173 #ifndef FMATVEC_NO_BOUNDS_CHECK
174  assert(i>=0);
175  assert(j>=0);
176  assert(i<M);
177  assert(j<M);
178 #endif
179 
180  return e(i,j);
181  };
182 
187  const AT& operator()(int i, int j) const {
188 #ifndef FMATVEC_NO_BOUNDS_CHECK
189  assert(i>=0);
190  assert(j>=0);
191  assert(i<M);
192  assert(j<M);
193 #endif
194 
195  return e(i,j);
196  };
197 
198  AT& ei(int i, int j) {
199  return ele[i+j*M];
200  };
201 
202  const AT& ei(int i, int j) const {
203  return ele[i+j*M];
204  };
205 
206  AT& ej(int i, int j) {
207  return ele[i*M+j];
208  };
209 
210  const AT& ej(int i, int j) const {
211  return ele[i*M+j];
212  };
213 
214  AT& e(int i, int j) {
215  return j > i ? ej(i,j) : ei(i,j);
216  };
217 
218  const AT& e(int i, int j) const {
219  return j > i ? ej(i,j) : ei(i,j);
220  };
221 
227  AT* operator()() {return ele;};
228 
233  const AT* operator()() const {return ele;};
234 
239  int size() const {return M;};
240 
245  int rows() const {return M;};
246 
251  int cols() const {return M;};
252 
257  int ldim() const {return M;};
258 
266  const CBLAS_ORDER blasOrder() const {
267  return CblasColMajor;
268  };
269 
277  const CBLAS_UPLO blasUplo() const {
278  return CblasLower;
279  };
280 
288  inline Matrix<Symmetric,Var,Var,AT>& init(const AT &a=0);
289  inline Matrix<Symmetric,Var,Var,AT>& init(Init, const AT &a=0) { return init(a); }
290  inline Matrix<Symmetric,Var,Var,AT>& init(Eye eye, const AT &a=1);
291  inline Matrix<Symmetric,Var,Var,AT>& init(Noinit, const AT &a=0) { return *this; }
292 
297  inline operator std::vector<std::vector<AT> >();
298  };
299 
300  template <class AT>
302 
303  if(!ele) {
304  delete[] ele;
305  M = A.size();
306  ele = new AT[M*M];
307  } else {
308 #ifndef FMATVEC_NO_SIZE_CHECK
309  assert(M == A.size());
310 #endif
311  }
312 
313  deepCopy(A);
314 
315  return *this;
316  }
317 
318  template <class AT> template <class Type, class Row, class Col>
320 
321  if(!ele) {
322  delete[] ele;
323  M = A.rows();
324  ele = new AT[M*M];
325  } else {
326 #ifndef FMATVEC_NO_SIZE_CHECK
327  assert(A.rows() == A.cols());
328  assert(M == A.rows());
329 #endif
330  }
331 
332  deepCopy(A);
333 
334  return *this;
335  }
336 
337  template <class AT> template <class Type, class Row, class Col>
338  inline Matrix<Symmetric,Var,Var,AT>& Matrix<Symmetric,Var,Var,AT>::operator<<(const Matrix<Type,Row,Col,AT> &A) {
339 
340 #ifndef FMATVEC_NO_SIZE_CHECK
341  assert(A.rows() == A.cols());
342 #endif
343 
344  if(M!=A.rows()) {
345  delete[] ele;
346  M = A.rows();
347  ele = new AT[M*M];
348  }
349 
350  deepCopy(A);
351 
352  return *this;
353  }
354 
355  template <class AT>
357 
358  for(int i=0; i<M; i++)
359  for(int j=i; j<M; j++)
360  ej(i,j) = val;
361 
362  return *this;
363  }
364 
365  template <class AT>
367 
368  for(int i=0; i<size(); i++) {
369  ej(i,i) = val;
370  for(int j=i+1; j<size(); j++) {
371  ej(i,j) = 0;
372  }
373  }
374  return *this;
375  }
376 
377  template <class AT>
378  inline Matrix<Symmetric,Var,Var,AT>::operator std::vector<std::vector<AT> >() {
379  std::vector<std::vector<AT> > ret(rows());
380  for(int r=0; r<rows(); r++) {
381  ret[r].resize(cols());
382  for(int c=0; c<cols(); c++)
383  ret[r][c]=operator()(r,c);
384  }
385  return ret;
386  }
387 
389 
390  template <class AT> template <class Type, class Row, class Col>
392  for(int i=0; i<M; i++)
393  for(int j=i; j<M; j++)
394  ej(i,j) = A.e(i,j);
395  }
396 
397  template <class AT> template <class Row>
398  inline void Matrix<Symmetric,Var,Var,AT>::deepCopy(const Matrix<Symmetric,Row,Row,AT> &A) {
399  for(int i=0; i<M; i++)
400  for(int j=i; j<M; j++)
401  ej(i,j) = A.ej(i,j);
402  }
403 
405 
406 }
407 
408 #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
int rows() const
Number of rows.
Definition: var_symmetric_matrix.h:245
Shape class for symmetric matrices.
Definition: types.h:116
int ldim() const
Leading dimension.
Definition: var_symmetric_matrix.h:257
Definition: matrix.h:39
Definition: types.h:65
const CBLAS_UPLO blasUplo() const
Symmetry convention.
Definition: var_symmetric_matrix.h:277
int rows() const
Number of rows.
Matrix< Symmetric, Var, Var, AT > & init(const AT &a=0)
Initialization.
Definition: var_symmetric_matrix.h:356
int cols() const
Number of columns.
Matrix(const Matrix< Symmetric, Var, Var, AT > &A)
Copy Constructor.
Definition: var_symmetric_matrix.h:85
AT * operator()()
Pointer operator.
Definition: var_symmetric_matrix.h:227
AT & operator()(int i, int j)
Element operator.
Definition: var_symmetric_matrix.h:172
const AT * operator()() const
Pointer operator.
Definition: var_symmetric_matrix.h:233
bool transposed()
The storage format of a var symmetric matrix is fortran-storge order -&gt; return always false...
Definition: var_symmetric_matrix.h:157
~Matrix()
Destructor.
Definition: var_symmetric_matrix.h:112
Matrix()
Standard constructor.
Definition: var_symmetric_matrix.h:59
Definition: matrix.h:38
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_symmetric_matrix.h:266
int size() const
Size.
Definition: var_symmetric_matrix.h:239
const AT & operator()(int i, int j) const
Element operator.
Definition: var_symmetric_matrix.h:187
void resize(int n, int m)
Definition: var_symmetric_matrix.h:150
int cols() const
Number of columns.
Definition: var_symmetric_matrix.h:251
Definition: matrix.h:40

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML