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, AT> {
41 
43 
44  protected:
45 
46  Memory<AT> memory;
47  AT *ele;
48  int n;
49  int kl;
50  int ku;
51 
52  void deepCopy(const Matrix<GeneralBand, 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 
78  Matrix(int n_, int kl_, int ku_) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) {
79 
80 #ifndef FMATVEC_NO_INITIALIZATION
81  init(0);
82 #endif
83  }
84 
98  Matrix(int n_, int kl_, int ku_, Initialization ini, const AT &a=0) : memory(n_*(kl_+ku_+1)), ele((AT*)memory.get()), n(n_), kl(kl_), ku(ku_) {
99 
100  if(ini == INIT)
101  init(a);
102  else if(ini == EYE){
103  init(0);
104  for(int i=0; i<n; i++)
105  ele[ku+i+i*(kl+ku)] = 1;
106  }
107 
108  }
109 
117  Matrix(const Matrix<GeneralBand, AT> &A) : memory(A.memory), ele(A.ele) , n(A.n), kl(A.kl), ku(A.ku) {
118  }
119 
129  Matrix(int n_, int kl_, int ku_, AT* ele_) : memory(), ele(ele_), n(n_), kl(kl_), ku(ku_) {
130  }
131 
135  }
136 
148  Matrix<GeneralBand, AT>& resize(int n_, int kl_, int ku_) {
149  n=n_;
150  kl=kl_;
151  ku=ku_;
152  memory.resize(n*(kl+ku+1));
153  ele = (AT*)memory.get();
154 
155 #ifndef FMATVEC_NO_INITIALIZATION
156  init(0);
157 #endif
158  return *this;
159  }
160 
173  Matrix<GeneralBand, AT>& resize(int n_, int kl_, int ku_, Initialization ini, const AT &a=0) {
174  n=n_;
175  kl=kl_;
176  ku=ku_;
177  memory.resize(n*(kl+ku+1));
178  ele = (AT*)memory.get();
179 
180  if(ini == INIT)
181  init(a);
182  else if(ini == EYE){
183  init(0);
184  for(int i=0; i<n; i++)
185  ele[ku+i+i*(kl+ku)] = 1;
186  }
187 
188  return *this;
189  }
190 
200 #ifndef FMATVEC_NO_DEEP_ASSIGNMENT
201  return operator<<(A);
202 #else
203  return operator>>(A);
204 #endif
205  }
206 
213  Matrix<GeneralBand, AT>& operator<<(const Matrix<GeneralBand, AT> &A);
214 
222 
234  const AT& operator()(int i, int j) const {
235 #ifndef FMATVEC_NO_BOUNDS_CHECK
236  assert(i>=0);
237  assert(j>=0);
238  assert(i<n);
239  assert(j<n);
240  // assert(i-j<=kl);
241  // assert(i-j>=-ku);
242 #endif
243  static AT zero=0;
244 
245  return ((i-j>kl) || (i-j<-ku)) ? zero : ele[ku+i+j*(kl+ku)];
246  };
247 
253  AT* operator()() {return ele;};
254 
259  const AT* operator()() const {return ele;};
260 
265  int size() const {return n;};
266 
271  int rows() const {return n;};
272 
277  int cols() const {return n;};
278 
286  const CBLAS_ORDER blasOrder() const {
287  return CblasColMajor;
288  };
289 
294  const Vector<AT> operator()(int i) const;
295 
303  Vector<AT> operator()(int i);
304 
310  Matrix<GeneralBand, AT>& resize() {n=0;return *this;};
311 
317  Matrix<GeneralBand, AT> copy() const;
318 
326  Matrix<GeneralBand, AT>& init(const AT &a);
327 
332  operator std::vector<std::vector<AT> >();
333  };
334 
335  template <class AT>
337 
338  if(n==0) {
339  n=A.n;
340  } else {
341 #ifdef FMATVEC_SIZE_CHECK
342  assert(n == A.n);
343 #endif
344  }
345 
346  memory = A.memory;
347  ele = A.ele;
348 
349  return *this;
350  }
351 
352  template <class AT>
354 
355  if(A.rows() == 0 || A.cols() == 0)
356  return *this;
357 
358  if(n==0) {
359  n = A.n;
360  memory.resize(n*n);
361  ele = (AT*)memory.get();
362  } else {
363 #ifdef FMATVEC_SIZE_CHECK
364  assert(n == A.n);
365 #endif
366  }
367 
368  deepCopy(A);
369 
370  return *this;
371  }
372 
373  template <class AT>
375 
376  for(int i=0; i<kl+ku+1; i++)
377  for(int j=0; j<n; j++)
378  ele[i+j*(kl+ku+1)] = val;
379 
380  return *this;
381  }
382 
383  template <class AT>
385 #ifndef FMATVEC_NO_BOUNDS_CHECK
386  assert(i<=ku);
387  assert(i>=-kl);
388 #endif
389 
390  //return Vector<AT>(n-i,ku+kl+1,memory,ele[ku-i + (i>0?i:0)*(kl+ku+1)]);
391  return Vector<AT>(n-abs(i),ku+kl+1,true,memory,ele+ku-i + (i>0?i:0)*(kl+ku+1));
392  //(ku-i,i>0?i:0));
393  }
394 
395  template <class AT>
397 #ifndef FMATVEC_NO_BOUNDS_CHECK
398  assert(i<=ku);
399  assert(i>=-kl);
400 #endif
401 
402  return Vector<AT>(n-abs(i),ku+kl+1,true,memory,ele+ku-i + (i>0?i:0)*(kl+ku+1));
403  //(ku-i,i>0?i:0));
404  }
405 
406  template <class AT>
408 
409  Matrix<GeneralBand, AT> A(n,kl,ku);
410  A.deepCopy(*this);
411 
412  return A;
413  }
414 
415  template <class AT>
417 
418  for(int i=0; i<kl+ku+1; i++)
419  for(int j=0; j<n; j++)
420  ele[i+j*(kl+ku+1)] = A.ele[i+j*(kl+ku+1)];
421  }
422 
423  template <class AT>
424  Matrix<GeneralBand, AT>::operator std::vector<std::vector<AT> >() {
425  std::vector<std::vector<AT> > ret(rows());
426  for(int r=0; r<rows(); r++) {
427  ret[r].resize(cols());
428  for(int c=0; c<cols(); c++)
429  ret[r][c]=operator()(r,c);
430  }
431  return ret;
432  }
433 
434  //extern void Matrix<GeneralBand, double>::deepCopy(const Matrix<GeneralBand, double> &A);
435 }
436 
437 #endif