Class documentation of Concepts

Loading...
Searching...
No Matches
matrix.hh
Go to the documentation of this file.
1
6#ifndef matrix_hh
7#define matrix_hh
8
9#include "compositions.hh"
10#include "matrixIterator.hh"
11#include "space/elementPairs.hh"
12#include "toolbox/sequence.hh"
13
14#include <type_traits>
15#include <typeinfo>
16
17#define ApplyMatrix_D 0
18
19namespace concepts {
20
21 // forward declarations
22 template<typename F, typename G>
23 class BilinearForm;
24
25 class InOutParameters;
26
27 // **************************************************************** Matrix **
28
38 template<class F>
39 class Matrix : public Operator<F> {
40 public:
41 typedef F value_type;
43 typedef typename Realtype<F>::type r_type;
45 typedef typename Cmplxtype<F>::type c_type;
47 typedef typename std::conditional<std::is_same<typename Realtype<F>::type, F>::value ,
48 typename Realtype<F>::type, typename Cmplxtype<F>::type >::type d_type;
49
52
54
56 const uint nofRows() const { return this->dimX(); }
58 const uint nofCols() const { return this->dimY(); }
59
64 iterator begin(uint r = 0) { return iterator(*this, r); }
66 iterator end() { return iterator(*this, nofRows(), 0); }
71 const_iterator begin(uint r = 0) const { return const_iterator(const_cast<Matrix<F>&>(*this), r); }
74 return const_iterator(const_cast<Matrix<F>&>(*this), nofRows(), 0);
75 }
76
82 virtual void set(const uint i, const uint j,
83 const F value,
84 const bool use_threshold = false,
85 const Real threshold_value = 1e-8);
86
92 virtual void add(const uint i, const uint j,
93 const F value,
94 const bool use_threshold = false,
95 const Real threshold_value = 1e-8);
96
101 template<class G>
102 static void assembly(Matrix<F>& dest, const Space<G>& spc,
103 const BilinearForm<F,G>& bf, const Real threshold = 0.0);
108 template<class G>
110 const BilinearForm<F,G>& bf, const Real threshold = 0.0);
115 template<class G>
116 static void assembly(Matrix<F>& dest,
118 const BilinearForm<F,G>& bf, const Real threshold = 0.0);
124 template<class G>
125 static void assembly(Matrix<F>& dest,
126 const Space<G>& spc, const Sequence<bool>& seq,
127 const BilinearForm<F,G>& bf, const Real threshold = 0.0);
128
133 template<class G>
134 static void assembly(Matrix<F>& dest,
135 const Space<G>& spcX, const Space<G>& spcY,
136 const BilinearForm<F,G>& bf, const Real threshold = 0.0, const bool single = false);
137
143 template<class G>
144 static void assembly(Matrix<F>& dest,
146 const Space<G>& spcY,
147 const BilinearForm<F,G>& bf, const Real threshold = 0.0);
148
154 template<class G>
155 static void assembly(Matrix<F>& dest, const BilinearForm<F,G>& bf,
162 template<class G>
163 static void assembly(Matrix<F>& dest,
164 const Space<G>& spcX, const Space<G>& spcY, const Sequence<bool>& seq,
165 const BilinearForm<F,G>& bf, const Real threshold = 0.0, const bool single = false);
166
167
169 virtual F operator()(const uint i, const uint j) const = 0;
171 virtual F& operator()(const uint i, const uint j) = 0;
172
174 virtual void operator()(const Function<r_type>& fncY,
175 Function<F>& fncX) = 0;
176 virtual void operator()(const Function<c_type>& fncY,
178
179 virtual bool operator==(const Matrix<F>& otherMat) const {
180 uint rows = nofRows();
181 uint cols = nofCols();
182
183 if( rows != otherMat.nofRows() || cols != otherMat.nofCols() )
184 return false;
185
186 for(uint i=0; i<rows; i++)
187 for(uint j=0; j<cols; j++)
188 if( (*this)(i,j)!=otherMat(i,j) )
189 return false;
190
191 return true;
192 }
193
195 virtual void transpMult(const Vector<r_type>& fncY,
196 Vector<F>& fncX) = 0;
197 virtual void transpMult(const Vector<c_type>& fncY,
198 Vector<c_type>& fncX) = 0;
199
230 static bool timings();
232 private:
234 static InOutParameters* timings_;
236 static uint timeCntr_;
237 };
238
239 // ****************************************************** getNumberofRows **
240
241 template<class F>
242 uint getNumberofRows(F& m) { return m.nofRows(); }
243
244
245 // **************************************************************** apply **
246
255 template<class F, class H, class I>
256 void apply(Operator<F>& op, const Matrix<H>& mX, Matrix<I>& mY) {
257 // number of rows
258 const uint n = op.dimX();
259
260 // number of rows should be the same as for the solver
261 conceptsAssert(mX.dimX() == n, Assertion());
262 conceptsAssert(mY.dimX() == n, Assertion());
263 // the resulting matrix should have the same number of columns
264 // then the applicated matrix
265 conceptsAssert(mX.dimY() == mY.dimY(), Assertion());
266
267 // loop over columns
268 Vector<H> fncX(mX.dimX());
269 Vector<I> fncY(mY.dimX());
270 I& f = fncY(0);
271 for (uint c = 0; c < mX.dimY(); ++c) {
272 // Write column of applicated matrix to vector.
273 // Optimal would be a replacement by a method of the matrix.
274 for (uint r = 0; r < n; ++r)
275 fncX(r) = mX(r, c);
276
277 DEBUGL(ApplyMatrix_D, "fncX = " << fncX);
278 op(fncX, fncY);
279 DEBUGL(ApplyMatrix_D, "fncY = " << fncY);
280
281 // Add vector to column of resulting matrix, could be
282 // Optimal would be a replacement by a method of the matrix.
283 for (uint r = 0; r < n; ++r)
284 if ((f = fncY(r)) != 0.0)
285 mY(r, c) += f;
286 DEBUGL(ApplyMatrix_D, "finished column " << c);
287 }
288 }
289
290} // namespace concepts
291
292#endif // matrix_hh
virtual void add(const uint i, const uint j, const F value, const bool use_threshold=false, const Real threshold_value=1e-8)
const_iterator end() const
Constant iterator, standing behind last element.
Definition matrix.hh:73
static void assembly(Matrix< F > &dest, const Space< G > &spcX, const Space< G > &spcY, const BilinearForm< F, G > &bf, const Real threshold=0.0, const bool single=false)
static void assembly(Matrix< F > &dest, const BilinearForm< F, G > &bf, const ElementPairList< G > &pairs)
iterator begin(uint r=0)
Definition matrix.hh:64
virtual F operator()(const uint i, const uint j) const =0
Returns entry with indices i and j.
virtual F & operator()(const uint i, const uint j)=0
Returns and allows access to entry with indices i and j.
Cmplxtype< F >::type c_type
Complex type of data type.
Definition matrix.hh:45
static void assembly(Matrix< F > &dest, const Sequence< ElementWithCell< G > * > seq, const BilinearForm< F, G > &bf, const Real threshold=0.0)
virtual void set(const uint i, const uint j, const F value, const bool use_threshold=false, const Real threshold_value=1e-8)
iterator end()
Iterator, standing behind last element.
Definition matrix.hh:66
static void assembly(Matrix< F > &dest, const Space< G > &spc, const Sequence< bool > &seq, const BilinearForm< F, G > &bf, const Real threshold=0.0)
static void setTimings(InOutParameters *timings)
static void assembly(Matrix< F > &dest, const Space< G > &spc, const BilinearForm< F, G > &bf, const Real threshold=0.0)
static void assembly(Matrix< F > &dest, Scan< Element< G > > *sc, const BilinearForm< F, G > &bf, const Real threshold=0.0)
static void assembly(Matrix< F > &dest, const Sequence< ElementWithCell< G > * > seqX, const Space< G > &spcY, const BilinearForm< F, G > &bf, const Real threshold=0.0)
static bool timings()
virtual void operator()(const Function< c_type > &fncY, Function< c_type > &fncX)=0
const uint nofCols() const
Number of columns.
Definition matrix.hh:58
std::conditional< std::is_same< typenameRealtype< F >::type, F >::value, typenameRealtype< F >::type, typenameCmplxtype< F >::type >::type d_type
Data type, depending if F is real or complex.
Definition matrix.hh:48
const uint nofRows() const
Number of rows.
Definition matrix.hh:56
Realtype< F >::type r_type
Real type of data type.
Definition matrix.hh:43
static void assembly(Matrix< F > &dest, const Space< G > &spcX, const Space< G > &spcY, const Sequence< bool > &seq, const BilinearForm< F, G > &bf, const Real threshold=0.0, const bool single=false)
virtual void operator()(const Function< r_type > &fncY, Function< F > &fncX)=0
Computes fncX = A(fncY) where A is this matrix.
virtual void transpMult(const Vector< r_type > &fncY, Vector< F > &fncX)=0
Computes fncX = AT fncY where A is this matrix.
const_iterator begin(uint r=0) const
Definition matrix.hh:71
virtual const uint dimY() const
F type
Type of data, e.g. matrix entries.
virtual const uint dimX() const
#define conceptsAssert(cond, exc)
#define DEBUGL(doit, msg)
Definition debug.hh:40
double Real
Definition typedefs.hh:39
Set< F > makeSet(uint n, const F &first,...)
Definition set.hh:320
void apply(Operator< F > &op, const Matrix< H > &mX, Matrix< I > &mY)
Definition matrix.hh:256