dune-fem  2.6-git
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 
20 namespace Dune
21 {
26  template<class B>
27  class ColCompMatrix<Fem::SparseRowMatrix<B> >
28  {
29  public:
34 
35  typedef typename Matrix::size_type size_type;
36 
41  explicit ColCompMatrix(const Matrix& mat)
42  {
43  setMatrix(mat);
44  }
45 
48  N_(0), M_(0), Nnz_(0), values_(0), rowindex_(0), colstart_(0)
49  {}
50 
52  virtual ~ColCompMatrix()
53  {
54  if(N_+M_+Nnz_ != 0)
55  free();
56  }
57 
59  size_type N() const
60  {
61  return N_;
62  }
63 
65  size_type nnz() const
66  {
67  return Nnz_;
68  }
69 
71  size_type M() const
72  {
73  return M_;
74  }
75 
77  B* getValues() const
78  {
79  return values_;
80  }
81 
83  int* getRowIndex() const
84  {
85  return rowindex_;
86  }
87 
89  int* getColStart() const
90  {
91  return colstart_;
92  }
93 
94  ThisType& operator=(const Matrix& mat)
95  {
96  if(N_+M_+Nnz_ != 0)
97  free();
98  setMatrix(mat);
99  return *this;
100  }
101 
103  {
104  if(N_+M_+Nnz_ != 0)
105  free();
106  N_=mat.N_;
107  M_=mat.M_;
108  Nnz_=mat.Nnz_;
109  if(M_>0)
110  {
111  colstart_=new int[M_+1];
112  for(size_type i=0; i<=M_; ++i)
113  colstart_[i]=mat.colstart[i];
114  }
115  if(Nnz_>0)
116  {
117  values_ = new B[Nnz_];
118  rowindex_ = new int[Nnz_];
119  for(size_type i=0; i<Nnz_; ++i)
120  values_[i]=mat.values[i];
121  for(size_type i=0; i<Nnz_; ++i)
122  rowindex_[i]=mat.rowindex[i];
123  }
124  return *this;
125  }
126 
128  void free()
129  {
130  delete[] values_;
131  delete[] rowindex_;
132  delete[] colstart_;
133  N_=0;
134  M_=0;
135  Nnz_=0;
136  }
137 
139  virtual void setMatrix(const Matrix& mat)
140  {
141  N_=mat.rows();
142  M_=mat.cols();
143 
144  // count the number of nonzeros per column
145  colstart_=new int[M_+1];
146  for(size_type i=0;i!=(M_+1);++i)
147  colstart_[i]=0;
148  Nnz_=0;
149  size_type count(0);
150  for(size_type i=0;i!=N_;++i)
151  {
152  Nnz_+=mat.numNonZeros(i);
153  for(;count<(mat.numNonZeros()*(i+1));++count)
154  {
155  const auto pairIdx(mat.realValue(count));
156  if(pairIdx.second!=Matrix::defaultCol)
157  ++(colstart_[pairIdx.second+1]);
158  }
159  }
160 
161  // compute the starting positions and compute colstart
162  std::vector<int> tempPos(M_,0);
163  for(size_type i=1;i!=(M_+1);++i)
164  {
165  colstart_[i]+=colstart_[i-1];
166  tempPos[i-1]=colstart_[i-1];
167  }
168 
169  // fill the values and the index arrays
170  values_=new B[Nnz_];
171  rowindex_=new int[Nnz_];
172  count=0;
173  for(size_type i=0;i!=N_;++i)
174  {
175  for(size_type localCount=0;localCount<mat.numNonZeros();++localCount,++count)
176  {
177  const auto pairIdx(mat.realValue(count));
178  if(pairIdx.second!=Matrix::defaultCol)
179  {
180  values_[tempPos[pairIdx.second]]=pairIdx.first;
181  rowindex_[tempPos[pairIdx.second]]=i;
182  ++(tempPos[pairIdx.second]);
183  }
184  }
185  }
186 
187  }
188 
189  private:
190  size_type N_;
191  size_type M_;
192  size_type Nnz_;
193  B* values_;
194  int* rowindex_;
195  int* colstart_;
196  };
197 
198 }
199 #endif // #ifndef DUNE_COLCOMPSPMATRIX_HH
200 
Definition: bindguard.hh:11
Definition: colcompspmatrix.hh:12
Converter for SparseRowMatrix to column-compressed matrix. Specialization for SparseRowMatrix.
Definition: colcompspmatrix.hh:28
int * getRowIndex() const
Get the row indices of the non-zero entries of the matrix.
Definition: colcompspmatrix.hh:83
ColCompMatrix< Fem::SparseRowMatrix< B > > ThisType
The type of the matrix converted.
Definition: colcompspmatrix.hh:31
size_type N() const
Get the number of rows.
Definition: colcompspmatrix.hh:59
ThisType & operator=(const Matrix &mat)
Definition: colcompspmatrix.hh:94
int * getColStart() const
Get the column start indices.
Definition: colcompspmatrix.hh:89
Fem::SparseRowMatrix< B > Matrix
The type of the matrix to convert.
Definition: colcompspmatrix.hh:33
B * getValues() const
Get the non-zero entries of the matrix.
Definition: colcompspmatrix.hh:77
ColCompMatrix()
Empty constructor.
Definition: colcompspmatrix.hh:47
void free()
Free allocated space.
Definition: colcompspmatrix.hh:128
ColCompMatrix(const Matrix &mat)
Constructor that initializes the data.
Definition: colcompspmatrix.hh:41
virtual void setMatrix(const Matrix &mat)
Initialize data from given matrix.
Definition: colcompspmatrix.hh:139
ThisType & operator=(const ThisType &mat)
Definition: colcompspmatrix.hh:102
size_type M() const
Get the number of columns.
Definition: colcompspmatrix.hh:71
virtual ~ColCompMatrix()
Destructor.
Definition: colcompspmatrix.hh:52
Matrix::size_type size_type
Definition: colcompspmatrix.hh:35
size_type nnz() const
Get the number of non zero entries.
Definition: colcompspmatrix.hh:65
SparseRowMatrix.
Definition: spmatrix.hh:86
size_type cols() const
return number of columns
Definition: spmatrix.hh:130
size_type rows() const
return number of rows
Definition: spmatrix.hh:124
std::size_t size_type
matrix index type
Definition: spmatrix.hh:91
std::pair< const field_type, size_type > realValue(size_type index) const
Definition: spmatrix.hh:249
size_type numNonZeros() const
Definition: spmatrix.hh:235