dune-fem  2.4.1-rc
colcompspmatrix.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_COLCOMPSPMATRIX_HH
4 #define DUNE_COLCOMPSPMATRIX_HH
5 
6 #if HAVE_DUNE_ISTL
7 #include <dune/istl/colcompmatrix.hh>
8 #else
9 namespace Dune
10 {
11  template<class M>
12  struct ColCompMatrix {};
13 } // namespace Dune
14 #endif
15 
17 
18 #include <vector>
19 #include <utility>
20 
21 namespace Dune
22 {
27  template<class B>
28  class ColCompMatrix<Fem::SparseRowMatrix<B> >
29  {
30  public:
35 
36  typedef int size_type;
37 
42  explicit ColCompMatrix(const Matrix& mat)
43  {
44  setMatrix(mat);
45  }
46 
49  N_(0), M_(0), Nnz_(0), values_(0), rowindex_(0), colstart_(0)
50  {}
51 
53  virtual ~ColCompMatrix()
54  {
55  if(N_+M_+Nnz_ != 0)
56  free();
57  }
58 
60  size_type N() const
61  {
62  return N_;
63  }
64 
66  size_type nnz() const
67  {
68  return Nnz_;
69  }
70 
72  size_type M() const
73  {
74  return M_;
75  }
76 
78  B* getValues() const
79  {
80  return values_;
81  }
82 
84  int* getRowIndex() const
85  {
86  return rowindex_;
87  }
88 
90  int* getColStart() const
91  {
92  return colstart_;
93  }
94 
95  ThisType& operator=(const Matrix& mat)
96  {
97  if(N_+M_+Nnz_ != 0)
98  free();
99  setMatrix(mat);
100  return *this;
101  }
102 
103  ThisType& operator=(const ThisType& mat)
104  {
105  if(N_+M_+Nnz_ != 0)
106  free();
107  N_=mat.N_;
108  M_=mat.M_;
109  Nnz_=mat.Nnz_;
110  if(M_>0)
111  {
112  colstart_=new int[M_+1];
113  for(int i=0; i<=M_; ++i)
114  colstart_[i]=mat.colstart[i];
115  }
116  if(Nnz_>0)
117  {
118  values_ = new B[Nnz_];
119  rowindex_ = new int[Nnz_];
120  for(int i=0; i<Nnz_; ++i)
121  values_[i]=mat.values[i];
122  for(int i=0; i<Nnz_; ++i)
123  rowindex_[i]=mat.rowindex[i];
124  }
125  return *this;
126  }
127 
129  void free()
130  {
131  delete[] values_;
132  delete[] rowindex_;
133  delete[] colstart_;
134  N_=0;
135  M_=0;
136  Nnz_=0;
137  }
138 
140  virtual void setMatrix(const Matrix& mat)
141  {
142  N_=mat.rows();
143  M_=mat.cols();
144 
145  // count the number of nonzeros per column
146  colstart_=new int[M_+1];
147  for(int i=0;i!=(M_+1);++i)
148  colstart_[i]=0;
149  Nnz_=0;
150  int count(0);
151  for(int i=0;i!=N_;++i)
152  {
153  Nnz_+=mat.numNonZeros(i);
154  while(count<(mat.numNonZeros()*(i+1)))
155  {
156  const std::pair<B,int> pairIdx(mat.realValue(count));
157  if(pairIdx.second>-1)
158  ++(colstart_[pairIdx.second+1]);
159  ++count;
160  }
161  }
162 
163  // compute the starting positions and compute colstart
164  std::vector<int> tempPos(M_,0);
165  for(int i=1;i!=(M_+1);++i)
166  {
167  colstart_[i]+=colstart_[i-1];
168  tempPos[i-1]=colstart_[i-1];
169  }
170 
171  // fill the values and the index arrays
172  values_=new B[Nnz_];
173  rowindex_=new int[Nnz_];
174  count=0;
175  for(int i=0;i!=N_;++i)
176  {
177  int localCount(0);
178  while(localCount<mat.numNonZeros())
179  {
180  const std::pair<B,int> pairIdx(mat.realValue(count));
181  if(pairIdx.second>-1)
182  {
183  values_[tempPos[pairIdx.second]]=pairIdx.first;
184  rowindex_[tempPos[pairIdx.second]]=i;
185  ++(tempPos[pairIdx.second]);
186  }
187  ++localCount;
188  ++count;
189  }
190  }
191 
192  }
193 
194  private:
195  size_type N_;
196  size_type M_;
197  size_type Nnz_;
198  B* values_;
199  int* rowindex_;
200  int* colstart_;
201  };
202 
203 }
204 #endif // #ifndef DUNE_COLCOMPSPMATRIX_HH
205 
Fem::SparseRowMatrix< B > Matrix
The type of the matrix to convert.
Definition: colcompspmatrix.hh:34
int cols() const
return number of columns
Definition: spmatrix.hh:246
virtual ~ColCompMatrix()
Destructor.
Definition: colcompspmatrix.hh:53
int size_type
Definition: colcompspmatrix.hh:36
ThisType & operator=(const ThisType &mat)
Definition: colcompspmatrix.hh:103
ColCompMatrix< Fem::SparseRowMatrix< B > > ThisType
The type of the matrix converted.
Definition: colcompspmatrix.hh:32
B * getValues() const
Get the non-zero entries of the matrix.
Definition: colcompspmatrix.hh:78
std::pair< T, int > realValue(int row, int fakeCol)
return pair< value, column >, used by BlockMatrix
Definition: spmatrix.hh:202
ThisType & operator=(const Matrix &mat)
Definition: colcompspmatrix.hh:95
int numNonZeros() const
return max number of non zeros
Definition: spmatrix.hh:249
Definition: colcompspmatrix.hh:12
size_type nnz() const
Get the number of non zero entries.
Definition: colcompspmatrix.hh:66
Definition: coordinate.hh:4
SparseRowMatrix.
Definition: spmatrix.hh:104
ColCompMatrix()
Empty constructor.
Definition: colcompspmatrix.hh:48
int * getRowIndex() const
Get the row indices of the non-zero entries of the matrix.
Definition: colcompspmatrix.hh:84
int rows() const
return number of rows
Definition: spmatrix.hh:243
ColCompMatrix(const Matrix &mat)
Constructor that initializes the data.
Definition: colcompspmatrix.hh:42
size_type N() const
Get the number of rows.
Definition: colcompspmatrix.hh:60
int * getColStart() const
Get the column start indices.
Definition: colcompspmatrix.hh:90
size_type M() const
Get the number of columns.
Definition: colcompspmatrix.hh:72
virtual void setMatrix(const Matrix &mat)
Initialize data from given matrix.
Definition: colcompspmatrix.hh:140
void free()
Free allocated space.
Definition: colcompspmatrix.hh:129