LoAdSG
extempAlg.h
1/**********************************************************************************
2* Author: Christoph Pflaum, Riccarda Scherner-Griesshammer
3 * Department Informatik Lehrstuhl 10 - Systemsimulation
4 * Friedrich-Alexander Universität Erlangen-Nürnberg
5 *
6*********************************************/
7
8#ifndef EXTEMP_SPARSE_double_H
9#define EXTEMP_SPARSE_double_H
10#include "../sgrid/sparseGrid.h"
11#include "../indices/index.h"
12
13// ExprSparseG makes constructor "_a(a)" possible
14// Wrapper Class
15// SiWir Skript -> S.63
16template <class A>
17struct ExprSparseG {
18 // operator () is only defined for const class A& element und returns const
19 inline operator const A&() const {
20 return *static_cast<const A*> ( this );
21 }
22};
23
24
25
26//----------------------------------------------------------------------------
27
28/*
29Exp_Function_Coord
30 */
31class ExpressionDescription {
32 public:
33 ExpressionDescription(bool needsIndex_) : needsIndex(needsIndex_) {};
34 ExpressionDescription(const ExpressionDescription& a, const ExpressionDescription& b) {
35 needsIndex = a.needsIndex || b.needsIndex; };
36 bool isIndexNeeded() { return needsIndex; };
37 private:
38 bool needsIndex;
39};
40
41//----------------------------------------------------------------------------
42template <class A, class B>
43class AddSparseG : public ExprSparseG<AddSparseG<A, B> > {
44 const A& a_;
45 const B& b_;
46
47 public:
48 inline AddSparseG ( const A& a, const B& b ) : a_ ( a ), b_ ( b ) {};
49
50 //inline double getValue(double* data, const IndexDimension& I) const {
51 // return a_.getValue(data,I) + b_.getValue(data,I);
52 //}
53
54
55 inline double getValue(int i, const IndexDimension& I) const {
56 return (a_.getValue(i,I) + b_.getValue(i,I));
57 };
58
59
60
61 ExpressionDescription getDescription() const {
62 return ExpressionDescription(a_.getDescription(),b_.getDescription());
63 };
64 AdaptiveSparseGrid* getSparseGrid() const { return a_.getSparseGrid(); };
65};
66
67//----------------------------------------------------------------------------
68template <class A, class B>
69inline AddSparseG<A, B> operator+ ( const ExprSparseG<A>& a, const ExprSparseG<B>& b ) {
70 return AddSparseG<A, B> ( a, b );
71}
72
73
74
75
76
77
78//----------------------------------------------------------------------------
79template <class A>
80class CAddSparseG : public ExprSparseG<CAddSparseG<A> > {
81 const A& a_;
82 double b_;
83
84 public:
85 inline CAddSparseG(const A& a, double b ) : a_ ( a ), b_ ( b ) {}
86
87
88 inline double getValue(int i, const IndexDimension& I) const {
89 return a_.getValue(i,I) + b_;
90 }
91
92
93
94
95 ExpressionDescription getDescription() const {
96 return a_.getDescription();
97 }
98 AdaptiveSparseGrid* getSparseGrid() const { return a_.getSparseGrid(); }
99};
100
101//----------------------------------------------------------------------------
102template <class A>
103inline CAddSparseG<A>
104operator+(const ExprSparseG<A>& a, double b ) {
105 return CAddSparseG<A> ( a, b );
106}
107
108//----------------------------------------------------------------------------
109template <class A>
110inline CAddSparseG<A>
111operator+(double b, ExprSparseG<A>& a ) {
112 return CAddSparseG<A> ( a, b );
113}
114
115//----------------------------------------------------------------------------
116template <class A>
117inline CAddSparseG<A>
118operator-(ExprSparseG<A>& a, double b ) {
119 return CAddSparseG<A> ( a, -1.0*b );
120}
121
122
123//----------------------------------------------------------------------------
124template <class A, class B>
125
126class SubSparseG : public ExprSparseG<SubSparseG<A, B> > {
127 const A& a_;
128 const B& b_;
129
130 public:
131 inline SubSparseG ( const A& a, const B& b ) : a_ ( a ), b_ ( b ) {}
132
133 inline double getValue(int i, const IndexDimension& I) const {
134 return a_.getValue(i,I) - b_.getValue(i,I);
135 }
136
137
138 ExpressionDescription getDescription() const {
139 return ExpressionDescription(a_.getDescription(),b_.getDescription());
140 }
141 AdaptiveSparseGrid* getSparseGrid() const { return a_.getSparseGrid(); }
142};
143
144//----------------------------------------------------------------------------
145template <class A, class B>
146inline SubSparseG<A, B> operator- ( const ExprSparseG<A>& a, const ExprSparseG<B>& b ) {
147 return SubSparseG<A, B> ( a, b );
148}
149
150//----------------------------------------------------------------------------
151template < class B>
152class CSubSparseG : public ExprSparseG<CSubSparseG< B> > {
153 double a_;
154 const B& b_;
155
156 public:
157 inline CSubSparseG ( double a, const B& b ) : a_ ( a ), b_ ( b ) {}
158
159 inline double getValue(int i, const IndexDimension& I) const {
160 return a_ - b_.getValue(i,I); }
161
162 ExpressionDescription getDescription() const {
163 return b_.getDescription();
164 }
165 AdaptiveSparseGrid* getSparseGrid() const { return b_.getSparseGrid(); }
166};
167
168//----------------------------------------------------------------------------
169template <class B>
170inline CSubSparseG< B> operator- ( double a, const ExprSparseG<B>& b ) {
171 return CSubSparseG< B> ( a, b );
172}
173
174
175//----------------------------------------------------------------------------
176template <class A>
177
178class MinusSparseG : public ExprSparseG<MinusSparseG<A> > {
179 const A& a_;
180
181 public:
182 inline MinusSparseG ( const A& a ) : a_ ( a ) {}
183
184
185 inline double getValue(int i, const IndexDimension& I) const {
186 return -1.0*a_.getValue(i,I);
187 }
188
189 ExpressionDescription getDescription() const {
190 return a_.getDescription();
191 }
192 AdaptiveSparseGrid* getSparseGrid() const { return a_.getSparseGrid(); }
193};
194
195//----------------------------------------------------------------------------
196template <class A>
197inline MinusSparseG<A> operator- ( const ExprSparseG<A>& a ) {
198 return MinusSparseG<A> ( a );
199}
200
201
202//----------------------------------------------------------------------------
203template <class A>
204
205class TimesSparseG : public ExprSparseG<TimesSparseG<A> > {
206 const A& a_;
207 double b_;
208
209 public:
210 inline TimesSparseG ( const A& a, const double& b ) : a_ ( a ), b_ ( b ) {}
211
212
213 inline double getValue(int i, const IndexDimension& I) const {
214 return a_.getValue(i,I) * b_;
215 }
216
217
218 ExpressionDescription getDescription() const {
219 return a_.getDescription();
220 }
221 AdaptiveSparseGrid* getSparseGrid() const { return a_.getSparseGrid(); }
222};
223
224
225//----------------------------------------------------------------------------
226template <class A>
227inline TimesSparseG<A>
228operator* ( const ExprSparseG<A>& a, double b ) {
229 return TimesSparseG<A> ( a, b );
230}
231
232//----------------------------------------------------------------------------
233template <class A>
234inline TimesSparseG<A>
235operator* ( double b, const ExprSparseG<A>& a ) {
236 return TimesSparseG<A> ( a, b );
237}
238
239
240//----------------------------------------------------------------------------
241template < class B>
242
243class DivSparseG : public ExprSparseG<DivSparseG< B> > {
244 const double a_;
245 const B& b_;
246
247 public:
248 inline DivSparseG ( const double& a, const B& b ) : a_ ( a ), b_ ( b ) {}
249
250 inline double getValue(int i, const IndexDimension& I) const {
251 return a_ / b_.getValue(i,I);
252 }
253
254
255 ExpressionDescription getDescription() const {
256 return b_.getDescription();
257 }
258 AdaptiveSparseGrid* getSparseGrid() const { return b_.getSparseGrid(); }
259};
260
261//----------------------------------------------------------------------------
262template <class A>
263inline TimesSparseG<A> operator/ ( const ExprSparseG<A>& a, double b ) {
264 return TimesSparseG<A> ( a, 1.0 / b );
265}
266
267//----------------------------------------------------------------------------
268
269template <class B>
270inline DivSparseG< B> operator/ ( double a, const ExprSparseG<B>& b ) {
271 return DivSparseG< B> ( a, b );
272}
273
274
275//----------------------------------------------------------------------------
276template <class A, class B>
277
278class VTimesSparseG : public ExprSparseG<VTimesSparseG<A, B> > {
279 const A &a_;
280 const B &b_;
281
282public:
283 inline VTimesSparseG(const A &a, const B &b) : a_(a), b_(b) {}
284
285 inline double getValue(int i, const IndexDimension &I) const {
286 if (a_.getValue(i, I) == 0 || b_.getValue(i, I) == 0) return 0.0;
287 return a_.getValue(i, I) * b_.getValue(i, I);
288 };
289
290
291 ExpressionDescription getDescription() const {
292 return ExpressionDescription(a_.getDescription(), b_.getDescription());
293 };
294
295 AdaptiveSparseGrid *getSparseGrid() const { return a_.getSparseGrid(); }
296};
297
298
299//----------------------------------------------------------------------------
300template <class A, class B>
301inline VTimesSparseG<A, B> operator* ( const ExprSparseG<A>& a, const ExprSparseG<B>& b ) {
302 return VTimesSparseG<A, B> ( a, b );
303}
304
305
306//----------------------------------------------------------------------------
307template <class A, class B>
308
309class VDivSparseG : public ExprSparseG<VDivSparseG<A, B> > {
310 const A& a_;
311 const B& b_;
312
313 public:
314 inline VDivSparseG ( const A& a, const B& b ) : a_ ( a ), b_ ( b ) {}
315
316 inline double getValue(int i, const IndexDimension& I) const {
317 return a_.getValue(i,I) / b_.getValue(i,I);
318 };
319
320
321 ExpressionDescription getDescription() const {
322 return ExpressionDescription(a_.getDescription(),b_.getDescription());
323 }
324 AdaptiveSparseGrid* getSparseGrid() const { return a_.getSparseGrid(); }
325};
326
327//----------------------------------------------------------------------------
328template <class A, class B>
329inline VDivSparseG<A, B> operator/ ( const ExprSparseG<A>& a, const ExprSparseG<B>& b ) {
330 return VDivSparseG<A, B> ( a, b );
331}
332
333//----------------------------------------------------------------------------
334
335#endif // EXTEMP_H
Definition sparseGrid.h:277