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