simplify_expr.hpp 13.2 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
/******************************************************************************
 *
 * 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 simplify_expr.hpp */

#ifndef AMDIS_SIMPLIFY_EXPRESSION_HPP
#define AMDIS_SIMPLIFY_EXPRESSION_HPP

#include "AMDiS_fwd.h"
#include "LazyOperatorTerm.h"
#include "value_expr.hpp"

#define SINGLE_ARG(...) __VA_ARGS__

namespace AMDiS 
{
  namespace traits 
  {
    template<typename T>
    struct is_ct_value : boost::mpl::false_ {};
    
    template<int I>
    struct is_ct_value< expressions::CValue<I> > : boost::mpl::true_ {};
    
  } // end namespace traits
  
    
  namespace expressions 
  {
    template<typename Term, typename Enabled = void>
    struct Simplify 
    { 
      typedef Term type; 
      
      static type eval(Term const& t) { return t; }
    }; // by default do not simplify a term
    
  } // end namespace expressions
  

  // generator function for simplification
  template<typename Term> 
  inline typename expressions::Simplify<Term>::type simplify(const Term& t) 
  { return expressions::Simplify<Term>::eval(t); }


  namespace expressions 
  {
    /// -(N) -> (-N)
    template<int N>
    struct Simplify< Negative<CValue<N> > > 
    { 
      typedef CValue<-N> type; 
      
      static type eval(Negative<CValue<N> > const& t)
      { return CValue<-N>(); }
      
    };
    
    /// -0 -> 0
    template<>
    struct Simplify< Negative<CValue<0> > > 
    { 
      typedef CValue<0> type;
      
      static type eval(Negative<CValue<0> > const& t) 
      { return CValue<0>(); }
    };
    
    /// (N) + (M) -> (N+M)
    template<int N, int M>
    struct Simplify< Add<CValue<N>, CValue<M> > > 
    { 
      typedef CValue<N+M> type;
      
      static type eval(Add<CValue<N>, CValue<M> > const& t) 
      { return CValue<N+M>(); }
    };
    
    /// (N) * (M) -> (N*M)
    template<int N, int M >
    struct Simplify< Mult<CValue<N>, CValue<M> > > 
    { 
      typedef CValue<N*M> type;
      
      static type eval(Mult<CValue<N>, CValue<M> > const& t) 
      { return CValue<N*M>(); }
    };
    
    /// (M) ^ N -> (M^N)
    template<int N, int M >
    struct Simplify< Pow<N, CValue<M> > > 
    { 
      typedef CValue<meta::pow<M,N>::value> type;
      
      static type eval(Pow<N, CValue<M> > const& t) 
      { return type(); }
    };
    
  } // end namespace expressions

  namespace expressions 
  {
    /// X + 0 -> X
    template<typename Term>
    struct Simplify< Add<Term, CValue<0> >, typename boost::enable_if_c<!traits::is_ct_value<Term>::value>::type >
    { 
      typedef typename Simplify<Term>::type type; 
      
      static type eval(Add<Term, CValue<0> > const& t) 
      { return simplify(t.term1); }
    };
    
    /// 0 + X -> X
    template<typename Term>
    struct Simplify< Add<CValue<0>, Term>, typename boost::enable_if_c<!traits::is_ct_value<Term>::value>::type >
    { 
      typedef typename Simplify<Term>::type type; 
      
      static type eval(Add<CValue<0>, Term> const& t) 
      { return simplify(t.term2); }
Praetorius, Simon's avatar
Praetorius, Simon committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    };
    /// X - 0 -> X
    template<typename Term>
    struct Simplify< Subtract<Term, CValue<0> >, typename boost::enable_if_c<!traits::is_ct_value<Term>::value>::type >
    { 
      typedef typename Simplify<Term>::type type; 
      
      static type eval(Subtract<Term, CValue<0> > const& t) 
      { return simplify(t.term1); }
    };
    
    /// 0 - X -> -X
    template<typename Term>
    struct Simplify< Subtract<CValue<0>, Term>, typename boost::enable_if_c<!traits::is_ct_value<Term>::value>::type >
    { 
      typedef Negative<typename Simplify<Term>::type> type; 
      
      static type eval(Subtract<CValue<0>, Term> const& t) 
      { return -simplify(t.term2); }
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
    };
      
    /// X * 0 -> 0
    template<typename Term>
    struct Simplify< Mult<Term, CValue<0> >, typename boost::enable_if_c<!traits::is_ct_value<Term>::value>::type > 
    { 
      typedef CValue<0> type; 
      
      static type eval(Mult<Term, CValue<0> > const& t) 
      { return CValue<0>(); }
    };
    
    /// 0 * X -> 0
    template<typename Term>
    struct Simplify< Mult<CValue<0>, Term>, typename boost::enable_if_c<!traits::is_ct_value<Term>::value>::type > 
    { 
      typedef CValue<0> type; 
      
      static type eval(Mult<CValue<0>, Term> const& t) 
      { return CValue<0>(); }
    };
      
    /// X * 1 -> X
    template<typename Term>
    struct Simplify< Mult<Term, CValue<1> >, typename boost::enable_if_c<!traits::is_ct_value<Term>::value>::type > 
    { 
      typedef typename Simplify<Term>::type type; 
      
      static type eval(Mult<Term, CValue<1> > const& t) 
      { return simplify(t.term1); }
    };
    
    /// 1 * X -> X
    template<typename Term>
    struct Simplify< Mult<CValue<1>, Term>, typename boost::enable_if_c<!traits::is_ct_value<Term>::value>::type > 
    { 
      typedef typename Simplify<Term>::type type; 
      
      static type eval(Mult<CValue<1>, Term> const& t) 
      { return simplify(t.term2); }
    };
    
  } // end namespace expressions

    
  namespace expressions 
  {
    /// X / X -> 1
    template<typename Term>
    struct Simplify< Mult<Term, MultInverse<Term> > > 
    { 
      typedef CValue<1> type;
      
      static type eval(Mult<Term, MultInverse<Term> > const& t) 
      { return CValue<1>(); }    
    };
    
    /// X / X -> 1
    template<typename Term>
    struct Simplify< Mult<MultInverse<Term>, Term> > 
    { 
      typedef CValue<1> type;
      
      static type eval(Mult<MultInverse<Term>, Term> const& t)  
      { return CValue<1>(); }
    };
    
    /// -(-X) -> X
    template<typename Term>
    struct Simplify< Negative<Negative<Term> > > 
    { 
      typedef Term type;
      
      static type eval(Negative<Negative<Term> > const& t)  
      { return t.term.term; }
    };
    
    /// (X^(-1))^(-1) -> X
    template<typename Term>
    struct Simplify< MultInverse<MultInverse<Term> > > 
    { 
      typedef Term type;
      
      static type eval(MultInverse<MultInverse<Term> > const& t)  
      { return t.term.term; }
    };
    
    /// -(X - Y) -> Y - X
    template<typename X, typename Y>
    struct Simplify< Negative<Add<X, Negative<Y> > > > 
    { 
      typedef Add<Y, Negative<X> > type;
      
      static type eval(Negative<Add<X, Negative<Y> > > const& t) 
      { return t.term.term2.term - t.term.term1; }
    };
    
  } // end namespace expressions
  
    
  namespace expressions 
  {
    /// X^1 -> X
    template<typename Term>
    struct Simplify< Pow<1, Term>, typename boost::enable_if_c<!traits::is_ct_value<Term>::value>::type >
    { 
      typedef Term type; 
      
      static type eval(Pow<1,Term> const& t) { return t.term; }
    };
    
    /// X^0 -> 1
    template<typename Term>
    struct Simplify< Pow<0, Term>, typename boost::enable_if_c<!traits::is_ct_value<Term>::value>::type >
    { 
      typedef CValue<1> type;
      
      static type eval(Pow<0,Term> const& t) { return CValue<1>(); }
    };

  } // end namespace expressions
  
    
  namespace expressions 
  {
    /// (XY) / (XZ) -> Y/Z
    template<typename X, typename Y, typename Z>
    struct Simplify< Mult<Mult<X,Y>, MultInverse<Mult<X,Z> > > > 
    { 
      typedef Mult<Y, MultInverse<Z> > type; 
      
      // Y = t.term1.term2, Z = t.term2.term.term2
      static type eval(Mult<Mult<X,Y>, MultInverse<Mult<X,Z> > > const& t) 
      { return t.term1.term2 / t.term2.term.term2; }    
    };
    
    /// (XY) / X -> Y
    template<typename X, typename Y>
    struct Simplify< Mult<Mult<X,Y>, MultInverse<X> > > 
    { 
      typedef Y type;
      
      // Y = t.term1.term2
      static type eval(Mult<Mult<X,Y>, MultInverse<X> > const& t)  
      { return t.term1.term2; }
    };
    
    /// X / (XY) -> 1/Y
    template<typename X, typename Y>
    struct Simplify< Mult<X, MultInverse<Mult<X,Y> > > > 
    { 
      typedef MultInverse<Y> type;
      
      // Y = t.term2.term.term2
      static type eval(Mult<Mult<X,Y>, MultInverse<X> > const& t)   
      { return MultInverse<Y>(t.term2.term.term2); }
    };
    
    /// N*(M*X) -> (N*M) * X
    template<int N, int M, typename X>
    struct Simplify< Mult<CValue<N>, Mult<CValue<M>, X> >, typename boost::enable_if_c< N!=0 && N!=1 && M!=0 && M!=1 >::type  > 
    { 
      typedef Mult<CValue<N*M>, X> type;
      
      // X = t.term2.term2
      static type eval(Mult<CValue<N>, Mult<CValue<M>, X> > const& t)    
      { return CValue<N*M>() * (t.term2.term2); } 
    };
      
    /// N*(X*M) -> (N*M) * X
    template<int N, int M, typename X>
    struct Simplify< Mult<CValue<N>, Mult<X, CValue<M> > >, typename boost::enable_if_c< N!=0 && N!=1 && M!=0 && M!=1 >::type > 
    { 
      typedef Mult<CValue<N*M>, X> type; 
      
      // X = t.term2.term1
      static type eval(Mult<CValue<N>, Mult<X, CValue<M> > > const& t)    
      { return CValue<N*M>() * (t.term2.term1); } 
    };
    
    
    /// (M*X)*N -> (N*M) * X
    template<int N, int M, typename X>
    struct Simplify< Mult<Mult<CValue<M>, X>, CValue<N> >, typename boost::enable_if_c< N!=0 && N!=1 && M!=0 && M!=1 >::type > 
    { 
      typedef Mult<CValue<N*M>, X> type; 
      
      // X = t.term1.term2
      static type eval(Mult<Mult<CValue<M>, X>, CValue<N> > const& t)    
      { return CValue<N*M>() * (t.term1.term2); }
    };
    
    
    /// (X*M)*N -> (N*M) * X
    template<int N, int M, typename X>
    struct Simplify< Mult<Mult<X, CValue<M> >, CValue<N> >, typename boost::enable_if_c< N!=0 && N!=1 && M!=0 && M!=1 >::type > 
    { 
      typedef Mult<CValue<N*M>, X> type; 
      
      // X = t.term1.term1
      static type eval(Mult<Mult<X, CValue<M> >, CValue<N> > const& t)    
      { return CValue<N*M>() * (t.term1.term1); }
    };

  } // end namespace expressions


  namespace expressions 
  {
    /// A + B -> simplify(A) + simplify(B)
    template<typename Term1, typename Term2>
    class Simplify< Add<Term1, Term2>, typename boost::enable_if_c<!(traits::is_ct_value<Term1>::value || traits::is_ct_value<Term2>::value)>::type > 
    { 
      typedef typename Simplify<Term1>::type S1;
      typedef typename Simplify<Term2>::type S2;
      
    public:
      typedef Add<S1, S2> type;
      
      static type eval(Add<Term1, Term2> const& t) 
      {
	return simplify(t.term1) + simplify(t.term2);
      }
    };
    
    /// A * B -> simplify(A) * simplify(B)
    template<typename Term1, typename Term2>
    class Simplify< Mult<Term1, Term2>, typename boost::enable_if_c<!(traits::is_ct_value<Term1>::value || traits::is_ct_value<Term2>::value)>::type > 
    { 
      typedef typename Simplify<Term1>::type S1;
      typedef typename Simplify<Term2>::type S2;
      
    public:
      typedef Mult<S1, S2> type;
      
      static type eval(Mult<Term1, Term2> const& t) 
      {
	return simplify(t.term1) * simplify(t.term2);
      }
    };
    
    /// X(A) -> X(simplify(A))
    template<template<class> class Outer, class Inner>
    class Simplify< Outer<Inner>, typename boost::enable_if_c<!(traits::is_ct_value<Inner>::value) && traits::is_expr<Inner>::value>::type > 
    { 
      typedef typename Simplify<Inner>::type S;
      
    public:
      typedef Outer<S> type;
      
      static type eval(Outer<Inner> const& t) 
      {
	return type(simplify(t.term));
      }
    };

  } // end namespace expressions


  // =============================================================================
  // multiple simplifications

  namespace expressions 
  {    
    template<int N, typename Term, typename Enabled = void>
    struct SimplifyRecursive 
    { 
      typedef typename Simplify< typename SimplifyRecursive<N-1, Term>::type >::type type;
    };
    
    template<typename Term>
    struct SimplifyRecursive<1, Term>
    { 
      typedef typename Simplify< Term >::type type;
    };
    
    template<int N, typename Term >
    struct SimplifyRecursive<N, Term, typename boost::enable_if_c<(N <= 0)>::type>
    { 
      typedef Term type;
    };
    
  } // end namespace expressions

    
  template<int N, typename Term> 
  inline typename expressions::SimplifyRecursive<N, Term>::type
  simplify(const Term& t);

  template<int N, typename Term> 
  inline typename expressions::SimplifyRecursive<N, Term>::type
  simplify(const Term& t, boost::mpl::int_<N>) { return simplify(simplify<N-1>(t)); }

  template<typename Term> 
  inline typename expressions::SimplifyRecursive<1, Term>::type
  simplify(const Term& t, boost::mpl::int_<1>) { return simplify(t); }

  template<typename Term> 
  inline typename expressions::SimplifyRecursive<0, Term>::type
  simplify(const Term& t, boost::mpl::int_<0>) { return t; }

  template<int N, typename Term> 
  inline typename expressions::SimplifyRecursive<N, Term>::type
  simplify(const Term& t) { return simplify(t, boost::mpl::int_<N>()); }

} // end namespace AMDiS

#endif // AMDIS_SIMPLIFY_EXPRESSION_HPP