compositions.hh

Go to the documentation of this file.
1 
6 #ifndef compositions_hh
7 #define compositions_hh
8 
10 #include "basics/exceptions.hh"
11 #include "space/space.hh"
12 #include "function/basis.hh"
13 #include "function/vector.hh"
14 #include "basics/debug.hh"
15 
16 // debugging
17 #define ComposeConstr_D 0
18 #define ComposeAppl_D 0
19 #define SchurComplContr_D 0
20 
21 namespace concepts {
22 
23  // forward declaration
24  template<class F>
25  class SparseMatrix;
26 
27  template<class F>
28  class DenseMatrix;
29 
30  template<class F>
31  class Matrix;
32 
33  template<class F>
35 
36  // ************************************************************** Operator **
37 
41  template<class F>
42  class Operator : public virtual OutputOperator {
43  public:
45  typedef F type;
47  typedef typename Realtype<F>::type r_type;
49  typedef typename Cmplxtype<F>::type c_type;
50 
51  Operator(uint dimX, uint dimY) : dimX_(dimX), dimY_(dimY) {}
52  virtual ~Operator() {}
53 
69  virtual void operator()(const Function<r_type>& fncY, Function<F>& fncX);
84  virtual void operator()(const Function<c_type>& fncY, Function<c_type>& fncX);
85 
88  virtual void operator()();
89 
93  virtual inline const uint dimX() const { return dimX_; }
94 
98  virtual inline const uint dimY() const { return dimY_; }
99 
100  virtual void show_messages() { }
101  protected:
102  virtual std::ostream& info(std::ostream& os) const;
104  uint dimX_, dimY_;
105  };
106 
107  // *********************************************************** VecOperator **
108 
114  template<class F>
115  class VecOperator : public Operator<F> {
116  public:
118  typedef typename Realtype<F>::type r_type;
120  typedef typename Cmplxtype<F>::type c_type;
121 
122  VecOperator(uint dimX, uint dimY) : Operator<F>(dimY, dimX) {}
123 
124  virtual void operator()(const Function<r_type>& fncY, Function<F>& fncX);
125  virtual void operator()(const Function<c_type>& fncY,
126  Function<c_type>& fncX);
127 
143  virtual void operator()(const Vector<r_type>& fncY, Vector<F>& fncX);
158  virtual void operator()(const Vector<c_type>& fncY, Vector<c_type>& fncX);
159 
161  void operator()(const Matrix<r_type>& mX, Matrix<F>& mY);
165  void operator()();
166  protected:
167  virtual std::ostream& info(std::ostream& os) const;
171  virtual void apply_(const Vector<F>& fncY, Vector<F>& fncX) = 0;
174  virtual void apply_() = 0;
175  };
176 
177  // *************************************************************** Compose **
178 
189  template<class F, class H = F>
190  class Compose : public Operator<F> {
191  public:
194 
197  inline void operator()(const Function<F>& fncY, Function<F>& fncX);
198 
207  bool collapse(Matrix<F>& dest) const;
208  protected:
209  virtual std::ostream& info(std::ostream& os) const;
210  private:
217 
219  };
220 
221  template<class F, class H>
223  : Operator<F>(A.dimX(), B.dimY()), A_(A), B_(B), f_(B.dimX()) {
225  "A is (" << A.dimX() << "x" << A.dimY() << ") *" <<
226  "B is (" << B.dimX() << "x" << B.dimY() << ")");
227  conceptsAssert(A.dimY() == B.dimX(), Assertion());
228  }
229 
230  template<class F, class H>
232  {
233  DEBUGL(ComposeAppl_D, *this);
234  conceptsAssert3(fncY.dim() == this->dimY_, Assertion(),
235  "fncY.dim = " << fncY.dim() << ", dimY = " << this->dimY());
236  conceptsAssert3(fncX.dim() == this->dimX(), Assertion(),
237  "fncX.dim = " << fncX.dim() << ", dimX = " << this->dimX());
238  DEBUGL(ComposeAppl_D, "y = " << fncY);
239  B_(fncY, f_);
240  DEBUGL(ComposeAppl_D, "f = " << f_);
241  A_(f_, fncX);
242  DEBUGL(ComposeAppl_D, "x = " << fncX);
243  }
244 
245  // ************************************************************** Multiple **
246 
250  template<class F>
251  class Multiple : public Operator<F> {
252  public:
254  typedef typename Realtype<F>::type r_type;
256  typedef typename Cmplxtype<F>::type c_type;
257 
259  : Operator<F>(A.dimX(), A.dimY()), A_(A), a_(a) {}
260 
268  inline void operator()(const Function<r_type>& fncY, Function<F>& fncX)
269  {
270  apply_(fncY, fncX);
271  }
272  inline void operator()(const Function<c_type>& fncY,
273  Function<c_type>& fncX) {
274  apply_(fncY, fncX);
275  }
276 
277  protected:
278  virtual std::ostream& info(std::ostream& os) const;
279  private:
283  F a_;
284 
285  template<class H, class I>
286  inline void apply_(const Function<H>& fncY, Function<I>& fncX);
287  };
288 
289  template<class F>
290  template<class H, class I>
291  void Multiple<F>:: apply_(const Function<H>& fncY, Function<I>& fncX) {
292  conceptsAssert(fncY.dim() == this->dimY(), Assertion());
293  conceptsAssert(fncX.dim() == this->dimX(), Assertion());
294  A_(fncY, fncX);
295  if (a_ != 1.0)
296  fncX *= a_;
297  }
298 
299  // ***************************************************************** LiCoI **
300 
305  template<class F>
306  class LiCoI : public Operator<F> {
307  public:
309  typedef typename Realtype<F>::type r_type;
311  typedef typename Cmplxtype<F>::type c_type;
312 
313  LiCoI(Operator<F>& A, F a = 1.0, F b = 1.0)
314  : Operator<F>(A.dimX(), A.dimY()), A_(A), a_(a), b_(b) {}
315 
323  virtual void operator()(const Function<r_type>& fncY, Function<F>& fncX);
324  virtual void operator()(const Function<c_type>& fncY,
325  Function<c_type>& fncX);
326 
343  bool collapse(Matrix<F>& dest, const F fact = 1.0) const;
344  protected:
345  virtual std::ostream& info(std::ostream& os) const;
346  private:
350  F a_;
352  F b_;
353  };
354 
355  // ****************************************************************** LiCo **
356 
363  template<class F>
364  class LiCo : public Operator<F> {
365  public:
367  typedef typename Realtype<F>::type r_type;
369  typedef typename Cmplxtype<F>::type c_type;
370 
379  LiCo(Operator<F>& A, Operator<F>& B, F a = 1.0, F b = 1.0) :
380  Operator<F>(A.dimX(), A.dimY()), A_(A), B_(B), a_(a), b_(b) {
381  conceptsAssert(A.dimX() == B.dimX(), Assertion());
382  conceptsAssert(A.dimY() == B.dimY(), Assertion());
383  }
384 
393  inline void operator()(const Function<r_type>& fncY, Function<F>& fncX)
394  {
395  apply_(fncY, fncX);
396  }
397  inline void operator()(const Function<c_type>& fncY, Function<c_type>& fncX)
398  {
399  apply_(fncY, fncX);
400  }
401 
417  bool collapse(Matrix<F>& dest, const F fact = 1.0) const;
418  protected:
419  virtual std::ostream& info(std::ostream& os) const;
420  private:
426  F a_;
428  F b_;
429 
430  template<class H, class I>
431  inline void apply_(const Function<H>& fncY, Function<I>& fncX);
432  };
433 
434  template<class F>
435  template<class H, class I>
436  void LiCo<F>::apply_(const Function<H>& fncY, Function<I>& fncX) {
437  conceptsAssert(this->dimX() == fncX.dim(), Assertion());
438  conceptsAssert(this->dimY() == fncY.dim(), Assertion());
439  A_(fncY, fncX);
440  fncX *= a_;
441  Vector<I> f(this->dimX()); // Intermediate vector
442  B_(fncY, f);
443  fncX.add(f, b_);
444  }
445 
446  // ************************************************************ SchurCompl **
447 
456  template<class F>
457  class SchurCompl : public LiCo<F> {
458  public:
460  SchurCompl(Operator<F>& A_II_inv, Operator<F>& A_IB,
461  Operator<F>& A_BI, Operator<F>& A_BB);
462  virtual ~SchurCompl() {
463  delete D_; delete C_;
464  }
465  private:
468  };
469 
470  template<class F>
472  Operator<F>& A_BI, Operator<F>& A_BB)
473  : LiCo<F>(A_BB,
474  *(D_ = new Compose<F>(A_BI,
475  *(C_ = new Compose<F>(A_II_inv, A_IB)))
476  ), 1.0, -1.0)
477  {
478  DEBUGL(SchurComplContr_D, "A_II_inv = " <<
479  (DenseMatrix<F>(SparseMatrix<F>(A_II_inv, true))));
480  DEBUGL(SchurComplContr_D, "A_IB = " <<
481  (DenseMatrix<F>(SparseMatrix<F>(A_IB, true))));
482  DEBUGL(SchurComplContr_D, "A_BI = " <<
483  (DenseMatrix<F>(SparseMatrix<F>(A_BI, true))));
484  DEBUGL(SchurComplContr_D, "A_BB = " <<
485  (DenseMatrix<F>(SparseMatrix<F>(A_BB, true))));
486  DEBUGL(SchurComplContr_D, "C_ = AII^-1 * AIB = " <<
487  (DenseMatrix<F>(SparseMatrix<F>(*C_, true))));
488  DEBUGL(SchurComplContr_D, "D_ = ABI * AII^-1 * AIB = " <<
489  (DenseMatrix<F>(SparseMatrix<F>(*D_, true))));
490  DEBUGL(SchurComplContr_D, "S_ = A_BB - A_BI * A_II^-1 * A_IB = " <<
491  (DenseMatrix<F>(SparseMatrix<F>(*this, true))));
492  }
493 
494 } // namespace concepts
495 
496 #endif // compositions_hh
#define ComposeConstr_D
Definition: compositions.hh:17
LiCo(Operator< F > &A, Operator< F > &B, F a=1.0, F b=1.0)
Constructor.
virtual std::ostream & info(std::ostream &os) const
Cmplxtype< F >::type c_type
Real type of data type.
void collapse_(DenseMatrix< F > &A, DenseMatrix< H > &B, Matrix< F > &dest) const
Realtype< F >::type r_type
Real type of data type.
Operator(uint dimX, uint dimY)
Definition: compositions.hh:51
Operator< H > & B_
Second operator.
Realtype< F >::type r_type
Real type of data type.
Definition: compositions.hh:47
void operator()(const Function< c_type > &fncY, Function< c_type > &fncX)
Application operator for complex function fncY.
F type
Type of data, e.g. matrix entries.
Definition: compositions.hh:45
Cmplxtype< F >::type c_type
Real type of data type.
void operator()()
Application method without second argument. Used for parallel solvers.
void operator()(const Function< c_type > &fncY, Function< c_type > &fncX)
Application operator for complex function fncY.
Realtype< F >::type r_type
Real type of data type.
bool collapse(Matrix< F > &dest, const F fact=1.0) const
Collapses the linear combination of matrices into one matrix.
Schur complement.
Abstract class for a function.
Definition: basis.hh:21
Cmplxtype< F >::type c_type
Real type of data type.
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
F b_
Scalar b.
uint dimX_
Dimension of image space and the source space.
Multiple(Operator< F > &A, F a)
virtual std::ostream & info(std::ostream &os) const
virtual void show_messages()
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
void apply_(const Function< H > &fncY, Function< I > &fncX)
virtual void operator()()
Application operator without argument.
virtual std::ostream & info(std::ostream &os) const
F a_
Scalar a.
void operator()(const Matrix< c_type > &mX, Matrix< c_type > &mY)
Application method to complex matrices. Calls apply_()
Operator< F > & A_
Operator A.
#define DEBUGL(doit, msg)
Vector< F > f_
Intermediate vector to store result after application of B.
#define SchurComplContr_D
Definition: compositions.hh:19
Multiple of an operator.
virtual Function< F > & add(const Function< F > &fnc, F a)
Adds a times fnc to this function.
virtual void apply_()=0
Intrinsic application method without argument.
void apply_(const Function< H > &fncY, Function< I > &fncX)
Realtype< F >::type r_type
Real type of data type.
Cmplxtype< F >::type c_type
Real type of data type.
Definition: compositions.hh:49
Exception class for assertions.
Definition: exceptions.hh:258
Abstract class for an operator.
Definition: compositions.hh:31
Computes the product of two operators.
void operator()(const Function< r_type > &fncY, Function< F > &fncX)
Application operator.
Abstract class for an operator.
Definition: ARPACK.hh:16
Linear combination of an operator with the identity.
#define ComposeAppl_D
Definition: compositions.hh:18
Operator< F > & A_
First operator.
Operator< F > & B_
Operator B.
virtual void operator()(const Function< c_type > &fncY, Function< c_type > &fncX)
Application operator for complex function fncY.
bool collapse(Matrix< F > &dest) const
Collapses the composition of matrices into one matrix.
VecOperator(uint dimX, uint dimY)
virtual void operator()(const Function< r_type > &fncY, Function< F > &fncX)
Application operator for real function fncY.
virtual std::ostream & info(std::ostream &os) const
void operator()(const Function< r_type > &fncY, Function< F > &fncX)
Application operator.
virtual void operator()(const Vector< c_type > &fncY, Vector< c_type > &fncX)
Application operator for complex function fncY.
A matrix in sparse storage using hashes.
Definition: compositions.hh:34
virtual void apply_(const Vector< F > &fncY, Vector< F > &fncX)=0
Intrinsic application method, i.e.
Cmplxtype< F >::type c_type
Real type of data type.
virtual void operator()(const Vector< r_type > &fncY, Vector< F > &fncX)
Application operator for real vector fncY.
virtual void operator()(const Function< c_type > &fncY, Function< c_type > &fncX)
Application operator for complex function fncY.
uint dim() const
Returns the dimension of the function.
Definition: basis.hh:53
Abstract class for an operator acting on vectors only, not arbitrary functions.
Compose(Operator< F > &A, Operator< H > &B)
Constructor.
LiCoI(Operator< F > &A, F a=1.0, F b=1.0)
bool collapse(Matrix< F > &dest, const F fact=1.0) const
Collapses the linear combination of matrices into one matrix.
Compose< F > * D_
Operator< F > & A_
Operator A.
virtual void operator()(const Function< r_type > &fncY, Function< F > &fncX)
Application operator.
Realtype< F >::type r_type
Real type of data type.
void operator()(const Matrix< r_type > &mX, Matrix< F > &mY)
Application method to real matrices. Calls function apply()
std::complex< F > type
Definition: typedefs.hh:116
virtual void operator()(const Function< r_type > &fncY, Function< F > &fncX)
Application operator for real function fncY.
virtual const uint dimY() const
Returns the size of the source space of the operator (number of columns of the corresponding matrix)
Definition: compositions.hh:98
#define conceptsAssert3(cond, exc, msg)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:442
Operator< F > & A_
Operator A.
virtual std::ostream & info(std::ostream &os) const
Linear combination of two operators.
virtual std::ostream & info(std::ostream &os) const
void operator()(const Function< F > &fncY, Function< F > &fncX)
Application operator.
Class providing an output operator.
virtual void operator()(const Function< c_type > &fncY, Function< c_type > &fncX)
Application operator for complex function fncY.
SchurCompl(Operator< F > &A_II_inv, Operator< F > &A_IB, Operator< F > &A_BI, Operator< F > &A_BB)
Constructor.
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Compose< F > * C_
Product of A_II^-1 * A_IB or A_BI * A_II^-1 * A_IB respectivly.
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich