fmatvec  2.0.0
Fast Matrix/Vector Library
diagonal_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 diagonal_matrix_h
23 #define diagonal_matrix_h
24 
25 #include "matrix.h"
26 #include "types.h"
27 #include "_memory.h"
28 
29 namespace fmatvec {
30 
39  template <class AT> class Matrix<Diagonal,Ref,Ref,AT> {
40 
41  protected:
42 
44 
45  Memory<AT> memory;
46  AT *ele;
47  int n{0};
48 
50 
51  const AT* elePtr(int i) const {
52  return ele+i;
53  }
54 
55  AT* elePtr(int i) {
56  return ele+i;
57  }
58 
60 
61  public:
62 
63  typedef AT value_type;
64 
69  explicit Matrix() : memory(), ele(0) { }
70 
71  // template<class Ini=All<AT>>
72  // Matrix(int n_, Ini ini=All<AT>()) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini); }
73  // template<class Ini=All<AT>>
74  // Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini); }
75 
76  explicit Matrix(int n_, Noinit) : memory(n_), ele((AT*)memory.get()), n(n_) { }
77  explicit Matrix(int n_, Init ini=INIT, const AT &a=AT()) : memory(n_), ele((AT*)memory.get()), n(n_) { init(a); }
78  explicit Matrix(int n_, Eye ini, const AT &a=1) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini,a); }
79  explicit Matrix(int m_, int n_, Noinit) : memory(n_), ele((AT*)memory.get()), n(n_) { }
80 
86  Matrix(const Matrix<Diagonal,Ref,Ref,AT> &A) : memory(A.n), ele((AT*)memory.get()) ,n(A.n) {
87  copy(A);
88  }
89 
95  template<class Type, class Row, class Col>
96  Matrix(const Matrix<Type,Row,Col,AT> &A) : memory(A.rows()), ele((AT*)memory.get()) ,n(A.rows()) {
97  FMATVEC_ASSERT(A.rows() == A.cols(), AT);
98  copy(A);
99  }
100 
103  ~Matrix() = default;
104 
105  Matrix<Diagonal,Ref,Ref,AT>& resize(int n_, Noinit) {
106  n = n_;
107  memory.resize(n);
108  ele = (AT*)memory.get();
109  return *this;
110  }
111 
112  Matrix<Diagonal,Ref,Ref,AT>& resize(int n, Init ini=INIT, const AT &a=AT()) { resize(n,Noinit()).init(a); }
113  Matrix<Diagonal,Ref,Ref,AT>& resize(int n, Eye ini, const AT &a=1) { resize(n,Noinit()).init(ini,a); }
114 
122  FMATVEC_ASSERT(n == A.n, AT);
123  return copy(A);
124  }
125 
132  template<class Type, class Row, class Col>
134  FMATVEC_ASSERT(n == A.rows(), AT);
135  FMATVEC_ASSERT(n == A.cols(), AT);
136  return copy(A);
137  }
138 
146  n=A.n;
147  memory = A.memory;
148  ele = A.ele;
149  return *this;
150  }
151 
158  template<class Type, class Row, class Col>
159  inline Matrix<Diagonal,Ref,Ref,AT>& operator<<=(const Matrix<Type,Row,Col,AT> &A) {
160  if(n!=A.n) resize(A.rows(),NONINIT);
161  return copy(A);
162  }
163 
173  const AT& operator()(int i, int j) const {
174 
175  FMATVEC_ASSERT(i>=0, AT);
176  FMATVEC_ASSERT(j>=0, AT);
177  FMATVEC_ASSERT(i<n, AT);
178  FMATVEC_ASSERT(j<n, AT);
179 
180  return e(i,j);
181  }
182 
187  const AT& operator()(int i) const {
188 
189  FMATVEC_ASSERT(i>=0, AT);
190  FMATVEC_ASSERT(i<n, AT);
191 
192  return e(i);
193  }
194 
203  AT& operator()(int i) {
204 
205  FMATVEC_ASSERT(i>=0, AT);
206  FMATVEC_ASSERT(i<n, AT);
207 
208  return e(i);
209  }
210 
211  const AT& e(int i, int j) const {
212  static AT zero=0;
213  return i==j ? ele[i] : zero;
214  }
215 
216  const AT& e(int i) const {
217  return ele[i];
218  }
219 
220  AT& e(int i) {
221  return ele[i];
222  }
223 
228  const AT* operator()() const {
229  return ele;
230  }
231 
237  AT* operator()() {
238  return ele;
239  }
240 
245  int rows() const {return n;}
246 
251  int cols() const {return n;}
252 
257  int size() const {return n;}
258 
266  const CBLAS_ORDER blasOrder() const {
267  return CblasColMajor;
268  }
269 
277  Matrix<Diagonal,Ref,Ref,AT>& init(const AT &val=AT());
278  inline Matrix<Diagonal,Ref,Ref,AT>& init(Init, const AT &a=AT()) { return init(a); }
279  inline Matrix<Diagonal,Ref,Ref,AT>& init(Eye, const AT &a=1) { return init(a); }
280  inline Matrix<Diagonal,Ref,Ref,AT>& init(Noinit, const AT &a=AT()) { return *this; }
281 
287  explicit Matrix<Diagonal,Ref,Ref,AT>(const std::vector<std::vector<AT>> &m);
288 
293  explicit operator std::vector<std::vector<AT>>() const;
294 
295 // /*! \brief Cast to AT.
296 // *
297 // * \return The AT representation of the matrix
298 // * */
299 // explicit operator AT() const {
300 // FMATVEC_ASSERT(n==1, AT);
301 // return ele[0];
302 // }
303 //
304 // /*! \brief AT Constructor.
305 // * Constructs and initializes a matrix with a AT object.
306 // * \param x The AT the matrix will be initialized with.
307 // * */
308 // explicit Matrix(const AT &x) : memory(1), ele((AT*)memory.get()), n(1) { ele[0] = x; }
309  };
310 
311  template <class AT>
312  Matrix<Diagonal,Ref,Ref,AT>::Matrix(const std::vector<std::vector<AT>> &m) : Matrix<Diagonal,Ref,Ref,AT>(m.size()) {
313  for(size_t r=0; r<m.size(); ++r) {
314  if(n!=static_cast<int>(m[r].size()))
315  throw std::runtime_error("The rows of the input have different lenght.");
316  for(size_t c=0; c<m[r].size(); ++c) {
317  if(r==c)
318  e(r)=m[r][r];
319  if(r!=c && (fabs(m[r][c])>1e-13 || fabs(m[r][c])>1e-13))
320  throw std::runtime_error("The input is not diagonal.");
321  }
322  }
323  }
324 
325 
326  template <class AT>
328  for(int i=0; i<rows(); i++)
329  e(i) = val;
330  return *this;
331  }
332 
333  template <class AT>
335  for(int i=0; i<n; i++)
336  e(i) = A.e(i);
337  return *this;
338  }
339 
340  template <class AT>
341  Matrix<Diagonal,Ref,Ref,AT>::operator std::vector<std::vector<AT>>() const {
342  std::vector<std::vector<AT>> ret(rows());
343  for(int r=0; r<rows(); r++) {
344  ret[r].resize(cols());
345  for(int c=0; c<cols(); c++)
346  ret[r][c]=e(r,c);
347  }
348  return ret;
349  }
350 
351 }
352 
353 #endif
AT & operator()(int i)
Element operator.
Definition: diagonal_matrix.h:203
Matrix(const Matrix< Diagonal, Ref, Ref, AT > &A)
Copy Constructor.
Definition: diagonal_matrix.h:86
Matrix< Diagonal, Ref, Ref, AT > & operator=(const Matrix< Type, Row, Col, AT > &A)
Assignment operator.
Definition: diagonal_matrix.h:133
Matrix(const Matrix< Type, Row, Col, AT > &A)
Copy Constructor.
Definition: diagonal_matrix.h:96
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Matrix()
Standard constructor.
Definition: diagonal_matrix.h:69
AT * operator()()
Pointer operator.
Definition: diagonal_matrix.h:237
Definition: types.h:85
const AT & operator()(int i, int j) const
Element operator.
Definition: diagonal_matrix.h:173
int cols() const
Number of columns.
Definition: diagonal_matrix.h:251
const AT * operator()() const
Pointer operator.
Definition: diagonal_matrix.h:228
int size() const
Number of rows and columns.
Definition: diagonal_matrix.h:257
Definition: types.h:94
Matrix< Diagonal, Ref, Ref, AT > & operator=(const Matrix< Diagonal, Ref, Ref, AT > &A)
Assignment operator.
Definition: diagonal_matrix.h:121
Matrix< Diagonal, Ref, Ref, AT > & operator&=(Matrix< Diagonal, Ref, Ref, AT > &A)
Reference operator.
Definition: diagonal_matrix.h:145
int cols() const
Number of columns.
const AT & operator()(int i) const
Element operator.
Definition: diagonal_matrix.h:187
Namespace fmatvec.
Definition: _memory.cc:28
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: diagonal_matrix.h:266
Shape class for diagonal matrices.
Definition: types.h:164
Matrix< Diagonal, Ref, Ref, AT > & init(const AT &val=AT())
Initialization.
Definition: diagonal_matrix.h:327
Definition: types.h:84
int rows() const
Number of rows.
Definition: diagonal_matrix.h:245
int rows() const
Number of rows.
This is a matrix class for diagonal matrices.
Definition: diagonal_matrix.h:39