Class documentation of Concepts

Loading...
Searching...
No Matches
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
14namespace 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>
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,
51 virtual void operator()(const Function<c_type>& fncY,
53
54 virtual F operator()(const uint i, const uint j) const;
55 virtual F& operator()(const uint i, const uint j);
56
58 virtual void transpMult(const Vector<c_type>& fncY,
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:
86 Array<F> entries_;
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>
116 void DiagonalMatrix<F>::applyT_(const Vector<H>& fncY, Vector<I>& fncX)
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>
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);
162
163 protected:
164 virtual std::ostream& info(std::ostream& os) const;
165 private:
166 DiagonalMatrix<F> inverse_;
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
virtual F & operator()(const uint i, const uint j)
Returns and allows access to entry with indices i and j.
virtual void operator()(const Function< c_type > &fncY, Function< c_type > &fncX)
Cmplxtype< F >::type c_type
Complex type of data type.
Definition diagonal.hh:29
virtual F operator()(const uint i, const uint j) const
Returns entry with indices i and j.
DiagonalMatrix(const Space< G > &spc)
Constructor. Creates a diagonal matrix with all entries set to 0.
DiagonalMatrix(const Space< G > &spc, const Array< F > entries)
virtual void transpMult(const Vector< r_type > &fncY, Vector< F > &fncX)
Computes fncX = AT fncY where A is this matrix.
DiagonalMatrix(const Matrix< F > &matrix)
void addInto(Matrix< H > &dest, const I fact)
Definition diagonal.hh:127
virtual void operator()(const Function< r_type > &fncY, Function< F > &fncX)
Computes fncX = A(fncY) where A is this matrix.
void zeros()
Fills the matrix diagonals with zeros.
Definition diagonal.hh:68
Realtype< F >::type r_type
Real type of data type.
Definition diagonal.hh:27
DiagonalMatrix< F > & operator=(F c)
Assignement operator.
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 diagonal.hh:32
DiagonalMatrix< F > & operator+=(const Matrix< F > &d)
Addition operator.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Realtype< F >::type r_type
Real type of data type.
Definition diagonal.hh:145
void operator()(const Matrix< c_type > &mX, Matrix< c_type > &mY)
Application method to complex matrices.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Cmplxtype< F >::type c_type
Real type of data type.
Definition diagonal.hh:147
void operator()(const Matrix< r_type > &mX, Matrix< F > &mY)
Application method to real matrices.
DiagonalSolver(const DiagonalMatrix< F > &m)
F type
Type of data, e.g. matrix entries.
virtual void operator()()
#define conceptsAssert(cond, exc)
Set< F > makeSet(uint n, const F &first,...)
Definition set.hh:320