All Classes Namespaces Functions Variables Typedefs Enumerations Pages
band_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 general_band_matrix_h
23 #define general_band_matrix_h
24 
25 #include "vector.h"
26 #include "types.h"
27 #include "_memory.h"
28 #include <cstdlib>
29 
30 namespace fmatvec {
31 
40  template <class AT> class Matrix<GeneralBand,Ref,Ref,AT> {
41 
43 
44  protected:
45 
46  Memory<AT> memory;
47  AT *ele;
48  int n{0};
49  int kl{0};
50  int ku{0};
51 
52  inline void deepCopy(const Matrix<GeneralBand,Ref,Ref,AT> &A);
53 
54  Matrix(int n_, int kl_, int ku_, Memory<AT> memory_, const AT* ele_) : memory(memory_), ele((AT*)ele_), n(n_), kl(kl_), ku(ku_) {
55  }
56 
58 
59  public:
60 
61  typedef AT value_type;
62 
67  Matrix() : memory(), ele(0) {
68  }
69 
70 // template<class Ini=All<AT> >
71 // Matrix(int n_, int kl_, int ku_, Ini ini=All<AT>()) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) {
72 // init(ini);
73 // }
74 // template<class Ini=All<AT> >
75 // Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(n_*(n_+n_+1)), ele((AT*)memory.get()), n(n_), kl(n_), ku(n_) {
76 // init(ini);
77 // }
78 
79  Matrix(int n_, int kl_, int ku_, Noinit) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { }
80  Matrix(int n_, int kl_, int ku_, Init ini=INIT, const AT &a=0) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { init(a); }
81  Matrix(int n_, int kl_, int ku_, Eye ini, const AT &a=1) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { init(ini,a); }
82  Matrix(int m_, int n_, int kl_, int ku_, Noinit) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { }
83  Matrix(int m_, int n_, int kl_, int ku_, Init ini=INIT, const AT &a=0) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { init(a); }
84  Matrix(int m_, int n_, int kl_, int ku_, Eye ini, const AT &a=1) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { init(ini,a); }
85 
93  Matrix(const Matrix<GeneralBand,Ref,Ref,AT> &A) : memory(A.memory), ele(A.ele) , n(A.n), kl(A.kl), ku(A.ku) {
94  }
95 
105  Matrix(int n_, int kl_, int ku_, AT* ele_) : memory(), ele(ele_), n(n_), kl(kl_), ku(ku_) {
106  }
107 
110  ~Matrix() = default;
111 
112  Matrix<GeneralBand,Ref,Ref,AT>& resize() {
113  n = kl = ku = 0;
114  memory.resize(0);
115  ele = 0;
116  return *this;
117  }
118 
119  Matrix<GeneralBand,Ref,Ref,AT>& resize(int n_, int kl_, int ku_, Noinit) {
120  n = n_; kl = kl_; ku = ku_;
121  memory.resize(n*(kl+ku+1));
122  ele = (AT*)memory.get();
123  return *this;
124  }
125 
126  Matrix<GeneralBand,Ref,Ref,AT>& resize(int n, int kl, int ku=0, Init ini=INIT, const AT &a=0) { return resize(n,kl,ku,Noinit()).init(a); }
127 
128  Matrix<GeneralBand,Ref,Ref,AT>& resize(int n, int kl, int ku, Eye ini, const AT &a=1) { return resize(n,kl,ku,Noinit()).init(a); }
129 
138  inline Matrix<GeneralBand,Ref,Ref,AT>& operator=(const Matrix<GeneralBand,Ref,Ref,AT> &A);
139 
146  inline Matrix<GeneralBand,Ref,Ref,AT>& operator<<(const Matrix<GeneralBand,Ref,Ref,AT> &A);
147 
154  inline Matrix<GeneralBand,Ref,Ref,AT>& operator>>(const Matrix<GeneralBand,Ref,Ref,AT> &A);
155 
167  const AT& operator()(int i, int j) const {
168 #ifndef FMATVEC_NO_BOUNDS_CHECK
169  assert(i>=0);
170  assert(j>=0);
171  assert(i<n);
172  assert(j<n);
173  // assert(i-j<=kl);
174  // assert(i-j>=-ku);
175 #endif
176  static AT zero=0;
177 
178  return ((i-j>kl) || (i-j<-ku)) ? zero : ele[ku+i+j*(kl+ku)];
179  };
180 
186  AT* operator()() {return ele;};
187 
192  const AT* operator()() const {return ele;};
193 
198  int size() const {return n;};
199 
204  int rows() const {return n;};
205 
210  int cols() const {return n;};
211 
219  const CBLAS_ORDER blasOrder() const {
220  return CblasColMajor;
221  };
222 
227  inline const Vector<Ref,AT> operator()(int i) const;
228 
236  inline Vector<Ref,AT> operator()(int i);
237 
243  inline Matrix<GeneralBand,Ref,Ref,AT> copy() const;
244 
252  inline Matrix<GeneralBand,Ref,Ref,AT>& init(const AT &val=0);
253  inline Matrix<GeneralBand,Ref,Ref,AT>& init(Init, const AT &a=0) { return init(a); }
254  inline Matrix<GeneralBand,Ref,Ref,AT>& init(Eye, const AT &val=1);
255  inline Matrix<GeneralBand,Ref,Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
256 
261  inline operator std::vector<std::vector<AT> >() const;
262  };
263 
264  template <class AT>
266 
267  n=A.n;
268  memory = A.memory;
269  ele = A.ele;
270 
271  return *this;
272  }
273 
274  template <class AT>
276 
277  if(!ele) {
278  n = A.n;
279  memory.resize(n*n);
280  ele = (AT*)memory.get();
281  }
282  else {
283 #ifndef FMATVEC_NO_SIZE_CHECK
284  assert(n == A.n);
285 #endif
286  }
287 
288  deepCopy(A);
289 
290  return *this;
291  }
292 
293  template <class AT>
295 
296  if(n!=A.n) {
297  n = A.n;
298  memory.resize(n*n);
299  ele = (AT*)memory.get();
300  }
301 
302  deepCopy(A);
303 
304  return *this;
305  }
306 
307  template <class AT>
309  for(int i=0; i<kl+ku+1; i++)
310  for(int j=0; j<n; j++)
311  ele[i+j*(kl+ku+1)] = val;
312  return *this;
313  }
314 
315  template <class AT>
317  for(int i=0; i<n; i++)
318  ele[i] = val;
319  return *this;
320  }
321 
322  template <class AT>
324 #ifndef FMATVEC_NO_BOUNDS_CHECK
325  assert(i<=ku);
326  assert(i>=-kl);
327 #endif
328 
329  //return Vector<Ref,AT>(n-i,ku+kl+1,memory,ele[ku-i + (i>0?i:0)*(kl+ku+1)]);
330  return Vector<Ref,AT>(n-abs(i),ku+kl+1,true,memory,ele+ku-i + (i>0?i:0)*(kl+ku+1));
331  //(ku-i,i>0?i:0));
332  }
333 
334  template <class AT>
336 #ifndef FMATVEC_NO_BOUNDS_CHECK
337  assert(i<=ku);
338  assert(i>=-kl);
339 #endif
340 
341  return Vector<Ref,AT>(n-abs(i),ku+kl+1,true,memory,ele+ku-i + (i>0?i:0)*(kl+ku+1));
342  //(ku-i,i>0?i:0));
343  }
344 
345  template <class AT>
347 
349  A.deepCopy(*this);
350 
351  return A;
352  }
353 
354  template <class AT>
355  inline Matrix<GeneralBand,Ref,Ref,AT>::operator std::vector<std::vector<AT> >() const {
356  std::vector<std::vector<AT> > ret(rows());
357  for(int r=0; r<rows(); r++) {
358  ret[r].resize(cols());
359  for(int c=0; c<cols(); c++)
360  ret[r][c]=operator()(r,c);
361  }
362  return ret;
363  }
364 
366 
367  template <class AT>
369 
370  for(int i=0; i<kl+ku+1; i++)
371  for(int j=0; j<n; j++)
372  ele[i+j*(kl+ku+1)] = A.ele[i+j*(kl+ku+1)];
373  }
374 
376 
377 }
378 
379 #endif
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: band_matrix.h:219
Matrix< GeneralBand, Ref, Ref, AT > & init(const AT &val=0)
Initialization.
Definition: band_matrix.h:308
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:90
Definition: matrix.h:43
Matrix(const Matrix< GeneralBand, Ref, Ref, AT > &A)
Copy Constructor.
Definition: band_matrix.h:93
This is a matrix class for general band matrices.
Definition: band_matrix.h:40
Shape class for general band matrices.
Definition: types.h:108
Definition: types.h:62
AT * operator()()
Pointer operator.
Definition: band_matrix.h:186
Matrix(int n_, int kl_, int ku_, AT *ele_)
Regular Constructor.
Definition: band_matrix.h:105
Matrix()
Standard constructor.
Definition: band_matrix.h:67
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:166
const AT & operator()(int i, int j) const
Element operator.
Definition: band_matrix.h:167
int rows() const
Number of rows.
Definition: band_matrix.h:204
int size() const
Size.
Definition: band_matrix.h:198
int cols() const
Number of columns.
Definition: band_matrix.h:210
const AT * operator()() const
Pointer operator.
Definition: band_matrix.h:192
Definition: matrix.h:42
This is a vector class of general shape in dense storage format.
Definition: vector.h:39
Definition: matrix.h:44

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home