All Classes Namespaces Functions 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 
70  Matrix() : memory(), ele(0), n(0) { }
71 
72 // template<class Ini=All<AT> >
73 // Matrix(int n_, Ini ini=All<AT>()) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini); }
74 // template<class Ini=All<AT> >
75 // Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini); }
76 
77  Matrix(int n_, Noinit) : memory(n_), ele((AT*)memory.get()), n(n_) { }
78  Matrix(int n_, Init ini=INIT, const AT &a=0) : memory(n_), ele((AT*)memory.get()), n(n_) { init(a); }
79  Matrix(int n_, Eye ini, const AT &a=1) : memory(n_), ele((AT*)memory.get()), n(n_) { init(ini,a); }
80  Matrix(int m_, int n_, Noinit) : memory(n_), ele((AT*)memory.get()), n(n_) { }
81 
89  Matrix(const Matrix<Diagonal,Ref,Ref,AT> &A) : memory(A.memory), ele(A.ele) ,n(A.n) {
90  }
91 
94  ~Matrix() {
95  }
96 
97  Matrix<Diagonal,Ref,Ref,AT>& resize() {
98  n = 0;
99  memory.resize(0);
100  ele = 0;
101  return *this;
102  }
103 
104  Matrix<Diagonal,Ref,Ref,AT>& resize(int n_, Noinit) {
105  n = n_;
106  memory.resize(n);
107  ele = (AT*)memory.get();
108  return *this;
109  }
110 
111  Matrix<Diagonal,Ref,Ref,AT>& resize(int n, Init ini=INIT, const AT &a=0) { resize(n,Noinit()).init(a); }
112  Matrix<Diagonal,Ref,Ref,AT>& resize(int n, Eye ini, const AT &a=1) { resize(n,Noinit()).init(ini,a); }
113 
120  inline Matrix<Diagonal,Ref,Ref,AT>& operator<<(const Matrix<Diagonal,Ref,Ref,AT> &A);
121 
128  inline Matrix<Diagonal,Ref,Ref,AT>& operator>>(const Matrix<Diagonal,Ref,Ref,AT> &A);
129 
138  inline Matrix<Diagonal,Ref,Ref,AT>& operator=(const Matrix<Diagonal,Ref,Ref,AT> &A);
139 
151  const AT& operator()(int i, int j) const {
152 
153 #ifndef FMATVEC_NO_BOUNDS_CHECK
154  assert(i>=0);
155  assert(j>=0);
156  assert(i<n);
157  assert(j<n);
158 #endif
159 
160  return e(i,j);
161  };
162 
167  const AT& operator()(int i) const {
168 
169 #ifndef FMATVEC_NO_BOUNDS_CHECK
170  assert(i>=0);
171  assert(i<n);
172 #endif
173 
174  return e(i);
175  };
176 
187  AT& operator()(int i) {
188 
189 #ifndef FMATVEC_NO_BOUNDS_CHECK
190  assert(i>=0);
191  assert(i<n);
192 #endif
193 
194  return e(i);
195  };
196 
197  const AT& e(int i, int j) const {
198  static AT zero=0;
199  return i==j ? ele[i] : zero;
200  };
201 
202  const AT& e(int i) const {
203  return ele[i];
204  };
205 
206  AT& e(int i) {
207  return ele[i];
208  };
209 
214  const AT* operator()() const {
215  return ele;
216  };
217 
223  AT* operator()() {
224  return ele;
225  };
226 
231  int rows() const {return n;};
232 
237  int cols() const {return n;};
238 
243  int size() const {return n;};
244 
252  const CBLAS_ORDER blasOrder() const {
253  return CblasColMajor;
254  };
255 
261  Matrix<Diagonal,Ref,Ref,AT> copy() const;
262 
270  Matrix<Diagonal,Ref,Ref,AT>& init(const AT &a=0);
271  inline Matrix<Diagonal,Ref,Ref,AT>& init(Init, const AT &a=0) { return init(a); }
272  inline Matrix<Diagonal,Ref,Ref,AT>& init(Eye, const AT &a=1) { return init(a); }
273  inline Matrix<Diagonal,Ref,Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
274 
279  operator std::vector<std::vector<AT> >();
280  };
281 
282  template <class AT>
284  n=A.n;
285  memory = A.memory;
286  ele = A.ele;
287 
288  return *this;
289  }
290 
291  template <class AT>
293  if(!ele) {
294  n = A.n;
295  memory.resize(n);
296  ele = (AT*)memory.get();
297  }
298  else {
299 #ifndef FMATVEC_NO_SIZE_CHECK
300  assert(n == A.n);
301 #endif
302  }
303 
304  deepCopy(A);
305 
306  return *this;
307  }
308 
309 
310  template <class AT>
312 
313  if(n!=A.n) {
314  n = A.n;
315  memory.resize(n);
316  ele = (AT*)memory.get();
317  }
318 
319  deepCopy(A);
320 
321  return *this;
322  }
323 
324  template <class AT>
326  for(int i=0; i<rows(); i++)
327  e(i) = val;
328  return *this;
329  }
330 
331  template <class AT>
333 
335 
336  A.deepCopy(*this);
337 
338  return A;
339  }
340 
341  template <class AT>
343  for(int i=0; i<n; i++)
344  e(i) = A.e(i);
345  }
346 
347  template <class AT>
348  Matrix<Diagonal,Ref,Ref,AT>::operator std::vector<std::vector<AT> >() {
349  std::vector<std::vector<AT> > ret(rows());
350  for(int r=0; r<rows(); r++) {
351  ret[r].resize(cols());
352  for(int c=0; c<cols(); c++)
353  ret[r][c]=e(r,c);
354  }
355  return ret;
356  }
357 
358 }
359 
360 #endif
AT & operator()(int i)
Element operator.
Definition: diagonal_matrix.h:187
Matrix(const Matrix< Diagonal, Ref, Ref, AT > &A)
Copy Constructor.
Definition: diagonal_matrix.h:89
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:56
Matrix()
Standard constructor.
Definition: diagonal_matrix.h:70
AT * operator()()
Pointer operator.
Definition: diagonal_matrix.h:223
Definition: matrix.h:39
const AT & operator()(int i, int j) const
Element operator.
Definition: diagonal_matrix.h:151
int size() const
Number of rows and columns.
Definition: diagonal_matrix.h:243
int rows() const
Number of rows.
int rows() const
Number of rows.
Definition: diagonal_matrix.h:231
const AT & operator()(int i) const
Element operator.
Definition: diagonal_matrix.h:167
Definition: types.h:62
~Matrix()
Destructor.
Definition: diagonal_matrix.h:94
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:170
Shape class for diagonal matrices.
Definition: types.h:132
const AT * operator()() const
Pointer operator.
Definition: diagonal_matrix.h:214
Definition: matrix.h:38
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: diagonal_matrix.h:252
int cols() const
Number of columns.
Definition: diagonal_matrix.h:237
Matrix< Diagonal, Ref, Ref, AT > & init(const AT &a=0)
Initialization.
Definition: diagonal_matrix.h:325
This is a matrix class for diagonal matrices.
Definition: diagonal_matrix.h:39

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML