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;
48 
49  void deepCopy(const Matrix<Diagonal,Ref,Ref,AT> &x);
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 AtomicType;
67 
72  Matrix() : memory(), ele(0), n(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() {
97  }
98 
99  Matrix<Diagonal,Ref,Ref,AT>& resize() {
100  n = 0;
101  memory.resize(0);
102  ele = 0;
103  return *this;
104  }
105 
106  Matrix<Diagonal,Ref,Ref,AT>& resize(int n_, Noinit) {
107  n = n_;
108  memory.resize(n);
109  ele = (AT*)memory.get();
110  return *this;
111  }
112 
113  Matrix<Diagonal,Ref,Ref,AT>& resize(int n, Init ini=INIT, const AT &a=0) { resize(n,Noinit()).init(a); }
114  Matrix<Diagonal,Ref,Ref,AT>& resize(int n, Eye ini, const AT &a=1) { resize(n,Noinit()).init(ini,a); }
115 
122  inline Matrix<Diagonal,Ref,Ref,AT>& operator<<(const Matrix<Diagonal,Ref,Ref,AT> &A);
123 
130  inline Matrix<Diagonal,Ref,Ref,AT>& operator>>(const Matrix<Diagonal,Ref,Ref,AT> &A);
131 
140  inline Matrix<Diagonal,Ref,Ref,AT>& operator=(const Matrix<Diagonal,Ref,Ref,AT> &A);
141 
153  const AT& operator()(int i, int j) const {
154 
155 #ifndef FMATVEC_NO_BOUNDS_CHECK
156  assert(i>=0);
157  assert(j>=0);
158  assert(i<n);
159  assert(j<n);
160 #endif
161 
162  return e(i,j);
163  };
164 
169  const AT& operator()(int i) const {
170 
171 #ifndef FMATVEC_NO_BOUNDS_CHECK
172  assert(i>=0);
173  assert(i<n);
174 #endif
175 
176  return e(i);
177  };
178 
189  AT& operator()(int i) {
190 
191 #ifndef FMATVEC_NO_BOUNDS_CHECK
192  assert(i>=0);
193  assert(i<n);
194 #endif
195 
196  return e(i);
197  };
198 
199  const AT& e(int i, int j) const {
200  static AT zero=0;
201  return i==j ? ele[i] : zero;
202  };
203 
204  const AT& e(int i) const {
205  return ele[i];
206  };
207 
208  AT& e(int i) {
209  return ele[i];
210  };
211 
216  const AT* operator()() const {
217  return ele;
218  };
219 
225  AT* operator()() {
226  return ele;
227  };
228 
233  int rows() const {return n;};
234 
239  int cols() const {return n;};
240 
245  int size() const {return n;};
246 
254  const CBLAS_ORDER blasOrder() const {
255  return CblasColMajor;
256  };
257 
263  Matrix<Diagonal,Ref,Ref,AT> copy() const;
264 
272  Matrix<Diagonal,Ref,Ref,AT>& init(const AT &a=0);
273  inline Matrix<Diagonal,Ref,Ref,AT>& init(Init, const AT &a=0) { return init(a); }
274  inline Matrix<Diagonal,Ref,Ref,AT>& init(Eye, const AT &a=1) { return init(a); }
275  inline Matrix<Diagonal,Ref,Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
276 
281  operator std::vector<std::vector<AT> >();
282  };
283 
284  template <class AT>
286  n=A.n;
287  memory = A.memory;
288  ele = A.ele;
289 
290  return *this;
291  }
292 
293  template <class AT>
295  if(!ele) {
296  n = A.n;
297  memory.resize(n);
298  ele = (AT*)memory.get();
299  }
300  else {
301 #ifndef FMATVEC_NO_SIZE_CHECK
302  assert(n == A.n);
303 #endif
304  }
305 
306  deepCopy(A);
307 
308  return *this;
309  }
310 
311 
312  template <class AT>
314 
315  if(n!=A.n) {
316  n = A.n;
317  memory.resize(n);
318  ele = (AT*)memory.get();
319  }
320 
321  deepCopy(A);
322 
323  return *this;
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 
337 
338  A.deepCopy(*this);
339 
340  return A;
341  }
342 
343  template <class AT>
345  for(int i=0; i<n; i++)
346  e(i) = A.e(i);
347  }
348 
349  template <class AT>
350  Matrix<Diagonal,Ref,Ref,AT>::operator std::vector<std::vector<AT> >() {
351  std::vector<std::vector<AT> > ret(rows());
352  for(int r=0; r<rows(); r++) {
353  ret[r].resize(cols());
354  for(int c=0; c<cols(); c++)
355  ret[r][c]=e(r,c);
356  }
357  return ret;
358  }
359 
360 }
361 
362 #endif
AT & operator()(int i)
Element operator.
Definition: diagonal_matrix.h:189
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:225
Definition: matrix.h:39
const AT & operator()(int i, int j) const
Element operator.
Definition: diagonal_matrix.h:153
int size() const
Number of rows and columns.
Definition: diagonal_matrix.h:245
int rows() const
Number of rows.
int rows() const
Number of rows.
Definition: diagonal_matrix.h:233
const AT & operator()(int i) const
Element operator.
Definition: diagonal_matrix.h:169
Definition: types.h:62
~Matrix()
Destructor.
Definition: diagonal_matrix.h:96
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:171
Shape class for diagonal matrices.
Definition: types.h:132
const AT * operator()() const
Pointer operator.
Definition: diagonal_matrix.h:216
Definition: matrix.h:38
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: diagonal_matrix.h:254
int cols() const
Number of columns.
Definition: diagonal_matrix.h:239
Matrix< Diagonal, Ref, Ref, AT > & init(const AT &a=0)
Initialization.
Definition: diagonal_matrix.h:327
This is a matrix class for diagonal matrices.
Definition: diagonal_matrix.h:39

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML