Arithmetic.hpp 6.08 KB
Newer Older
1
2
3
4
#pragma once

#include <algorithm>

5
6
7
8
#include <amdis/common/Math.hpp>
#include <amdis/common/Concepts.hpp>
#include <amdis/operations/Basic.hpp>
#include <amdis/operations/Composer.hpp>
9
10
11
12
13
14
15
16
17

namespace AMDiS
{
  namespace Operation
  {
    /** \addtogroup operations
     *  @{
     **/

18
    /// Functor that represents A+B
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    struct Plus
    {
#ifdef AMDIS_HAS_CXX_FOLD_EXPRESSIONS
      template <class... Ts>
      constexpr auto operator()(Ts const&... ts) const
      {
        return (ts + ...);
      }
#else
      template <class T0, class T1>
      constexpr auto operator()(T0 const& t0, T1 const& t1) const
      {
        return t0 + t1;
      }

      template <class T0, class... Ts>
      constexpr auto operator()(T0 const& t0, Ts const&... ts) const
      {
        return t0 + operator()(ts...);
      }
#endif
    };

42
43
#ifndef DOXYGEN
    // [0] + g => g
44
45
46
47
    template <class G>
    struct ComposerBuilder<Plus, Zero, G>
      : ComposerBuilder<Plus, G> {};

48
    // g + [0] => g
49
50
51
52
53
54
55
56
    template <class G>
    struct ComposerBuilder<Plus, G, Zero>
      : ComposerBuilder<Plus, G> {};

    // [0] + [0] => [0]
    template <>
    struct ComposerBuilder<Plus, Zero, Zero>
      : ComposerBuilder<Id, Zero> {};
57
#endif
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

    template <class... Int>
    int order(Plus, Int... orders)
    {
      return Math::max(int(orders)...);
    }

    template <std::size_t I>
    auto partial(Plus, index_t<I>)
    {
      static_assert((I < 2), "Derivatives of `Plus` only defined for the binary case.");
      return One{};
    }

    // -------------------------------------------------------------------------

74
    /// Functor that represents A-B
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    struct Minus
    {
      template <class T, class S>
      constexpr auto operator()(T const& lhs, S const& rhs) const
      {
        return lhs - rhs;
      }

      friend int order(Minus, int lhs, int rhs)
      {
        return Math::max(lhs, rhs);
      }

      friend auto partial(Minus, index_t<0>)
      {
        return One{};
      }

      friend auto partial(Minus, index_t<1>)
      {
        return StaticConstant<int,-1>{};
      }
    };

99
#ifndef DOXYGEN
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    // [0] - g => -g
    // template <class G>
    // struct ComposerBuilder<Minus, Zero, G>
    //   : ComposerBuilder<Id, Negate> {};

    // g - [0] => g
    template <class G>
    struct ComposerBuilder<Minus, G, Zero>
      : ComposerBuilder<Minus, G> {};

    // [0] - [0] => [0]
    template <>
    struct ComposerBuilder<Minus, Zero, Zero>
      : ComposerBuilder<Id, Zero> {};
114
#endif
115
116
117

    // -------------------------------------------------------------------------

118
    /// Functor that represents A*B
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    struct Multiplies
    {
#ifdef AMDIS_HAS_CXX_FOLD_EXPRESSIONS
      template <class... Ts>
      constexpr auto operator()(Ts const&... ts) const
      {
        return (ts * ...);
      }
#else
      template <class T, class S>
      constexpr auto operator()(T const& lhs, S const& rhs) const
      {
        return lhs * rhs;
      }
#endif
    };

136
137
138

#ifndef DOXYGEN
    // [0] * g => [0]
139
140
141
142
    template <class G>
    struct ComposerBuilder<Multiplies, Zero, G>
      : ComposerBuilder<Id, Zero> {};

143
    // g * [0] => [0]
144
145
146
147
148
149
150
151
    template <class G>
    struct ComposerBuilder<Multiplies, G, Zero>
      : ComposerBuilder<Id, Zero> {};

    // [0] * [0] => [0]
    template <>
    struct ComposerBuilder<Multiplies, Zero, Zero>
      : ComposerBuilder<Id, Zero> {};
152
#endif
153
154
155
156
157
158
159
160
161
162
163
164
165
166


    template <class... Int>
    int order(Multiplies, Int... orders)
    {
      return Plus{}(int(orders)...);
    }

    // only for binary *
    // d_0 (x * y) = y, d_1 (x * y) = x
    template <std::size_t I>
    auto partial(Multiplies, index_t<I>)
    {
      static_assert((I < 2), "Derivatives of `Multiplies` only defined for the binary case.");
167
      return Arg<1-I>{};
168
169
170
171
    }

    // -------------------------------------------------------------------------

172
    /// Functor that represents x^p
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
    template <int p>
    struct Pow
    {
      static_assert(p > 1, "Exponent in power must be non-negative!");

      template <class T>
      constexpr auto operator()(T const& x) const
      {
        return Math::pow<p>(x);
      }

      friend int order(Pow, int d)
      {
        return p*d;
      }

      friend auto partial(Pow, index_t<0>)
      {
        return compose(Multiplies{}, StaticConstant<int,p>{}, Pow<p-1>{});
      }
    };

    using Sqr = Pow<2>;

Praetorius, Simon's avatar
Praetorius, Simon committed
197
    /// \see Pow
198
199
200
201
    template <>
    struct Pow<1>
        : public Id {};

Praetorius, Simon's avatar
Praetorius, Simon committed
202
    /// \see Pow
203
204
205
206
    template <>
    struct Pow<0>
        : public Zero {};

Praetorius, Simon's avatar
Praetorius, Simon committed
207
    /// Functor that represents x^p, \see \ref Pow
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
    struct Pow_
    {
      Pow_(int p)
        : p_(p)
      {
        assert( p_ >= 0 );
      }

      template <class T>
      auto operator()(T const& x) const
      {
        return std::pow(x, p_);
      }

      friend int order(Pow_ P, int d)
      {
        return P.p_ * d;
      }

      friend auto partial(Pow_ P, index_t<0>)
      {
        return compose(Multiplies{}, Constant<int>{P.p_}, Pow_{P.p_-1});
      }

      int p_;
    };

235
236
    // -------------------------------------------------------------------------

237
    /// Functor that represents A/B
238
239
240
241
242
243
244
245
246
247
248
    struct Divides
    {
      template <class T, class S>
      constexpr auto operator()(T const& lhs, S const& rhs) const
      {
        return lhs / rhs;
      }

      // d_0 f(x,y) = 1 / y
      friend auto partial(Divides, index_t<0>)
      {
249
        return compose(Divides{}, One{}, Arg<1>{});
250
251
252
253
254
      }

      // d_1 f(x,y) = (y - x)/y^2
      friend auto partial(Divides, index_t<1>)
      {
255
256
        return compose(Divides{}, compose(Minus{}, Arg<1>{}, Arg<0>{}),
                                  compose(Pow<2>{}, Arg<1>{}));
257
258
259
260
261
      }
    };

    // -------------------------------------------------------------------------

262
    /// Functor that represents A-B
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
    struct Negate
    {
      template <class T>
      constexpr auto operator()(T const& x) const
      {
        return -x;
      }

      friend int order(Negate, int d)
      {
        return d;
      }

      friend auto partial(Negate, index_t<0>)
      {
        return StaticConstant<int,-1>{};
      }
    };

    /** @} **/

  } // end namespace Operation
} // end namespace AMDiS