All Classes Namespaces Functions Variables Typedefs Enumerations Pages
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 
49  void deepCopy(const Matrix<Diagonal,Ref,Ref,AT> &A);
50 
51  Matrix(int n_, Memory<AT> memory_, const AT* ele_) : memory(memory_), ele((AT*)ele_), n(n_) {
52  }
53 
54  const AT* elePtr(int i) const {
55  return ele+i;
56  }
57 
58  AT* elePtr(int i) {
59  return ele+i;
60  }
61 
63 
64  public:
65 
66  typedef AT value_type;
67 
72  Matrix() : memory(), ele(0) { }
73 
74 // template<class Ini=All<AT> >
75 // Matrix(int n_, Ini ini=All<AT>()) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini); }
76 // template<class Ini=All<AT> >
77 // Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini); }
78 
79  Matrix(int n_, Noinit) : memory(n_), ele((AT*)memory.get()), n(n_) { }
80  Matrix(int n_, Init ini=INIT, const AT &a=0) : memory(n_), ele((AT*)memory.get()), n(n_) { init(a); }
81  Matrix(int n_, Eye ini, const AT &a=1) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini,a); }
82  Matrix(int m_, int n_, Noinit) : memory(n_), ele((AT*)memory.get()), n(n_) { }
83 
91  Matrix(const Matrix<Diagonal,Ref,Ref,AT> &A) : memory(A.memory), ele(A.ele) ,n(A.n) {
92  }
93 
96  ~Matrix() = default;
97 
98  Matrix<Diagonal,Ref,Ref,AT>& resize() {
99  n = 0;
100  memory.resize(0);
101  ele = 0;
102  return *this;
103  }
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=0) { 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 
121  inline Matrix<Diagonal,Ref,Ref,AT>& operator<<(const Matrix<Diagonal,Ref,Ref,AT> &A);
122 
129  inline Matrix<Diagonal,Ref,Ref,AT>& operator>>(const Matrix<Diagonal,Ref,Ref,AT> &A);
130 
139  inline Matrix<Diagonal,Ref,Ref,AT>& operator=(const Matrix<Diagonal,Ref,Ref,AT> &A);
140 
141  Matrix<Diagonal,Ref,Ref,AT>(const std::vector<std::vector<AT>> &A);
142 
154  const AT& operator()(int i, int j) const {
155 
156 #ifndef FMATVEC_NO_BOUNDS_CHECK
157  assert(i>=0);
158  assert(j>=0);
159  assert(i<n);
160  assert(j<n);
161 #endif
162 
163  return e(i,j);
164  };
165 
170  const AT& operator()(int i) const {
171 
172 #ifndef FMATVEC_NO_BOUNDS_CHECK
173  assert(i>=0);
174  assert(i<n);
175 #endif
176 
177  return e(i);
178  };
179 
190  AT& operator()(int i) {
191 
192 #ifndef FMATVEC_NO_BOUNDS_CHECK
193  assert(i>=0);
194  assert(i<n);
195 #endif
196 
197  return e(i);
198  };
199 
200  const AT& e(int i, int j) const {
201  static AT zero=0;
202  return i==j ? ele[i] : zero;
203  };
204 
205  const AT& e(int i) const {
206  return ele[i];
207  };
208 
209  AT& e(int i) {
210  return ele[i];
211  };
212 
217  const AT* operator()() const {
218  return ele;
219  };
220 
226  AT* operator()() {
227  return ele;
228  };
229 
234  int rows() const {return n;};
235 
240  int cols() const {return n;};
241 
246  int size() const {return n;};
247 
255  const CBLAS_ORDER blasOrder() const {
256  return CblasColMajor;
257  };
258 
264  Matrix<Diagonal,Ref,Ref,AT> copy() const;
265 
273  Matrix<Diagonal,Ref,Ref,AT>& init(const AT &val=0);
274  inline Matrix<Diagonal,Ref,Ref,AT>& init(Init, const AT &a=0) { return init(a); }
275  inline Matrix<Diagonal,Ref,Ref,AT>& init(Eye, const AT &a=1) { return init(a); }
276  inline Matrix<Diagonal,Ref,Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
277 
282  operator std::vector<std::vector<AT> >() const;
283  };
284 
285  template <class AT>
287  n=A.n;
288  memory = A.memory;
289  ele = A.ele;
290 
291  return *this;
292  }
293 
294  template <class AT>
296  if(!ele) {
297  n = A.n;
298  memory.resize(n);
299  ele = (AT*)memory.get();
300  }
301  else {
302 #ifndef FMATVEC_NO_SIZE_CHECK
303  assert(n == A.n);
304 #endif
305  }
306 
307  deepCopy(A);
308 
309  return *this;
310  }
311 
312  template <class AT>
313  Matrix<Diagonal,Ref,Ref,AT>::Matrix(const std::vector<std::vector<AT>> &A) : Matrix<Diagonal,Ref,Ref,AT>(A.size()) {
314  for(size_t r=0; r<A.size(); ++r) {
315  if(n!=static_cast<int>(A[r].size()))
316  throw std::runtime_error("The rows of the input have different lenght.");
317  for(size_t c=0; c<A[r].size(); ++c) {
318  if(r==c)
319  e(r)=A[r][r];
320  if(r!=c && (fabs(A[r][c])>1e-13 || fabs(A[r][c])>1e-13))
321  throw std::runtime_error("The input is not diagonal.");
322  }
323  }
324  }
325 
326 
327  template <class AT>
329 
330  if(n!=A.n) {
331  n = A.n;
332  memory.resize(n);
333  ele = (AT*)memory.get();
334  }
335 
336  deepCopy(A);
337 
338  return *this;
339  }
340 
341  template <class AT>
343  for(int i=0; i<rows(); i++)
344  e(i) = val;
345  return *this;
346  }
347 
348  template <class AT>
350 
352 
353  A.deepCopy(*this);
354 
355  return A;
356  }
357 
358  template <class AT>
360  for(int i=0; i<n; i++)
361  e(i) = A.e(i);
362  }
363 
364  template <class AT>
365  Matrix<Diagonal,Ref,Ref,AT>::operator std::vector<std::vector<AT> >() const {
366  std::vector<std::vector<AT> > ret(rows());
367  for(int r=0; r<rows(); r++) {
368  ret[r].resize(cols());
369  for(int c=0; c<cols(); c++)
370  ret[r][c]=e(r,c);
371  }
372  return ret;
373  }
374 
375 }
376 
377 #endif
AT & operator()(int i)
Element operator.
Definition: diagonal_matrix.h:190
Matrix(const Matrix< Diagonal, Ref, Ref, AT > &A)
Copy Constructor.
Definition: diagonal_matrix.h:91
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
Matrix()
Standard constructor.
Definition: diagonal_matrix.h:72
AT * operator()()
Pointer operator.
Definition: diagonal_matrix.h:226
Definition: matrix.h:43
Matrix< Diagonal, Ref, Ref, AT > & init(const AT &val=0)
Initialization.
Definition: diagonal_matrix.h:342
const AT & operator()(int i, int j) const
Element operator.
Definition: diagonal_matrix.h:154
int size() const
Number of rows and columns.
Definition: diagonal_matrix.h:246
int rows() const
Number of rows.
int rows() const
Number of rows.
Definition: diagonal_matrix.h:234
const AT & operator()(int i) const
Element operator.
Definition: diagonal_matrix.h:170
Definition: types.h:62
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:166
Shape class for diagonal matrices.
Definition: types.h:132
const AT * operator()() const
Pointer operator.
Definition: diagonal_matrix.h:217
Definition: matrix.h:42
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: diagonal_matrix.h:255
int cols() const
Number of columns.
Definition: diagonal_matrix.h:240
This is a matrix class for diagonal matrices.
Definition: diagonal_matrix.h:39

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home