simplify_expr.hpp 21.7 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
/******************************************************************************
 *
 * 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 
  {
49
    template<typename Term>
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
    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>(); }
    };
    
99
100
101
102
103
104
105
106
107
108
    /// (N) - (M) -> (N-M)
    template<int N, int M>
    struct Simplify< Subtract<CValue<N>, CValue<M> > > 
    { 
      typedef CValue<N-M> type;
      
      static type eval(Subtract<CValue<N>, CValue<M> > const& t) 
      { return CValue<N-M>(); }
    };
    
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
    /// (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>
135
    struct Simplify< Add<Term, CValue<0> > >
136
137
138
139
140
141
142
    { 
      typedef typename Simplify<Term>::type type; 
      
      static type eval(Add<Term, CValue<0> > const& t) 
      { return simplify(t.term1); }
    };
    
143
144
145
    /// (N) + 0 -> X
    template<int N>
    struct Simplify< Add<CValue<N>, CValue<0> > >
146
    { 
147
      typedef CValue<N> type; 
148
      
149
150
      static type eval(Add<CValue<N>, CValue<0> > const&) 
      { return CValue<N>(); }
Praetorius, Simon's avatar
Praetorius, Simon committed
151
    };
152
    
Praetorius, Simon's avatar
Praetorius, Simon committed
153
154
    /// X - 0 -> X
    template<typename Term>
155
    struct Simplify< Subtract<Term, CValue<0> > >
Praetorius, Simon's avatar
Praetorius, Simon committed
156
157
158
159
160
161
162
    { 
      typedef typename Simplify<Term>::type type; 
      
      static type eval(Subtract<Term, CValue<0> > const& t) 
      { return simplify(t.term1); }
    };
    
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
    /// (N) - 0 -> (N)
    template<int N>
    struct Simplify< Subtract<CValue<N>, CValue<0> > >
    { 
      typedef CValue<N> type; 
      
      static type eval(Subtract<CValue<N>, CValue<0> > const&) 
      { return CValue<N>(); }
    };
    
    /// 0 + X -> X
    template<typename Term>
    struct Simplify< Add<CValue<0>, Term> >
    { 
      typedef typename Simplify<Term>::type type; 
      
      static type eval(Add<CValue<0>, Term> const& t) 
      { return simplify(t.term2); }
    };
    
    /// 0 + (N) -> (N)
    template<int N>
    struct Simplify< Add<CValue<0>, CValue<N> > >
    { 
      typedef CValue<N> type; 
      
      static type eval(Add<CValue<0>, CValue<N> > const&) 
      { return CValue<N>(); }
    };
    
Praetorius, Simon's avatar
Praetorius, Simon committed
193
194
    /// 0 - X -> -X
    template<typename Term>
195
    struct Simplify< Subtract<CValue<0>, Term> >
Praetorius, Simon's avatar
Praetorius, Simon committed
196
197
198
199
200
    { 
      typedef Negative<typename Simplify<Term>::type> type; 
      
      static type eval(Subtract<CValue<0>, Term> const& t) 
      { return -simplify(t.term2); }
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
    
    /// 0 - (N) -> (-N)
    template<int N>
    struct Simplify< Subtract<CValue<0>, CValue<N> > >
    { 
      typedef CValue<-N> type; 
      
      static type eval(Subtract<CValue<0>, CValue<N> > const&) 
      { return CValue<-N>(); }
    };
    
    /// 0 - 0 -> 0
    template<>
    struct Simplify< Subtract<CValue<0>, CValue<0> > >
    { 
      typedef CValue<0> type; 
      
      static type eval(Subtract<CValue<0>, CValue<0> > const&) 
      { return type(); }
    };
    
    /// 0 + 0 -> 0
    template<>
    struct Simplify< Add<CValue<0>, CValue<0> > >
    { 
      typedef CValue<0> type; 
      
      static type eval(Add<CValue<0>, CValue<0> > const&) 
      { return type(); }
    };
232
233
234
      
    /// X * 0 -> 0
    template<typename Term>
235
    struct Simplify< Mult<Term, CValue<0> > > 
236
237
238
    { 
      typedef CValue<0> type; 
      
239
240
241
242
243
244
245
246
247
248
249
      static type eval(Mult<Term, CValue<0> > const&) 
      { return CValue<0>(); }
    };
      
    /// (N) * 0 -> 0
    template<int N>
    struct Simplify< Mult<CValue<N>, CValue<0> > > 
    { 
      typedef CValue<0> type; 
      
      static type eval(Mult<CValue<N>, CValue<0> > const&) 
250
251
252
253
254
      { return CValue<0>(); }
    };
    
    /// 0 * X -> 0
    template<typename Term>
255
    struct Simplify< Mult<CValue<0>, Term> > 
256
257
258
259
260
261
    { 
      typedef CValue<0> type; 
      
      static type eval(Mult<CValue<0>, Term> const& t) 
      { return CValue<0>(); }
    };
262
263
264
265
266
267
268
269
270
271
    
    /// 0 * (N) -> 0
    template<int N>
    struct Simplify< Mult<CValue<0>, CValue<N> > > 
    { 
      typedef CValue<0> type; 
      
      static type eval(Mult<CValue<0>, CValue<N> > const&) 
      { return CValue<0>(); }
    };
272
273
274
      
    /// X * 1 -> X
    template<typename Term>
275
    struct Simplify< Mult<Term, CValue<1> > > 
276
277
278
279
280
281
    { 
      typedef typename Simplify<Term>::type type; 
      
      static type eval(Mult<Term, CValue<1> > const& t) 
      { return simplify(t.term1); }
    };
282
283
284
285
286
287
288
289
290
291
      
    /// (N) * 1 -> (N)
    template<int N>
    struct Simplify< Mult<CValue<N>, CValue<1> > > 
    { 
      typedef CValue<N> type; 
      
      static type eval(Mult<CValue<N>, CValue<1> > const&) 
      { return CValue<N>(); }
    };
292
293
294
    
    /// 1 * X -> X
    template<typename Term>
295
    struct Simplify< Mult<CValue<1>, Term> > 
296
297
298
299
300
301
302
    { 
      typedef typename Simplify<Term>::type type; 
      
      static type eval(Mult<CValue<1>, Term> const& t) 
      { return simplify(t.term2); }
    };
    
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
    /// 1 * (N) -> (N)
    template<int N>
    struct Simplify< Mult<CValue<1>, CValue<N> > > 
    { 
      typedef CValue<N> type; 
      
      static type eval(Mult<CValue<1>, CValue<N> > const&) 
      { return CValue<N>(); }
    };
      
    /// X * -1 -> -X
    template<typename Term>
    struct Simplify< Mult<Term, CValue<-1> > > 
    { 
      typedef Negative<typename Simplify<Term>::type> type; 
      
      static type eval(Mult<Term, CValue<-1> > const& t) 
      { return -simplify(t.term1); }
    };
      
    /// (N) * -1 -> (-N)
    template<int N>
    struct Simplify< Mult<CValue<N>, CValue<-1> > > 
    { 
      typedef CValue<-N> type; 
      
      static type eval(Mult<CValue<N>, CValue<-1> > const&) 
      { return CValue<-N>(); }
    };
    
    /// -1 * X -> -X
    template<typename Term>
    struct Simplify< Mult<CValue<-1>, Term> > 
    { 
      typedef Negative<typename Simplify<Term>::type> type; 
      
      static type eval(Mult<CValue<-1>, Term> const& t) 
      { return -simplify(t.term2); }
    };
    
    /// -1 * (N) -> (-N)
    template<int N>
    struct Simplify< Mult<CValue<-1>, CValue<N> > > 
    { 
      typedef CValue<-N> type; 
      
      static type eval(Mult<CValue<-1>, CValue<N> > const&) 
      { return type(); }
    };
    
    
#define MULT_CVALUE_IMPL(_A_,_B_) \
  template<> struct Simplify< Mult<CValue<_A_>, CValue<_B_> > > { \
    typedef CValue<_A_ * _B_> type; \
    static type eval(Mult<CValue<_A_>, CValue<_B_> > const&) { return type(); } \
  };
  
    // implement all specializations with special values
    MULT_CVALUE_IMPL(0,0)
    MULT_CVALUE_IMPL(0,1)
    MULT_CVALUE_IMPL(1,0)
    MULT_CVALUE_IMPL(1,1)
    MULT_CVALUE_IMPL(0,-1)
    MULT_CVALUE_IMPL(-1,0)
    MULT_CVALUE_IMPL(-1,-1)
    MULT_CVALUE_IMPL(1,-1)
    MULT_CVALUE_IMPL(-1,1)
    
371
372
  } // end namespace expressions

373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
  
  namespace expressions
  {
    // move constants to the beginning of the expression
//     /// X * N -> N * X
//     template<int N, typename Term>
//     struct Simplify< Mult<Term, CValue<N> > > 
//     { 
//       typedef Mult<CValue<N>, Term> type; 
//       
//       static type eval(Mult<Term, CValue<N> > const& t) 
//       { return CValue<N>() * t.term1; }
//     };
    
  } // end namespace expressions
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
    
  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; }
    };
    
431
    /// (X + (-Y)) -> X - Y
432
433
434
    template<typename X, typename Y>
    struct Simplify< Negative<Add<X, Negative<Y> > > > 
    { 
435
      typedef Subtract<X, Y> type;
436
437
      
      static type eval(Negative<Add<X, Negative<Y> > > const& t) 
438
439
440
441
442
443
444
445
446
447
448
      { return t.term1 - t.term2.term; }
    };
    
    /// -(X - Y) -> Y - X
    template<typename X, typename Y>
    struct Simplify< Negative<Subtract<X, Y> > > 
    { 
      typedef Subtract<Y, X> type;
      
      static type eval(Negative<Subtract<X, Y> > const& t) 
      { return t.term.term2 - t.term.term1; }
449
450
451
452
453
454
455
456
457
    };
    
  } // end namespace expressions
  
    
  namespace expressions 
  {
    /// X^1 -> X
    template<typename Term>
458
    struct Simplify< Pow<1, Term> >
459
460
461
462
463
464
465
466
    { 
      typedef Term type; 
      
      static type eval(Pow<1,Term> const& t) { return t.term; }
    };
    
    /// X^0 -> 1
    template<typename Term>
467
    struct Simplify< Pow<0, Term> >
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
    { 
      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>
514
    struct Simplify< Mult<CValue<N>, Mult<CValue<M>, X> > > 
515
516
517
518
519
520
521
    { 
      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); } 
    };
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
    
    /// 0*(M*X) -> 0
    template<int M, typename X>
    struct Simplify< Mult<CValue<0>, Mult<CValue<M>, X> > > 
    { 
      typedef CValue<0> type;
      
      // X = t.term2.term2
      static type eval(Mult<CValue<0>, Mult<CValue<M>, X> > const& t)    
      { return type(); } 
    };
    
    /// 1*(M*X) -> M * X
    template<int M, typename X>
    struct Simplify< Mult<CValue<1>, Mult<CValue<M>, X> > > 
    { 
      typedef Mult<CValue<M>, X> type;
      
      // X = t.term2.term2
      static type eval(Mult<CValue<1>, Mult<CValue<M>, X> > const& t)    
      { return CValue<M>() * (t.term2.term2); } 
    };
    
    /// -1*(M*X) -> (-M) * X
    template<int M, typename X>
    struct Simplify< Mult<CValue<-1>, Mult<CValue<M>, X> > > 
    { 
      typedef Mult<CValue<-M>, X> type;
      
      // X = t.term2.term2
      static type eval(Mult<CValue<-1>, Mult<CValue<M>, X> > const& t)    
      { return CValue<-M>() * (t.term2.term2); } 
    };
    
556
557
558
      
    /// N*(X*M) -> (N*M) * X
    template<int N, int M, typename X>
559
    struct Simplify< Mult<CValue<N>, Mult<X, CValue<M> > > > 
560
561
562
563
564
565
566
    { 
      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); } 
    };
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
      
    /// 0*(X*M) -> 0
    template<int M, typename X>
    struct Simplify< Mult<CValue<0>, Mult<X, CValue<M> > > > 
    { 
      typedef CValue<0> type; 
      
      // X = t.term2.term1
      static type eval(Mult<CValue<0>, Mult<X, CValue<M> > > const& t)    
      { return type(); } 
    };
      
    /// 1*(X*M) -> M * X
    template<int M, typename X>
    struct Simplify< Mult<CValue<1>, Mult<X, CValue<M> > > > 
    { 
      typedef Mult<CValue<M>, X> type; 
      
      // X = t.term2.term1
      static type eval(Mult<CValue<1>, Mult<X, CValue<M> > > const& t)    
      { return CValue<M>() * (t.term2.term1); } 
    };
      
    /// -1*(X*M) -> (-M) * X
    template<int M, typename X>
    struct Simplify< Mult<CValue<-1>, Mult<X, CValue<M> > > > 
    { 
      typedef Mult<CValue<-M>, X> type; 
      
      // X = t.term2.term1
      static type eval(Mult<CValue<-1>, Mult<X, CValue<M> > > const& t)    
      { return CValue<-M>() * (t.term2.term1); } 
    };
600
601
602
603
    
    
    /// (M*X)*N -> (N*M) * X
    template<int N, int M, typename X>
604
    struct Simplify< Mult<Mult<CValue<M>, X>, CValue<N> > > 
605
606
607
608
609
610
    { 
      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); }
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
    };    
    
    /// (M*X)*0 -> 0
    template<int M, typename X>
    struct Simplify< Mult<Mult<CValue<M>, X>, CValue<0> > > 
    { 
      typedef CValue<0> type; 
      
      // X = t.term1.term2
      static type eval(Mult<Mult<CValue<M>, X>, CValue<0> > const& t)    
      { return type(); }
    };
    
    /// (M*X)*1 -> M * X
    template<int M, typename X>
    struct Simplify< Mult<Mult<CValue<M>, X>, CValue<1> > > 
    { 
      typedef Mult<CValue<M>, X> type; 
      
      // X = t.term1.term2
      static type eval(Mult<Mult<CValue<M>, X>, CValue<1> > const& t)    
      { return CValue<M>() * (t.term1.term2); }
    };  
    
    /// (M*X)*-1 -> (-M) * X
    template<int M, typename X>
    struct Simplify< Mult<Mult<CValue<M>, X>, CValue<-1> > > 
    { 
      typedef Mult<CValue<-M>, X> type; 
      
      // X = t.term1.term2
      static type eval(Mult<Mult<CValue<M>, X>, CValue<-1> > const& t)    
      { return CValue<-M>() * (t.term1.term2); }
644
645
646
647
648
    };
    
    
    /// (X*M)*N -> (N*M) * X
    template<int N, int M, typename X>
649
    struct Simplify< Mult<Mult<X, CValue<M> >, CValue<N> > > 
650
651
652
653
654
655
    { 
      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); }
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
    };    
    
    /// (X*M)*0 -> 0
    template<int M, typename X>
    struct Simplify< Mult<Mult<X, CValue<M> >, CValue<0> > > 
    { 
      typedef CValue<0> type; 
      
      // X = t.term1.term1
      static type eval(Mult<Mult<X, CValue<M> >, CValue<0> > const& t)    
      { return type(); }
    };
    
    /// (X*M)*1 -> M * X
    template<int M, typename X>
    struct Simplify< Mult<Mult<X, CValue<M> >, CValue<1> > > 
    { 
      typedef Mult<CValue<M>, X> type; 
      
      // X = t.term1.term1
      static type eval(Mult<Mult<X, CValue<M> >, CValue<1> > const& t)    
      { return CValue<M>() * (t.term1.term1); }
    };    
    
    /// (X*M)*-1 -> (-M) * X
    template<int M, typename X>
    struct Simplify< Mult<Mult<X, CValue<M> >, CValue<-1> > > 
    { 
      typedef Mult<CValue<-M>, X> type; 
      
      // X = t.term1.term1
      static type eval(Mult<Mult<X, CValue<M> >, CValue<-1> > const& t)    
      { return CValue<-M>() * (t.term1.term1); }
    };   
    
    
    /// X * (N * Y) -> N * (X * Y)
    template<typename X, int N, typename Y>
    struct Simplify< Mult<X, Mult<CValue<N>, Y> > > 
    { 
      typedef Mult<CValue<N>, Mult<X,Y> > type; 
      
      // X = t.term1
      // Y = t.term2.term2
      static type eval(Mult<X, Mult<CValue<N>, Y> > const& t) 
      { return CValue<N>() * (t.term1 * t.term2.term2); }
    };
    
    
    /// -(N*X) -> (-N) * X
    template<int N, typename X>
    struct Simplify< Negative<Mult<CValue<N>,X> > > 
    { 
      typedef Mult<CValue<-N>, typename Simplify<X>::type> type;
      
      // X = t.term.term2
      static type eval(Negative<Mult<CValue<N>,X> > const& t)    
      { return CValue<-N>() * simplify(t.term.term2); }
714
715
716
717
718
719
720
721
722
    };

  } // end namespace expressions


  namespace expressions 
  {
    /// A + B -> simplify(A) + simplify(B)
    template<typename Term1, typename Term2>
723
    class Simplify< Add<Term1, Term2> > 
724
725
726
727
728
729
730
731
732
733
734
735
736
    { 
      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);
      }
    };
    
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
    /// A - B -> simplify(A) - simplify(B)
    template<typename Term1, typename Term2>
    class Simplify< Subtract<Term1, Term2> > 
    { 
      typedef typename Simplify<Term1>::type S1;
      typedef typename Simplify<Term2>::type S2;
      
    public:
      typedef Subtract<S1, S2> type;
      
      static type eval(Subtract<Term1, Term2> const& t) 
      {
	return simplify(t.term1) - simplify(t.term2);
      }
    };
    
753
754
    /// A * B -> simplify(A) * simplify(B)
    template<typename Term1, typename Term2>
755
    class Simplify< Mult<Term1, Term2> > 
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
    { 
      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>
771
    class Simplify< Outer<Inner> > 
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
    { 
      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