All Classes Namespaces Functions 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 
65  Matrix() : memory(), ele(0), n(0), kl(0), ku(0) {
66  }
67 
68 // template<class Ini=All<AT> >
69 // 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_) {
70 // init(ini);
71 // }
72 // template<class Ini=All<AT> >
73 // Matrix(int m_, int n_, Ini ini=All<AT>()) : memory(n_*(n_+n_+1)), ele((AT*)memory.get()), n(n_), kl(n_), ku(n_) {
74 // init(ini);
75 // }
76 
77  Matrix(int n_, int kl_, int ku_, Noinit) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) { }
78  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); }
79  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); }
80  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_) { }
81  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); }
82  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); }
83 
91  Matrix(const Matrix<GeneralBand,Ref,Ref,AT> &A) : memory(A.memory), ele(A.ele) , n(A.n), kl(A.kl), ku(A.ku) {
92  }
93 
103  Matrix(int n_, int kl_, int ku_, AT* ele_) : memory(), ele(ele_), n(n_), kl(kl_), ku(ku_) {
104  }
105 
109  }
110 
111  Matrix<GeneralBand,Ref,Ref,AT>& resize() {
112  n = kl = ku = 0;
113  memory.resize(0);
114  ele = 0;
115  return *this;
116  }
117 
118  Matrix<GeneralBand,Ref,Ref,AT>& resize(int n_, int kl_, int ku_, Noinit) {
119  n = n_; kl = kl_; ku = ku_;
120  memory.resize(n*(kl+ku+1));
121  ele = (AT*)memory.get();
122  return *this;
123  }
124 
125  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); }
126 
127  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); }
128 
137  inline Matrix<GeneralBand,Ref,Ref,AT>& operator=(const Matrix<GeneralBand,Ref,Ref,AT> &A);
138 
145  inline Matrix<GeneralBand,Ref,Ref,AT>& operator<<(const Matrix<GeneralBand,Ref,Ref,AT> &A);
146 
153  inline Matrix<GeneralBand,Ref,Ref,AT>& operator>>(const Matrix<GeneralBand,Ref,Ref,AT> &A);
154 
166  const AT& operator()(int i, int j) const {
167 #ifndef FMATVEC_NO_BOUNDS_CHECK
168  assert(i>=0);
169  assert(j>=0);
170  assert(i<n);
171  assert(j<n);
172  // assert(i-j<=kl);
173  // assert(i-j>=-ku);
174 #endif
175  static AT zero=0;
176 
177  return ((i-j>kl) || (i-j<-ku)) ? zero : ele[ku+i+j*(kl+ku)];
178  };
179 
185  AT* operator()() {return ele;};
186 
191  const AT* operator()() const {return ele;};
192 
197  int size() const {return n;};
198 
203  int rows() const {return n;};
204 
209  int cols() const {return n;};
210 
218  const CBLAS_ORDER blasOrder() const {
219  return CblasColMajor;
220  };
221 
226  inline const Vector<Ref,AT> operator()(int i) const;
227 
235  inline Vector<Ref,AT> operator()(int i);
236 
242  inline Matrix<GeneralBand,Ref,Ref,AT> copy() const;
243 
251  inline Matrix<GeneralBand,Ref,Ref,AT>& init(const AT &a=0);
252  inline Matrix<GeneralBand,Ref,Ref,AT>& init(Init, const AT &a=0) { return init(a); }
253  inline Matrix<GeneralBand,Ref,Ref,AT>& init(Eye, const AT &a=1);
254  inline Matrix<GeneralBand,Ref,Ref,AT>& init(Noinit, const AT &a=0) { return *this; }
255 
260  inline operator std::vector<std::vector<AT> >();
261  };
262 
263  template <class AT>
265 
266  n=A.n;
267  memory = A.memory;
268  ele = A.ele;
269 
270  return *this;
271  }
272 
273  template <class AT>
275 
276  if(!ele) {
277  n = A.n;
278  memory.resize(n*n);
279  ele = (AT*)memory.get();
280  }
281  else {
282 #ifndef FMATVEC_NO_SIZE_CHECK
283  assert(n == A.n);
284 #endif
285  }
286 
287  deepCopy(A);
288 
289  return *this;
290  }
291 
292  template <class AT>
294 
295  if(n!=A.n) {
296  n = A.n;
297  memory.resize(n*n);
298  ele = (AT*)memory.get();
299  }
300 
301  deepCopy(A);
302 
303  return *this;
304  }
305 
306  template <class AT>
308  for(int i=0; i<kl+ku+1; i++)
309  for(int j=0; j<n; j++)
310  ele[i+j*(kl+ku+1)] = val;
311  return *this;
312  }
313 
314  template <class AT>
316  for(int i=0; i<n; i++)
317  ele[i] = val;
318  return *this;
319  }
320 
321  template <class AT>
323 #ifndef FMATVEC_NO_BOUNDS_CHECK
324  assert(i<=ku);
325  assert(i>=-kl);
326 #endif
327 
328  //return Vector<Ref,AT>(n-i,ku+kl+1,memory,ele[ku-i + (i>0?i:0)*(kl+ku+1)]);
329  return Vector<Ref,AT>(n-abs(i),ku+kl+1,true,memory,ele+ku-i + (i>0?i:0)*(kl+ku+1));
330  //(ku-i,i>0?i:0));
331  }
332 
333  template <class AT>
335 #ifndef FMATVEC_NO_BOUNDS_CHECK
336  assert(i<=ku);
337  assert(i>=-kl);
338 #endif
339 
340  return Vector<Ref,AT>(n-abs(i),ku+kl+1,true,memory,ele+ku-i + (i>0?i:0)*(kl+ku+1));
341  //(ku-i,i>0?i:0));
342  }
343 
344  template <class AT>
346 
348  A.deepCopy(*this);
349 
350  return A;
351  }
352 
353  template <class AT>
354  inline Matrix<GeneralBand,Ref,Ref,AT>::operator std::vector<std::vector<AT> >() {
355  std::vector<std::vector<AT> > ret(rows());
356  for(int r=0; r<rows(); r++) {
357  ret[r].resize(cols());
358  for(int c=0; c<cols(); c++)
359  ret[r][c]=operator()(r,c);
360  }
361  return ret;
362  }
363 
365 
366  template <class AT>
368 
369  for(int i=0; i<kl+ku+1; i++)
370  for(int j=0; j<n; j++)
371  ele[i+j*(kl+ku+1)] = A.ele[i+j*(kl+ku+1)];
372  }
373 
375 
376 }
377 
378 #endif
const CBLAS_ORDER blasOrder() const
Storage convention.
Definition: band_matrix.h:218
This is the basic matrix class for arbitrary matrices.
Definition: matrix.h:56
AT & operator()(int i, int j)
Standard constructor.
Definition: matrix.h:85
Definition: matrix.h:39
Matrix(const Matrix< GeneralBand, Ref, Ref, AT > &A)
Copy Constructor.
Definition: band_matrix.h:91
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:185
Matrix(int n_, int kl_, int ku_, AT *ele_)
Regular Constructor.
Definition: band_matrix.h:103
~Matrix()
Destructor.
Definition: band_matrix.h:108
Matrix()
Standard constructor.
Definition: band_matrix.h:65
std::istream & operator>>(std::istream &is, Matrix< Type, Row, Col, AT > &A)
Matrix input.
Definition: matrix.h:170
const AT & operator()(int i, int j) const
Element operator.
Definition: band_matrix.h:166
Matrix< GeneralBand, Ref, Ref, AT > & init(const AT &a=0)
Initialization.
Definition: band_matrix.h:307
int rows() const
Number of rows.
Definition: band_matrix.h:203
int size() const
Size.
Definition: band_matrix.h:197
int cols() const
Number of columns.
Definition: band_matrix.h:209
const AT * operator()() const
Pointer operator.
Definition: band_matrix.h:191
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