CMS 3D CMS Logo

SimplifySum.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_SimplifySum_h
2 #define PhysicsTools_Utilities_SimplifySum_h
3 
10 #include <type_traits>
11 #include <boost/mpl/if.hpp>
12 
14 
15 namespace funct {
16 
17  // A + ( B + C ) => ( A + B ) + C
18  SUM_RULE(TYPT3, A, SUM_S(B, C), SUM(SUM(A, B), C), (_1 + _2._1) + _2._2);
19 
20  // ( A + B ) + ( C + D ) => ( ( A + B ) + C ) + D
21  SUM_RULE(TYPT4, SUM_S(A, B), SUM_S(C, D), SUM(SUM(SUM(A, B), C), D), (_1 + _2._1) + _2._2);
22 
23  // n + A = A + n
24  SUM_RULE(TYPN1T1, NUM(n), A, SUM(A, NUM(n)), _2 + _1);
25 
26  // n + ( A + B )= ( A + B ) + n
27  SUM_RULE(TYPN1T2, NUM(n), SUM_S(A, B), SUM(SUM_S(A, B), NUM(n)), _2 + _1);
28 
29  // A + 0 = A
30  SUM_RULE(TYPT1, A, NUM(0), A, _1);
31 
32  // 0 + 0 = 0
33  SUM_RULE(TYP0, NUM(0), NUM(0), NUM(0), num<0>());
34 
35  // ( A * B ) + 0 = ( A * B )
36  SUM_RULE(TYPT2, PROD_S(A, B), NUM(0), PROD_S(A, B), _1);
37 
38  // 0 + ( A * B ) = ( A * B )
39  SUM_RULE(TYPT2, NUM(0), PROD_S(A, B), PROD_S(A, B), _2);
40 
41  // 0 - ( A * B ) = - ( A * B )
43 
44  // ( A + B ) + 0 = ( A + B )
45  SUM_RULE(TYPT2, SUM_S(A, B), NUM(0), SUM_S(A, B), _1);
46 
47  // 0 + ( A + B ) = ( A + B )
48  SUM_RULE(TYPT2, NUM(0), SUM_S(A, B), SUM_S(A, B), _2);
49 
50  // A - ( -B ) = A + B
51  DIFF_RULE(TYPT2, A, MINUS_S(B), SUM(A, B), _1 + _2._);
52 
53  // n * A + m * A => ( n + m ) * A
56  typedef PROD(NUM(n), A) arg1;
57  typedef PROD(NUM(m), A) arg2;
58  typedef SUM_S(arg1, arg2) type;
59  COMBINE(arg1, arg2, type(_1, _2));
60  };
61 
62  TEMPL(N2T1)
64  typedef PROD(NUM(n + m), A) type;
65  typedef DecomposeProduct<PROD(NUM(n), A), A> Dec;
66  COMBINE(PROD(NUM(n), A), PROD(NUM(m), A), num<n + m>() * Dec::get(_1));
67  };
68 
69  TEMPL(T1)
70  struct ParametricSimplifiedSum<1, 1, A, true> {
72  COMBINE(A, A, type(_1, _2));
73  };
74 
75  TEMPL(T1)
76  struct ParametricSimplifiedSum<1, 1, A, false> {
77  typedef PROD(NUM(2), A) type;
78  COMBINE(A, A, num<2>() * _1);
79  };
80 
81  TEMPL(N2T1)
82  struct Sum<PROD_S(NUM(n), A), PROD_S(NUM(m), A)> : public ParametricSimplifiedSum<n, m, A> {};
83 
84  TEMPL(N1T1)
85  struct Sum<A, PROD_S(NUM(n), A)> : public ParametricSimplifiedSum<1, n, A> {};
86 
87  TEMPL(N1T1)
88  struct Sum<PROD_S(NUM(n), A), A> : public ParametricSimplifiedSum<n, 1, A> {};
89 
90  TEMPL(T1)
91  struct Sum<A, A> : public ParametricSimplifiedSum<1, 1, A> {};
92 
93  TEMPL(T1)
94  struct Sum<MINUS_S(A), MINUS_S(A)> : public ParametricSimplifiedSum<1, 1, MINUS_S(A)> {};
95 
96  TEMPL(T2)
97  struct Sum<MINUS_S(PROD_S(A, B)), MINUS_S(PROD_S(A, B))>
98  : public ParametricSimplifiedSum<1, 1, MINUS_S(PROD_S(A, B))> {};
99 
100  TEMPL(N1)
101  struct Sum<NUM(n), NUM(n)> : public ParametricSimplifiedSum<1, 1, NUM(n)> {};
102 
103  TEMPL(T2)
104  struct Sum<PROD_S(A, B), PROD_S(A, B)> : public ParametricSimplifiedSum<1, 1, PROD_S(A, B)> {};
105 
106  TEMPL(N1T1)
107  struct Sum<PROD_S(NUM(n), A), PROD_S(NUM(n), A)> : public ParametricSimplifiedSum<1, 1, PROD_S(NUM(n), A)> {};
108 
109  // simplify f + g + h regardless of the order
110  template <typename Prod, bool simplify = Prod::value>
111  struct AuxSum {
112  typedef SUM(typename Prod::AB, typename Prod::C) type;
113  COMBINE(typename Prod::AB, typename Prod::C, _1 + _2);
114  };
115 
116  template <typename Prod>
117  struct AuxSum<Prod, false> {
118  typedef SUM_S(typename Prod::AB, typename Prod::C) type;
119  COMBINE(typename Prod::AB, typename Prod::C, type(_1, _2));
120  };
121 
122  template <typename F, typename G, typename H>
123  struct SimplSumOrd {
124  struct prod0 {
125  typedef F A;
126  typedef G B;
127  typedef H C;
128  typedef SUM_S(A, B) AB;
129  inline static const A& a(const F& f, const G& g, const H& h) { return f; }
130  inline static const B& b(const F& f, const G& g, const H& h) { return g; }
131  inline static const C& c(const F& f, const G& g, const H& h) { return h; }
132  enum { value = false };
133  };
134  struct prod1 {
135  typedef F A;
136  typedef H B;
137  typedef G C;
138  typedef SUM_S(A, B) base;
139  typedef SUM(A, B) AB;
140  inline static const A& a(const F& f, const G& g, const H& h) { return f; }
141  inline static const B& b(const F& f, const G& g, const H& h) { return h; }
142  inline static const C& c(const F& f, const G& g, const H& h) { return g; }
144  };
145  struct prod2 {
146  typedef G A;
147  typedef H B;
148  typedef F C;
149  typedef SUM_S(A, B) base;
150  typedef SUM(A, B) AB;
151  inline static const A& a(const F& f, const G& g, const H& h) { return g; }
152  inline static const B& b(const F& f, const G& g, const H& h) { return h; }
153  inline static const C& c(const F& f, const G& g, const H& h) { return f; }
155  };
156 
158  typedef typename AuxSum<prod>::type type;
159  inline static type combine(const SUM_S(F, G) & fg, const H& h) {
160  const F& f = fg._1;
161  const G& g = fg._2;
162  const typename prod::A& a = prod::a(f, g, h);
163  const typename prod::B& b = prod::b(f, g, h);
164  const typename prod::C& c = prod::c(f, g, h);
165  return AuxSum<prod>::combine(a + b, c);
166  }
167  };
168 
169  TEMPL(T3)
170  struct Sum<SUM_S(A, B), C> : public SimplSumOrd<A, B, C> {};
171 
172  TEMPL(T4)
173  struct Sum<SUM_S(A, B), PROD_S(C, D)> : public SimplSumOrd<A, B, PROD_S(C, D)> {};
174 
175 } // namespace funct
176 
178 
179 #endif
class-composition.H
H
Definition: class-composition.py:31
funct::AuxSum::combine
static type combine(const typename Prod::AB &_1, const typename Prod::C &_2)
Definition: SimplifySum.h:113
funct::DecomposeProduct
Definition: DecomposeProduct.h:8
SUM
#define SUM(A, B)
Definition: Simplify_begin.h:52
funct::SimplSumOrd::type
AuxSum< prod >::type type
Definition: SimplifySum.h:158
Product.h
TYPN1T1
#define TYPN1T1
Definition: Simplify_begin.h:16
funct::SimplSumOrd::prod
::boost::mpl::if_< prod1, prod1, typename ::boost::mpl::if_< prod2, prod2, prod0 >::type >::type prod
Definition: SimplifySum.h:157
TYP0
#define TYP0
Definition: Simplify_begin.h:4
funct::false
false
Definition: Factorize.h:34
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
funct::SimplSumOrd::prod2::c
static const C & c(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:153
funct::SimplSumOrd::prod0::B
G B
Definition: SimplifySum.h:126
funct::AuxSum::type
Sum< typename Prod::AB, typename Prod::C >::type type
Definition: SimplifySum.h:112
funct::PROD_S
PROD_S(B, C)>
Definition: Factorize.h:114
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
funct::SimplSumOrd::prod1::A
F A
Definition: SimplifySum.h:135
funct::B
TEMPL(T2) struct Divides B
Definition: Factorize.h:29
funct::AuxSum< Prod, false >::type
SumStruct< typename Prod::AB, typename Prod::C > type
Definition: SimplifySum.h:118
h
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
Definition: L1TUtmAlgorithmRcd.h:4
TYPT4
#define TYPT4
Definition: Simplify_begin.h:9
funct::SimplSumOrd::prod2::base
SumStruct< A, B > base
Definition: SimplifySum.h:149
funct::SimplSumOrd::prod2::AB
Sum< A, B >::type AB
Definition: SimplifySum.h:150
callgraph.G
G
Definition: callgraph.py:13
funct::SimplSumOrd::prod2::A
G A
Definition: SimplifySum.h:146
funct::ProductStruct
Definition: Product.h:7
funct::C
C
Definition: Factorize.h:133
funct::SimplSumOrd::prod1::b
static const B & b(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:141
funct::SimplSumOrd::prod2::C
F C
Definition: SimplifySum.h:148
funct::SimplSumOrd::prod2::B
H B
Definition: SimplifySum.h:147
TYPT1
#define TYPT1
Definition: Simplify_begin.h:6
funct::ParametricSimplifiedSum::type
SumStruct< arg1, arg2 > type
Definition: SimplifySum.h:58
funct::SimplSumOrd::prod0
Definition: SimplifySum.h:124
F
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
funct::SimplSumOrd::prod0::c
static const C & c(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:131
TYPT2
#define TYPT2
Definition: Simplify_begin.h:7
Difference.h
funct::SimplSumOrd::prod1::B
H B
Definition: SimplifySum.h:136
Numerical.h
funct::ParametricSimplifiedSum< 1, 1, A, false >::type
Product< Numerical< 2 >, A >::type type
Definition: SimplifySum.h:77
funct::SimplSumOrd::prod1::base
SumStruct< A, B > base
Definition: SimplifySum.h:138
funct::SimplSumOrd
Definition: SimplifySum.h:123
funct::AuxSum
Definition: SimplifySum.h:111
funct::SimplSumOrd::prod1::AB
Sum< A, B >::type AB
Definition: SimplifySum.h:139
funct::m
m
Definition: Factorize.h:50
SUM_S
#define SUM_S(A, B)
Definition: Simplify_begin.h:37
h
b
double b
Definition: hdecay.h:118
funct::SimplSumOrd::prod0::C
H C
Definition: SimplifySum.h:127
funct::SimplSumOrd::prod1::a
static const A & a(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:140
Sum.h
funct::true
true
Definition: Factorize.h:173
funct::ParametricSimplifiedSum< n, m, A, false >::Dec
DecomposeProduct< typename Product< Numerical< n >, A >::type, A > Dec
Definition: SimplifySum.h:65
ParametricTrait.h
funct::SimplSumOrd::combine
static type combine(const SumStruct< F, G > &fg, const H &h)
Definition: SimplifySum.h:159
a
double a
Definition: hdecay.h:119
funct::SimplSumOrd::prod0::b
static const B & b(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:130
funct::MINUS_S
typedef MINUS_S(A) arg
A
DecomposeProduct.h
funct::SimplSumOrd::prod0::a
static const A & a(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:129
funct::type
arg type
Definition: Factorize.h:37
value
Definition: value.py:1
funct::TEMPL
TEMPL(T1) struct Divides0
Definition: Factorize.h:20
funct::ParametricSimplifiedSum::arg2
Product< Numerical< m >, A >::type arg2
Definition: SimplifySum.h:57
funct::SimplSumOrd::prod1::c
static const C & c(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:142
HltBtagPostValidation_cff.c
c
Definition: HltBtagPostValidation_cff.py:31
get
#define get
funct::ParametricSimplifiedSum< 1, 1, A, true >::type
SumStruct< A, A > type
Definition: SimplifySum.h:71
funct::SimplSumOrd::prod0::AB
SumStruct< A, B > AB
Definition: SimplifySum.h:128
TYPN1T2
#define TYPN1T2
Definition: Simplify_begin.h:17
Simplify_end.h
SUM_RULE
#define SUM_RULE(TMPL, T1, T2, RES, COMB)
Definition: Simplify_begin.h:81
funct::SimplSumOrd::prod2
Definition: SimplifySum.h:145
TtFullHadDaughter::B
static const std::string B
Definition: TtFullHadronicEvent.h:9
MaterialEffects_cfi.A
A
Definition: MaterialEffects_cfi.py:11
funct::SimplSumOrd::prod0::A
F A
Definition: SimplifySum.h:125
type
type
Definition: HCALResponse.h:21
funct::ParametricSimplifiedSum::arg1
Product< Numerical< n >, A >::type arg1
Definition: SimplifySum.h:56
gen::C
C
Definition: PomwigHadronizer.cc:76
relativeConstraints.value
value
Definition: relativeConstraints.py:53
funct::NUM
PROD_S(A, B)> NUM(n))
Definition: Factorize.h:87
COMBINE
#define COMBINE(A, B, RES)
Definition: Simplify_begin.h:70
TYPT3
#define TYPT3
Definition: Simplify_begin.h:8
funct::SumStruct
Definition: Sum.h:7
funct::Sum
Definition: Sum.h:18
Simplify_begin.h
DIFF_RULE
#define DIFF_RULE(TMPL, T1, T2, RES, COMB)
Definition: Simplify_begin.h:88
funct::SimplSumOrd::prod2::a
static const A & a(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:151
funct::ParametricSimplifiedSum< n, m, A, false >::type
Product< Numerical< n+m >, A >::type type
Definition: SimplifySum.h:64
funct::PROD
typedef PROD(F, SUM(RATIO(A, F), RATIO(B, F))) type
funct::SimplSumOrd::prod2::b
static const B & b(const F &f, const G &g, const H &h)
Definition: SimplifySum.h:152
funct::SimplSumOrd::prod1::C
G C
Definition: SimplifySum.h:137
funct
Definition: Abs.h:5
funct::SimplSumOrd::prod1
Definition: SimplifySum.h:134
funct::ParametricSimplifiedSum
Definition: SimplifySum.h:55
g
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4