diagonal.hh

Go to the documentation of this file.
1 
6 #ifndef diagonal_hh
7 #define diagonal_hh
8 
9 #include "matrix.hh"
10 
11 #include <type_traits>
12 #include <typeinfo>
13 
14 namespace concepts {
15 
16  // ******************************************************** DiagonalMatrix **
17 
23  template<typename F>
24  class DiagonalMatrix : public Matrix<F> {
25  public:
27  typedef typename Realtype<F>::type r_type;
29  typedef typename Cmplxtype<F>::type c_type;
31  typedef typename std::conditional<std::is_same<typename Realtype<F>::type, F>::value ,
32  typename Realtype<F>::type, typename Cmplxtype<F>::type >::type d_type;
33 
35  template<class G>
36  DiagonalMatrix(const Space<G>& spc);
37  DiagonalMatrix(uint dim=0);
41  template<class G>
42  DiagonalMatrix(const Space<G>& spc, const Array<F> entries);
46  DiagonalMatrix(const Matrix<F>& matrix);
47  virtual ~DiagonalMatrix();
48 
49  virtual void operator()(const Function<r_type>& fncY,
50  Function<F>& fncX);
51  virtual void operator()(const Function<c_type>& fncY,
52  Function<c_type>& fncX);
53 
54  virtual F operator()(const uint i, const uint j) const;
55  virtual F& operator()(const uint i, const uint j);
56 
57  virtual void transpMult(const Vector<r_type>& fncY, Vector<F>& fncX);
58  virtual void transpMult(const Vector<c_type>& fncY,
59  Vector<c_type>& fncX);
60 
63 
64  //Resize to a nxn diagonal matrix
65  void resize(uint n);
66 
68  void zeros() { entries_.zeros(); }
69 
71  operator F*() { return (F*)entries_; }
72 
75 
80  template<class H, class I>
81  void addInto(Matrix<H>& dest, const I fact);
82  protected:
83  virtual std::ostream& info(std::ostream& os) const;
84  private:
88  F dummy_;
89 
90  template<typename H, typename I>
91  void apply_(const Function<H>& fncY, Function<I>& fncX);
92 
93  template<typename H, typename I>
94  void applyT_(const Vector<H>& fncY, Vector<I>& fncX);
95  };
96 
97  template<typename F>
98  template<typename H, typename I>
100  conceptsAssert(fncY.dim() == this->dimY(), Assertion());
101  conceptsAssert(fncX.dim() == this->dimX(), Assertion());
102  const Vector<H>* vecY = dynamic_cast<const Vector<H>*>(&fncY);
103  Vector<I>* vecX = dynamic_cast<Vector<I>*>(&fncX);
104  if ((vecY != 0) && (vecY != 0)) {
105  applyT_(*vecY, *vecX);
106  return;
107  }
108 
109  // dynamic_casts did not work
110  for (uint i = 0; i < this->dimX(); ++i)
111  fncX(i) = fncY(i) * entries_[i];
112  }
113 
114  template<typename F>
115  template<typename H, typename I>
117  {
118  const H* vecYdata(fncY);
119  I* vecXdata(fncX);
120  const F* entryData(entries_);
121  for (uint i = 0; i < this->dimX(); ++i)
122  *vecXdata++ = *vecYdata++ * *entryData++;
123  }
124 
125  template<typename F>
126  template<class H, class I>
127  void DiagonalMatrix<F>::addInto(Matrix<H>& dest, const I fact) {
128  conceptsAssert(dest.dimX() == dest.dimY(), Assertion());
129  conceptsAssert(this->dimX() == dest.dimX(), Assertion());
130  for (uint j = 0; j < this->dimX(); ++j)
131  dest(j, j) += entries_[j] * fact;
132  }
133 
134  // ******************************************************** DiagonalSolver **
135 
141  template <typename F>
142  class DiagonalSolver : public Operator<F> {
143  public:
145  typedef typename Realtype<F>::type r_type;
147  typedef typename Cmplxtype<F>::type c_type;
148 
153  virtual ~DiagonalSolver();
154 
155  virtual void operator()(const Function<r_type>& rhs, Function<F>& sol);
156  virtual void operator()(const Function<c_type>& rhs,
157  Function<c_type>& sol);
159  void operator()(const Matrix<r_type>& mX, Matrix<F>& mY);
162 
163  protected:
164  virtual std::ostream& info(std::ostream& os) const;
165  private:
167 
168  template <typename H, typename I>
169  void apply_(const Matrix<H>& mX, Matrix<I>& mY);
170  };
171 
172  // This construction allow the solving of real right hand side for
173  // both, real and complex diagonal matrices.
174  template <typename F>
176  Function<F>& sol) {
177  inverse_(rhs, sol);
178  }
179 
180  // This construction allow the solving of complex right hand side for
181  // both, real and complex diagonal matrices.
182  template <typename F>
184  Function<c_type>& sol) {
185  inverse_(rhs, sol);
186  }
187 
188  template <typename F>
189  template <typename H, typename I>
191  // number of rows
192  const uint n = this->dimX();
193 
194  // number of rows should be the same as for the solver
195  conceptsAssert(mX.dimX() == n, Assertion());
196  conceptsAssert(mY.dimX() == n, Assertion());
197  // the resulting matrix should have the same number of columns
198  // then the applicated matrix
199  conceptsAssert(mX.dimY() == mY.dimY(), Assertion());
200 
201  for(uint i = 0; i < n; i++) {
202  F& d = inverse_(i,i);
203  for(uint j = 0; j < n; j++) {
204  const H val = mX(i,j);
205  if (val != (H)0.0)
206  mY(i,j) += d * val;
207  }
208  }
209  }
210 
211 } // namespace concepts
212 
213 #endif // diagonal_hh
void operator()(const Matrix< c_type > &mX, Matrix< c_type > &mY)
Application method to complex matrices.
Realtype< F >::type r_type
Real type of data type.
Definition: diagonal.hh:145
void zeros()
Fills the matrix diagonals with zeros.
Definition: diagonal.hh:68
Abstract class for a function.
Definition: basis.hh:21
virtual void operator()(const Function< r_type > &fncY, Function< F > &fncX)
Computes fncX = A(fncY) where A is this matrix.
DiagonalMatrix(const Space< G > &spc, const Array< F > entries)
Constructor.
A solver for diagonal matrices.
Definition: diagonal.hh:142
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
virtual void operator()()
Application operator without argument.
DiagonalMatrix< F > & operator=(F c)
Assignement operator.
An array of objects.
Definition: bilinearForm.hh:23
Exception class for assertions.
Definition: exceptions.hh:258
DiagonalMatrix(const Space< G > &spc)
Constructor. Creates a diagonal matrix with all entries set to 0.
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: diagonal.hh:32
DiagonalMatrix< F > & operator+=(const Matrix< F > &d)
Addition operator.
DiagonalMatrix(const Matrix< F > &matrix)
Constructor.
Abstract class for an operator.
Definition: compositions.hh:31
DiagonalMatrix< F > inverse_
Definition: diagonal.hh:166
virtual F operator()(const uint i, const uint j) const
Returns entry with indices i and j.
Abstract class for an operator.
Definition: ARPACK.hh:16
virtual std::ostream & info(std::ostream &os) const
void apply_(const Matrix< H > &mX, Matrix< I > &mY)
Definition: diagonal.hh:190
Diagonal matrix.
Definition: diagonal.hh:24
virtual std::ostream & info(std::ostream &os) const
Cmplxtype< F >::type c_type
Complex type of data type.
Definition: diagonal.hh:29
Cmplxtype< F >::type c_type
Real type of data type.
Definition: diagonal.hh:147
virtual void transpMult(const Vector< c_type > &fncY, Vector< c_type > &fncX)
uint dim() const
Returns the dimension of the function.
Definition: basis.hh:53
void addInto(Matrix< H > &dest, const I fact)
This matrix is added to the given matrix dest.
Definition: diagonal.hh:127
DiagonalSolver(const DiagonalMatrix< F > &m)
Constructor.
virtual void operator()(const Function< c_type > &fncY, Function< c_type > &fncX)
void apply_(const Function< H > &fncY, Function< I > &fncX)
Definition: diagonal.hh:99
void applyT_(const Vector< H > &fncY, Vector< I > &fncX)
Definition: diagonal.hh:116
std::complex< F > type
Definition: typedefs.hh:116
Realtype< F >::type r_type
Real type of data type.
Definition: diagonal.hh:27
void operator()(const Matrix< r_type > &mX, Matrix< F > &mY)
Application method to real matrices.
virtual void transpMult(const Vector< r_type > &fncY, Vector< F > &fncX)
Computes fncX = AT fncY where A is this matrix.
virtual F & operator()(const uint i, const uint j)
Returns and allows access to entry with indices i and j.
Array< F > entries_
Diagonal entries of the matrix.
Definition: diagonal.hh:86
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
F dummy_
Dummy entry for the access to the off diagonal entries.
Definition: diagonal.hh:88
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich