hashedSMatrix.hh

Go to the documentation of this file.
1 
6 #ifndef hSparseMatrix_hh
7 #define hSparseMatrix_hh
8 
9 #include <iostream>
10 #include <fstream>
11 #if __GNUC__ == 2
12 # include <float.h>
13 # define EPS DBL_EPSILON
14 #else
15 # include <limits>
16 # define EPS std::numeric_limits<double>::epsilon()
17 #endif
18 #include "basics/typedefs.hh"
19 #include "basics/outputOperator.hh"
20 #include "toolbox/pool.hh"
21 #include "hashedSMatrixIterator.hh"
22 
23 #include <type_traits>
24 #include <typeinfo>
25 
26 namespace concepts {
27 
28  // forward declaration
29  template<class F>
30  class Matrix;
31 
32  template<class F>
33  class DenseMatrix;
34 
35  // **************************************************** HashedSparseMatrix **
36 
39  template <class T>
40  class HashedSparseMatrix {
41  public:
42  typedef T value_type;
44  typedef typename Realtype<T>::type r_type;
46  typedef typename Cmplxtype<T>::type c_type;
48  typedef typename std::conditional<std::is_same<typename Realtype<T>::type, T>::value ,
49  typename Realtype<T>::type, typename Cmplxtype<T>::type >::type d_type;
50 
53 
57  struct Value {
59  uint idx;
60  T val;
61 
71  Value(Value* l, int i, T v = 0.0) { lnk = l; idx = i; val = v; }
72  void* operator new(size_t sz) { return new char[sz]; }
73  void* operator new
75  return pool.alloc(); }
77  Value& operator*=(const T factor) {
78  val *= factor;
79  return *this;
80  }
81  };
82 
88  HashedSparseMatrix(uint r, uint c, uint h);
90 
91  inline Value** Matrix() const { return matrix; } // new
92  inline uint HashBits() const { return hashBits; } // new
93 
100  T& operator()(const uint r, const uint c);
101  T operator()(const uint r, const uint c) const;
102 
104  const uint rows() const { return nofRows; }
106  const uint cols() const { return nofCols; }
107 
109  template<typename F, typename G>
110  void operator()(const F f[], G g[]) const;
111 
114 
119  template<typename F, typename G>
120  void transpMult(const F f[], G g[]) const;
121 
123  uint used() const { return _used; }
124  float memory() const {
125  return sizeof(HashedSparseMatrix<T>) + pool.memory()
126  - sizeof(pool);
127  }
128 
129  void write(std::ostream& ofs) const;
130  void outputMatlab(std::ostream& ofs) const;
131  void outputSparseQR(std::ostream& ofs) const;
132 
134  void multiply(const HashedSparseMatrix<T>* const fact,
135  concepts::Matrix<T>& dest) const;
137  void multiply(const concepts::Matrix<T>* const fact,
138  concepts::Matrix<T>& dest) const;
139 
146  const_iterator begin(uint row = 0) const;
153  iterator begin(uint row = 0);
155  static iterator end();
156 
157  std::ostream& info(std::ostream& os) const;
158 
164  void compress(Real threshold = EPS);
165  private:
167  Value** matrix;
168  uint _used;
169 
171  uint nofRows;
172 
174  uint nofCols;
175 
176  uint hashBits;
177  uint hashMsk;
178 
181 
192  void dropEntry_(Value*& v, Value*& vPrev, const uint r, const uint c);
193  };
194 
195  template<typename T>
196  std::ostream& operator<<(std::ostream& os, const HashedSparseMatrix<T>& o) {
197 #ifdef DEBUG
198  os << std::flush;
199 #endif
200  return o.info(os);
201  }
202 
203  template<class T>
204  template<class F, class G>
205  void HashedSparseMatrix<T>::transpMult(const F f[], G g[]) const {
206  uint r = nofRows << hashBits;
207 
208  while (r--)
209  for(Value* v = matrix[r]; v != NULL; v = v->lnk)
210  g[v->idx] += v->val * f[r >> hashBits];
211  }
212 
213  template<class T>
215  concepts::Matrix<T>& dest) const {
216  conceptsAssert(nofCols == fact->nofRows, Assertion());
217  conceptsAssert(nofRows == dest.dimX(), Assertion());
218  conceptsAssert(fact->nofCols == dest.dimY(), Assertion());
219  uint r = nofRows << hashBits;
220 
221  while (r--) {
222  for (Value* v= matrix[r]; v != 0; v = v->lnk) {
223  for (uint k = 0; k < fact->nofCols; ++k) {
224  Value* w = fact->matrix[v->idx << fact->hashBits | (k & fact->hashMsk)];
225  for (; w != 0 && w->idx != k; w = w->lnk);
226  if (w != 0) {
227  T sum = v->val * w->val;
228  if (sum != (T)0)
229  dest(r >> hashBits, k) += sum;
230  }
231  } // for k
232  } // for v
233  } // while
234  }
235 
236 
237  template<class T>
239  concepts::Matrix<T>& dest) const {
240  conceptsAssert(nofCols == fact->dimX(), Assertion());
241  conceptsAssert(nofRows == dest.dimX(), Assertion());
242  conceptsAssert(fact->dimY() == dest.dimY(), Assertion());
243  uint r = nofRows << hashBits;
244  const uint BnofCols = fact->dimY();
245  while (r--) {
246  for (Value* v= matrix[r]; v != 0; v = v->lnk) {
247  for (uint k = 0; k < BnofCols; ++k) {
248  T sum = v->val * (*fact)(v->idx,k);
249  if (sum != (T)0)
250  dest(r >> hashBits, k) += sum;
251  } // for k
252  } // for v
253  } // while
254  }
255 
256  template<class T>
257  template<class F, class G>
258  void HashedSparseMatrix<T>::operator()(const F f[], G g[]) const {
259  G sum = 0.0;
260  uint r = nofRows << hashBits;
261 
262  while (r--) {
263  for(Value* v = matrix[r]; v != NULL; v = v->lnk)
264  sum += v->val * f[v->idx];
265  if (!(r & hashMsk)) {
266  g[r >> hashBits] = sum;
267  sum = 0.0;
268  }
269  }
270  }
271 
272  // ****************************************************** getNumberofRows **
273 
274  template<class F>
275  uint getNumberofRows(HashedSparseMatrix<F>& m) { return m.rows(); }
276 
277 } // namespace concepts
278 
279 #endif // hSparseMatrix_hh
T operator()(const uint r, const uint c) const
Realtype< T >::type r_type
Real type of data type.
#define EPS
HashedSparseMatrix< T > & operator*=(const T factor)
Multiplies all entrices with a certain constant.
std::ostream & info(std::ostream &os) const
void write(std::ostream &ofs) const
void transpMult(const F f[], G g[]) const
Multiplies the transpose of the matrix with f and adds the results to g.
Value(Value *l, int i, T v=0.0)
Constructor.
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
void operator=(const HashedSparseMatrix< T > &)
const_iterator begin(uint row=0) const
Returns constant iterator over the entrances.
void multiply(const HashedSparseMatrix< T > *const fact, concepts::Matrix< T > &dest) const
Multiplies this matrix and fact and adds the result to dest.
_HashedSMatrix_iterator< T, const T &, const T * > const_iterator
void outputMatlab(std::ostream &ofs) const
_HashedSMatrix_iterator< T, T &, T * > iterator
Cmplxtype< T >::type c_type
Complex type of data type.
HashedSparseMatrix(uint r, uint c, uint h)
Constructor.
uint nofRows
Number of rows.
Exception class for assertions.
Definition: exceptions.hh:258
void outputSparseQR(std::ostream &ofs) const
const uint rows() const
Number of rows.
Abstract class for an operator.
Definition: compositions.hh:31
std::ostream & operator<<(std::ostream &os, const Level< dim > &c)
uint used() const
Returns the number of used entries in the matrix.
iterator begin(uint row=0)
Returns iterator over the entrances.
static iterator end()
Last entrance of the particular order.
void dropEntry_(Value *&v, Value *&vPrev, const uint r, const uint c)
Removes entry (r,c) from the matrix.
A matrix in sparse storage using hashes.
Definition: compositions.hh:34
HashedSparseMatrix(const HashedSparseMatrix< T > &)
const uint cols() const
Number of columns.
uint nofCols
Number of columns.
void multiply(const concepts::Matrix< T > *const fact, concepts::Matrix< T > &dest) const
Multiplies this matrix and fact and adds the result to dest.
Values of a matrix in sparse notation.
void compress(Real threshold=EPS)
Compresses the matrix by dropping small entries.
Pool for blockwise memory allocation.
Definition: pool.hh:23
Value & operator*=(const T factor)
Mulitply value with a certain constant.
std::complex< F > type
Definition: typedefs.hh:116
uint getNumberofRows(HashedSparseMatrix< F > &m)
STL like iterator for hashed sparse matrices.
T & operator()(const uint r, const uint c)
Returns the entry with index (r,c).
std::conditional< std::is_same< typename Realtype< T >::type, T >::value, typename Realtype< T >::type, typename Cmplxtype< T >::type >::type d_type
Data type, depending if F is real or complex.
double Real
Type normally used for a floating point number.
Definition: typedefs.hh:36
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
void operator()(const F f[], G g[]) const
Multiplies the matrix with f. The result is g.
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich