functors.hpp 8.61 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/



/** \file functors.hpp */

#ifndef AMDIS_OPERATIONS_FUNCTOR_HPP
#define AMDIS_OPERATIONS_FUNCTOR_HPP

#include <complex>
#include <cmath>

#include <boost/math/special_functions/cbrt.hpp>
#include <boost/math/special_functions/pow.hpp> 
#include "traits/types.hpp"
#include "operations/meta.hpp"

namespace AMDiS 
{
  struct FunctorBase
  {
    int getDegree() const { return 0; }
    int getDegree(int d0) const { return 0; }
    int getDegree(int d0, int d1) const { return 0; }
    int getDegree(int d0, int d1, int d2) const { return 0; }
    int getDegree(int d0, int d1, int d2, int d3) const { return 0; }
  };

  namespace functors
  {
    /// identity(v) == v
    template<typename T>
    struct identity : FunctorBase
    {
      typedef T result_type;
      int degree(int d0) const { return d0; }

      static T eval(const T& v) { return v; }
      T operator()(const T& v) const { return eval(v); }
    };

    /// constant(v) == val
    template<typename T>
    struct constant : FunctorBase
    {
      typedef T result_type;
      constant(T val_) : val(val_) {}

      template<typename V>
      result_type operator()(const V& v) const { return val; }

    private:
      T val;
    };
73
    
Praetorius, Simon's avatar
Praetorius, Simon committed
74
    template<typename T, typename S=T>
75
76
77
78
79
80
81
82
83
    struct add_constant : FunctorBase
    {
      typedef T result_type;
      S value;
      add_constant(S value) : value(value) {}
      
      result_type& operator()(T& v) { return (v += value); }
    };
    
Praetorius, Simon's avatar
Praetorius, Simon committed
84
    template<typename T, typename S=T>
85
86
87
88
89
90
91
92
93
    struct minus_constant : FunctorBase
    {
      typedef T result_type;
      S value;
      minus_constant(S value) : value(value) {}
      
      result_type& operator()(T& v) { return (v -= value); }
    };
    
Praetorius, Simon's avatar
Praetorius, Simon committed
94
    template<typename T, typename S=T>
95
96
97
98
99
100
101
102
103
    struct mult_constant : FunctorBase
    {
      typedef T result_type;
      S value;
      mult_constant(S value) : value(value) {}
      
      result_type& operator()(T& v) { return (v *= value); }
    };
    
Praetorius, Simon's avatar
Praetorius, Simon committed
104
    template<typename T, typename S=T>
105
106
107
108
109
110
111
112
    struct div_constant : FunctorBase
    {
      typedef T result_type;
      S value;
      div_constant(S value) : value(value) {}
      
      result_type& operator()(T& v) { return (v /= value); }
    };
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

    /// functor for operator+=
    template<typename T>
    struct assign : FunctorBase
    {
      typedef T result_type;
      
      static result_type& apply(T& v, T const& v0) { return (v = v0); }
      result_type& operator()(T& v, T const& v0) { return apply(v,v0); }
    };

    /// functor for operator+=
    template<typename T>
    struct add_assign : FunctorBase
    {
      typedef T result_type;
      
      static result_type& apply(T& v, T const& v0) { return (v += v0); }
      result_type& operator()(T& v, T const& v0) { return apply(v,v0); }
    };

    /// functor for operator*=
    template<typename T>
    struct mult_assign : FunctorBase
    {
      typedef T result_type;
      
      static result_type& apply(T& v, T const& v0) { return (v *= v0); }
      result_type& operator()(T& v, T const& v0) { return apply(v,v0); }
    };
143
    
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

    /// abs(v) == |v|
    template<typename T>
    struct abs : FunctorBase
    {
      typedef T result_type;
      int getDegree(int d0) const { return d0; }

      static result_type eval(const T& v) { return std::abs(v); }
      result_type operator()(const T &v) const { return eval(v); }
    };

    template<typename T>
    struct abs<std::complex<T> > : FunctorBase
    {
      typedef T result_type;
      int getDegree(int d0) const { return d0; }

      static result_type eval(const T& v) { return std::norm(v); }
      result_type operator()(const T &v) const { return eval(v); }
    };

    /// max(a,b)
    template<typename T>
    struct max : FunctorBase
    {
      typedef T result_type;
      int getDegree(int d0) const { return d0; }

      static result_type eval(const T& v0, const T& v1) { return std::max(v0, v1); }
      result_type operator()(const T &v0, const T& v1) const { return eval(v0,v1); }
    };

    /// min(a,b)
    template<typename T>
    struct min : FunctorBase
    {
      typedef T result_type;
      int getDegree(int d0) const { return d0; }

      static result_type eval(const T& v0, const T& v1) { return std::min(v0, v1); }
      result_type operator()(const T &v0, const T& v1) const { return eval(v0,v1); }
    };

    /// max(|a|,|b|)
    template<typename T>
    struct abs_max : FunctorBase
    {
      typedef T result_type;
      int getDegree(int d0) const { return d0; }

      static result_type eval(const T& v0, const T& v1) { return std::max(std::abs(v0), std::abs(v1)); }
      result_type operator()(const T &v0, const T& v1) const { return eval(v0,v1); }
    };

    /// min(|a|,|b|)
    template<typename T>
    struct abs_min : FunctorBase
    {
      typedef T result_type;
      int getDegree(int d0) const { return d0; }

      static result_type eval(const T& v0, const T& v1) { return std::min(std::abs(v0), std::abs(v1)); }
      result_type operator()(const T &v0, const T& v1) const { return eval(v0,v1); }
    };
    

    /// apply a functor N times
    template<typename Functor, int N>
    struct apply
    {
      typedef typename Functor::result_type result_type;

      apply(const Functor& f_) : f(f_), inner(f_) {}
      int getDegree(int d0) const
      {
        return f.getDegree(inner.getDegree(d0));
      }

      template<typename V>
      static result_type eval(const V& v) { return Functor::eval(apply<Functor, N-1>::eval(v)); }
      template<typename V>
      result_type operator()(const V& v) const 
      {
	return f(inner(v));
      }

    private:
      const Functor& f;
      apply<Functor, N-1> inner; 
    };

    template<typename Functor>
    struct apply<Functor, 0>
    {
      typedef typename Functor::result_type result_type;

      apply(const Functor& f_) : f(f_) {}
      int getDegree(int d0) const { return d0; }

      template<typename V>
      static result_type eval(const V& v) { return v; }
      template<typename V>
      result_type operator()(const V& v) const { return v; }

    private:
      const Functor& f;
    };

    /// pow<p>(v) == v^p
    template<int p, typename T>
    struct pow : FunctorBase
    {
      typedef T result_type;
      int getDegree(int d0) const { return p*d0; }

      static result_type eval(const T& v) { return boost::math::pow<p>(v); }
      result_type operator()(const T& v) const { return eval(v); }
    };
  
    /// root<p>(v) == p-th-root(v)
    template<int p, typename T, typename Enabled = void>
    struct root_dispatch;

    template<int p, typename T>
    struct root : FunctorBase
    {
      typedef T result_type;
      int getDegree(int d0) const { return p*d0; } // optimal polynomial approximation degree ?

      static result_type eval(const T& v) { return root_dispatch<p,T>::eval(v); }
      result_type operator()(const T& v) const { return eval(v); }
    };

    template<int p, typename T, typename Enabled>
    struct root_dispatch { static T eval(const T& v) { return std::pow(v, 1.0/p); } };

    template<int p, typename T>
    struct root_dispatch<p, T, typename boost::enable_if<typename meta::is_power_of<p, 3>::type>::type> 
    {
      static T eval(const T& v) { return apply<root<3, T>, meta::log<p, 3>::value>::eval(v); }
    };
  
    template<int p, typename T>
    struct root_dispatch<p, T, typename boost::enable_if<typename meta::is_power_of<p, 2>::type>::type> 
    {
      static T eval(const T& v) { return apply<root<2, T>, meta::log<p, 2>::value>::eval(v); }
    };
  
    template<typename T>
    struct root_dispatch<3, T> { static T eval(const T& v) { return boost::math::cbrt(v); } };

    template<typename T>
    struct root_dispatch<2, T> { static T eval(const T& v) { return std::sqrt(v); } };
  
    template<typename T>
    struct root_dispatch<1, T> { static T eval(const T& v) { return v; } };
  
    template<typename T>
    struct root_dispatch<0, T> { static T eval(const T& v) { return 1.0; } };

  } // end namespace functors

} // end namespace AMDiS

#endif // AMDIS_OPERATIONS_FUNCTOR_HPP