All Classes Namespaces Functions Variables Typedefs Enumerations Pages
fixed_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 fixed_symmetric_matrix_h
23 #define fixed_symmetric_matrix_h
24 
25 #include "types.h"
26 
27 namespace fmatvec {
28 
37  template <int M, class AT> class Matrix<Symmetric,Fixed<M>,Fixed<M>,AT> {
38 
39  protected:
40 
42 
43  AT ele[M][M];
44 
45  template <class Type, class Row, class Col> inline void deepCopy(const Matrix<Type,Row,Col,AT> &A);
46  template <class Row> inline void deepCopy(const Matrix<Symmetric,Row,Row,AT> &A);
47 
49 
50  public:
51  typedef AT AtomicType;
52 
53 // template<class Ini=All<AT> >
54 // Matrix(Ini ini=All<AT>()) {
55 // init(ini);
56 // }
57 // template<class Ini=All<AT> >
58 // Matrix(int m_, int n_, Ini ini=All<AT>()) {
59 // init(ini);
60 // }
61 
62  Matrix(Noinit ini) { }
63  Matrix(Init ini=INIT, const AT &a=0) { init(a); }
64  Matrix(Eye ini, const AT &a=1) { init(ini,a); }
65  Matrix(int m, int n, Noinit ini) { }
66  Matrix(int m, int n, Init ini, const AT &a=0) { init(a); }
67  Matrix(int m, int n, Eye ini, const AT &a=1) { init(ini,a); }
68 
69  explicit Matrix(const Matrix<General,Fixed<M>,Fixed<M>,AT>& A) {
70  deepCopy(A);
71  }
72 
73  template<class Row>
75 
76 #ifndef FMATVEC_NO_SIZE_CHECK
77  assert(A.size() == M);
78 #endif
79 
80  deepCopy(A);
81  }
82 
83  template<class Type, class Row, class Col>
84  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) {
85 
86 #ifndef FMATVEC_NO_SIZE_CHECK
87  assert(A.rows() == M);
88  assert(A.cols() == M);
89 #endif
90 
91  deepCopy(A);
92  }
93 
96  void resize(int n, int m) {
97  if(n!=M || m!=M)
98  throw std::runtime_error("A fixed symmetric matrix cannot be resized.");
99  }
100 
101  // A fixed matrix is stored in c-storage order -> transposed is always true
102  bool transposed() {
103  return true;
104  }
105 
117  AT& operator()(int i, int j) {
118 #ifndef FMATVEC_NO_BOUNDS_CHECK
119  assert(i>=0);
120  assert(j>=0);
121  assert(i<M);
122  assert(j<M);
123 #endif
124 
125  return e(i,j);
126  };
127 
132  const AT& operator()(int i, int j) const {
133 #ifndef FMATVEC_NO_BOUNDS_CHECK
134  assert(i>=0);
135  assert(j>=0);
136  assert(i<M);
137  assert(j<M);
138 #endif
139 
140  return e(i,j);
141  };
142 
143  AT& ei(int i, int j) {
144  return ele[i][j];
145  };
146 
147  const AT& ei(int i, int j) const {
148  return ele[i][j];
149  };
150 
151  AT& ej(int i, int j) {
152  return ele[j][i];
153  };
154 
155  const AT& ej(int i, int j) const {
156  return ele[j][i];
157  };
158 
159  AT& e(int i, int j) {
160  return j > i ? ej(i,j) : ei(i,j);
161  };
162 
163  const AT& e(int i, int j) const {
164  return j > i ? ej(i,j) : ei(i,j);
165  };
166 
172  AT* operator()() {return ele;};
173 
178  const AT* operator()() const {return ele;};
179 
184  int size() const {return M;};
185 
190  int rows() const {return M;};
191 
196  int cols() const {return M;};
197 
202  int ldim() const {return M;};
203 
211  const CBLAS_ORDER blasOrder() const {
212  return CblasColMajor;
213  };
214 
222  const CBLAS_UPLO blasUplo() const {
223  return CblasLower;
224  };
225 
233  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(const AT &a=0);
234  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(Init, const AT &a=0) { return init(a); }
235  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(Eye, const AT &a=1);
236  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(Noinit, const AT &a=0) { return *this; }
237 
242  inline operator std::vector<std::vector<AT> >();
243  };
244 
245  template <int M, class AT>
247 
248  for(int i=0; i<M; i++)
249  for(int j=i; j<M; j++)
250  ej(i,j) = val;
251 
252  return *this;
253  }
254 
255  template <int M, class AT>
256  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::init(Eye, const AT &val) {
257 
258  for(int i=0; i<size(); i++) {
259  ej(i,i) = val;
260  for(int j=i+1; j<size(); j++) {
261  ej(i,j) = 0;
262  }
263  }
264  return *this;
265  }
266 
267  template <int M, class AT>
268  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::operator std::vector<std::vector<AT> >() {
269  std::vector<std::vector<AT> > ret(rows());
270  for(int r=0; r<rows(); r++) {
271  ret[r].resize(cols());
272  for(int c=0; c<cols(); c++)
273  ret[r][c]=operator()(r,c);
274  }
275  return ret;
276  }
277 
279 
280  template <int M, class AT> template <class Type, class Row, class Col>
281  inline void Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::deepCopy(const Matrix<Type,Row,Col,AT> &A) {
282  for(int i=0; i<M; i++)
283  for(int j=i; j<M; j++)
284  ej(i,j) = A.e(i,j);
285  }
286 
287  template <int M, class AT> template <class Row>
288  inline void Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::deepCopy(const Matrix<Symmetric,Row,Row,AT> &A) {
289  for(int i=0; i<M; i++)
290  for(int j=i; j<M; j++)
291  ej(i,j) = A.ej(i,j);
292  }
293 
295 
296 }
297 
298 #endif
Definition: fmatvec.h:38
void resize(int n, int m)
Definition: fixed_symmetric_matrix.h:96
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Shape class for symmetric matrices.
Definition: types.h:116
Definition: matrix.h:39
int rows() const
Number of rows.
Definition: fixed_symmetric_matrix.h:190
const CBLAS_UPLO blasUplo() const
Symmetry convention.
Definition: fixed_symmetric_matrix.h:222
int rows() const
Number of rows.
const AT * operator()() const
Pointer operator.
Definition: fixed_symmetric_matrix.h:178
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_symmetric_matrix.h:132
int cols() const
Number of columns.
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_symmetric_matrix.h:211
int ldim() const
Leading dimension.
Definition: fixed_symmetric_matrix.h:202
AT * operator()()
Pointer operator.
Definition: fixed_symmetric_matrix.h:172
Definition: matrix.h:38
int cols() const
Number of columns.
Definition: fixed_symmetric_matrix.h:196
Basic shape class for matrices.
Definition: types.h:100
int size() const
Size.
Definition: fixed_symmetric_matrix.h:184
AT & operator()(int i, int j)
Element operator.
Definition: fixed_symmetric_matrix.h:117
Definition: matrix.h:40

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML