fmatvec  0.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  static constexpr bool isVector {false};
52  typedef AT value_type;
53  typedef Symmetric shape_type;
54 
55  explicit Matrix(Noinit ini) { }
56  explicit Matrix(Init ini=INIT, const AT &a=AT()) { init(a); }
57  explicit Matrix(Eye ini, const AT &a=1) { init(ini,a); }
58  explicit Matrix(int m, int n, Noinit ini) { FMATVEC_ASSERT(m==M && n==M, AT); }
59  explicit Matrix(int m, int n, Init ini, const AT &a=AT()) { FMATVEC_ASSERT(m==M && n==M, AT); init(a); }
60  explicit Matrix(int m, int n, Eye ini, const AT &a=1) { FMATVEC_ASSERT(m==M && n==M, AT); init(ini,a); }
61 
67  Matrix(const Matrix<Symmetric,Fixed<M>,Fixed<M>,AT> &A) = default;
68 
74  template<class Row>
76  FMATVEC_ASSERT(A.size() == M, AT);
77  copy(A);
78  }
79 
85  template<class Type, class Row, class Col>
86  explicit Matrix(const Matrix<Type,Row,Col,AT> &A) {
87  FMATVEC_ASSERT(A.rows() == M, AT);
88  FMATVEC_ASSERT(A.cols() == M, AT);
89  copy(A);
90  }
91 
98  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& operator=(const Matrix<Symmetric,Fixed<M>,Fixed<M>,AT> &A) = default;
99 
106  template<class Type, class Row, class Col>
108  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
109  FMATVEC_ASSERT(M == A.rows(), AT);
110  return copy(A);
111  }
112 
119  template<class Type, class Row, class Col>
120  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) { return operator=(A); }
121 
124  void resize(int n, int m) {
125  if(n!=M || m!=M)
126  throw std::runtime_error("A fixed symmetric matrix cannot be resized.");
127  }
128 
138  AT& operator()(int i, int j) {
139  FMATVEC_ASSERT(i>=0, AT);
140  FMATVEC_ASSERT(j>=0, AT);
141  FMATVEC_ASSERT(i<M, AT);
142  FMATVEC_ASSERT(j<M, AT);
143 
144  return e(i,j);
145  }
146 
151  const AT& operator()(int i, int j) const {
152  FMATVEC_ASSERT(i>=0, AT);
153  FMATVEC_ASSERT(j>=0, AT);
154  FMATVEC_ASSERT(i<M, AT);
155  FMATVEC_ASSERT(j<M, AT);
156 
157  return e(i,j);
158  }
159 
160  AT& ei(int i, int j) {
161  return ele[i][j];
162  }
163 
164  const AT& ei(int i, int j) const {
165  return ele[i][j];
166  }
167 
168  AT& ej(int i, int j) {
169  return ele[j][i];
170  }
171 
172  const AT& ej(int i, int j) const {
173  return ele[j][i];
174  }
175 
176  AT& e(int i, int j) {
177  return j > i ? ej(i,j) : ei(i,j);
178  }
179 
180  const AT& e(int i, int j) const {
181  return j > i ? ej(i,j) : ei(i,j);
182  }
183 
189  AT* operator()() {return ele;}
190 
195  const AT* operator()() const {return ele;}
196 
201  constexpr int size() const {return M;}
202 
207  constexpr int rows() const {return M;}
208 
213  constexpr int cols() const {return M;}
214 
219  int ldim() const {return M;}
220 
228  const CBLAS_ORDER blasOrder() const {
229  return CblasRowMajor;
230  }
231 
239  const CBLAS_UPLO blasUplo() const {
240  return CblasLower;
241  }
242 
250  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(const AT &val=AT());
251  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(Init, const AT &a=AT()) { return init(a); }
252  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(Eye, const AT &val=1);
253  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& init(Noinit, const AT &a=AT()) { return *this; }
254 
259  explicit inline operator std::vector<std::vector<AT>>() const;
260 
266  explicit inline Matrix(const std::vector<std::vector<AT>> &m);
267 
268 // /*! \brief Cast to AT.
269 // *
270 // * \return The AT representation of the matrix
271 // * */
272 // explicit operator AT() const {
273 // FMATVEC_ASSERT(M==1, AT);
274 // return ele[0][0];
275 // }
276 //
277 // /*! \brief AT Constructor.
278 // * Constructs and initializes a matrix with a AT object.
279 // * \param x The AT the matrix will be initialized with.
280 // * */
281 // explicit Matrix(const AT &x) {
282 // FMATVEC_ASSERT(M==1, AT);
283 // ele[0][0] = x;
284 // }
285  };
286 
287  template <int M, class AT>
289 
290  for(int i=0; i<M; i++)
291  for(int j=i; j<M; j++)
292  ej(i,j) = val;
293 
294  return *this;
295  }
296 
297  template <int M, class AT>
298  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::init(Eye, const AT &val) {
299 
300  for(int i=0; i<size(); i++) {
301  ej(i,i) = val;
302  for(int j=i+1; j<size(); j++) {
303  ej(i,j) = 0;
304  }
305  }
306  return *this;
307  }
308 
309  template <int M, class AT>
310  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::operator std::vector<std::vector<AT>>() const {
311  std::vector<std::vector<AT>> ret(rows());
312  for(int r=0; r<rows(); r++) {
313  ret[r].resize(cols());
314  for(int c=0; c<cols(); c++)
315  ret[r][c]=operator()(r,c);
316  }
317  return ret;
318  }
319 
320  template <int M, class AT>
321  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::Matrix(const std::vector<std::vector<AT>> &m) {
322  if(static_cast<int>(m.size())!=rows())
323  throw std::runtime_error("The number of rows does not match.");
324  for(int r=0; r<rows(); r++) {
325  if(static_cast<int>(m[r].size())!=cols())
326  throw std::runtime_error("The rows of the input have different length.");
327  for(int c=0; c<cols(); c++) {
328  using std::abs;
329  e(r,c)=m[r][c];
330  if(r>c && abs(m[r][c]-m[c][r])>abs(m[r][c])*1e-13+1e-13)
331  throw std::runtime_error("The input is not symmetric.");
332  }
333  }
334  }
335 
337 
338  template <int M, class AT> template <class Type, class Row, class Col>
340  for(int i=0; i<M; i++)
341  for(int j=i; j<M; j++)
342  ej(i,j) = A.e(i,j);
343  return *this;
344  }
345 
346  template <int M, class AT> template <class Row>
347  inline Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>& Matrix<Symmetric,Fixed<M>,Fixed<M>,AT>::copy(const Matrix<Symmetric,Row,Row,AT> &A) {
348  for(int i=0; i<M; i++)
349  for(int j=i; j<M; j++)
350  ej(i,j) = A.ej(i,j);
351  return *this;
352  }
353 
355 
356 }
357 
358 #endif
Definition: fmatvec.h:38
void resize(int n, int m)
Definition: fixed_symmetric_matrix.h:124
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Shape class for symmetric matrices.
Definition: types.h:152
constexpr int size() const
Size.
Definition: fixed_symmetric_matrix.h:201
Definition: types.h:89
const AT & operator()(int i, int j) const
Element operator.
Definition: fixed_symmetric_matrix.h:151
const CBLAS_UPLO blasUplo() const
Symmetry convention.
Definition: fixed_symmetric_matrix.h:239
Matrix(const Matrix< Symmetric, Row, Row, AT > &A)
Copy Constructor.
Definition: fixed_symmetric_matrix.h:75
int cols() const
Number of columns.
Namespace fmatvec.
Definition: _memory.cc:28
AT * operator()()
Pointer operator.
Definition: fixed_symmetric_matrix.h:189
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: fixed_symmetric_matrix.h:86
constexpr int rows() const
Number of rows.
Definition: fixed_symmetric_matrix.h:207
Definition: types.h:88
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: fixed_symmetric_matrix.h:228
const AT * operator()() const
Pointer operator.
Definition: fixed_symmetric_matrix.h:195
int ldim() const
Leading dimension.
Definition: fixed_symmetric_matrix.h:219
Matrix< Symmetric, Fixed< M >, Fixed< M >, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: fixed_symmetric_matrix.h:107
constexpr int cols() const
Number of columns.
Definition: fixed_symmetric_matrix.h:213
int rows() const
Number of rows.
AT & operator()(int i, int j)
Element operator.
Definition: fixed_symmetric_matrix.h:138
Definition: types.h:90