fmatvec  2.0.0
Fast Matrix/Vector Library
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 Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& copy(const Matrix<Type,Row,Col,AT> &A);
46  template <class Row> inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& copy(const Matrix<Symmetric,Row,Row,AT> &A);
47 
49 
50  public:
51  typedef AT value_type;
52 
53  explicit Matrix(Noinit ini) { }
54  explicit Matrix(Init ini=INIT, const AT &a=AT()) { init(a); }
55  explicit Matrix(Eye ini, const AT &a=1) { init(ini,a); }
56  explicit Matrix(int m, int n, Noinit ini) { FMATVEC_ASSERT(m==M && n==M, AT); }
57  explicit Matrix(int m, int n, Init ini, const AT &a=AT()) { FMATVEC_ASSERT(m==M && n==M, AT); init(a); }
58  explicit Matrix(int m, int n, Eye ini, const AT &a=1) { FMATVEC_ASSERT(m==M && n==M, AT); init(ini,a); }
59 
65  Matrix(const Matrix<Symmetric,Fixed<M>,Fixed<M>,AT> &A) = default;
66 
72  template<class Row>
74  FMATVEC_ASSERT(A.size() == M, AT);
75  copy(A);
76  }
77 
83  template<class Type, class Row, class Col>
84  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) {
85  FMATVEC_ASSERT(A.rows() == M, AT);
86  FMATVEC_ASSERT(A.cols() == M, AT);
87  copy(A);
88  }
89 
96  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& operator=(const Matrix<Symmetric,Fixed<M>,Fixed<M>,AT> &A) = default;
97 
104  template<class Type, class Row, class Col>
106  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
107  FMATVEC_ASSERT(M == A.rows(), AT);
108  return copy(A);
109  }
110 
117  template<class Type, class Row, class Col>
118  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) { return operator=(A); }
119 
122  void resize(int n, int m) {
123  if(n!=M || m!=M)
124  throw std::runtime_error("A fixed symmetric matrix cannot be resized.");
125  }
126 
136  AT& operator()(int i, int j) {
137  FMATVEC_ASSERT(i>=0, AT);
138  FMATVEC_ASSERT(j>=0, AT);
139  FMATVEC_ASSERT(i<M, AT);
140  FMATVEC_ASSERT(j<M, AT);
141 
142  return e(i,j);
143  }
144 
149  const AT& operator()(int i, int j) const {
150  FMATVEC_ASSERT(i>=0, AT);
151  FMATVEC_ASSERT(j>=0, AT);
152  FMATVEC_ASSERT(i<M, AT);
153  FMATVEC_ASSERT(j<M, AT);
154 
155  return e(i,j);
156  }
157 
158  AT& ei(int i, int j) {
159  return ele[i][j];
160  }
161 
162  const AT& ei(int i, int j) const {
163  return ele[i][j];
164  }
165 
166  AT& ej(int i, int j) {
167  return ele[j][i];
168  }
169 
170  const AT& ej(int i, int j) const {
171  return ele[j][i];
172  }
173 
174  AT& e(int i, int j) {
175  return j > i ? ej(i,j) : ei(i,j);
176  }
177 
178  const AT& e(int i, int j) const {
179  return j > i ? ej(i,j) : ei(i,j);
180  }
181 
187  AT* operator()() {return ele;}
188 
193  const AT* operator()() const {return ele;}
194 
199  int size() const {return M;}
200 
205  int rows() const {return M;}
206 
211  int cols() const {return M;}
212 
217  int ldim() const {return M;}
218 
226  const CBLAS_ORDER blasOrder() const {
227  return CblasRowMajor;
228  }
229 
237  const CBLAS_UPLO blasUplo() const {
238  return CblasLower;
239  }
240 
248  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(const AT &val=AT());
249  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(Init, const AT &a=AT()) { return init(a); }
250  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(Eye, const AT &val=1);
251  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(Noinit, const AT &a=AT()) { return *this; }
252 
257  explicit inline operator std::vector<std::vector<AT>>() const;
258 
264  explicit inline Matrix(const std::vector<std::vector<AT>> &m);
265 
266 // /*! \brief Cast to AT.
267 // *
268 // * \return The AT representation of the matrix
269 // * */
270 // explicit operator AT() const {
271 // FMATVEC_ASSERT(M==1, AT);
272 // return ele[0][0];
273 // }
274 //
275 // /*! \brief AT Constructor.
276 // * Constructs and initializes a matrix with a AT object.
277 // * \param x The AT the matrix will be initialized with.
278 // * */
279 // explicit Matrix(const AT &x) {
280 // FMATVEC_ASSERT(M==1, AT);
281 // ele[0][0] = x;
282 // }
283  };
284 
285  template <int M, class AT>
287 
288  for(int i=0; i<M; i++)
289  for(int j=i; j<M; j++)
290  ej(i,j) = val;
291 
292  return *this;
293  }
294 
295  template <int M, class AT>
296  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::init(Eye, const AT &val) {
297 
298  for(int i=0; i<size(); i++) {
299  ej(i,i) = val;
300  for(int j=i+1; j<size(); j++) {
301  ej(i,j) = 0;
302  }
303  }
304  return *this;
305  }
306 
307  template <int M, class AT>
308  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::operator std::vector<std::vector<AT>>() const {
309  std::vector<std::vector<AT>> ret(rows());
310  for(int r=0; r<rows(); r++) {
311  ret[r].resize(cols());
312  for(int c=0; c<cols(); c++)
313  ret[r][c]=operator()(r,c);
314  }
315  return ret;
316  }
317 
318  template <int M, class AT>
319  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::Matrix(const std::vector<std::vector<AT>> &m) {
320  if(static_cast<int>(m.size())!=rows())
321  throw std::runtime_error("The number of rows does not match.");
322  for(int r=0; r<rows(); r++) {
323  if(static_cast<int>(m[r].size())!=cols())
324  throw std::runtime_error("The rows of the input have different length.");
325  for(int c=0; c<cols(); c++) {
326  using std::abs;
327  e(r,c)=m[r][c];
328  if(r>c && abs(m[r][c]-m[c][r])>abs(m[r][c])*1e-13+1e-13)
329  throw std::runtime_error("The input is not symmetric.");
330  }
331  }
332  }
333 
335 
336  template <int M, class AT> template <class Type, class Row, class Col>
338  for(int i=0; i<M; i++)
339  for(int j=i; j<M; j++)
340  ej(i,j) = A.e(i,j);
341  return *this;
342  }
343 
344  template <int M, class AT> template <class Row>
345  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::copy(const Matrix<Symmetric,Row,Row,AT> &A) {
346  for(int i=0; i<M; i++)
347  for(int j=i; j<M; j++)
348  ej(i,j) = A.ej(i,j);
349  return *this;
350  }
351 
353 
354 }
355 
356 #endif
Definition: fmatvec.h:38
int size() const
Size.
Definition: fixed_symmetric_matrix.h:199
void resize(int n, int m)
Definition: fixed_symmetric_matrix.h:122
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Shape class for symmetric matrices.
Definition: types.h:148
Definition: types.h:85
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_symmetric_matrix.h:149
const CBLAS_UPLO blasUplo() const
Symmetry convention.
Definition: fixed_symmetric_matrix.h:237
Matrix(const Matrix< Symmetric, Row, Row, AT > &A)
Copy Constructor.
Definition: fixed_symmetric_matrix.h:73
int cols() const
Number of columns.
Definition: fixed_symmetric_matrix.h:211
int cols() const
Number of columns.
Namespace fmatvec.
Definition: _memory.cc:28
AT * operator()()
Pointer operator.
Definition: fixed_symmetric_matrix.h:187
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_symmetric_matrix.h:84
Definition: types.h:84
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_symmetric_matrix.h:226
const AT * operator()() const
Pointer operator.
Definition: fixed_symmetric_matrix.h:193
int ldim() const
Leading dimension.
Definition: fixed_symmetric_matrix.h:217
Matrix< Symmetric, Fixed< M >, Fixed< M >, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: fixed_symmetric_matrix.h:105
int rows() const
Number of rows.
AT & operator()(int i, int j)
Element operator.
Definition: fixed_symmetric_matrix.h:136
Definition: types.h:86
int rows() const
Number of rows.
Definition: fixed_symmetric_matrix.h:205