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;
49  int kl;
50  int ku;
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 AtomicType;
62 
67  Matrix() : memory(), ele(0), n(0), kl(0), ku(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 
111  }
112 
113  Matrix<GeneralBand,Ref,Ref,AT>& resize() {
114  n = kl = ku = 0;
115  memory.resize(0);
116  ele = 0;
117  return *this;
118  }
119 
120  Matrix<GeneralBand,Ref,Ref,AT>& resize(int n_, int kl_, int ku_, Noinit) {
121  n = n_; kl = kl_; ku = ku_;
122  memory.resize(n*(kl+ku+1));
123  ele = (AT*)memory.get();
124  return *this;
125  }
126 
127  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); }
128 
129  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); }
130 
139  inline Matrix<GeneralBand,Ref,Ref,AT>& operator=(const Matrix<GeneralBand,Ref,Ref,AT> &A);
140 
147  inline Matrix<GeneralBand,Ref,Ref,AT>& operator<<(const Matrix<GeneralBand,Ref,Ref,AT> &A);
148 
155  inline Matrix<GeneralBand,Ref,Ref,AT>& operator>>(const Matrix<GeneralBand,Ref,Ref,AT> &A);
156 
168  const AT& operator()(int i, int j) const {
169 #ifndef FMATVEC_NO_BOUNDS_CHECK
170  assert(i>=0);
171  assert(j>=0);
172  assert(i<n);
173  assert(j<n);
174  // assert(i-j<=kl);
175  // assert(i-j>=-ku);
176 #endif
177  static AT zero=0;
178 
179  return ((i-j>kl) || (i-j<-ku)) ? zero : ele[ku+i+j*(kl+ku)];
180  };
181 
187  AT* operator()() {return ele;};
188 
193  const AT* operator()() const {return ele;};
194 
199  int size() const {return n;};
200 
205  int rows() const {return n;};
206 
211  int cols() const {return n;};
212 
220  const CBLAS_ORDER blasOrder() const {
221  return CblasColMajor;
222  };
223 
228  inline const Vector<Ref,AT> operator()(int i) const;
229 
237  inline Vector<Ref,AT> operator()(int i);
238 
244  inline Matrix<GeneralBand,Ref,Ref,AT> copy() const;
245 
253  inline Matrix<GeneralBand,Ref,Ref,AT>& init(const AT &a=0);
254  inline Matrix<GeneralBand,Ref,Ref,AT>& init(Init, const AT &a=0) { return init(a); }
255  inline Matrix<GeneralBand,Ref,Ref,AT>& init(Eye, const AT &a=1);
256  inline Matrix<GeneralBand,Ref,Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
257 
262  inline operator std::vector<std::vector<AT> >();
263  };
264 
265  template <class AT>
267 
268  n=A.n;
269  memory = A.memory;
270  ele = A.ele;
271 
272  return *this;
273  }
274 
275  template <class AT>
277 
278  if(!ele) {
279  n = A.n;
280  memory.resize(n*n);
281  ele = (AT*)memory.get();
282  }
283  else {
284 #ifndef FMATVEC_NO_SIZE_CHECK
285  assert(n == A.n);
286 #endif
287  }
288 
289  deepCopy(A);
290 
291  return *this;
292  }
293 
294  template <class AT>
296 
297  if(n!=A.n) {
298  n = A.n;
299  memory.resize(n*n);
300  ele = (AT*)memory.get();
301  }
302 
303  deepCopy(A);
304 
305  return *this;
306  }
307 
308  template <class AT>
310  for(int i=0; i<kl+ku+1; i++)
311  for(int j=0; j<n; j++)
312  ele[i+j*(kl+ku+1)] = val;
313  return *this;
314  }
315 
316  template <class AT>
318  for(int i=0; i<n; i++)
319  ele[i] = val;
320  return *this;
321  }
322 
323  template <class AT>
325 #ifndef FMATVEC_NO_BOUNDS_CHECK
326  assert(i<=ku);
327  assert(i>=-kl);
328 #endif
329 
330  //return Vector<Ref,AT>(n-i,ku+kl+1,memory,ele[ku-i + (i>0?i:0)*(kl+ku+1)]);
331  return Vector<Ref,AT>(n-abs(i),ku+kl+1,true,memory,ele+ku-i + (i>0?i:0)*(kl+ku+1));
332  //(ku-i,i>0?i:0));
333  }
334 
335  template <class AT>
337 #ifndef FMATVEC_NO_BOUNDS_CHECK
338  assert(i<=ku);
339  assert(i>=-kl);
340 #endif
341 
342  return Vector<Ref,AT>(n-abs(i),ku+kl+1,true,memory,ele+ku-i + (i>0?i:0)*(kl+ku+1));
343  //(ku-i,i>0?i:0));
344  }
345 
346  template <class AT>
348 
350  A.deepCopy(*this);
351 
352  return A;
353  }
354 
355  template <class AT>
356  inline Matrix<GeneralBand,Ref,Ref,AT>::operator std::vector<std::vector<AT> >() {
357  std::vector<std::vector<AT> > ret(rows());
358  for(int r=0; r<rows(); r++) {
359  ret[r].resize(cols());
360  for(int c=0; c<cols(); c++)
361  ret[r][c]=operator()(r,c);
362  }
363  return ret;
364  }
365 
367 
368  template <class AT>
370 
371  for(int i=0; i<kl+ku+1; i++)
372  for(int j=0; j<n; j++)
373  ele[i+j*(kl+ku+1)] = A.ele[i+j*(kl+ku+1)];
374  }
375 
377 
378 }
379 
380 #endif
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: band_matrix.h:220
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:86
Definition: matrix.h:39
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:187
Matrix(int n_, int kl_, int ku_, AT *ele_)
Regular Constructor.
Definition: band_matrix.h:105
~Matrix()
Destructor.
Definition: band_matrix.h:110
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:171
const AT & operator()(int i, int j) const
Element operator.
Definition: band_matrix.h:168
Matrix< GeneralBand, Ref, Ref, AT > & init(const AT &a=0)
Initialization.
Definition: band_matrix.h:309
int rows() const
Number of rows.
Definition: band_matrix.h:205
int size() const
Size.
Definition: band_matrix.h:199
int cols() const
Number of columns.
Definition: band_matrix.h:211
const AT * operator()() const
Pointer operator.
Definition: band_matrix.h:193
Definition: matrix.h:38
This is a vector class of general shape in dense storage format.
Definition: vector.h:39
Definition: matrix.h:40

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML