Class documentation of Concepts

Loading...
Searching...
No Matches
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"
20#include "toolbox/pool.hh"
22
23#include <type_traits>
24#include <typeinfo>
25
26namespace 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>
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 {
58 Value* lnk;
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(); }
78 val *= factor;
79 return *this;
80 }
81 };
82
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
139
146 const_iterator begin(uint row = 0) const;
155 static iterator end();
156
157 std::ostream& info(std::ostream& os) const;
158
165 private:
166 Pool<Value> pool;
167 Value** matrix;
168 uint _used;
169
171 uint nofRows;
172
174 uint nofCols;
175
176 uint hashBits;
177 uint hashMsk;
178
180 void operator =(const HashedSparseMatrix<T>&);
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
uint used() const
Returns the number of used entries in the matrix.
T & operator()(const uint r, const uint c)
const uint cols() const
Number of columns.
Cmplxtype< T >::type c_type
Complex type of data type.
void multiply(const HashedSparseMatrix< T > *const fact, concepts::Matrix< T > &dest) const
Multiplies this matrix and fact and adds the result to dest.
iterator begin(uint row=0)
static iterator end()
Last entrance of the particular order.
void transpMult(const F f[], G g[]) const
const uint rows() const
Number of rows.
void operator()(const F f[], G g[]) const
Multiplies the matrix with f. The result is g.
Realtype< T >::type r_type
Real type of data type.
void multiply(const concepts::Matrix< T > *const fact, concepts::Matrix< T > &dest) const
Multiplies this matrix and fact and adds the result to dest.
std::conditional< std::is_same< typenameRealtype< T >::type, T >::value, typenameRealtype< T >::type, typenameCmplxtype< T >::type >::type d_type
Data type, depending if F is real or complex.
const_iterator begin(uint row=0) const
HashedSparseMatrix(uint r, uint c, uint h)
HashedSparseMatrix< T > & operator*=(const T factor)
Multiplies all entrices with a certain constant.
void compress(Real threshold=EPS)
#define conceptsAssert(cond, exc)
double Real
Definition typedefs.hh:39
Set< F > makeSet(uint n, const F &first,...)
Definition set.hh:320
Value & operator*=(const T factor)
Mulitply value with a certain constant.
Value(Value *l, int i, T v=0.0)