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 
19 namespace 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 
53  Matrix(uint nofRows, uint nofCols) : Operator<F>(nofRows, nofCols) {}
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); }
73  const_iterator end() const {
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>
109  static void assembly(Matrix<F>& dest, Scan<Element<G> > * sc,
110  const BilinearForm<F,G>& bf, const Real threshold = 0.0);
115  template<class G>
116  static void assembly(Matrix<F>& dest,
117  const Sequence<ElementWithCell<G> * > seq,
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,
145  const Sequence<ElementWithCell<G> * > seqX,
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,
156  const ElementPairList<G>& pairs);
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,
177  Function<c_type>& fncX) = 0;
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:
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
static void assembly(Matrix< F > &dest, Scan< Element< G > > *sc, const BilinearForm< F, G > &bf, const Real threshold=0.0)
Assembly operator for dest using the bilinear form bf.
iterator begin(uint r=0)
Iterator over the elements, standing at position (r,0).
Definition: matrix.hh:64
const_iterator begin(uint r=0) const
Constant iterator over the elements, standing at position (r,0)
Definition: matrix.hh:71
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)
Assembly operator for dest using the bilinear form bf.
STL iterator for matrices.
Cmplxtype< F >::type c_type
Complex type of data type.
Definition: matrix.hh:45
static bool timings()
Returns true if the class is able to do timings.
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)
Assembly operator for dest using the bilinear form bf.
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)
Assembly operator for dest using the bilinear form bf.
virtual void add(const uint i, const uint j, const F value, const bool use_threshold=false, const Real threshold_value=1e-8)
Addition operator Add the value to the entry (i,j) if not zero (for use_threshold = false),...
const uint nofRows() const
Number of rows.
Definition: matrix.hh:56
static void assembly(Matrix< F > &dest, const Sequence< ElementWithCell< G > * > seq, const BilinearForm< F, G > &bf, const Real threshold=0.0)
Assembly operator for dest using the bilinear form bf.
static uint timeCntr_
Counter for timing table.
Definition: matrix.hh:236
std::conditional< std::is_same< typename Realtype< F >::type, F >::value, typename Realtype< F >::type, typename Cmplxtype< F >::type >::type d_type
Data type, depending if F is real or complex.
Definition: matrix.hh:48
virtual void operator()(const Function< c_type > &fncY, Function< c_type > &fncX)=0
Abstract class for a function.
Definition: basis.hh:21
virtual const uint dimX() const
Returns the size of the image space of the operator (number of rows of the corresponding matrix)
Definition: compositions.hh:93
Holds parameters in hashes.
Definition: inputOutput.hh:75
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
Element with cell.
#define DEBUGL(doit, msg)
Abstract function class to evaluate a bilinear form.
Definition: bilinearForm.hh:33
const uint nofCols() const
Number of columns.
Definition: matrix.hh:58
Exception class for assertions.
Definition: exceptions.hh:258
static void assembly(Matrix< F > &dest, const Space< G > &spc, const Sequence< bool > &seq, const BilinearForm< F, G > &bf, const Real threshold=0.0)
Assembly operator for dest using the bilinear form bf on space spc.
Abstract class for an operator.
Definition: compositions.hh:31
An abstract class for scanning a mesh (a set of cells) or a space (a set of elements).
virtual void set(const uint i, const uint j, const F value, const bool use_threshold=false, const Real threshold_value=1e-8)
Affectation operator Affet the value to the entry (i,j) if not zero (for use_threshold = false),...
Realtype< F >::type r_type
Real type of data type.
Definition: matrix.hh:43
Abstract class for an operator.
Definition: ARPACK.hh:16
Sequence with operations, output operator, and method of the particular element types.
Definition: sequence.hh:39
virtual F & operator()(const uint i, const uint j)=0
Returns and allows access to entry with indices i and j.
Matrix(uint nofRows, uint nofCols)
Definition: matrix.hh:53
virtual F operator()(const uint i, const uint j) const =0
Returns entry with indices i and j.
Holds a list of ElementPair and allows to scan over this list.
Definition: elementPairs.hh:41
virtual void transpMult(const Vector< r_type > &fncY, Vector< F > &fncX)=0
Computes fncX = AT fncY where A is this matrix.
static InOutParameters * timings_
Place to store timing values.
Definition: matrix.hh:234
const_iterator end() const
Constant iterator, standing behind last element.
Definition: matrix.hh:73
_Matrix_iterator< F, F &, F * > iterator
Definition: matrix.hh:50
static void assembly(Matrix< F > &dest, const BilinearForm< F, G > &bf, const ElementPairList< G > &pairs)
Assembly operator for dest using the bilinear form bf.
static void setTimings(InOutParameters *timings)
Sets the class to store the timing values in.
virtual bool operator==(const Matrix< F > &otherMat) const
Definition: matrix.hh:179
iterator end()
Iterator, standing behind last element.
Definition: matrix.hh:66
#define ApplyMatrix_D
Definition: matrix.hh:17
void apply(Operator< F > &op, const Matrix< H > &mX, Matrix< I > &mY)
Multiplication with a matrix.
Definition: matrix.hh:256
std::complex< F > type
Definition: typedefs.hh:116
uint getNumberofRows(HashedSparseMatrix< F > &m)
virtual void operator()(const Function< r_type > &fncY, Function< F > &fncX)=0
Computes fncX = A(fncY) where A is this matrix.
static void assembly(Matrix< F > &dest, const Space< G > &spc, const BilinearForm< F, G > &bf, const Real threshold=0.0)
Assembly operator for dest using the bilinear form bf.
virtual void transpMult(const Vector< c_type > &fncY, Vector< c_type > &fncX)=0
_Matrix_iterator< F, const F &, const F * > const_iterator
Definition: matrix.hh:51
double Real
Type normally used for a floating point number.
Definition: typedefs.hh:36
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich