All Classes Namespaces Functions 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 
58  Matrix() : M(0), ele(0) {
59  }
60 
61 // template<class Ini=All<AT> >
62 // Matrix(int m, Ini ini=All<AT>()) : M(m), ele(new AT[M*M]) {
63 // init(ini);
64 // }
65 // template<class Ini=All<AT> >
66 // Matrix(int m, int n, Ini ini=All<AT>()) : M(m), ele(new AT[M*M]) {
67 // init(ini);
68 // }
69 
70  Matrix(int m, Noinit) : M(m), ele(new AT[M*M]) { }
71  Matrix(int m, Init ini=INIT, const AT &a=0) : M(m), ele(new AT[M*M]) { init(a); }
72  Matrix(int m, Eye ini, const AT &a=1) : M(m), ele(new AT[M*M]) { init(ini,a); }
73  Matrix(int m, int n, Noinit) : M(m), ele(new AT[M*M]) { }
74  Matrix(int m, int n, Init ini=INIT, const AT &a=0) : M(m), ele(new AT[M*M]) { init(a); }
75  Matrix(int m, int n, Eye ini, const AT &a=1) : M(m), ele(new AT[M*M]) { init(ini,a); }
76 
84  Matrix(const Matrix<Symmetric,Var,Var,AT> &A) : M(A.M), ele(new AT[M*M]) {
85  deepCopy(A);
86  }
87 
88  template<class Row>
89  Matrix(const Matrix<Symmetric,Row,Row,AT> &A) : M(A.size()), ele(new AT[M*M]) {
90  deepCopy(A);
91  }
92 
93  template<int M>
94  explicit Matrix(const Matrix<General,Fixed<M>,Fixed<M>,AT>& A) : M(A.M), ele(new AT[M*M]) {
95  deepCopy(A);
96  }
97 
98  template<class Type, class Row, class Col>
99  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) : M(A.rows()), ele(new AT[M*M]) {
100 
101 #ifndef FMATVEC_NO_SIZE_CHECK
102  assert(A.rows() == M);
103  assert(A.cols() == M);
104 #endif
105 
106  deepCopy(A);
107  }
108 
112  delete[] ele;
113  }
114 
115  Matrix<Symmetric,Var,Var,AT>& resize() {
116  delete[] ele;
117  M = 0;
118  ele = 0;
119  return *this;
120  }
121 
122  Matrix<Symmetric,Var,Var,AT>& resize(int m, Noinit) {
123  delete[] ele;
124  M = m;
125  ele = new AT[M*M];
126  return *this;
127  }
128 
129  Matrix<Symmetric,Var,Var,AT>& resize(int m, Init ini=INIT, const AT &a=0) { return resize(m,Noinit()).init(a); }
130 
131  Matrix<Symmetric,Var,Var,AT>& resize(int m, Eye ini, const AT &a=1) { return resize(m,Noinit()).init(ini,a); }
132 
139  inline Matrix<Symmetric,Var,Var,AT>& operator=(const Matrix<Symmetric,Var,Var,AT> &A);
140 
141  template<class Type, class Row, class Col>
142  inline Matrix<Symmetric,Var,Var,AT>& operator=(const Matrix<Type,Row,Col,AT> &A);
143 
144  template<class Type, class Row, class Col>
145  inline Matrix<Symmetric,Var,Var,AT>& operator<<(const Matrix<Type,Row,Col,AT> &A);
146 
158  AT& operator()(int i, int j) {
159 #ifndef FMATVEC_NO_BOUNDS_CHECK
160  assert(i>=0);
161  assert(j>=0);
162  assert(i<M);
163  assert(j<M);
164 #endif
165 
166  return e(i,j);
167  };
168 
173  const AT& operator()(int i, int j) const {
174 #ifndef FMATVEC_NO_BOUNDS_CHECK
175  assert(i>=0);
176  assert(j>=0);
177  assert(i<M);
178  assert(j<M);
179 #endif
180 
181  return e(i,j);
182  };
183 
184  AT& ei(int i, int j) {
185  return ele[i+j*M];
186  };
187 
188  const AT& ei(int i, int j) const {
189  return ele[i+j*M];
190  };
191 
192  AT& ej(int i, int j) {
193  return ele[i*M+j];
194  };
195 
196  const AT& ej(int i, int j) const {
197  return ele[i*M+j];
198  };
199 
200  AT& e(int i, int j) {
201  return j > i ? ej(i,j) : ei(i,j);
202  };
203 
204  const AT& e(int i, int j) const {
205  return j > i ? ej(i,j) : ei(i,j);
206  };
207 
213  AT* operator()() {return ele;};
214 
219  const AT* operator()() const {return ele;};
220 
225  int size() const {return M;};
226 
231  int rows() const {return M;};
232 
237  int cols() const {return M;};
238 
243  int ldim() const {return M;};
244 
252  const CBLAS_ORDER blasOrder() const {
253  return CblasColMajor;
254  };
255 
263  const CBLAS_UPLO blasUplo() const {
264  return CblasLower;
265  };
266 
274  inline Matrix<Symmetric,Var,Var,AT>& init(const AT &a=0);
275  inline Matrix<Symmetric,Var,Var,AT>& init(Init, const AT &a=0) { return init(a); }
276  inline Matrix<Symmetric,Var,Var,AT>& init(Eye eye, const AT &a=1);
277  inline Matrix<Symmetric,Var,Var,AT>& init(Noinit, const AT &a=0) { return *this; }
278 
283  inline operator std::vector<std::vector<AT> >();
284  };
285 
286  template <class AT>
288 
289  if(!ele) {
290  delete[] ele;
291  M = A.size();
292  ele = new AT[M*M];
293  } else {
294 #ifndef FMATVEC_NO_SIZE_CHECK
295  assert(M == A.size());
296 #endif
297  }
298 
299  deepCopy(A);
300 
301  return *this;
302  }
303 
304  template <class AT> template <class Type, class Row, class Col>
306 
307  if(!ele) {
308  delete[] ele;
309  M = A.rows();
310  ele = new AT[M*M];
311  } else {
312 #ifndef FMATVEC_NO_SIZE_CHECK
313  assert(A.rows() == A.cols());
314  assert(M == A.rows());
315 #endif
316  }
317 
318  deepCopy(A);
319 
320  return *this;
321  }
322 
323  template <class AT> template <class Type, class Row, class Col>
324  inline Matrix<Symmetric,Var,Var,AT>& Matrix<Symmetric,Var,Var,AT>::operator<<(const Matrix<Type,Row,Col,AT> &A) {
325 
326 #ifndef FMATVEC_NO_SIZE_CHECK
327  assert(A.rows() == A.cols());
328 #endif
329 
330  if(M!=A.rows()) {
331  delete[] ele;
332  M = A.rows();
333  ele = new AT[M*M];
334  }
335 
336  deepCopy(A);
337 
338  return *this;
339  }
340 
341  template <class AT>
343 
344  for(int i=0; i<M; i++)
345  for(int j=i; j<M; j++)
346  ej(i,j) = val;
347 
348  return *this;
349  }
350 
351  template <class AT>
353 
354  for(int i=0; i<size(); i++) {
355  ej(i,i) = val;
356  for(int j=i+1; j<size(); j++) {
357  ej(i,j) = 0;
358  }
359  }
360  return *this;
361  }
362 
363  template <class AT>
364  inline Matrix<Symmetric,Var,Var,AT>::operator std::vector<std::vector<AT> >() {
365  std::vector<std::vector<AT> > ret(rows());
366  for(int r=0; r<rows(); r++) {
367  ret[r].resize(cols());
368  for(int c=0; c<cols(); c++)
369  ret[r][c]=operator()(r,c);
370  }
371  return ret;
372  }
373 
375 
376  template <class AT> template <class Type, class Row, class Col>
378  for(int i=0; i<M; i++)
379  for(int j=i; j<M; j++)
380  ej(i,j) = A.e(i,j);
381  }
382 
383  template <class AT> template <class Row>
384  inline void Matrix<Symmetric,Var,Var,AT>::deepCopy(const Matrix<Symmetric,Row,Row,AT> &A) {
385  for(int i=0; i<M; i++)
386  for(int j=i; j<M; j++)
387  ej(i,j) = A.ej(i,j);
388  }
389 
391 
392 }
393 
394 #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: matrix.h:56
int rows() const
Number of rows.
Definition: var_symmetric_matrix.h:231
Shape class for symmetric matrices.
Definition: types.h:116
int ldim() const
Leading dimension.
Definition: var_symmetric_matrix.h:243
Definition: matrix.h:39
Definition: types.h:65
const CBLAS_UPLO blasUplo() const
Symmetry convention.
Definition: var_symmetric_matrix.h:263
int rows() const
Number of rows.
Matrix< Symmetric, Var, Var, AT > & init(const AT &a=0)
Initialization.
Definition: var_symmetric_matrix.h:342
int cols() const
Number of columns.
Matrix(const Matrix< Symmetric, Var, Var, AT > &A)
Copy Constructor.
Definition: var_symmetric_matrix.h:84
AT * operator()()
Pointer operator.
Definition: var_symmetric_matrix.h:213
AT & operator()(int i, int j)
Element operator.
Definition: var_symmetric_matrix.h:158
const AT * operator()() const
Pointer operator.
Definition: var_symmetric_matrix.h:219
~Matrix()
Destructor.
Definition: var_symmetric_matrix.h:111
Matrix()
Standard constructor.
Definition: var_symmetric_matrix.h:58
Definition: matrix.h:38
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: var_symmetric_matrix.h:252
int size() const
Size.
Definition: var_symmetric_matrix.h:225
const AT & operator()(int i, int j) const
Element operator.
Definition: var_symmetric_matrix.h:173
int cols() const
Number of columns.
Definition: var_symmetric_matrix.h:237
Definition: matrix.h:40

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML